blob: 5cffdf13046219214051e01069f92a11da38115c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200125static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100126static void win_draw_end(win_T *wp, int c1, int c2, int draw_margin, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
129static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
130static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200132static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000134#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200138# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void start_search_hl(void);
140static void end_search_hl(void);
141static void init_search_hl(win_T *wp);
142static void prepare_search_hl(win_T *wp, linenr_T lnum);
143static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
144static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static void screen_char(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200149static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200151static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void win_rest_invalid(win_T *wp);
153static void msg_pos_mode(void);
154static void recording_mode(int attr);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200155static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200157#ifdef FEAT_MENU
158static void redraw_win_toolbar(win_T *wp);
159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100161static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
163#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200164static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167/* Ugly global: overrule attribute used by screen_char() */
168static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200170#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
171/* Can limit syntax highlight time to 'redrawtime'. */
172# define SYN_TIME_LIMIT 1
173#endif
174
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200175#ifdef FEAT_RIGHTLEFT
176# define HAS_RIGHTLEFT(x) x
177#else
178# define HAS_RIGHTLEFT(x) FALSE
179#endif
180
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200181// flags for screen_line()
182#define SLF_RIGHTLEFT 1
183#define SLF_POPUP 2
184
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185/*
186 * Redraw the current window later, with update_screen(type).
187 * Set must_redraw only if not already set to a higher value.
Bram Moolenaarae654382019-01-17 21:09:05 +0100188 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 */
190 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100191redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192{
193 redraw_win_later(curwin, type);
194}
195
196 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100197redraw_win_later(
198 win_T *wp,
199 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200201 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 {
203 wp->w_redr_type = type;
204 if (type >= NOT_VALID)
205 wp->w_lines_valid = 0;
206 if (must_redraw < type) /* must_redraw is the maximum of all windows */
207 must_redraw = type;
208 }
209}
210
211/*
212 * Force a complete redraw later. Also resets the highlighting. To be used
213 * after executing a shell command that messes up the screen.
214 */
215 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100216redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217{
218 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000219#ifdef FEAT_GUI
220 if (gui.in_use)
221 /* Use a code that will reset gui.highlight_mask in
222 * gui_stop_highlight(). */
223 screen_attr = HL_ALL + 1;
224 else
225#endif
226 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200227 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228}
229
230/*
231 * Mark all windows to be redrawn later.
232 */
233 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100234redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 win_T *wp;
237
238 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 redraw_win_later(wp, type);
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100240 // This may be needed when switching tabs.
241 if (must_redraw < type)
242 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243}
244
245/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000246 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 */
248 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100249redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250{
251 redraw_buf_later(curbuf, type);
252}
253
254 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100255redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256{
257 win_T *wp;
258
259 FOR_ALL_WINDOWS(wp)
260 {
261 if (wp->w_buffer == buf)
262 redraw_win_later(wp, type);
263 }
264}
265
Bram Moolenaar113e1072019-01-20 15:30:40 +0100266#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200267 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100268redraw_buf_line_later(buf_T *buf, linenr_T lnum)
269{
270 win_T *wp;
271
272 FOR_ALL_WINDOWS(wp)
273 if (wp->w_buffer == buf && lnum >= wp->w_topline
274 && lnum < wp->w_botline)
275 redrawWinline(wp, lnum);
276}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100277#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100278
Bram Moolenaar113e1072019-01-20 15:30:40 +0100279#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100280 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200281redraw_buf_and_status_later(buf_T *buf, int type)
282{
283 win_T *wp;
284
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200285#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200286 if (wild_menu_showing != 0)
287 /* Don't redraw while the command line completion is displayed, it
288 * would disappear. */
289 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200290#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200291 FOR_ALL_WINDOWS(wp)
292 {
293 if (wp->w_buffer == buf)
294 {
295 redraw_win_later(wp, type);
296 wp->w_redr_status = TRUE;
297 }
298 }
299}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100300#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200301
Bram Moolenaar113e1072019-01-20 15:30:40 +0100302#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200304 * Redraw as soon as possible. When the command line is not scrolled redraw
305 * right away and restore what was on the command line.
306 * Return a code indicating what happened.
307 */
308 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100309redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200310{
311 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200312 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313 int r;
314 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200315 schar_T *screenline; /* copy from ScreenLines[] */
316 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200317 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200318 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200319 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200320 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200321
322 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200323 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200324 return ret;
325
326 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200327 rows = screen_Rows - cmdline_row;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200328 screenline = LALLOC_MULT(schar_T, rows * cols);
329 screenattr = LALLOC_MULT(sattr_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200330 if (screenline == NULL || screenattr == NULL)
331 ret = 2;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200332 if (enc_utf8)
333 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200334 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200335 if (screenlineUC == NULL)
336 ret = 2;
337 for (i = 0; i < p_mco; ++i)
338 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200339 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200340 if (screenlineC[i] == NULL)
341 ret = 2;
342 }
343 }
344 if (enc_dbcs == DBCS_JPNU)
345 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200346 screenline2 = LALLOC_MULT(schar_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200347 if (screenline2 == NULL)
348 ret = 2;
349 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200350
351 if (ret != 2)
352 {
353 /* Save the text displayed in the command line area. */
354 for (r = 0; r < rows; ++r)
355 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 (size_t)cols * sizeof(schar_T));
359 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200360 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200362 if (enc_utf8)
363 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200364 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200368 mch_memmove(screenlineC[i] + r * cols,
369 ScreenLinesC[i] + LineOffset[cmdline_row + r],
370 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200371 }
372 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200373 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200374 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200375 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200376 }
377
378 update_screen(0);
379 ret = 3;
380
381 if (must_redraw == 0)
382 {
383 int off = (int)(current_ScreenLine - ScreenLines);
384
385 /* Restore the text displayed in the command line area. */
386 for (r = 0; r < rows; ++r)
387 {
388 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200389 screenline + r * cols,
390 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200391 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200392 screenattr + r * cols,
393 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200394 if (enc_utf8)
395 {
396 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200397 screenlineUC + r * cols,
398 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200399 for (i = 0; i < p_mco; ++i)
400 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200401 screenlineC[i] + r * cols,
402 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200403 }
404 if (enc_dbcs == DBCS_JPNU)
405 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200406 screenline2 + r * cols,
407 (size_t)cols * sizeof(schar_T));
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200408 screen_line(cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200409 }
410 ret = 4;
411 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200412 }
413
414 vim_free(screenline);
415 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200416 if (enc_utf8)
417 {
418 vim_free(screenlineUC);
419 for (i = 0; i < p_mco; ++i)
420 vim_free(screenlineC[i]);
421 }
422 if (enc_dbcs == DBCS_JPNU)
423 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200424
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200425 /* Show the intro message when appropriate. */
426 maybe_intro_message();
427
428 setcursor();
429
Bram Moolenaar2951b772013-07-03 12:45:31 +0200430 return ret;
431}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100432#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200433
434/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100435 * Invoked after an asynchronous callback is called.
436 * If an echo command was used the cursor needs to be put back where
437 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200438 * If "call_update_screen" is FALSE don't call update_screen() when at the
439 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100440 */
441 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200442redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100443{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200444 ++redrawing_for_callback;
445
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100446 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200447 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100448 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200449 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200450 // Don't redraw when in prompt_for_number().
451 if (cmdline_row > 0)
452 {
453 // Redrawing only works when the screen didn't scroll. Don't clear
454 // wildmenu entries.
455 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200456#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200457 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200458#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200459 && call_update_screen)
460 update_screen(0);
461
462 // Redraw in the same position, so that the user can continue
463 // editing the command.
464 redrawcmdline_ex(FALSE);
465 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200466 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200467 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100468 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200469 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200470 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100471 setcursor();
472 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100473 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100474#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100475 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200476 // Don't update the cursor when it is blinking and off to avoid
477 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100478 out_flush_cursor(FALSE, FALSE);
479 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100480#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100481 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200482
483 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100484}
485
486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 * Changed something in the current window, at buffer line "lnum", that
488 * requires that line and possibly other lines to be redrawn.
489 * Used when entering/leaving Insert mode with the cursor on a folded line.
490 * Used to remove the "$" from a change command.
491 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
492 * may become invalid and the whole window will have to be redrawn.
493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100495redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200496 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100497 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200499 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
500 wp->w_redraw_top = lnum;
501 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
502 wp->w_redraw_bot = lnum;
503 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504}
505
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200506/*
507 * To be called when "updating_screen" was set before and now the postponed
508 * side effects may take place.
509 */
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200510 void
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200511after_updating_screen(int may_resize_shell UNUSED)
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200512{
513 updating_screen = FALSE;
514#ifdef FEAT_GUI
515 if (may_resize_shell)
516 gui_may_resize_shell();
517#endif
518#ifdef FEAT_TERMINAL
519 term_check_channel_closed_recently();
520#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200521
522#ifdef HAVE_DROP_FILE
523 // If handle_drop() was called while updating_screen was TRUE need to
524 // handle the drop now.
525 handle_any_postponed_drop();
526#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200527}
528
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200530 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 */
532 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100533update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534{
535 redraw_curbuf_later(type);
536 update_screen(type);
537}
538
539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 * Based on the current value of curwin->w_topline, transfer a screenfull
541 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200542 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200544 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200545update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200547 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 win_T *wp;
549 static int did_intro = FALSE;
550#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
551 int did_one;
552#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200553#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200554 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200555 int gui_cursor_col = 0;
556 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200557#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200558 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000560 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200562 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200564 if (type == VALID_NO_UPDATE)
565 {
566 no_update = TRUE;
567 type = 0;
568 }
569
Bram Moolenaara3347722019-05-11 21:14:24 +0200570#ifdef FEAT_EVAL
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200571 {
572 buf_T *buf;
573
574 // Before updating the screen, notify any listeners of changed text.
575 FOR_ALL_BUFFERS(buf)
576 invoke_listeners(buf);
577 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200578#endif
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (must_redraw)
581 {
582 if (type < must_redraw) /* use maximal type */
583 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000584
585 /* must_redraw is reset here, so that when we run into some weird
586 * reason to redraw while busy redrawing (e.g., asynchronous
587 * scrolling), or update_topline() in win_update() will cause a
588 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 must_redraw = 0;
590 }
591
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200592 /* May need to update w_lines[]. */
593 if (curwin->w_lines_valid == 0 && type < NOT_VALID
594#ifdef FEAT_TERMINAL
595 && !term_do_update_window(curwin)
596#endif
597 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 type = NOT_VALID;
599
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000600 /* Postpone the redrawing when it's not needed and when being called
601 * recursively. */
602 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 {
604 redraw_later(type); /* remember type for next time */
605 must_redraw = type;
606 if (type > INVERTED_ALL)
607 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200608 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +0200610 updating_screen = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200611
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200612#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200613 // Update popup_mask if needed. This may set w_redraw_top and w_redraw_bot
614 // in some windows.
615 may_update_popup_mask(type);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#ifdef FEAT_SYN_HL
619 ++display_tick; /* let syntax code know we're in a next round of
620 * display updating */
621#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200622 if (no_update)
623 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
625 /*
626 * if the screen was scrolled up when displaying a message, scroll it down
627 */
628 if (msg_scrolled)
629 {
630 clear_cmdline = TRUE;
631 if (msg_scrolled > Rows - 5) /* clearing is faster */
632 type = CLEAR;
633 else if (type != CLEAR)
634 {
635 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200636 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
637 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 type = CLEAR;
639 FOR_ALL_WINDOWS(wp)
640 {
Bram Moolenaar98fb65c2019-06-02 20:33:32 +0200641 if (wp->w_winrow < msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 {
643 if (W_WINROW(wp) + wp->w_height > msg_scrolled
644 && wp->w_redr_type < REDRAW_TOP
645 && wp->w_lines_valid > 0
646 && wp->w_topline == wp->w_lines[0].wl_lnum)
647 {
648 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
649 wp->w_redr_type = REDRAW_TOP;
650 }
651 else
652 {
653 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200654 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
655 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 }
658 }
659 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200660 if (!no_update)
661 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000662 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
664 msg_scrolled = 0;
665 need_wait_return = FALSE;
666 }
667
668 /* reset cmdline_row now (may have been changed temporarily) */
669 compute_cmdrow();
670
671 /* Check for changed highlighting */
672 if (need_highlight_changed)
673 highlight_changed();
674
675 if (type == CLEAR) /* first clear screen */
676 {
677 screenclear(); /* will reset clear_cmdline */
678 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200679 /* must_redraw may be set indirectly, avoid another redraw later */
680 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 }
682
683 if (clear_cmdline) /* going to clear cmdline (done below) */
684 check_for_delay(FALSE);
685
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000686#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200687 /* Force redraw when width of 'number' or 'relativenumber' column
688 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000689 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200690 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
691 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000692 curwin->w_redr_type = NOT_VALID;
693#endif
694
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 /*
696 * Only start redrawing if there is really something to do.
697 */
698 if (type == INVERTED)
699 update_curswant();
700 if (curwin->w_redr_type < type
701 && !((type == VALID
702 && curwin->w_lines[0].wl_valid
703#ifdef FEAT_DIFF
704 && curwin->w_topfill == curwin->w_old_topfill
705 && curwin->w_botfill == curwin->w_old_botfill
706#endif
707 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000709 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
711 && curwin->w_old_visual_mode == VIsual_mode
712 && (curwin->w_valid & VALID_VIRTCOL)
713 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 ))
715 curwin->w_redr_type = type;
716
Bram Moolenaar5a305422006-04-28 22:38:25 +0000717 /* Redraw the tab pages line if needed. */
718 if (redraw_tabline || type >= NOT_VALID)
719 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#ifdef FEAT_SYN_HL
722 /*
723 * Correct stored syntax highlighting info for changes in each displayed
724 * buffer. Each buffer must only be done once.
725 */
726 FOR_ALL_WINDOWS(wp)
727 {
728 if (wp->w_buffer->b_mod_set)
729 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 win_T *wwp;
731
732 /* Check if we already did this buffer. */
733 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
734 if (wwp->w_buffer == wp->w_buffer)
735 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200736 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 syn_stack_apply_changes(wp->w_buffer);
738 }
739 }
740#endif
741
742 /*
743 * Go from top to bottom through the windows, redrawing the ones that need
744 * it.
745 */
746#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
747 did_one = FALSE;
748#endif
749#ifdef FEAT_SEARCH_EXTRA
750 search_hl.rm.regprog = NULL;
751#endif
752 FOR_ALL_WINDOWS(wp)
753 {
754 if (wp->w_redr_type != 0)
755 {
756 cursor_off();
757#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
758 if (!did_one)
759 {
760 did_one = TRUE;
761# ifdef FEAT_SEARCH_EXTRA
762 start_search_hl();
763# endif
764# ifdef FEAT_CLIPBOARD
765 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200766 if (clip_star.available && clip_isautosel_star())
767 clip_update_selection(&clip_star);
768 if (clip_plus.available && clip_isautosel_plus())
769 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770# endif
771#ifdef FEAT_GUI
772 /* Remove the cursor before starting to do anything, because
773 * scrolling may make it difficult to redraw the text under
774 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200775 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200776 {
777 gui_cursor_col = gui.cursor_col;
778 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200780 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782#endif
783 }
784#endif
785 win_update(wp);
786 }
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 /* redraw status line after the window to minimize cursor movement */
789 if (wp->w_redr_status)
790 {
791 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200792 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 }
795#if defined(FEAT_SEARCH_EXTRA)
796 end_search_hl();
797#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100798#ifdef FEAT_INS_EXPAND
799 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200800 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 /* Reset b_mod_set flags. Going through all windows is probably faster
804 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200805 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200808 after_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
810 /* Clear or redraw the command line. Done last, because scrolling may
811 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200812 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 showmode();
814
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200815 if (no_update)
816 --no_win_do_lines_ins;
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200819 if (!did_intro)
820 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 did_intro = TRUE;
822
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200823#ifdef FEAT_TEXT_PROP
Bram Moolenaar988c4332019-06-02 14:12:11 +0200824 // Display popup windows on top of the windows.
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200825 update_popups(win_update);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200826#endif
827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828#ifdef FEAT_GUI
829 /* Redraw the cursor and update the scrollbars when all screen updating is
830 * done. */
831 if (gui.in_use)
832 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200833 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200834 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100835 mch_disable_flush();
836 out_flush(); /* required before updating the cursor */
837 mch_enable_flush();
838
Bram Moolenaar144445d2016-07-08 21:41:54 +0200839 /* Put the GUI position where the cursor was, gui_update_cursor()
840 * uses that. */
841 gui.col = gui_cursor_col;
842 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200843 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100845 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200846 screen_cur_col = gui.col;
847 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200848 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100849 else
850 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 gui_update_scrollbars(FALSE);
852 }
853#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200854 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855}
856
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100857#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100858/*
859 * Prepare for updating one or more windows.
860 * Caller must check for "updating_screen" already set to avoid recursiveness.
861 */
862 static void
863update_prepare(void)
864{
865 cursor_off();
866 updating_screen = TRUE;
867#ifdef FEAT_GUI
868 /* Remove the cursor before starting to do anything, because scrolling may
869 * make it difficult to redraw the text under it. */
870 if (gui.in_use)
871 gui_undraw_cursor();
872#endif
873#ifdef FEAT_SEARCH_EXTRA
874 start_search_hl();
875#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +0200876#ifdef FEAT_TEXT_PROP
877 // Update popup_mask if needed.
Bram Moolenaar68acb412019-06-26 03:40:36 +0200878 may_update_popup_mask(must_redraw);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200879#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100880}
881
882/*
883 * Finish updating one or more windows.
884 */
885 static void
886update_finish(void)
887{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200888 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100889 showmode();
890
891# ifdef FEAT_SEARCH_EXTRA
892 end_search_hl();
893# endif
894
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200895 after_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100896
897# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100898 /* Redraw the cursor and update the scrollbars when all screen updating is
899 * done. */
900 if (gui.in_use)
901 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100902 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100903 gui_update_scrollbars(FALSE);
904 }
905# endif
906}
907#endif
908
Bram Moolenaar860cae12010-06-05 23:22:07 +0200909#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200910/*
911 * Return TRUE if the cursor line in window "wp" may be concealed, according
912 * to the 'concealcursor' option.
913 */
914 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100915conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200916{
917 int c;
918
919 if (*wp->w_p_cocu == NUL)
920 return FALSE;
921 if (get_real_state() & VISUAL)
922 c = 'v';
923 else if (State & INSERT)
924 c = 'i';
925 else if (State & NORMAL)
926 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200927 else if (State & CMDLINE)
928 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200929 else
930 return FALSE;
931 return vim_strchr(wp->w_p_cocu, c) != NULL;
932}
933
934/*
935 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
936 */
937 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200938conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200939{
940 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
941 {
942 need_cursor_line_redraw = TRUE;
943 /* Need to recompute cursor column, e.g., when starting Visual mode
944 * without concealing. */
945 curs_columns(TRUE);
946 }
947}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948#endif
949
Bram Moolenaar113e1072019-01-20 15:30:40 +0100950#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100952update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953{
954 win_T *wp;
955 int doit = FALSE;
956
957# ifdef FEAT_FOLDING
958 win_foldinfo.fi_level = 0;
959# endif
960
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100961 // update/delete a specific sign
962 redraw_buf_line_later(buf, lnum);
963
964 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 if (wp->w_redr_type != 0)
967 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100969 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200970 * happening (recursive call), messages on the screen or still starting up.
971 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100972 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200973 || State == ASKMORE || State == HITRETURN
974 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100975#ifdef FEAT_GUI
976 || gui.starting
977#endif
978 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 return;
980
981 /* update all windows that need updating */
982 update_prepare();
983
Bram Moolenaar29323592016-07-24 22:04:11 +0200984 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 {
986 if (wp->w_redr_type != 0)
987 win_update(wp);
988 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200989 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991
992 update_finish();
993}
994#endif
995
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200996/*
997 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
998 * window then get the "Pmenu" highlight attribute.
999 */
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001000 int
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001001get_wcr_attr(win_T *wp)
1002{
1003 int wcr_attr = 0;
1004
1005 if (*wp->w_p_wcr != NUL)
1006 wcr_attr = syn_name2attr(wp->w_p_wcr);
1007#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001008 if (WIN_IS_POPUP(wp) && wcr_attr == 0)
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001009 wcr_attr = HL_ATTR(HLF_PNI);
1010#endif
1011 return wcr_attr;
1012}
1013
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014#if defined(FEAT_GUI) || defined(PROTO)
1015/*
1016 * Update a single window, its status line and maybe the command line msg.
1017 * Used for the GUI scrollbar.
1018 */
1019 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001020updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001022 /* return if already busy updating */
1023 if (updating_screen)
1024 return;
1025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 update_prepare();
1027
1028#ifdef FEAT_CLIPBOARD
1029 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001030 if (clip_star.available && clip_isautosel_star())
1031 clip_update_selection(&clip_star);
1032 if (clip_plus.available && clip_isautosel_plus())
1033 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001035
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001037
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001038 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001039 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001040 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001041
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 if (wp->w_redr_status
1043# ifdef FEAT_CMDL_INFO
1044 || p_ru
1045# endif
1046# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001047 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048# endif
1049 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001050 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
Bram Moolenaar988c4332019-06-02 14:12:11 +02001052#ifdef FEAT_TEXT_PROP
1053 // Display popup windows on top of everything.
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001054 update_popups(win_update);
Bram Moolenaar988c4332019-06-02 14:12:11 +02001055#endif
1056
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 update_finish();
1058}
1059#endif
1060
1061/*
1062 * Update a single window.
1063 *
1064 * This may cause the windows below it also to be redrawn (when clearing the
1065 * screen or scrolling lines).
1066 *
1067 * How the window is redrawn depends on wp->w_redr_type. Each type also
1068 * implies the one below it.
1069 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001070 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1072 * INVERTED redraw the changed part of the Visual area
1073 * INVERTED_ALL redraw the whole Visual area
1074 * VALID 1. scroll up/down to adjust for a changed w_topline
1075 * 2. update lines at the top when scrolled down
1076 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001077 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 * b_mod_top and b_mod_bot.
1079 * - if wp->w_redraw_top non-zero, redraw lines between
1080 * wp->w_redraw_top and wp->w_redr_bot.
1081 * - continue redrawing when syntax status is invalid.
1082 * 4. if scrolled up, update lines at the bottom.
1083 * This results in three areas that may need updating:
1084 * top: from first row to top_end (when scrolled down)
1085 * mid: from mid_start to mid_end (update inversion or changed text)
1086 * bot: from bot_start to last row (when scrolled up)
1087 */
1088 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001089win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
1091 buf_T *buf = wp->w_buffer;
1092 int type;
1093 int top_end = 0; /* Below last row of the top area that needs
1094 updating. 0 when no top area updating. */
1095 int mid_start = 999;/* first row of the mid area that needs
1096 updating. 999 when no mid area updating. */
1097 int mid_end = 0; /* Below last row of the mid area that needs
1098 updating. 0 when no mid area updating. */
1099 int bot_start = 999;/* first row of the bot area that needs
1100 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 int scrolled_down = FALSE; /* TRUE when scrolled down when
1102 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001104 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 int top_to_mod = FALSE; /* redraw above mod_top */
1106#endif
1107
1108 int row; /* current window row to display */
1109 linenr_T lnum; /* current buffer lnum to display */
1110 int idx; /* current index in w_lines[] */
1111 int srow; /* starting row of the current line */
1112
1113 int eof = FALSE; /* if TRUE, we hit the end of the file */
1114 int didline = FALSE; /* if TRUE, we finished the last line */
1115 int i;
1116 long j;
1117 static int recursive = FALSE; /* being called recursively */
1118 int old_botline = wp->w_botline;
1119#ifdef FEAT_FOLDING
1120 long fold_count;
1121#endif
1122#ifdef FEAT_SYN_HL
1123 /* remember what happened to the previous line, to know if
1124 * check_visual_highlight() can be used */
1125#define DID_NONE 1 /* didn't update a line */
1126#define DID_LINE 2 /* updated a normal line */
1127#define DID_FOLD 3 /* updated a folded line */
1128 int did_update = DID_NONE;
1129 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1130#endif
1131 linenr_T mod_top = 0;
1132 linenr_T mod_bot = 0;
1133#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1134 int save_got_int;
1135#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001136#ifdef SYN_TIME_LIMIT
1137 proftime_T syntax_tm;
1138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139
1140 type = wp->w_redr_type;
1141
1142 if (type == NOT_VALID)
1143 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 wp->w_lines_valid = 0;
1146 }
1147
1148 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001149 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 {
1151 wp->w_redr_type = 0;
1152 return;
1153 }
1154
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 /* Window is zero-width: Only need to draw the separator. */
1156 if (wp->w_width == 0)
1157 {
1158 /* draw the vertical separator right of this window */
1159 draw_vsep_win(wp, 0);
1160 wp->w_redr_type = 0;
1161 return;
1162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001164#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001165 // If this window contains a terminal, redraw works completely differently.
1166 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001167 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001168 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001169# ifdef FEAT_MENU
1170 /* Draw the window toolbar, if there is one. */
1171 if (winbar_height(wp) > 0)
1172 redraw_win_toolbar(wp);
1173# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001174 wp->w_redr_type = 0;
1175 return;
1176 }
1177#endif
1178
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001180 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181#endif
1182
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001183#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001184 /* Force redraw when width of 'number' or 'relativenumber' column
1185 * changes. */
1186 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001187 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001188 {
1189 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001190 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001191 }
1192 else
1193#endif
1194
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1196 {
1197 /*
1198 * When there are both inserted/deleted lines and specific lines to be
1199 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1200 * everything (only happens when redrawing is off for while).
1201 */
1202 type = NOT_VALID;
1203 }
1204 else
1205 {
1206 /*
1207 * Set mod_top to the first line that needs displaying because of
1208 * changes. Set mod_bot to the first line after the changes.
1209 */
1210 mod_top = wp->w_redraw_top;
1211 if (wp->w_redraw_bot != 0)
1212 mod_bot = wp->w_redraw_bot + 1;
1213 else
1214 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (buf->b_mod_set)
1216 {
1217 if (mod_top == 0 || mod_top > buf->b_mod_top)
1218 {
1219 mod_top = buf->b_mod_top;
1220#ifdef FEAT_SYN_HL
1221 /* Need to redraw lines above the change that may be included
1222 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001223 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001225 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (mod_top < 1)
1227 mod_top = 1;
1228 }
1229#endif
1230 }
1231 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1232 mod_bot = buf->b_mod_bot;
1233
1234#ifdef FEAT_SEARCH_EXTRA
1235 /* When 'hlsearch' is on and using a multi-line search pattern, a
1236 * change in one line may make the Search highlighting in a
1237 * previous line invalid. Simple solution: redraw all visible
1238 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001239 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001241 if (search_hl.rm.regprog != NULL
1242 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001244 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001245 {
1246 cur = wp->w_match_head;
1247 while (cur != NULL)
1248 {
1249 if (cur->match.regprog != NULL
1250 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001251 {
1252 top_to_mod = TRUE;
1253 break;
1254 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001255 cur = cur->next;
1256 }
1257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258#endif
1259 }
1260#ifdef FEAT_FOLDING
1261 if (mod_top != 0 && hasAnyFolding(wp))
1262 {
1263 linenr_T lnumt, lnumb;
1264
1265 /*
1266 * A change in a line can cause lines above it to become folded or
1267 * unfolded. Find the top most buffer line that may be affected.
1268 * If the line was previously folded and displayed, get the first
1269 * line of that fold. If the line is folded now, get the first
1270 * folded line. Use the minimum of these two.
1271 */
1272
1273 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1274 * the line below it. If there is no valid entry, use w_topline.
1275 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1276 * to this line. If there is no valid entry, use MAXLNUM. */
1277 lnumt = wp->w_topline;
1278 lnumb = MAXLNUM;
1279 for (i = 0; i < wp->w_lines_valid; ++i)
1280 if (wp->w_lines[i].wl_valid)
1281 {
1282 if (wp->w_lines[i].wl_lastlnum < mod_top)
1283 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1284 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1285 {
1286 lnumb = wp->w_lines[i].wl_lnum;
1287 /* When there is a fold column it might need updating
1288 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001289 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 ++lnumb;
1291 }
1292 }
1293
1294 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1295 if (mod_top > lnumt)
1296 mod_top = lnumt;
1297
1298 /* Now do the same for the bottom line (one above mod_bot). */
1299 --mod_bot;
1300 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1301 ++mod_bot;
1302 if (mod_bot < lnumb)
1303 mod_bot = lnumb;
1304 }
1305#endif
1306
1307 /* When a change starts above w_topline and the end is below
1308 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001309 * If the end of the change is above w_topline: do like no change was
1310 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (mod_top != 0 && mod_top < wp->w_topline)
1312 {
1313 if (mod_bot > wp->w_topline)
1314 mod_top = wp->w_topline;
1315#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001316 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 top_end = 1;
1318#endif
1319 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001320
1321 /* When line numbers are displayed need to redraw all lines below
1322 * inserted/deleted lines. */
1323 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1324 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001326 wp->w_redraw_top = 0; // reset for next time
1327 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328
1329 /*
1330 * When only displaying the lines at the top, set top_end. Used when
1331 * window has scrolled down for msg_scrolled.
1332 */
1333 if (type == REDRAW_TOP)
1334 {
1335 j = 0;
1336 for (i = 0; i < wp->w_lines_valid; ++i)
1337 {
1338 j += wp->w_lines[i].wl_size;
1339 if (j >= wp->w_upd_rows)
1340 {
1341 top_end = j;
1342 break;
1343 }
1344 }
1345 if (top_end == 0)
1346 /* not found (cannot happen?): redraw everything */
1347 type = NOT_VALID;
1348 else
1349 /* top area defined, the rest is VALID */
1350 type = VALID;
1351 }
1352
Bram Moolenaar367329b2007-08-30 11:53:22 +00001353 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001354 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1355 * non-zero and thus not FALSE) will indicate that screenclear() was not
1356 * called. */
1357 if (screen_cleared)
1358 screen_cleared = MAYBE;
1359
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 /*
1361 * If there are no changes on the screen that require a complete redraw,
1362 * handle three cases:
1363 * 1: we are off the top of the screen by a few lines: scroll down
1364 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1365 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1366 * w_lines[] that needs updating.
1367 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001368 if ((type == VALID || type == SOME_VALID
1369 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370#ifdef FEAT_DIFF
1371 && !wp->w_botfill && !wp->w_old_botfill
1372#endif
1373 )
1374 {
1375 if (mod_top != 0 && wp->w_topline == mod_top)
1376 {
1377 /*
1378 * w_topline is the first changed line, the scrolling will be done
1379 * further down.
1380 */
1381 }
1382 else if (wp->w_lines[0].wl_valid
1383 && (wp->w_topline < wp->w_lines[0].wl_lnum
1384#ifdef FEAT_DIFF
1385 || (wp->w_topline == wp->w_lines[0].wl_lnum
1386 && wp->w_topfill > wp->w_old_topfill)
1387#endif
1388 ))
1389 {
1390 /*
1391 * New topline is above old topline: May scroll down.
1392 */
1393#ifdef FEAT_FOLDING
1394 if (hasAnyFolding(wp))
1395 {
1396 linenr_T ln;
1397
1398 /* count the number of lines we are off, counting a sequence
1399 * of folded lines as one */
1400 j = 0;
1401 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1402 {
1403 ++j;
1404 if (j >= wp->w_height - 2)
1405 break;
1406 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1407 }
1408 }
1409 else
1410#endif
1411 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1412 if (j < wp->w_height - 2) /* not too far off */
1413 {
1414 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1415#ifdef FEAT_DIFF
1416 /* insert extra lines for previously invisible filler lines */
1417 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1418 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1419 - wp->w_old_topfill;
1420#endif
1421 if (i < wp->w_height - 2) /* less than a screen off */
1422 {
1423 /*
1424 * Try to insert the correct number of lines.
1425 * If not the last window, delete the lines at the bottom.
1426 * win_ins_lines may fail when the terminal can't do it.
1427 */
1428 if (i > 0)
1429 check_for_delay(FALSE);
1430 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1431 {
1432 if (wp->w_lines_valid != 0)
1433 {
1434 /* Need to update rows that are new, stop at the
1435 * first one that scrolled down. */
1436 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438
1439 /* Move the entries that were scrolled, disable
1440 * the entries for the lines to be redrawn. */
1441 if ((wp->w_lines_valid += j) > wp->w_height)
1442 wp->w_lines_valid = wp->w_height;
1443 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1444 wp->w_lines[idx] = wp->w_lines[idx - j];
1445 while (idx >= 0)
1446 wp->w_lines[idx--].wl_valid = FALSE;
1447 }
1448 }
1449 else
1450 mid_start = 0; /* redraw all lines */
1451 }
1452 else
1453 mid_start = 0; /* redraw all lines */
1454 }
1455 else
1456 mid_start = 0; /* redraw all lines */
1457 }
1458 else
1459 {
1460 /*
1461 * New topline is at or below old topline: May scroll up.
1462 * When topline didn't change, find first entry in w_lines[] that
1463 * needs updating.
1464 */
1465
1466 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1467 j = -1;
1468 row = 0;
1469 for (i = 0; i < wp->w_lines_valid; i++)
1470 {
1471 if (wp->w_lines[i].wl_valid
1472 && wp->w_lines[i].wl_lnum == wp->w_topline)
1473 {
1474 j = i;
1475 break;
1476 }
1477 row += wp->w_lines[i].wl_size;
1478 }
1479 if (j == -1)
1480 {
1481 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1482 * lines */
1483 mid_start = 0;
1484 }
1485 else
1486 {
1487 /*
1488 * Try to delete the correct number of lines.
1489 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1490 */
1491#ifdef FEAT_DIFF
1492 /* If the topline didn't change, delete old filler lines,
1493 * otherwise delete filler lines of the new topline... */
1494 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1495 row += wp->w_old_topfill;
1496 else
1497 row += diff_check_fill(wp, wp->w_topline);
1498 /* ... but don't delete new filler lines. */
1499 row -= wp->w_topfill;
1500#endif
1501 if (row > 0)
1502 {
1503 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001504 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1505 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 bot_start = wp->w_height - row;
1507 else
1508 mid_start = 0; /* redraw all lines */
1509 }
1510 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1511 {
1512 /*
1513 * Skip the lines (below the deleted lines) that are still
1514 * valid and don't need redrawing. Copy their info
1515 * upwards, to compensate for the deleted lines. Set
1516 * bot_start to the first row that needs redrawing.
1517 */
1518 bot_start = 0;
1519 idx = 0;
1520 for (;;)
1521 {
1522 wp->w_lines[idx] = wp->w_lines[j];
1523 /* stop at line that didn't fit, unless it is still
1524 * valid (no lines deleted) */
1525 if (row > 0 && bot_start + row
1526 + (int)wp->w_lines[j].wl_size > wp->w_height)
1527 {
1528 wp->w_lines_valid = idx + 1;
1529 break;
1530 }
1531 bot_start += wp->w_lines[idx++].wl_size;
1532
1533 /* stop at the last valid entry in w_lines[].wl_size */
1534 if (++j >= wp->w_lines_valid)
1535 {
1536 wp->w_lines_valid = idx;
1537 break;
1538 }
1539 }
1540#ifdef FEAT_DIFF
1541 /* Correct the first entry for filler lines at the top
1542 * when it won't get updated below. */
1543 if (wp->w_p_diff && bot_start > 0)
1544 wp->w_lines[0].wl_size =
1545 plines_win_nofill(wp, wp->w_topline, TRUE)
1546 + wp->w_topfill;
1547#endif
1548 }
1549 }
1550 }
1551
1552 /* When starting redraw in the first line, redraw all lines. When
1553 * there is only one window it's probably faster to clear the screen
1554 * first. */
1555 if (mid_start == 0)
1556 {
1557 mid_end = wp->w_height;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001558 if (ONE_WINDOW && !WIN_IS_POPUP(wp))
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001559 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001560 /* Clear the screen when it was not done by win_del_lines() or
1561 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1562 * then. */
1563 if (screen_cleared != TRUE)
1564 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001565 /* The screen was cleared, redraw the tab pages line. */
1566 if (redraw_tabline)
1567 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001570
1571 /* When win_del_lines() or win_ins_lines() caused the screen to be
1572 * cleared (only happens for the first window) or when screenclear()
1573 * was called directly above, "must_redraw" will have been set to
1574 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1575 if (screen_cleared == TRUE)
1576 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
1578 else
1579 {
1580 /* Not VALID or INVERTED: redraw all lines. */
1581 mid_start = 0;
1582 mid_end = wp->w_height;
1583 }
1584
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001585 if (type == SOME_VALID)
1586 {
1587 /* SOME_VALID: redraw all lines. */
1588 mid_start = 0;
1589 mid_end = wp->w_height;
1590 type = NOT_VALID;
1591 }
1592
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 /* check if we are updating or removing the inverted part */
1594 if ((VIsual_active && buf == curwin->w_buffer)
1595 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1596 {
1597 linenr_T from, to;
1598
1599 if (VIsual_active)
1600 {
1601 if (VIsual_active
1602 && (VIsual_mode != wp->w_old_visual_mode
1603 || type == INVERTED_ALL))
1604 {
1605 /*
1606 * If the type of Visual selection changed, redraw the whole
1607 * selection. Also when the ownership of the X selection is
1608 * gained or lost.
1609 */
1610 if (curwin->w_cursor.lnum < VIsual.lnum)
1611 {
1612 from = curwin->w_cursor.lnum;
1613 to = VIsual.lnum;
1614 }
1615 else
1616 {
1617 from = VIsual.lnum;
1618 to = curwin->w_cursor.lnum;
1619 }
1620 /* redraw more when the cursor moved as well */
1621 if (wp->w_old_cursor_lnum < from)
1622 from = wp->w_old_cursor_lnum;
1623 if (wp->w_old_cursor_lnum > to)
1624 to = wp->w_old_cursor_lnum;
1625 if (wp->w_old_visual_lnum < from)
1626 from = wp->w_old_visual_lnum;
1627 if (wp->w_old_visual_lnum > to)
1628 to = wp->w_old_visual_lnum;
1629 }
1630 else
1631 {
1632 /*
1633 * Find the line numbers that need to be updated: The lines
1634 * between the old cursor position and the current cursor
1635 * position. Also check if the Visual position changed.
1636 */
1637 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1638 {
1639 from = curwin->w_cursor.lnum;
1640 to = wp->w_old_cursor_lnum;
1641 }
1642 else
1643 {
1644 from = wp->w_old_cursor_lnum;
1645 to = curwin->w_cursor.lnum;
1646 if (from == 0) /* Visual mode just started */
1647 from = to;
1648 }
1649
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001650 if (VIsual.lnum != wp->w_old_visual_lnum
1651 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 {
1653 if (wp->w_old_visual_lnum < from
1654 && wp->w_old_visual_lnum != 0)
1655 from = wp->w_old_visual_lnum;
1656 if (wp->w_old_visual_lnum > to)
1657 to = wp->w_old_visual_lnum;
1658 if (VIsual.lnum < from)
1659 from = VIsual.lnum;
1660 if (VIsual.lnum > to)
1661 to = VIsual.lnum;
1662 }
1663 }
1664
1665 /*
1666 * If in block mode and changed column or curwin->w_curswant:
1667 * update all lines.
1668 * First compute the actual start and end column.
1669 */
1670 if (VIsual_mode == Ctrl_V)
1671 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001672 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001673#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001674 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaar404406a2014-10-09 13:24:43 +02001676 if (curwin->w_p_lbr)
1677 ve_flags = VE_ALL;
1678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001680#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001681 ve_flags = save_ve_flags;
1682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 ++toc;
1684 if (curwin->w_curswant == MAXCOL)
1685 toc = MAXCOL;
1686
1687 if (fromc != wp->w_old_cursor_fcol
1688 || toc != wp->w_old_cursor_lcol)
1689 {
1690 if (from > VIsual.lnum)
1691 from = VIsual.lnum;
1692 if (to < VIsual.lnum)
1693 to = VIsual.lnum;
1694 }
1695 wp->w_old_cursor_fcol = fromc;
1696 wp->w_old_cursor_lcol = toc;
1697 }
1698 }
1699 else
1700 {
1701 /* Use the line numbers of the old Visual area. */
1702 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1703 {
1704 from = wp->w_old_cursor_lnum;
1705 to = wp->w_old_visual_lnum;
1706 }
1707 else
1708 {
1709 from = wp->w_old_visual_lnum;
1710 to = wp->w_old_cursor_lnum;
1711 }
1712 }
1713
1714 /*
1715 * There is no need to update lines above the top of the window.
1716 */
1717 if (from < wp->w_topline)
1718 from = wp->w_topline;
1719
1720 /*
1721 * If we know the value of w_botline, use it to restrict the update to
1722 * the lines that are visible in the window.
1723 */
1724 if (wp->w_valid & VALID_BOTLINE)
1725 {
1726 if (from >= wp->w_botline)
1727 from = wp->w_botline - 1;
1728 if (to >= wp->w_botline)
1729 to = wp->w_botline - 1;
1730 }
1731
1732 /*
1733 * Find the minimal part to be updated.
1734 * Watch out for scrolling that made entries in w_lines[] invalid.
1735 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1736 * top_end; need to redraw from top_end to the "to" line.
1737 * A middle mouse click with a Visual selection may change the text
1738 * above the Visual area and reset wl_valid, do count these for
1739 * mid_end (in srow).
1740 */
1741 if (mid_start > 0)
1742 {
1743 lnum = wp->w_topline;
1744 idx = 0;
1745 srow = 0;
1746 if (scrolled_down)
1747 mid_start = top_end;
1748 else
1749 mid_start = 0;
1750 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1751 {
1752 if (wp->w_lines[idx].wl_valid)
1753 mid_start += wp->w_lines[idx].wl_size;
1754 else if (!scrolled_down)
1755 srow += wp->w_lines[idx].wl_size;
1756 ++idx;
1757# ifdef FEAT_FOLDING
1758 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1759 lnum = wp->w_lines[idx].wl_lnum;
1760 else
1761# endif
1762 ++lnum;
1763 }
1764 srow += mid_start;
1765 mid_end = wp->w_height;
1766 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1767 {
1768 if (wp->w_lines[idx].wl_valid
1769 && wp->w_lines[idx].wl_lnum >= to + 1)
1770 {
1771 /* Only update until first row of this line */
1772 mid_end = srow;
1773 break;
1774 }
1775 srow += wp->w_lines[idx].wl_size;
1776 }
1777 }
1778 }
1779
1780 if (VIsual_active && buf == curwin->w_buffer)
1781 {
1782 wp->w_old_visual_mode = VIsual_mode;
1783 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1784 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001785 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 wp->w_old_curswant = curwin->w_curswant;
1787 }
1788 else
1789 {
1790 wp->w_old_visual_mode = 0;
1791 wp->w_old_cursor_lnum = 0;
1792 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001793 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
1796#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1797 /* reset got_int, otherwise regexp won't work */
1798 save_got_int = got_int;
1799 got_int = 0;
1800#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001801#ifdef SYN_TIME_LIMIT
1802 /* Set the time limit to 'redrawtime'. */
1803 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001804 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806#ifdef FEAT_FOLDING
1807 win_foldinfo.fi_level = 0;
1808#endif
1809
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001810#ifdef FEAT_MENU
1811 /*
1812 * Draw the window toolbar, if there is one.
1813 * TODO: only when needed.
1814 */
1815 if (winbar_height(wp) > 0)
1816 redraw_win_toolbar(wp);
1817#endif
1818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 /*
1820 * Update all the window rows.
1821 */
1822 idx = 0; /* first entry in w_lines[].wl_size */
1823 row = 0;
1824 srow = 0;
1825 lnum = wp->w_topline; /* first line shown in window */
1826 for (;;)
1827 {
1828 /* stop updating when reached the end of the window (check for _past_
1829 * the end of the window is at the end of the loop) */
1830 if (row == wp->w_height)
1831 {
1832 didline = TRUE;
1833 break;
1834 }
1835
1836 /* stop updating when hit the end of the file */
1837 if (lnum > buf->b_ml.ml_line_count)
1838 {
1839 eof = TRUE;
1840 break;
1841 }
1842
1843 /* Remember the starting row of the line that is going to be dealt
1844 * with. It is used further down when the line doesn't fit. */
1845 srow = row;
1846
1847 /*
1848 * Update a line when it is in an area that needs updating, when it
1849 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001850 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 * win_del_lines(), check if the current line includes it.
1852 * When syntax folding is being used, the saved syntax states will
1853 * already have been updated, we can't see where the syntax state is
1854 * the same again, just update until the end of the window.
1855 */
1856 if (row < top_end
1857 || (row >= mid_start && row < mid_end)
1858#ifdef FEAT_SEARCH_EXTRA
1859 || top_to_mod
1860#endif
1861 || idx >= wp->w_lines_valid
1862 || (row + wp->w_lines[idx].wl_size > bot_start)
1863 || (mod_top != 0
1864 && (lnum == mod_top
1865 || (lnum >= mod_top
1866 && (lnum < mod_bot
1867#ifdef FEAT_SYN_HL
1868 || did_update == DID_FOLD
1869 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001870 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 && (
1872# ifdef FEAT_FOLDING
1873 (foldmethodIsSyntax(wp)
1874 && hasAnyFolding(wp)) ||
1875# endif
1876 syntax_check_changed(lnum)))
1877#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001878#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001879 /* match in fixed position might need redraw
1880 * if lines were inserted or deleted */
1881 || (wp->w_match_head != NULL
1882 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 )))))
1885 {
1886#ifdef FEAT_SEARCH_EXTRA
1887 if (lnum == mod_top)
1888 top_to_mod = FALSE;
1889#endif
1890
1891 /*
1892 * When at start of changed lines: May scroll following lines
1893 * up or down to minimize redrawing.
1894 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001895 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 */
1897 if (lnum == mod_top
1898 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001899 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
1901 int old_rows = 0;
1902 int new_rows = 0;
1903 int xtra_rows;
1904 linenr_T l;
1905
1906 /* Count the old number of window rows, using w_lines[], which
1907 * should still contain the sizes for the lines as they are
1908 * currently displayed. */
1909 for (i = idx; i < wp->w_lines_valid; ++i)
1910 {
1911 /* Only valid lines have a meaningful wl_lnum. Invalid
1912 * lines are part of the changed area. */
1913 if (wp->w_lines[i].wl_valid
1914 && wp->w_lines[i].wl_lnum == mod_bot)
1915 break;
1916 old_rows += wp->w_lines[i].wl_size;
1917#ifdef FEAT_FOLDING
1918 if (wp->w_lines[i].wl_valid
1919 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1920 {
1921 /* Must have found the last valid entry above mod_bot.
1922 * Add following invalid entries. */
1923 ++i;
1924 while (i < wp->w_lines_valid
1925 && !wp->w_lines[i].wl_valid)
1926 old_rows += wp->w_lines[i++].wl_size;
1927 break;
1928 }
1929#endif
1930 }
1931
1932 if (i >= wp->w_lines_valid)
1933 {
1934 /* We can't find a valid line below the changed lines,
1935 * need to redraw until the end of the window.
1936 * Inserting/deleting lines has no use. */
1937 bot_start = 0;
1938 }
1939 else
1940 {
1941 /* Able to count old number of rows: Count new window
1942 * rows, and may insert/delete lines */
1943 j = idx;
1944 for (l = lnum; l < mod_bot; ++l)
1945 {
1946#ifdef FEAT_FOLDING
1947 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1948 ++new_rows;
1949 else
1950#endif
1951#ifdef FEAT_DIFF
1952 if (l == wp->w_topline)
1953 new_rows += plines_win_nofill(wp, l, TRUE)
1954 + wp->w_topfill;
1955 else
1956#endif
1957 new_rows += plines_win(wp, l, TRUE);
1958 ++j;
1959 if (new_rows > wp->w_height - row - 2)
1960 {
1961 /* it's getting too much, must redraw the rest */
1962 new_rows = 9999;
1963 break;
1964 }
1965 }
1966 xtra_rows = new_rows - old_rows;
1967 if (xtra_rows < 0)
1968 {
1969 /* May scroll text up. If there is not enough
1970 * remaining text or scrolling fails, must redraw the
1971 * rest. If scrolling works, must redraw the text
1972 * below the scrolled text. */
1973 if (row - xtra_rows >= wp->w_height - 2)
1974 mod_bot = MAXLNUM;
1975 else
1976 {
1977 check_for_delay(FALSE);
1978 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001979 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 mod_bot = MAXLNUM;
1981 else
1982 bot_start = wp->w_height + xtra_rows;
1983 }
1984 }
1985 else if (xtra_rows > 0)
1986 {
1987 /* May scroll text down. If there is not enough
1988 * remaining text of scrolling fails, must redraw the
1989 * rest. */
1990 if (row + xtra_rows >= wp->w_height - 2)
1991 mod_bot = MAXLNUM;
1992 else
1993 {
1994 check_for_delay(FALSE);
1995 if (win_ins_lines(wp, row + old_rows,
1996 xtra_rows, FALSE, FALSE) == FAIL)
1997 mod_bot = MAXLNUM;
1998 else if (top_end > row + old_rows)
1999 /* Scrolled the part at the top that requires
2000 * updating down. */
2001 top_end += xtra_rows;
2002 }
2003 }
2004
2005 /* When not updating the rest, may need to move w_lines[]
2006 * entries. */
2007 if (mod_bot != MAXLNUM && i != j)
2008 {
2009 if (j < i)
2010 {
2011 int x = row + new_rows;
2012
2013 /* move entries in w_lines[] upwards */
2014 for (;;)
2015 {
2016 /* stop at last valid entry in w_lines[] */
2017 if (i >= wp->w_lines_valid)
2018 {
2019 wp->w_lines_valid = j;
2020 break;
2021 }
2022 wp->w_lines[j] = wp->w_lines[i];
2023 /* stop at a line that won't fit */
2024 if (x + (int)wp->w_lines[j].wl_size
2025 > wp->w_height)
2026 {
2027 wp->w_lines_valid = j + 1;
2028 break;
2029 }
2030 x += wp->w_lines[j++].wl_size;
2031 ++i;
2032 }
2033 if (bot_start > x)
2034 bot_start = x;
2035 }
2036 else /* j > i */
2037 {
2038 /* move entries in w_lines[] downwards */
2039 j -= i;
2040 wp->w_lines_valid += j;
2041 if (wp->w_lines_valid > wp->w_height)
2042 wp->w_lines_valid = wp->w_height;
2043 for (i = wp->w_lines_valid; i - j >= idx; --i)
2044 wp->w_lines[i] = wp->w_lines[i - j];
2045
2046 /* The w_lines[] entries for inserted lines are
2047 * now invalid, but wl_size may be used above.
2048 * Reset to zero. */
2049 while (i >= idx)
2050 {
2051 wp->w_lines[i].wl_size = 0;
2052 wp->w_lines[i--].wl_valid = FALSE;
2053 }
2054 }
2055 }
2056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 }
2058
2059#ifdef FEAT_FOLDING
2060 /*
2061 * When lines are folded, display one line for all of them.
2062 * Otherwise, display normally (can be several display lines when
2063 * 'wrap' is on).
2064 */
2065 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2066 if (fold_count != 0)
2067 {
2068 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2069 ++row;
2070 --fold_count;
2071 wp->w_lines[idx].wl_folded = TRUE;
2072 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2073# ifdef FEAT_SYN_HL
2074 did_update = DID_FOLD;
2075# endif
2076 }
2077 else
2078#endif
2079 if (idx < wp->w_lines_valid
2080 && wp->w_lines[idx].wl_valid
2081 && wp->w_lines[idx].wl_lnum == lnum
2082 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002083 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002084 && !WIN_IS_POPUP(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 && srow + wp->w_lines[idx].wl_size > wp->w_height
2086#ifdef FEAT_DIFF
2087 && diff_check_fill(wp, lnum) == 0
2088#endif
2089 )
2090 {
2091 /* This line is not going to fit. Don't draw anything here,
2092 * will draw "@ " lines below. */
2093 row = wp->w_height + 1;
2094 }
2095 else
2096 {
2097#ifdef FEAT_SEARCH_EXTRA
2098 prepare_search_hl(wp, lnum);
2099#endif
2100#ifdef FEAT_SYN_HL
2101 /* Let the syntax stuff know we skipped a few lines. */
2102 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002103 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 syntax_end_parsing(syntax_last_parsed + 1);
2105#endif
2106
2107 /*
2108 * Display one line.
2109 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002110 row = win_line(wp, lnum, srow, wp->w_height,
2111 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112
2113#ifdef FEAT_FOLDING
2114 wp->w_lines[idx].wl_folded = FALSE;
2115 wp->w_lines[idx].wl_lastlnum = lnum;
2116#endif
2117#ifdef FEAT_SYN_HL
2118 did_update = DID_LINE;
2119 syntax_last_parsed = lnum;
2120#endif
2121 }
2122
2123 wp->w_lines[idx].wl_lnum = lnum;
2124 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002125
2126 /* Past end of the window or end of the screen. Note that after
2127 * resizing wp->w_height may be end up too big. That's a problem
2128 * elsewhere, but prevent a crash here. */
2129 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 {
2131 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002132 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2134 ++idx;
2135 break;
2136 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002137 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 wp->w_lines[idx].wl_size = row - srow;
2139 ++idx;
2140#ifdef FEAT_FOLDING
2141 lnum += fold_count + 1;
2142#else
2143 ++lnum;
2144#endif
2145 }
2146 else
2147 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002148 if (wp->w_p_rnu)
2149 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002150#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002151 // 'relativenumber' set: The text doesn't need to be drawn, but
2152 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002153 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2154 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002155 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002156 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002157#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002158 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002159 }
2160
2161 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 row += wp->w_lines[idx++].wl_size;
2163 if (row > wp->w_height) /* past end of screen */
2164 break;
2165#ifdef FEAT_FOLDING
2166 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2167#else
2168 ++lnum;
2169#endif
2170#ifdef FEAT_SYN_HL
2171 did_update = DID_NONE;
2172#endif
2173 }
2174
2175 if (lnum > buf->b_ml.ml_line_count)
2176 {
2177 eof = TRUE;
2178 break;
2179 }
2180 }
2181 /*
2182 * End of loop over all window lines.
2183 */
2184
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002185#ifdef FEAT_VTP
2186 /* Rewrite the character at the end of the screen line. */
2187 if (use_vtp())
2188 {
2189 int i;
2190
2191 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002192 if (enc_utf8)
2193 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2194 LineOffset[i] + screen_Columns) > 1)
2195 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2196 else
2197 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2198 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002199 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2200 }
2201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202
2203 if (idx > wp->w_lines_valid)
2204 wp->w_lines_valid = idx;
2205
2206#ifdef FEAT_SYN_HL
2207 /*
2208 * Let the syntax stuff know we stop parsing here.
2209 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002210 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 syntax_end_parsing(syntax_last_parsed + 1);
2212#endif
2213
2214 /*
2215 * If we didn't hit the end of the file, and we didn't finish the last
2216 * line we were working on, then the line didn't fit.
2217 */
2218 wp->w_empty_rows = 0;
2219#ifdef FEAT_DIFF
2220 wp->w_filler_rows = 0;
2221#endif
2222 if (!eof && !didline)
2223 {
2224 if (lnum == wp->w_topline)
2225 {
2226 /*
2227 * Single line that does not fit!
2228 * Don't overwrite it, it can be edited.
2229 */
2230 wp->w_botline = lnum + 1;
2231 }
2232#ifdef FEAT_DIFF
2233 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2234 {
2235 /* Window ends in filler lines. */
2236 wp->w_botline = lnum;
2237 wp->w_filler_rows = wp->w_height - srow;
2238 }
2239#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002240#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002241 else if (WIN_IS_POPUP(wp))
Bram Moolenaar17146962019-05-30 00:12:11 +02002242 {
2243 // popup line that doesn't fit is left as-is
2244 wp->w_botline = lnum;
2245 }
2246#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002247 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2248 {
2249 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2250
2251 /*
2252 * Last line isn't finished: Display "@@@" in the last screen line.
2253 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002254 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002255 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002256 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002257 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002258 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002259 set_empty_rows(wp, srow);
2260 wp->w_botline = lnum;
2261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2263 {
2264 /*
2265 * Last line isn't finished: Display "@@@" at the end.
2266 */
2267 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2268 W_WINROW(wp) + wp->w_height,
2269 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002270 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 set_empty_rows(wp, srow);
2272 wp->w_botline = lnum;
2273 }
2274 else
2275 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002276 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 wp->w_botline = lnum;
2278 }
2279 }
2280 else
2281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 if (eof) /* we hit the end of the file */
2284 {
2285 wp->w_botline = buf->b_ml.ml_line_count + 1;
2286#ifdef FEAT_DIFF
2287 j = diff_check_fill(wp, wp->w_botline);
2288 if (j > 0 && !wp->w_botfill)
2289 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002290 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 if (char2cells(fill_diff) > 1)
2292 i = '-';
2293 else
2294 i = fill_diff;
2295 if (row + j > wp->w_height)
2296 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002297 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 row += j;
2299 }
2300#endif
2301 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002302 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 wp->w_botline = lnum;
2304
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002305 // Make sure the rest of the screen is blank
2306 // put '~'s on rows that aren't part of the file.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002307 win_draw_end(wp, WIN_IS_POPUP(wp) ? ' ' : '~',
2308 ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
2310
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002311#ifdef SYN_TIME_LIMIT
2312 syn_set_timeout(NULL);
2313#endif
2314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 /* Reset the type of redrawing required, the window has been updated. */
2316 wp->w_redr_type = 0;
2317#ifdef FEAT_DIFF
2318 wp->w_old_topfill = wp->w_topfill;
2319 wp->w_old_botfill = wp->w_botfill;
2320#endif
2321
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002322 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
2324 /*
2325 * There is a trick with w_botline. If we invalidate it on each
2326 * change that might modify it, this will cause a lot of expensive
2327 * calls to plines() in update_topline() each time. Therefore the
2328 * value of w_botline is often approximated, and this value is used to
2329 * compute the value of w_topline. If the value of w_botline was
2330 * wrong, check that the value of w_topline is correct (cursor is on
2331 * the visible part of the text). If it's not, we need to redraw
2332 * again. Mostly this just means scrolling up a few lines, so it
2333 * doesn't look too bad. Only do this for the current window (where
2334 * changes are relevant).
2335 */
2336 wp->w_valid |= VALID_BOTLINE;
2337 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2338 {
2339 recursive = TRUE;
2340 curwin->w_valid &= ~VALID_TOPLINE;
2341 update_topline(); /* may invalidate w_botline again */
2342 if (must_redraw != 0)
2343 {
2344 /* Don't update for changes in buffer again. */
2345 i = curbuf->b_mod_set;
2346 curbuf->b_mod_set = FALSE;
2347 win_update(curwin);
2348 must_redraw = 0;
2349 curbuf->b_mod_set = i;
2350 }
2351 recursive = FALSE;
2352 }
2353 }
2354
2355#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2356 /* restore got_int, unless CTRL-C was hit while redrawing */
2357 if (!got_int)
2358 got_int = save_got_int;
2359#endif
2360}
2361
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002363 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2364 * Return the new offset.
2365 */
2366 static int
2367screen_fill_end(
2368 win_T *wp,
2369 int c1,
2370 int c2,
2371 int off,
2372 int width,
2373 int row,
2374 int endrow,
2375 int attr)
2376{
2377 int nn = off + width;
2378
2379 if (nn > wp->w_width)
2380 nn = wp->w_width;
2381#ifdef FEAT_RIGHTLEFT
2382 if (wp->w_p_rl)
2383 {
2384 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2385 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2386 c1, c2, attr);
2387 }
2388 else
2389#endif
2390 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2391 wp->w_wincol + off, (int)wp->w_wincol + nn,
2392 c1, c2, attr);
2393 return nn;
2394}
2395
2396/*
2397 * Clear lines near the end the window and mark the unused lines with "c1".
2398 * use "c2" as the filler character.
2399 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 */
2401 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002402win_draw_end(
2403 win_T *wp,
2404 int c1,
2405 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002406 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002407 int row,
2408 int endrow,
2409 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002412 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002413 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002414
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002415 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002416
2417 if (draw_margin)
2418 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002419#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002420 int fdc = compute_foldcolumn(wp, 0);
2421
2422 if (fdc > 0)
2423 // draw the fold column
2424 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002425 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002426#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002427#ifdef FEAT_SIGNS
2428 if (signcolumn_on(wp))
2429 // draw the sign column
2430 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002431 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002432#endif
2433 if ((wp->w_p_nu || wp->w_p_rnu)
2434 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2435 // draw the number column
2436 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002437 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
2440#ifdef FEAT_RIGHTLEFT
2441 if (wp->w_p_rl)
2442 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002444 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002445 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002447 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002448 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
2450 else
2451#endif
2452 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002454 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002455 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002457
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 set_empty_rows(wp, row);
2459}
2460
Bram Moolenaar1a384422010-07-14 19:53:30 +02002461#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002462/*
2463 * Advance **color_cols and return TRUE when there are columns to draw.
2464 */
2465 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002466advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002467{
2468 while (**color_cols >= 0 && vcol > **color_cols)
2469 ++*color_cols;
2470 return (**color_cols >= 0);
2471}
2472#endif
2473
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002474#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2475/*
2476 * Copy "text" to ScreenLines using "attr".
2477 * Returns the next screen column.
2478 */
2479 static int
2480text_to_screenline(win_T *wp, char_u *text, int col)
2481{
2482 int off = (int)(current_ScreenLine - ScreenLines);
2483
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002484 if (has_mbyte)
2485 {
2486 int cells;
2487 int u8c, u8cc[MAX_MCO];
2488 int i;
2489 int idx;
2490 int c_len;
2491 char_u *p;
2492# ifdef FEAT_ARABIC
2493 int prev_c = 0; /* previous Arabic character */
2494 int prev_c1 = 0; /* first composing char for prev_c */
2495# endif
2496
2497# ifdef FEAT_RIGHTLEFT
2498 if (wp->w_p_rl)
2499 idx = off;
2500 else
2501# endif
2502 idx = off + col;
2503
2504 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2505 for (p = text; *p != NUL; )
2506 {
2507 cells = (*mb_ptr2cells)(p);
2508 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002509 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002510# ifdef FEAT_RIGHTLEFT
2511 - (wp->w_p_rl ? col : 0)
2512# endif
2513 )
2514 break;
2515 ScreenLines[idx] = *p;
2516 if (enc_utf8)
2517 {
2518 u8c = utfc_ptr2char(p, u8cc);
2519 if (*p < 0x80 && u8cc[0] == 0)
2520 {
2521 ScreenLinesUC[idx] = 0;
2522#ifdef FEAT_ARABIC
2523 prev_c = u8c;
2524#endif
2525 }
2526 else
2527 {
2528#ifdef FEAT_ARABIC
2529 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2530 {
2531 /* Do Arabic shaping. */
2532 int pc, pc1, nc;
2533 int pcc[MAX_MCO];
2534 int firstbyte = *p;
2535
2536 /* The idea of what is the previous and next
2537 * character depends on 'rightleft'. */
2538 if (wp->w_p_rl)
2539 {
2540 pc = prev_c;
2541 pc1 = prev_c1;
2542 nc = utf_ptr2char(p + c_len);
2543 prev_c1 = u8cc[0];
2544 }
2545 else
2546 {
2547 pc = utfc_ptr2char(p + c_len, pcc);
2548 nc = prev_c;
2549 pc1 = pcc[0];
2550 }
2551 prev_c = u8c;
2552
2553 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2554 pc, pc1, nc);
2555 ScreenLines[idx] = firstbyte;
2556 }
2557 else
2558 prev_c = u8c;
2559#endif
2560 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002561 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002562 for (i = 0; i < Screen_mco; ++i)
2563 {
2564 ScreenLinesC[i][idx] = u8cc[i];
2565 if (u8cc[i] == 0)
2566 break;
2567 }
2568 }
2569 if (cells > 1)
2570 ScreenLines[idx + 1] = 0;
2571 }
2572 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2573 /* double-byte single width character */
2574 ScreenLines2[idx] = p[1];
2575 else if (cells > 1)
2576 /* double-width character */
2577 ScreenLines[idx + 1] = p[1];
2578 col += cells;
2579 idx += cells;
2580 p += c_len;
2581 }
2582 }
2583 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002584 {
2585 int len = (int)STRLEN(text);
2586
Bram Moolenaar02631462017-09-22 15:20:32 +02002587 if (len > wp->w_width - col)
2588 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002589 if (len > 0)
2590 {
2591#ifdef FEAT_RIGHTLEFT
2592 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002593 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002594 else
2595#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002596 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002597 col += len;
2598 }
2599 }
2600 return col;
2601}
2602#endif
2603
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604#ifdef FEAT_FOLDING
2605/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002606 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2607 * space is available for window "wp", minus "col".
2608 */
2609 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002610compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002611{
2612 int fdc = wp->w_p_fdc;
2613 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002614 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002615
2616 if (fdc > wwidth - (col + wmw))
2617 fdc = wwidth - (col + wmw);
2618 return fdc;
2619}
2620
2621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 * Display one folded line.
2623 */
2624 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002625fold_line(
2626 win_T *wp,
2627 long fold_count,
2628 foldinfo_T *foldinfo,
2629 linenr_T lnum,
2630 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002632 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 pos_T *top, *bot;
2634 linenr_T lnume = lnum + fold_count - 1;
2635 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002636 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 int col;
2639 int txtcol;
2640 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002641 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
2643 /* Build the fold line:
2644 * 1. Add the cmdwin_type for the command-line window
2645 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002646 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 * 4. Compose the text
2648 * 5. Add the text
2649 * 6. set highlighting for the Visual area an other text
2650 */
2651 col = 0;
2652
2653 /*
2654 * 1. Add the cmdwin_type for the command-line window
2655 * Ignores 'rightleft', this window is never right-left.
2656 */
2657#ifdef FEAT_CMDWIN
2658 if (cmdwin_type != 0 && wp == curwin)
2659 {
2660 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002661 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (enc_utf8)
2663 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 ++col;
2665 }
2666#endif
2667
2668 /*
2669 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002670 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002672 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 if (fdc > 0)
2674 {
2675 fill_foldcolumn(buf, wp, TRUE, lnum);
2676#ifdef FEAT_RIGHTLEFT
2677 if (wp->w_p_rl)
2678 {
2679 int i;
2680
Bram Moolenaar02631462017-09-22 15:20:32 +02002681 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002682 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 /* reverse the fold column */
2684 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002685 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 }
2687 else
2688#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002689 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 col += fdc;
2691 }
2692
2693#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002694# define RL_MEMSET(p, v, l) \
2695 do { \
2696 if (wp->w_p_rl) \
2697 for (ri = 0; ri < l; ++ri) \
2698 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2699 else \
2700 for (ri = 0; ri < l; ++ri) \
2701 ScreenAttrs[off + (p) + ri] = v; \
2702 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002704# define RL_MEMSET(p, v, l) \
2705 do { \
2706 for (ri = 0; ri < l; ++ri) \
2707 ScreenAttrs[off + (p) + ri] = v; \
2708 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709#endif
2710
Bram Moolenaar64486672010-05-16 15:46:46 +02002711 /* Set all attributes of the 'number' or 'relativenumber' column and the
2712 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002713 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714
2715#ifdef FEAT_SIGNS
2716 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002717 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002719 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 if (len > 0)
2721 {
2722 if (len > 2)
2723 len = 2;
2724# ifdef FEAT_RIGHTLEFT
2725 if (wp->w_p_rl)
2726 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002727 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002728 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 else
2730# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002731 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 col += len;
2733 }
2734 }
2735#endif
2736
2737 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002738 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002740 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002742 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 if (len > 0)
2744 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002745 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002746 long num;
2747 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002748
2749 if (len > w + 1)
2750 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002751
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002752 if (wp->w_p_nu && !wp->w_p_rnu)
2753 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002754 num = (long)lnum;
2755 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002756 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002757 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002758 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002759 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002760 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002761 /* 'number' + 'relativenumber': cursor line shows absolute
2762 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002763 num = lnum;
2764 fmt = "%-*ld ";
2765 }
2766 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002767
Bram Moolenaar700e7342013-01-30 12:31:36 +01002768 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769#ifdef FEAT_RIGHTLEFT
2770 if (wp->w_p_rl)
2771 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002772 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002773 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 else
2775#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002776 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 col += len;
2778 }
2779 }
2780
2781 /*
2782 * 4. Compose the folded-line string with 'foldtext', if set.
2783 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002784 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785
2786 txtcol = col; /* remember where text starts */
2787
2788 /*
2789 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2790 * Right-left text is put in columns 0 - number-col, normal text is put
2791 * in columns number-col - window-width.
2792 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002793 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
2795 /* Fill the rest of the line with the fold filler */
2796#ifdef FEAT_RIGHTLEFT
2797 if (wp->w_p_rl)
2798 col -= txtcol;
2799#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002800 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801#ifdef FEAT_RIGHTLEFT
2802 - (wp->w_p_rl ? txtcol : 0)
2803#endif
2804 )
2805 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 if (enc_utf8)
2807 {
2808 if (fill_fold >= 0x80)
2809 {
2810 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002811 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002812 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 }
2814 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002817 ScreenLines[off + col] = fill_fold;
2818 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002819 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002821 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002822 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 }
2824
2825 if (text != buf)
2826 vim_free(text);
2827
2828 /*
2829 * 6. set highlighting for the Visual area an other text.
2830 * If all folded lines are in the Visual area, highlight the line.
2831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2833 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002834 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 {
2836 /* Visual is after curwin->w_cursor */
2837 top = &curwin->w_cursor;
2838 bot = &VIsual;
2839 }
2840 else
2841 {
2842 /* Visual is before curwin->w_cursor */
2843 top = &VIsual;
2844 bot = &curwin->w_cursor;
2845 }
2846 if (lnum >= top->lnum
2847 && lnume <= bot->lnum
2848 && (VIsual_mode != 'v'
2849 || ((lnum > top->lnum
2850 || (lnum == top->lnum
2851 && top->col == 0))
2852 && (lnume < bot->lnum
2853 || (lnume == bot->lnum
2854 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002855 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 {
2857 if (VIsual_mode == Ctrl_V)
2858 {
2859 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002860 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002862 if (wp->w_old_cursor_lcol != MAXCOL
2863 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002864 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 len = wp->w_old_cursor_lcol;
2866 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002867 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002868 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002869 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 }
2871 }
2872 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002875 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 }
2878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002880#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002881 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2882 if (wp->w_p_cc_cols)
2883 {
2884 int i = 0;
2885 int j = wp->w_p_cc_cols[i];
2886 int old_txtcol = txtcol;
2887
2888 while (j > -1)
2889 {
2890 txtcol += j;
2891 if (wp->w_p_wrap)
2892 txtcol -= wp->w_skipcol;
2893 else
2894 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002895 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002896 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002897 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002898 txtcol = old_txtcol;
2899 j = wp->w_p_cc_cols[++i];
2900 }
2901 }
2902
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002903 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002904 if (wp->w_p_cuc)
2905 {
2906 txtcol += wp->w_virtcol;
2907 if (wp->w_p_wrap)
2908 txtcol -= wp->w_skipcol;
2909 else
2910 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002911 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002912 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002913 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002914 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916
Bram Moolenaar02631462017-09-22 15:20:32 +02002917 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002918 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919
2920 /*
2921 * Update w_cline_height and w_cline_folded if the cursor line was
2922 * updated (saves a call to plines() later).
2923 */
2924 if (wp == curwin
2925 && lnum <= curwin->w_cursor.lnum
2926 && lnume >= curwin->w_cursor.lnum)
2927 {
2928 curwin->w_cline_row = row;
2929 curwin->w_cline_height = 1;
2930 curwin->w_cline_folded = TRUE;
2931 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2932 }
2933}
2934
2935/*
2936 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2937 */
2938 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002939copy_text_attr(
2940 int off,
2941 char_u *buf,
2942 int len,
2943 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002945 int i;
2946
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 if (enc_utf8)
2949 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002950 for (i = 0; i < len; ++i)
2951 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952}
2953
2954/*
2955 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002956 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 */
2958 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002959fill_foldcolumn(
2960 char_u *p,
2961 win_T *wp,
2962 int closed, /* TRUE of FALSE */
2963 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964{
2965 int i = 0;
2966 int level;
2967 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002968 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002969 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970
2971 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002972 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974 level = win_foldinfo.fi_level;
2975 if (level > 0)
2976 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002977 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002978 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002979
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 /* If the column is too narrow, we start at the lowest level that
2981 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002982 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 if (first_level < 1)
2984 first_level = 1;
2985
Bram Moolenaar1c934292015-01-27 16:39:29 +01002986 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 {
2988 if (win_foldinfo.fi_lnum == lnum
2989 && first_level + i >= win_foldinfo.fi_low_level)
2990 p[i] = '-';
2991 else if (first_level == 1)
2992 p[i] = '|';
2993 else if (first_level + i <= 9)
2994 p[i] = '0' + first_level + i;
2995 else
2996 p[i] = '>';
2997 if (first_level + i == level)
2998 break;
2999 }
3000 }
3001 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003002 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003}
3004#endif /* FEAT_FOLDING */
3005
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003006#ifdef FEAT_TEXT_PROP
3007static textprop_T *current_text_props = NULL;
3008static buf_T *current_buf = NULL;
3009
3010 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003011text_prop_compare(const void *s1, const void *s2)
3012{
3013 int idx1, idx2;
3014 proptype_T *pt1, *pt2;
3015 colnr_T col1, col2;
3016
3017 idx1 = *(int *)s1;
3018 idx2 = *(int *)s2;
3019 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3020 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3021 if (pt1 == pt2)
3022 return 0;
3023 if (pt1 == NULL)
3024 return -1;
3025 if (pt2 == NULL)
3026 return 1;
3027 if (pt1->pt_priority != pt2->pt_priority)
3028 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3029 col1 = current_text_props[idx1].tp_col;
3030 col2 = current_text_props[idx2].tp_col;
3031 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3032}
3033#endif
3034
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003035#ifdef FEAT_SIGNS
3036/*
3037 * Get information needed to display the sign in line 'lnum' in window 'wp'.
3038 * If 'nrcol' is TRUE, the sign is going to be displayed in the number column.
3039 * Otherwise the sign is going to be displayed in the sign column.
3040 */
3041 static void
3042get_sign_display_info(
3043 int nrcol,
3044 win_T *wp,
Bram Moolenaar4e038572019-07-04 18:28:35 +02003045 linenr_T lnum UNUSED,
3046 sign_attrs_T *sattr,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003047 int wcr_attr,
3048 int row,
3049 int startrow,
Bram Moolenaarbf8c3ad2019-06-19 14:28:43 +02003050 int filler_lines UNUSED,
3051 int filler_todo UNUSED,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003052 int *c_extrap,
3053 int *c_finalp,
3054 char_u *extra,
3055 char_u **pp_extra,
3056 int *n_extrap,
3057 int *char_attrp)
3058{
3059 int text_sign;
3060# ifdef FEAT_SIGN_ICONS
3061 int icon_sign;
3062# endif
3063
3064 // Draw two cells with the sign value or blank.
3065 *c_extrap = ' ';
3066 *c_finalp = NUL;
3067 if (nrcol)
3068 *n_extrap = number_width(wp) + 1;
3069 else
3070 {
3071 *char_attrp = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
3072 *n_extrap = 2;
3073 }
3074
3075 if (row == startrow
3076#ifdef FEAT_DIFF
3077 + filler_lines && filler_todo <= 0
3078#endif
3079 )
3080 {
Bram Moolenaar4e038572019-07-04 18:28:35 +02003081 text_sign = (sattr->text != NULL) ? sattr->typenr : 0;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003082# ifdef FEAT_SIGN_ICONS
Bram Moolenaar4e038572019-07-04 18:28:35 +02003083 icon_sign = (sattr->icon != NULL) ? sattr->typenr : 0;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003084 if (gui.in_use && icon_sign != 0)
3085 {
3086 // Use the image in this position.
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003087 if (nrcol)
3088 {
3089 *c_extrap = NUL;
3090 sprintf((char *)extra, "%-*c ", number_width(wp), SIGN_BYTE);
3091 *pp_extra = extra;
3092 *n_extrap = (int)STRLEN(*pp_extra);
3093 }
3094 else
3095 *c_extrap = SIGN_BYTE;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003096# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4e038572019-07-04 18:28:35 +02003097 if (netbeans_active() && (buf_signcount(wp->w_buffer, lnum) > 1))
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003098 {
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003099 if (nrcol)
3100 {
3101 *c_extrap = NUL;
3102 sprintf((char *)extra, "%-*c ", number_width(wp),
3103 MULTISIGN_BYTE);
3104 *pp_extra = extra;
3105 *n_extrap = (int)STRLEN(*pp_extra);
3106 }
3107 else
3108 *c_extrap = MULTISIGN_BYTE;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003109 }
3110# endif
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003111 *c_finalp = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003112 *char_attrp = icon_sign;
3113 }
3114 else
3115# endif
3116 if (text_sign != 0)
3117 {
Bram Moolenaar4e038572019-07-04 18:28:35 +02003118 *pp_extra = sattr->text;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003119 if (*pp_extra != NULL)
3120 {
3121 if (nrcol)
3122 {
Bram Moolenaard6bcff42019-07-18 12:48:16 +02003123 int n, width = number_width(wp) - 2;
3124
3125 for (n = 0; n < width; n++)
3126 extra[n] = ' ';
3127 extra[n] = 0;
3128 STRCAT(extra, *pp_extra);
3129 STRCAT(extra, " ");
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003130 *pp_extra = extra;
3131 }
3132 *c_extrap = NUL;
3133 *c_finalp = NUL;
3134 *n_extrap = (int)STRLEN(*pp_extra);
3135 }
Bram Moolenaar4e038572019-07-04 18:28:35 +02003136 *char_attrp = sattr->texthl;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003137 }
3138 }
3139}
3140#endif
3141
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142/*
3143 * Display line "lnum" of window 'wp' on the screen.
3144 * Start at row "startrow", stop when "endrow" is reached.
3145 * wp->w_virtcol needs to be valid.
3146 *
3147 * Return the number of last row the line occupies.
3148 */
3149 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003150win_line(
3151 win_T *wp,
3152 linenr_T lnum,
3153 int startrow,
3154 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003155 int nochange UNUSED, // not updating for changed text
3156 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003158 int col = 0; // visual column on screen
3159 unsigned off; // offset in ScreenLines/ScreenAttrs
3160 int c = 0; // init for GCC
3161 long vcol = 0; // virtual column (for tabs)
Bram Moolenaard574ea22015-01-14 19:35:14 +01003162#ifdef FEAT_LINEBREAK
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003163 long vcol_sbr = -1; // virtual column after showbreak
Bram Moolenaard574ea22015-01-14 19:35:14 +01003164#endif
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003165 long vcol_prev = -1; // "vcol" of previous character
3166 char_u *line; // current line
3167 char_u *ptr; // current position in "line"
3168 int row; // row in the window, excl w_winrow
3169 int screen_row; // row on the screen, incl w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003171 char_u extra[21]; // "%ld " and 'fdc' must fit in here
3172 int n_extra = 0; // number of extra chars
3173 char_u *p_extra = NULL; // string of extra chars, plus NUL
3174 char_u *p_extra_free = NULL; // p_extra needs to be freed
3175 int c_extra = NUL; // extra chars, all the same
3176 int c_final = NUL; // final char, mandatory if set
3177 int extra_attr = 0; // attributes when n_extra != 0
3178 static char_u *at_end_str = (char_u *)""; // used for p_extra when
3179 // displaying lcs_eol at end-of-line
3180 int lcs_eol_one = lcs_eol; // lcs_eol until it's been used
3181 int lcs_prec_todo = lcs_prec; // lcs_prec until it's been used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003183 // saved "extra" items for when draw_state becomes WL_LINE (again)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 int saved_n_extra = 0;
3185 char_u *saved_p_extra = NULL;
3186 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003187 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 int saved_char_attr = 0;
3189
3190 int n_attr = 0; /* chars with special attr */
3191 int saved_attr2 = 0; /* char_attr saved for n_attr */
3192 int n_attr3 = 0; /* chars with overruling special attr */
3193 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3194
3195 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3196
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003197 int fromcol = -10; // start of inverting
3198 int tocol = MAXCOL; // end of inverting
3199 int fromcol_prev = -2; // start of inverting after cursor
3200 int noinvcur = FALSE; // don't invert the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003202 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 pos_T pos;
3204 long v;
3205
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003206 int char_attr = 0; // attributes for next character
3207 int attr_pri = FALSE; // char_attr has priority
3208 int area_highlighting = FALSE; // Visual or incsearch highlighting
3209 // in this line
3210 int vi_attr = 0; // attributes for Visual and incsearch
3211 // highlighting
3212 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003213 int win_attr = 0; // background for whole window, except
3214 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003215 int area_attr = 0; // attributes desired by highlighting
3216 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003218 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 int syntax_attr = 0; /* attributes desired by syntax */
3220 int has_syntax = FALSE; /* this buffer has syntax highl. */
3221 int save_did_emsg;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003222 int draw_color_col = FALSE; /* highlight colorcolumn */
3223 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003224#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02003225 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003226#ifdef FEAT_TEXT_PROP
3227 int text_prop_count;
3228 int text_prop_next = 0; // next text property to use
3229 textprop_T *text_props = NULL;
3230 int *text_prop_idxs = NULL;
3231 int text_props_active = 0;
3232 proptype_T *text_prop_type = NULL;
3233 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003234 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003235#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003236#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003237 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003238# define SPWORDLEN 150
3239 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003240 int nextlinecol = 0; /* column where nextline[] starts */
3241 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003242 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003243 int spell_attr = 0; /* attributes desired by spelling */
3244 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003245 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3246 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003247 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003248 static int cap_col = -1; /* column to check for Cap word */
3249 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003250 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003252 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 int multi_attr = 0; /* attributes desired by multibyte */
3254 int mb_l = 1; /* multi-byte byte length */
3255 int mb_c = 0; /* decoded multi-byte character */
3256 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003257 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaare9726e32019-06-19 18:01:21 +02003258#if defined(FEAT_DIFF) || defined(FEAT_SIGNS)
Bram Moolenaarbf8c3ad2019-06-19 14:28:43 +02003259 int filler_lines = 0; /* nr of filler lines to be drawn */
3260 int filler_todo = 0; /* nr of filler lines still to do + 1 */
Bram Moolenaare9726e32019-06-19 18:01:21 +02003261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262#ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003263 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 int change_start = MAXCOL; /* first col of changed area */
3265 int change_end = -1; /* last col of changed area */
3266#endif
3267 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3268#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003269 int need_showbreak = FALSE; /* overlong line, skipping first x
3270 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003272#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003273 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003275 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276#endif
Bram Moolenaarb4d9b892019-07-04 22:59:06 +02003277#ifdef FEAT_SIGNS
3278 int sign_present = FALSE;
3279 sign_attrs_T sattr;
3280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003282 matchitem_T *cur; /* points to the match list */
3283 match_T *shl; /* points to search_hl or a match */
3284 int shl_flag; /* flag to indicate whether search_hl
3285 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003286 int pos_inprogress; /* marks that position match search is
3287 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003288 int prevcol_hl_flag; /* flag to indicate whether prevcol
3289 equals startcol of search_hl or one
3290 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291#endif
3292#ifdef FEAT_ARABIC
3293 int prev_c = 0; /* previous Arabic character */
3294 int prev_c1 = 0; /* first composing char for prev_c */
3295#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003296#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003297 int did_line_attr = 0;
3298#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003299#ifdef FEAT_TERMINAL
3300 int get_term_attr = FALSE;
3301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302
3303 /* draw_state: items that are drawn in sequence: */
3304#define WL_START 0 /* nothing done yet */
3305#ifdef FEAT_CMDWIN
3306# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3307#else
3308# define WL_CMDLINE WL_START
3309#endif
3310#ifdef FEAT_FOLDING
3311# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3312#else
3313# define WL_FOLD WL_CMDLINE
3314#endif
3315#ifdef FEAT_SIGNS
3316# define WL_SIGN WL_FOLD + 1 /* column for signs */
3317#else
3318# define WL_SIGN WL_FOLD /* column for signs */
3319#endif
3320#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003321#ifdef FEAT_LINEBREAK
3322# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003324# define WL_BRI WL_NR
3325#endif
3326#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3327# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3328#else
3329# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330#endif
3331#define WL_LINE WL_SBR + 1 /* text in the line */
3332 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003333#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 int feedback_col = 0;
3335 int feedback_old_attr = -1;
3336#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003337 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
Bram Moolenaar860cae12010-06-05 23:22:07 +02003339#ifdef FEAT_CONCEAL
3340 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003341 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003342 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003343 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003344 int is_concealing = FALSE;
3345 int boguscols = 0; /* nonexistent columns added to force
3346 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003347 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003348 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003349 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003350 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003351# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003352# define FIX_FOR_BOGUSCOLS \
3353 { \
3354 n_extra += vcol_off; \
3355 vcol -= vcol_off; \
3356 vcol_off = 0; \
3357 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003358 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003359 boguscols = 0; \
3360 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003361#else
3362# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364
3365 if (startrow > endrow) /* past the end already! */
3366 return startrow;
3367
3368 row = startrow;
3369 screen_row = row + W_WINROW(wp);
3370
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003371 if (!number_only)
3372 {
3373 /*
3374 * To speed up the loop below, set extra_check when there is linebreak,
3375 * trailing white space and/or syntax processing to be done.
3376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003378 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379#endif
3380#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003381 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003382# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003383 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003384# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003385 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003387 /* Prepare for syntax highlighting in this line. When there is an
3388 * error, stop syntax highlighting. */
3389 save_did_emsg = did_emsg;
3390 did_emsg = FALSE;
3391 syntax_start(wp, lnum);
3392 if (did_emsg)
3393 wp->w_s->b_syn_error = TRUE;
3394 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003395 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003396 did_emsg = save_did_emsg;
3397#ifdef SYN_TIME_LIMIT
3398 if (!wp->w_s->b_syn_slow)
3399#endif
3400 {
3401 has_syntax = TRUE;
3402 extra_check = TRUE;
3403 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003406
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003407 /* Check for columns to display for 'colorcolumn'. */
3408 color_cols = wp->w_p_cc_cols;
3409 if (color_cols != NULL)
3410 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003411#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003412
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003413#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003414 if (term_show_buffer(wp->w_buffer))
3415 {
3416 extra_check = TRUE;
3417 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003418 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003419 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003420#endif
3421
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003422#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003423 if (wp->w_p_spell
3424 && *wp->w_s->b_p_spl != NUL
3425 && wp->w_s->b_langp.ga_len > 0
3426 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003427 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003428 /* Prepare for spell checking. */
3429 has_spell = TRUE;
3430 extra_check = TRUE;
3431
Bram Moolenaar7701f302018-10-02 21:20:32 +02003432 /* Get the start of the next line, so that words that wrap to the
3433 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003434 * Trick: skip a few chars for C/shell/Vim comments */
3435 nextline[SPWORDLEN] = NUL;
3436 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3437 {
3438 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3439 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3440 }
3441
Bram Moolenaar7701f302018-10-02 21:20:32 +02003442 /* When a word wrapped from the previous line the start of the
3443 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003444 if (lnum == checked_lnum)
3445 cur_checked_col = checked_col;
3446 checked_lnum = 0;
3447
3448 /* When there was a sentence end in the previous line may require a
3449 * word starting with capital in this line. In line 1 always check
3450 * the first word. */
3451 if (lnum != capcol_lnum)
3452 cap_col = -1;
3453 if (lnum == 1)
3454 cap_col = 0;
3455 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457#endif
3458
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003459 /*
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003460 * handle Visual active in this window
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003461 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003462 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003464 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003466 // Visual is after curwin->w_cursor
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003467 top = &curwin->w_cursor;
3468 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003470 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003472 // Visual is before curwin->w_cursor
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003473 top = &VIsual;
3474 bot = &curwin->w_cursor;
3475 }
3476 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003477 if (VIsual_mode == Ctrl_V)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003478 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003479 // block mode
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003480 if (lnum_in_visual_area)
3481 {
3482 fromcol = wp->w_old_cursor_fcol;
3483 tocol = wp->w_old_cursor_lcol;
3484 }
3485 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003486 else
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003487 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003488 // non-block mode
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003489 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003491 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003493 if (VIsual_mode == 'V') // linewise
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003494 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 else
3496 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003497 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3498 if (gchar_pos(top) == NUL)
3499 tocol = fromcol + 1;
3500 }
3501 }
3502 if (VIsual_mode != 'V' && lnum == bot->lnum)
3503 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003504 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003505 {
3506 fromcol = -10;
3507 tocol = MAXCOL;
3508 }
3509 else if (bot->col == MAXCOL)
3510 tocol = MAXCOL;
3511 else
3512 {
3513 pos = *bot;
3514 if (*p_sel == 'e')
3515 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3516 else
3517 {
3518 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3519 ++tocol;
3520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 }
3522 }
3523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003525 /* Check if the character under the cursor should not be inverted */
3526 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003527#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003528 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003529#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003530 )
3531 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003533 /* if inverting in this line set area_highlighting */
3534 if (fromcol >= 0)
3535 {
3536 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003537 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003539 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003540 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003541 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003542 && clip_isautosel_plus()))
3543 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003548 /*
3549 * handle 'incsearch' and ":s///c" highlighting
3550 */
3551 else if (highlight_match
3552 && wp == curwin
3553 && lnum >= curwin->w_cursor.lnum
3554 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003556 if (lnum == curwin->w_cursor.lnum)
3557 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003558 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003559 else
3560 fromcol = 0;
3561 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3562 {
3563 pos.lnum = lnum;
3564 pos.col = search_match_endcol;
3565 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3566 }
3567 else
3568 tocol = MAXCOL;
3569 /* do at least one character; happens when past end of line */
3570 if (fromcol == tocol)
3571 tocol = fromcol + 1;
3572 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003573 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
3576
3577#ifdef FEAT_DIFF
3578 filler_lines = diff_check(wp, lnum);
3579 if (filler_lines < 0)
3580 {
3581 if (filler_lines == -1)
3582 {
3583 if (diff_find_change(wp, lnum, &change_start, &change_end))
3584 diff_hlf = HLF_ADD; /* added line */
3585 else if (change_start == 0)
3586 diff_hlf = HLF_TXD; /* changed text */
3587 else
3588 diff_hlf = HLF_CHD; /* changed line */
3589 }
3590 else
3591 diff_hlf = HLF_ADD; /* added line */
3592 filler_lines = 0;
3593 area_highlighting = TRUE;
3594 }
3595 if (lnum == wp->w_topline)
3596 filler_lines = wp->w_topfill;
3597 filler_todo = filler_lines;
3598#endif
3599
Bram Moolenaar4e038572019-07-04 18:28:35 +02003600#ifdef FEAT_SIGNS
3601 sign_present = buf_get_signattrs(wp->w_buffer, lnum, &sattr);
3602#endif
3603
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604#ifdef LINE_ATTR
3605# ifdef FEAT_SIGNS
3606 /* If this line has a sign with line highlighting set line_attr. */
Bram Moolenaar4e038572019-07-04 18:28:35 +02003607 if (sign_present)
3608 line_attr = sattr.linehl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003610# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003612 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003613 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614# endif
3615 if (line_attr != 0)
3616 area_highlighting = TRUE;
3617#endif
3618
3619 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3620 ptr = line;
3621
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003622#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003623 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003624 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003625 /* For checking first word with a capital skip white space. */
3626 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003627 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003628
Bram Moolenaar30abd282005-06-22 22:35:10 +00003629 /* To be able to spell-check over line boundaries copy the end of the
3630 * current line into nextline[]. Above the start of the next line was
3631 * copied to nextline[SPWORDLEN]. */
3632 if (nextline[SPWORDLEN] == NUL)
3633 {
3634 /* No next line or it is empty. */
3635 nextlinecol = MAXCOL;
3636 nextline_idx = 0;
3637 }
3638 else
3639 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003640 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003641 if (v < SPWORDLEN)
3642 {
3643 /* Short line, use it completely and append the start of the
3644 * next line. */
3645 nextlinecol = 0;
3646 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003647 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003648 nextline_idx = v + 1;
3649 }
3650 else
3651 {
3652 /* Long line, use only the last SPWORDLEN bytes. */
3653 nextlinecol = v - SPWORDLEN;
3654 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3655 nextline_idx = SPWORDLEN + 1;
3656 }
3657 }
3658 }
3659#endif
3660
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003661 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003663 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003664 extra_check = TRUE;
3665 /* find start of trailing whitespace */
3666 if (lcs_trail)
3667 {
3668 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003669 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003670 --trailcol;
3671 trailcol += (colnr_T) (ptr - line);
3672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 }
3674
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003675 wcr_attr = get_wcr_attr(wp);
3676 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003677 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003678 win_attr = wcr_attr;
3679 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003680 }
3681#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02003682 if (WIN_IS_POPUP(wp))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003683 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003684#endif
3685
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 /*
3687 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3688 * first character to be displayed.
3689 */
3690 if (wp->w_p_wrap)
3691 v = wp->w_skipcol;
3692 else
3693 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003694 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003697
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 while (vcol < v && *ptr != NUL)
3699 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003700 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003703 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
3705
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003706 /* When:
3707 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003708 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003709 * - 'virtualedit' is set, or
3710 * - the visual mode is active,
3711 * the end of the line may be before the start of the displayed part.
3712 */
3713 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003714#ifdef FEAT_SYN_HL
3715 wp->w_p_cuc || draw_color_col ||
3716#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003717 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003718 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720
3721 /* Handle a character that's not completely on the screen: Put ptr at
3722 * that character but skip the first few screen characters. */
3723 if (vcol > v)
3724 {
3725 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003727 /* If the character fits on the screen, don't need to skip it.
3728 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003729 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003730 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732
3733 /*
3734 * Adjust for when the inverted text is before the screen,
3735 * and when the start of the inverted text is before the screen.
3736 */
3737 if (tocol <= vcol)
3738 fromcol = 0;
3739 else if (fromcol >= 0 && fromcol < vcol)
3740 fromcol = vcol;
3741
3742#ifdef FEAT_LINEBREAK
3743 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3744 if (wp->w_p_wrap)
3745 need_showbreak = TRUE;
3746#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003747#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003748 /* When spell checking a word we need to figure out the start of the
3749 * word and if it's badly spelled or not. */
3750 if (has_spell)
3751 {
3752 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003753 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003754 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003755
3756 pos = wp->w_cursor;
3757 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003758 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003759 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003760
3761 /* spell_move_to() may call ml_get() and make "line" invalid */
3762 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3763 ptr = line + linecol;
3764
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003765 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003766 {
3767 /* no bad word found at line start, don't check until end of a
3768 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003769 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003770 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003771 }
3772 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003773 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003774 /* bad word found, use attributes until end of word */
3775 word_end = wp->w_cursor.col + len + 1;
3776
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003777 /* Turn index into actual attributes. */
3778 if (spell_hlf != HLF_COUNT)
3779 spell_attr = highlight_attr[spell_hlf];
3780 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003781 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003782
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003783# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003784 /* Need to restart syntax highlighting for this line. */
3785 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003786 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003787# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003788 }
3789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
3791
3792 /*
3793 * Correct highlighting for cursor that can't be disabled.
3794 * Avoids having to check this for each character.
3795 */
3796 if (fromcol >= 0)
3797 {
3798 if (noinvcur)
3799 {
3800 if ((colnr_T)fromcol == wp->w_virtcol)
3801 {
3802 /* highlighting starts at cursor, let it start just after the
3803 * cursor */
3804 fromcol_prev = fromcol;
3805 fromcol = -1;
3806 }
3807 else if ((colnr_T)fromcol < wp->w_virtcol)
3808 /* restart highlighting after the cursor */
3809 fromcol_prev = wp->w_virtcol;
3810 }
3811 if (fromcol >= tocol)
3812 fromcol = -1;
3813 }
3814
3815#ifdef FEAT_SEARCH_EXTRA
3816 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003817 * Handle highlighting the last used search pattern and matches.
3818 * Do this for both search_hl and the match list.
Bram Moolenaarac2450a2019-06-09 18:04:28 +02003819 * Do not use search_hl in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003821 cur = wp->w_match_head;
Bram Moolenaarac2450a2019-06-09 18:04:28 +02003822 shl_flag = (screen_line_flags & SLF_POPUP);
3823 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003825 if (shl_flag == FALSE)
3826 {
3827 shl = &search_hl;
3828 shl_flag = TRUE;
3829 }
3830 else
3831 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003832 shl->startcol = MAXCOL;
3833 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003835 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003836 v = (long)(ptr - line);
3837 if (cur != NULL)
3838 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003839 next_search_hl(wp, shl, lnum, (colnr_T)v,
3840 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003841
3842 /* Need to get the line again, a multi-line regexp may have made it
3843 * invalid. */
3844 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3845 ptr = line + v;
3846
3847 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003849 if (shl->lnum == lnum)
3850 shl->startcol = shl->rm.startpos[0].col;
3851 else
3852 shl->startcol = 0;
3853 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3854 - shl->rm.startpos[0].lnum)
3855 shl->endcol = shl->rm.endpos[0].col;
3856 else
3857 shl->endcol = MAXCOL;
3858 /* Highlight one character for an empty match. */
3859 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003861 if (has_mbyte && line[shl->endcol] != NUL)
3862 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3863 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003864 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003866 if ((long)shl->startcol < v) /* match at leftcol */
3867 {
3868 shl->attr_cur = shl->attr;
3869 search_attr = shl->attr;
3870 }
3871 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003873 if (shl != &search_hl && cur != NULL)
3874 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
3876#endif
3877
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003878#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003879 // Cursor line highlighting for 'cursorline' in the current window.
3880 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003881 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003882 // Do not show the cursor line when Visual mode is active, because it's
3883 // not clear what is selected then. Do update w_last_cursorline.
3884 if (!(wp == curwin && VIsual_active))
3885 {
3886 line_attr = HL_ATTR(HLF_CUL);
3887 area_highlighting = TRUE;
3888 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003889 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003890 }
3891#endif
3892
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003893#ifdef FEAT_TEXT_PROP
3894 {
3895 char_u *prop_start;
3896
3897 text_prop_count = get_text_props(wp->w_buffer, lnum,
3898 &prop_start, FALSE);
3899 if (text_prop_count > 0)
3900 {
3901 // Make a copy of the properties, so that they are properly
3902 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003903 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003904 if (text_props != NULL)
3905 mch_memmove(text_props, prop_start,
3906 text_prop_count * sizeof(textprop_T));
3907
3908 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003909 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003910 area_highlighting = TRUE;
3911 extra_check = TRUE;
3912 }
3913 }
3914#endif
3915
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003916 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003918
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919#ifdef FEAT_RIGHTLEFT
3920 if (wp->w_p_rl)
3921 {
3922 /* Rightleft window: process the text in the normal direction, but put
3923 * it in current_ScreenLine[] from right to left. Start at the
3924 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003925 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003927 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
3929#endif
3930
3931 /*
3932 * Repeat for the whole displayed line.
3933 */
3934 for (;;)
3935 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003936#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003937 int has_match_conc = 0; // match wants to conceal
3938 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 /* Skip this quickly when working on the text. */
3941 if (draw_state != WL_LINE)
3942 {
3943#ifdef FEAT_CMDWIN
3944 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3945 {
3946 draw_state = WL_CMDLINE;
3947 if (cmdwin_type != 0 && wp == curwin)
3948 {
3949 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003951 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003952 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003953 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 }
3955 }
3956#endif
3957
3958#ifdef FEAT_FOLDING
3959 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3960 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003961 int fdc = compute_foldcolumn(wp, 0);
3962
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003964 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003966 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003967 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003968 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003969 p_extra_free = alloc(12 + 1);
3970
3971 if (p_extra_free != NULL)
3972 {
3973 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3974 n_extra = fdc;
3975 p_extra_free[n_extra] = NUL;
3976 p_extra = p_extra_free;
3977 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003978 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003979 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01003980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982 }
3983#endif
3984
3985#ifdef FEAT_SIGNS
3986 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3987 {
3988 draw_state = WL_SIGN;
3989 /* Show the sign column when there are any signs in this
3990 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003991 if (signcolumn_on(wp))
Bram Moolenaar4e038572019-07-04 18:28:35 +02003992 get_sign_display_info(FALSE, wp, lnum, &sattr, wcr_attr,
3993 row, startrow, filler_lines, filler_todo, &c_extra,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003994 &c_final, extra, &p_extra, &n_extra, &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 }
3996#endif
3997
3998 if (draw_state == WL_NR - 1 && n_extra == 0)
3999 {
4000 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02004001 /* Display the absolute or relative line number. After the
4002 * first fill with blanks when the 'n' flag isn't in 'cpo' */
4003 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 && (row == startrow
4005#ifdef FEAT_DIFF
4006 + filler_lines
4007#endif
4008 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
4009 {
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004010#ifdef FEAT_SIGNS
4011 // If 'signcolumn' is set to 'number' and a sign is present
4012 // in 'lnum', then display the sign instead of the line
4013 // number.
4014 if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
Bram Moolenaar4e038572019-07-04 18:28:35 +02004015 && sign_present)
4016 get_sign_display_info(TRUE, wp, lnum, &sattr, wcr_attr,
4017 row, startrow, filler_lines, filler_todo,
4018 &c_extra, &c_final, extra, &p_extra, &n_extra,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004019 &char_attr);
4020 else
4021#endif
4022 {
4023 /* Draw the line number (empty space after wrapping). */
4024 if (row == startrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025#ifdef FEAT_DIFF
4026 + filler_lines
4027#endif
4028 )
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004029 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004030 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01004031 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02004032
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004033 if (wp->w_p_nu && !wp->w_p_rnu)
4034 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02004035 num = (long)lnum;
4036 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01004037 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004038 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01004039 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004040 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004041 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004042 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004043 num = lnum;
4044 fmt = "%-*ld ";
4045 }
4046 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004047
Bram Moolenaar700e7342013-01-30 12:31:36 +01004048 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004049 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 if (wp->w_skipcol > 0)
4051 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4052 *p_extra = '-';
4053#ifdef FEAT_RIGHTLEFT
4054 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004055 {
4056 char_u *p1, *p2;
4057 int t;
4058
4059 // like rl_mirror(), but keep the space at the end
4060 p2 = skiptowhite(extra) - 1;
4061 for (p1 = extra; p1 < p2; ++p1, --p2)
4062 {
4063 t = *p1;
4064 *p1 = *p2;
4065 *p2 = t;
4066 }
4067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068#endif
4069 p_extra = extra;
4070 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004071 c_final = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004072 }
4073 else
4074 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004076 c_final = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004077 }
4078 n_extra = number_width(wp) + 1;
4079 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004080#ifdef FEAT_SYN_HL
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004081 /* When 'cursorline' is set highlight the line number of
4082 * the current line differently.
4083 * TODO: Can we use CursorLine instead of CursorLineNr
4084 * when CursorLineNr isn't set? */
4085 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004086 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004087 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004088#endif
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091 }
4092
Bram Moolenaar597a4222014-06-25 14:39:50 +02004093#ifdef FEAT_LINEBREAK
4094 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4095 && n_extra == 0 && *p_sbr != NUL)
4096 /* draw indent after showbreak value */
4097 draw_state = WL_BRI;
4098 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4099 /* After the showbreak, draw the breakindent */
4100 draw_state = WL_BRI - 1;
4101
4102 /* draw 'breakindent': indent wrapped text accordingly */
4103 if (draw_state == WL_BRI - 1 && n_extra == 0)
4104 {
4105 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004106 /* if need_showbreak is set, breakindent also applies */
4107 if (wp->w_p_bri && n_extra == 0
4108 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004109# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004110 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004111# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004112 )
4113 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004114 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004115# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004116 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004117 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004118 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004119# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004120 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4121 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004122 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004123# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004124 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004125# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004126 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004127 c_extra = ' ';
4128 n_extra = get_breakindent_win(wp,
4129 ml_get_buf(wp->w_buffer, lnum, FALSE));
4130 /* Correct end of highlighted area for 'breakindent',
4131 * required when 'linebreak' is also set. */
4132 if (tocol == vcol)
4133 tocol += n_extra;
4134 }
4135 }
4136#endif
4137
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4139 if (draw_state == WL_SBR - 1 && n_extra == 0)
4140 {
4141 draw_state = WL_SBR;
4142# ifdef FEAT_DIFF
4143 if (filler_todo > 0)
4144 {
4145 /* Draw "deleted" diff line(s). */
4146 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004147 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004149 c_final = NUL;
4150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004152 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004154 c_final = NUL;
4155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156# ifdef FEAT_RIGHTLEFT
4157 if (wp->w_p_rl)
4158 n_extra = col + 1;
4159 else
4160# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004161 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004162 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 }
4164# endif
4165# ifdef FEAT_LINEBREAK
4166 if (*p_sbr != NUL && need_showbreak)
4167 {
4168 /* Draw 'showbreak' at the start of each broken line. */
4169 p_extra = p_sbr;
4170 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004171 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004173 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004175 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 /* Correct end of highlighted area for 'showbreak',
4177 * required when 'linebreak' is also set. */
4178 if (tocol == vcol)
4179 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004180#ifdef FEAT_SYN_HL
4181 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004182 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004183 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004184 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187# endif
4188 }
4189#endif
4190
4191 if (draw_state == WL_LINE - 1 && n_extra == 0)
4192 {
4193 draw_state = WL_LINE;
4194 if (saved_n_extra)
4195 {
4196 /* Continue item from end of wrapped line. */
4197 n_extra = saved_n_extra;
4198 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004199 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 p_extra = saved_p_extra;
4201 char_attr = saved_char_attr;
4202 }
4203 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004204 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
4206 }
4207
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004208 // When still displaying '$' of change command, stop at cursor.
4209 // When only displaying the (relative) line number and that's done,
4210 // stop here.
4211 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004212 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213#ifdef FEAT_DIFF
4214 && filler_todo <= 0
4215#endif
4216 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004217 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004219 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004220 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004221 /* Pretend we have finished updating the window. Except when
4222 * 'cursorcolumn' is set. */
4223#ifdef FEAT_SYN_HL
4224 if (wp->w_p_cuc)
4225 row = wp->w_cline_row + wp->w_cline_height;
4226 else
4227#endif
4228 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 break;
4230 }
4231
Bram Moolenaar637532b2019-01-03 21:44:40 +01004232 if (draw_state == WL_LINE && (area_highlighting
4233#ifdef FEAT_SPELL
4234 || has_spell
4235#endif
4236 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 {
4238 /* handle Visual or match highlighting in this line */
4239 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4241 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004243 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004245 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 else if (area_attr != 0
4247 && (vcol == tocol
4248 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251#ifdef FEAT_SEARCH_EXTRA
4252 if (!n_extra)
4253 {
4254 /*
4255 * Check for start/end of search pattern match.
4256 * After end, check for start/end of next match.
4257 * When another match, have to check for start again.
4258 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004259 * Do this for 'search_hl' and the match list (ordered by
4260 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004262 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004263 cur = wp->w_match_head;
Bram Moolenaara730e552019-06-16 19:05:31 +02004264 shl_flag = FALSE;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004265 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004267 if (shl_flag == FALSE
4268 && ((cur != NULL
4269 && cur->priority > SEARCH_HL_PRIORITY)
4270 || cur == NULL))
4271 {
4272 shl = &search_hl;
4273 shl_flag = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02004274 if (screen_line_flags & SLF_POPUP)
4275 continue; // do not use search_hl
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004276 }
4277 else
4278 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004279 if (cur != NULL)
4280 cur->pos.cur = 0;
4281 pos_inprogress = TRUE;
4282 while (shl->rm.regprog != NULL
4283 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004285 if (shl->startcol != MAXCOL
4286 && v >= (long)shl->startcol
4287 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004289 int tmp_col = v + MB_PTR2LEN(ptr);
4290
4291 if (shl->endcol < tmp_col)
4292 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004294#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004295 // Match with the "Conceal" group results in hiding
4296 // the match.
4297 if (cur != NULL
4298 && shl != &search_hl
4299 && syn_name2id((char_u *)"Conceal")
4300 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004301 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004302 has_match_conc =
4303 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004304 match_conc = cur->conceal_char;
4305 }
4306 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004307 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004310 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 {
4312 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004313 next_search_hl(wp, shl, lnum, (colnr_T)v,
4314 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004315 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004316 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317
4318 /* Need to get the line again, a multi-line regexp
4319 * may have made it invalid. */
4320 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4321 ptr = line + v;
4322
4323 if (shl->lnum == lnum)
4324 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004325 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004327 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004329 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004331 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 {
4333 /* highlight empty match, try again after
4334 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004336 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004337 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004339 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 }
4341
4342 /* Loop to check if the match starts at the
4343 * current position */
4344 continue;
4345 }
4346 }
4347 break;
4348 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004349 if (shl != &search_hl && cur != NULL)
4350 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004352
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004353 /* Use attributes from match with highest priority among
4354 * 'search_hl' and the match list. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004355 cur = wp->w_match_head;
4356 shl_flag = FALSE;
Bram Moolenaara730e552019-06-16 19:05:31 +02004357 search_attr = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004358 while (cur != NULL || shl_flag == FALSE)
4359 {
4360 if (shl_flag == FALSE
4361 && ((cur != NULL
4362 && cur->priority > SEARCH_HL_PRIORITY)
4363 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004364 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004365 shl = &search_hl;
4366 shl_flag = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02004367 if (screen_line_flags & SLF_POPUP)
4368 continue; // do not use search_hl
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004369 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004370 else
4371 shl = &cur->hl;
4372 if (shl->attr_cur != 0)
4373 search_attr = shl->attr_cur;
4374 if (shl != &search_hl && cur != NULL)
4375 cur = cur->next;
4376 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004377 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004378 if (*ptr == NUL && (did_line_attr >= 1
4379 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004380 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 }
4382#endif
4383
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004385 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004387 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4388 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004390 if (diff_hlf == HLF_TXD && ptr - line > change_end
4391 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004393 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004394 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004395 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
4397#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004398
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004399#ifdef FEAT_TEXT_PROP
4400 if (text_props != NULL)
4401 {
4402 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004403 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004404
Bram Moolenaara956bf62019-06-19 17:34:24 +02004405 if (n_extra > 0)
4406 --bcol; // still working on the previous char, e.g. Tab
4407
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004408 // Check if any active property ends.
4409 for (pi = 0; pi < text_props_active; ++pi)
4410 {
4411 int tpi = text_prop_idxs[pi];
4412
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004413 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004414 + text_props[tpi].tp_len)
4415 {
4416 if (pi + 1 < text_props_active)
4417 mch_memmove(text_prop_idxs + pi,
4418 text_prop_idxs + pi + 1,
4419 sizeof(int)
4420 * (text_props_active - (pi + 1)));
4421 --text_props_active;
4422 --pi;
4423 }
4424 }
4425
4426 // Add any text property that starts in this column.
4427 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004428 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004429 text_prop_idxs[text_props_active++] = text_prop_next++;
4430
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004431 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004432 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004433 if (text_props_active > 0)
4434 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004435 // Sort the properties on priority and/or starting last.
4436 // Then combine the attributes, highest priority last.
4437 current_text_props = text_props;
4438 current_buf = wp->w_buffer;
4439 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4440 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004441
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004442 for (pi = 0; pi < text_props_active; ++pi)
4443 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004444 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004445 proptype_T *pt = text_prop_type_by_id(
4446 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004447
Bram Moolenaard74af422019-06-28 21:38:00 +02004448 if (pt != NULL && pt->pt_hl_id > 0)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004449 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004450 int pt_attr = syn_id2attr(pt->pt_hl_id);
4451
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004452 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004453 text_prop_attr =
4454 hl_combine_attr(text_prop_attr, pt_attr);
4455 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004456 }
4457 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004458 }
4459 }
4460#endif
4461
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004462 /* Decide which of the highlight attributes to use. */
4463 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004464#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004465 if (area_attr != 0)
4466 char_attr = hl_combine_attr(line_attr, area_attr);
4467 else if (search_attr != 0)
4468 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004469# ifdef FEAT_TEXT_PROP
4470 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004471 {
4472 char_attr = hl_combine_attr(
4473 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4474 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004475# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004476 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004477 || vcol < fromcol || vcol_prev < fromcol_prev
4478 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004479 // Use line_attr when not in the Visual or 'incsearch' area
4480 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004481 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004482#else
4483 if (area_attr != 0)
4484 char_attr = area_attr;
4485 else if (search_attr != 0)
4486 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004487#endif
4488 else
4489 {
4490 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004491#ifdef FEAT_TEXT_PROP
4492 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004493 {
4494 if (text_prop_combine)
4495 char_attr = hl_combine_attr(
4496 syntax_attr, text_prop_attr);
4497 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004498 char_attr = hl_combine_attr(
4499 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004500 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004501 else
4502#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004503#ifdef FEAT_SYN_HL
4504 if (has_syntax)
4505 char_attr = syntax_attr;
4506 else
4507#endif
4508 char_attr = 0;
4509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004511 if (char_attr == 0)
4512 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
4514 /*
4515 * Get the next character to put on the screen.
4516 */
4517 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004518 * The "p_extra" points to the extra stuff that is inserted to
4519 * represent special characters (non-printable stuff) and other
4520 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004521 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004522 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4523 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4525 */
4526 if (n_extra > 0)
4527 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004528 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004530 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004532 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 {
4534 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004535 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004536 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 }
4538 else
4539 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 }
4541 else
4542 {
4543 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 if (has_mbyte)
4545 {
4546 mb_c = c;
4547 if (enc_utf8)
4548 {
4549 /* If the UTF-8 character is more than one byte:
4550 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004551 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 mb_utf8 = FALSE;
4553 if (mb_l > n_extra)
4554 mb_l = 1;
4555 else if (mb_l > 1)
4556 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004557 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004559 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 }
4561 }
4562 else
4563 {
4564 /* if this is a DBCS character, put it in "mb_c" */
4565 mb_l = MB_BYTE2LEN(c);
4566 if (mb_l >= n_extra)
4567 mb_l = 1;
4568 else if (mb_l > 1)
4569 mb_c = (c << 8) + p_extra[1];
4570 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004571 if (mb_l == 0) /* at the NUL at end-of-line */
4572 mb_l = 1;
4573
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 /* If a double-width char doesn't fit display a '>' in the
4575 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004576 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577# ifdef FEAT_RIGHTLEFT
4578 wp->w_p_rl ? (col <= 0) :
4579# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004580 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 && (*mb_char2cells)(mb_c) == 2)
4582 {
4583 c = '>';
4584 mb_c = c;
4585 mb_l = 1;
4586 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004587 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 /* put the pointer back to output the double-width
4589 * character at the start of the next line. */
4590 ++n_extra;
4591 --p_extra;
4592 }
4593 else
4594 {
4595 n_extra -= mb_l - 1;
4596 p_extra += mb_l - 1;
4597 }
4598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 ++p_extra;
4600 }
4601 --n_extra;
4602 }
4603 else
4604 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004605#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004606 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004607#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004608
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004609 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004610 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 /*
4612 * Get a character from the line itself.
4613 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004614 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004615#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004616 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 if (has_mbyte)
4619 {
4620 mb_c = c;
4621 if (enc_utf8)
4622 {
4623 /* If the UTF-8 character is more than one byte: Decode it
4624 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004625 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 mb_utf8 = FALSE;
4627 if (mb_l > 1)
4628 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004629 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 /* Overlong encoded ASCII or ASCII with composing char
4631 * is displayed normally, except a NUL. */
4632 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004633 {
4634 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004635#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004636 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004637#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004640
4641 /* At start of the line we can have a composing char.
4642 * Draw it as a space with a composing char. */
4643 if (utf_iscomposing(mb_c))
4644 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004645 int i;
4646
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004647 for (i = Screen_mco - 1; i > 0; --i)
4648 u8cc[i] = u8cc[i - 1];
4649 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004650 mb_c = ' ';
4651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 }
4653
4654 if ((mb_l == 1 && c >= 0x80)
4655 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004656 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 {
4658 /*
4659 * Illegal UTF-8 byte: display as <xx>.
4660 * Non-BMP character : display as ? or fullwidth ?.
4661 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004662 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004663# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004664 if (wp->w_p_rl) /* reverse */
4665 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004666# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 p_extra = extra;
4668 c = *p_extra;
4669 mb_c = mb_ptr2char_adv(&p_extra);
4670 mb_utf8 = (c >= 0x80);
4671 n_extra = (int)STRLEN(p_extra);
4672 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004673 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 if (area_attr == 0 && search_attr == 0)
4675 {
4676 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004677 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 saved_attr2 = char_attr; /* save current attr */
4679 }
4680 }
4681 else if (mb_l == 0) /* at the NUL at end-of-line */
4682 mb_l = 1;
4683#ifdef FEAT_ARABIC
4684 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4685 {
4686 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004687 int pc, pc1, nc;
4688 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689
4690 /* The idea of what is the previous and next
4691 * character depends on 'rightleft'. */
4692 if (wp->w_p_rl)
4693 {
4694 pc = prev_c;
4695 pc1 = prev_c1;
4696 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004697 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 }
4699 else
4700 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004701 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004703 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
4705 prev_c = mb_c;
4706
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004707 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
4709 else
4710 prev_c = mb_c;
4711#endif
4712 }
4713 else /* enc_dbcs */
4714 {
4715 mb_l = MB_BYTE2LEN(c);
4716 if (mb_l == 0) /* at the NUL at end-of-line */
4717 mb_l = 1;
4718 else if (mb_l > 1)
4719 {
4720 /* We assume a second byte below 32 is illegal.
4721 * Hopefully this is OK for all double-byte encodings!
4722 */
4723 if (ptr[1] >= 32)
4724 mb_c = (c << 8) + ptr[1];
4725 else
4726 {
4727 if (ptr[1] == NUL)
4728 {
4729 /* head byte at end of line */
4730 mb_l = 1;
4731 transchar_nonprint(extra, c);
4732 }
4733 else
4734 {
4735 /* illegal tail byte */
4736 mb_l = 2;
4737 STRCPY(extra, "XX");
4738 }
4739 p_extra = extra;
4740 n_extra = (int)STRLEN(extra) - 1;
4741 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004742 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 c = *p_extra++;
4744 if (area_attr == 0 && search_attr == 0)
4745 {
4746 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004747 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 saved_attr2 = char_attr; /* save current attr */
4749 }
4750 mb_c = c;
4751 }
4752 }
4753 }
4754 /* If a double-width char doesn't fit display a '>' in the
4755 * last column; the character is displayed at the start of the
4756 * next line. */
4757 if ((
4758# ifdef FEAT_RIGHTLEFT
4759 wp->w_p_rl ? (col <= 0) :
4760# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004761 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 && (*mb_char2cells)(mb_c) == 2)
4763 {
4764 c = '>';
4765 mb_c = c;
4766 mb_utf8 = FALSE;
4767 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004768 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004769 // Put pointer back so that the character will be
4770 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004772#ifdef FEAT_CONCEAL
4773 did_decrement_ptr = TRUE;
4774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 }
4776 else if (*ptr != NUL)
4777 ptr += mb_l - 1;
4778
4779 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004780 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004781 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004782 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004785 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004786 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 c = ' ';
4788 if (area_attr == 0 && search_attr == 0)
4789 {
4790 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004791 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 saved_attr2 = char_attr; /* save current attr */
4793 }
4794 mb_c = c;
4795 mb_utf8 = FALSE;
4796 mb_l = 1;
4797 }
4798
4799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 ++ptr;
4801
4802 if (extra_check)
4803 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004804#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004805 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004806#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004807
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004808#ifdef FEAT_TERMINAL
4809 if (get_term_attr)
4810 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004811 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004812
4813 if (!attr_pri)
4814 char_attr = syntax_attr;
4815 else
4816 char_attr = hl_combine_attr(syntax_attr, char_attr);
4817 }
4818#endif
4819
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004820#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004821 // Get syntax attribute, unless still at the start of the line
4822 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004823 v = (long)(ptr - line);
4824 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 {
4826 /* Get the syntax attribute for the character. If there
4827 * is an error, disable syntax highlighting. */
4828 save_did_emsg = did_emsg;
4829 did_emsg = FALSE;
4830
Bram Moolenaar217ad922005-03-20 22:37:15 +00004831 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004832# ifdef FEAT_SPELL
4833 has_spell ? &can_spell :
4834# endif
4835 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836
4837 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004838 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004839 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004840 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004841 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 else
4844 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004845
4846 // combine syntax attribute with 'wincolor'
4847 if (win_attr != 0)
4848 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4849
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004850#ifdef SYN_TIME_LIMIT
4851 if (wp->w_s->b_syn_slow)
4852 has_syntax = FALSE;
4853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854
4855 /* Need to get the line again, a multi-line regexp may
4856 * have made it invalid. */
4857 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4858 ptr = line + v;
4859
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004860# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004861 // Text properties overrule syntax highlighting or combine.
4862 if (text_prop_attr == 0 || text_prop_combine)
4863# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004864 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004865 int comb_attr = syntax_attr;
4866# ifdef FEAT_TEXT_PROP
4867 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4868# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004869 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004870 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004871 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004872 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004873 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004874# ifdef FEAT_CONCEAL
4875 /* no concealing past the end of the line, it interferes
4876 * with line highlighting */
4877 if (c == NUL)
4878 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004879 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004880 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004883#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004884
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004885#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004886 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004887 * Only do this when there is no syntax highlighting, the
4888 * @Spell cluster is not used or the current syntax item
4889 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004890 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004891 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004892 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004893 if (c != 0 && (
4894# ifdef FEAT_SYN_HL
4895 !has_syntax ||
4896# endif
4897 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004898 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004899 char_u *prev_ptr, *p;
4900 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004901 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004902 if (has_mbyte)
4903 {
4904 prev_ptr = ptr - mb_l;
4905 v -= mb_l - 1;
4906 }
4907 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004908 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004909
4910 /* Use nextline[] if possible, it has the start of the
4911 * next line concatenated. */
4912 if ((prev_ptr - line) - nextlinecol >= 0)
4913 p = nextline + (prev_ptr - line) - nextlinecol;
4914 else
4915 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004916 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004917 len = spell_check(wp, p, &spell_hlf, &cap_col,
4918 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004919 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004920
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004921 /* In Insert mode only highlight a word that
4922 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004923 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004924 && (State & INSERT) != 0
4925 && wp->w_cursor.lnum == lnum
4926 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004927 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004928 && wp->w_cursor.col < (colnr_T)word_end)
4929 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004930 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004931 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004932 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004933
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004934 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004935 && (p - nextline) + len > nextline_idx)
4936 {
4937 /* Remember that the good word continues at the
4938 * start of the next line. */
4939 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004940 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004941 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004942
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004943 /* Turn index into actual attributes. */
4944 if (spell_hlf != HLF_COUNT)
4945 spell_attr = highlight_attr[spell_hlf];
4946
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004947 if (cap_col > 0)
4948 {
4949 if (p != prev_ptr
4950 && (p - nextline) + cap_col >= nextline_idx)
4951 {
4952 /* Remember that the word in the next line
4953 * must start with a capital. */
4954 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004955 cap_col = (int)((p - nextline) + cap_col
4956 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004957 }
4958 else
4959 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004960 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004961 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004962 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004963 }
4964 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004965 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004966 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004967 char_attr = hl_combine_attr(char_attr, spell_attr);
4968 else
4969 char_attr = hl_combine_attr(spell_attr, char_attr);
4970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971#endif
4972#ifdef FEAT_LINEBREAK
4973 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004974 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004976 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004977 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004979 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004980 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004981
Bram Moolenaar597a4222014-06-25 14:39:50 +02004982 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004983 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004984 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004985 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004986# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004987 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004988 wp->w_buffer->b_p_vts_array) - 1;
4989# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004990 n_extra = (int)wp->w_buffer->b_p_ts
4991 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004992# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004993
Bram Moolenaar4df70292015-03-21 14:20:16 +01004994 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004995 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004996 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004997 {
4998#ifdef FEAT_CONCEAL
4999 if (c == TAB)
5000 /* See "Tab alignment" below. */
5001 FIX_FOR_BOGUSCOLS;
5002#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005003 if (!wp->w_p_list)
5004 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
5007#endif
5008
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005009 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
5010 // But not when the character is followed by a composing
5011 // character (use mb_l to check that).
5012 if (wp->w_p_list
5013 && ((((c == 160 && mb_l == 1)
5014 || (mb_utf8
5015 && ((mb_c == 160 && mb_l == 2)
5016 || (mb_c == 0x202f && mb_l == 3))))
5017 && lcs_nbsp)
5018 || (c == ' '
5019 && mb_l == 1
5020 && lcs_space
5021 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005022 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005023 c = (c == ' ') ? lcs_space : lcs_nbsp;
5024 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005025 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005026 n_attr = 1;
5027 extra_attr = HL_ATTR(HLF_8);
5028 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005029 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005030 mb_c = c;
5031 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005032 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005033 mb_utf8 = TRUE;
5034 u8cc[0] = 0;
5035 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005036 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005037 else
5038 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005039 }
5040
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
5042 {
5043 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005044 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 {
5046 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005047 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 saved_attr2 = char_attr; /* save current attr */
5049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005051 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 {
5053 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005054 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005055 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057 else
5058 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060 }
5061
5062 /*
5063 * Handling of non-printable characters.
5064 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005065 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
5067 /*
5068 * when getting a character from the file, we may have to
5069 * turn it into something else on the way to putting it
5070 * into "ScreenLines".
5071 */
5072 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5073 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005074 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005075 long vcol_adjusted = vcol; /* removed showbreak length */
5076#ifdef FEAT_LINEBREAK
5077 /* only adjust the tab_len, when at the first column
5078 * after the showbreak value was drawn */
5079 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5080 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005083#ifdef FEAT_VARTABS
5084 tab_len = tabstop_padding(vcol_adjusted,
5085 wp->w_buffer->b_p_ts,
5086 wp->w_buffer->b_p_vts_array) - 1;
5087#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005088 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005089 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5090#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005091
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005092#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005093 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005094#endif
5095 /* tab amount depends on current column */
5096 n_extra = tab_len;
5097#ifdef FEAT_LINEBREAK
5098 else
5099 {
5100 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005101 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005102 int i;
5103 int saved_nextra = n_extra;
5104
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005105#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005106 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005107 /* there are characters to conceal */
5108 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005109 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5110 */
5111 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5112 && n_extra > tab_len)
5113 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005114#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005115
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005116 /* if n_extra > 0, it gives the number of chars, to
5117 * use for a tab, else we need to calculate the width
5118 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005119 len = (tab_len * mb_char2len(lcs_tab2));
5120 if (n_extra > 0)
5121 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005122 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005123 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005124 vim_memset(p, ' ', len);
5125 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005126 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005127 p_extra_free = p;
5128 for (i = 0; i < tab_len; i++)
5129 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005130 if (*p == NUL)
5131 {
5132 tab_len = i;
5133 break;
5134 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005135 mb_char2bytes(lcs_tab2, p);
5136 p += mb_char2len(lcs_tab2);
5137 n_extra += mb_char2len(lcs_tab2)
5138 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005139 }
5140 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005141#ifdef FEAT_CONCEAL
5142 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5143 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005144 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005145 n_extra -= vcol_off;
5146#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005147 }
5148#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005149#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005150 {
5151 int vc_saved = vcol_off;
5152
5153 /* Tab alignment should be identical regardless of
5154 * 'conceallevel' value. So tab compensates of all
5155 * previous concealed characters, and thus resets
5156 * vcol_off and boguscols accumulated so far in the
5157 * line. Note that the tab can be longer than
5158 * 'tabstop' when there are concealed characters. */
5159 FIX_FOR_BOGUSCOLS;
5160
5161 /* Make sure, the highlighting for the tab char will be
5162 * correctly set further below (effectively reverts the
5163 * FIX_FOR_BOGSUCOLS macro */
5164 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005165 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005166 tab_len += vc_saved;
5167 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 if (wp->w_p_list)
5171 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005172 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005173#ifdef FEAT_LINEBREAK
5174 if (wp->w_p_lbr)
5175 c_extra = NUL; /* using p_extra from above */
5176 else
5177#endif
5178 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005179 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005180 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005181 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005184 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 {
5186 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005187 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005188 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
5191 else
5192 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005193 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 c_extra = ' ';
5195 c = ' ';
5196 }
5197 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005198 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005199 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005200 || ((fromcol >= 0 || fromcol_prev >= 0)
5201 && tocol > vcol
5202 && VIsual_mode != Ctrl_V
5203 && (
5204# ifdef FEAT_RIGHTLEFT
5205 wp->w_p_rl ? (col >= 0) :
5206# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005207 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005208 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005209 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005210 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005211 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005213 /* Display a '$' after the line or highlight an extra
5214 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5216 /* For a diff line the highlighting continues after the
5217 * "$". */
5218 if (
5219# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005220 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221# ifdef LINE_ATTR
5222 &&
5223# endif
5224# endif
5225# ifdef LINE_ATTR
5226 line_attr == 0
5227# endif
5228 )
5229#endif
5230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 /* In virtualedit, visual selections may extend
5232 * beyond end of line. */
5233 if (area_highlighting && virtual_active()
5234 && tocol != MAXCOL && vcol < tocol)
5235 n_extra = 0;
5236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 {
5238 p_extra = at_end_str;
5239 n_extra = 1;
5240 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005241 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 }
5243 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005244 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005245 c = lcs_eol;
5246 else
5247 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 lcs_eol_one = -1;
5249 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005250 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005252 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 n_attr = 1;
5254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005256 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 {
5258 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005259 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005260 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 }
5262 else
5263 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 }
5265 else if (c != NUL)
5266 {
5267 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005268 if (n_extra == 0)
5269 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270#ifdef FEAT_RIGHTLEFT
5271 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5272 rl_mirror(p_extra); /* reverse "<12>" */
5273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005275 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005276#ifdef FEAT_LINEBREAK
5277 if (wp->w_p_lbr)
5278 {
5279 char_u *p;
5280
5281 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005282 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005283 vim_memset(p, ' ', n_extra);
5284 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5285 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005286 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005287 p_extra_free = p_extra = p;
5288 }
5289 else
5290#endif
5291 {
5292 n_extra = byte2cells(c) - 1;
5293 c = *p_extra++;
5294 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005295 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 {
5297 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005298 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 saved_attr2 = char_attr; /* save current attr */
5300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 else if (VIsual_active
5304 && (VIsual_mode == Ctrl_V
5305 || VIsual_mode == 'v')
5306 && virtual_active()
5307 && tocol != MAXCOL
5308 && vcol < tocol
5309 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005310#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005312#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005313 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 {
5315 c = ' ';
5316 --ptr; /* put it back at the NUL */
5317 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005318#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 else if ((
5320# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005321 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005323# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005324 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005325# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 ) && (
5328# ifdef FEAT_RIGHTLEFT
5329 wp->w_p_rl ? (col >= 0) :
5330# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005331 (col
5332# ifdef FEAT_CONCEAL
5333 - boguscols
5334# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005335 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 {
5337 /* Highlight until the right side of the window */
5338 c = ' ';
5339 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005340
5341 /* Remember we do the char for line highlighting. */
5342 ++did_line_attr;
5343
5344 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005345 if (line_attr != 0 && char_attr == search_attr
5346 && (did_line_attr > 1
5347 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005348 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349# ifdef FEAT_DIFF
5350 if (diff_hlf == HLF_TXD)
5351 {
5352 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005353 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005354 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005355 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005356 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5357 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005358 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
5361# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005362# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005363 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005364 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005365 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005366 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5367 char_attr = hl_combine_attr(char_attr,
5368 HL_ATTR(HLF_CUL));
5369 }
5370# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 }
5372#endif
5373 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005374
5375#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005376 if ( wp->w_p_cole > 0
5377 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005378 conceal_cursor_line(wp))
5379 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005380 && !(lnum_in_visual_area
5381 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005382 {
5383 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005384 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005385 && (syn_get_sub_char() != NUL || match_conc
5386 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005387 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005388 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005389 /* First time at this concealed item: display one
5390 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005391 if (match_conc)
5392 c = match_conc;
5393 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005394 c = syn_get_sub_char();
5395 else if (lcs_conceal != NUL)
5396 c = lcs_conceal;
5397 else
5398 c = ' ';
5399
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005400 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005401
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005402 if (n_extra > 0)
5403 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005404 vcol += n_extra;
5405 if (wp->w_p_wrap && n_extra > 0)
5406 {
5407# ifdef FEAT_RIGHTLEFT
5408 if (wp->w_p_rl)
5409 {
5410 col -= n_extra;
5411 boguscols -= n_extra;
5412 }
5413 else
5414# endif
5415 {
5416 boguscols += n_extra;
5417 col += n_extra;
5418 }
5419 }
5420 n_extra = 0;
5421 n_attr = 0;
5422 }
5423 else if (n_skip == 0)
5424 {
5425 is_concealing = TRUE;
5426 n_skip = 1;
5427 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005428 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005429 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005430 {
5431 mb_utf8 = TRUE;
5432 u8cc[0] = 0;
5433 c = 0xc0;
5434 }
5435 else
5436 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005437 }
5438 else
5439 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005440 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005441 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005442 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005443
5444 if (n_skip > 0 && did_decrement_ptr)
5445 // not showing the '>', put pointer back to avoid getting stuck
5446 ++ptr;
5447
5448#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 }
5450
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005451#ifdef FEAT_CONCEAL
5452 /* In the cursor line and we may be concealing characters: correct
5453 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005454 if (!did_wcol && draw_state == WL_LINE
5455 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005456 && conceal_cursor_line(wp)
5457 && (int)wp->w_virtcol <= vcol + n_skip)
5458 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005459# ifdef FEAT_RIGHTLEFT
5460 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005461 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005462 else
5463# endif
5464 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005465 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005466 did_wcol = TRUE;
5467 }
5468#endif
5469
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 /* Don't override visual selection highlighting. */
5471 if (n_attr > 0
5472 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005473 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 char_attr = extra_attr;
5475
Bram Moolenaar81695252004-12-29 20:58:21 +00005476#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 /* XIM don't send preedit_start and preedit_end, but they send
5478 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5479 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005480 if (p_imst == IM_ON_THE_SPOT
5481 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005482 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 && (State & INSERT)
5484 && !p_imdisable
5485 && im_is_preediting()
5486 && draw_state == WL_LINE)
5487 {
5488 colnr_T tcol;
5489
5490 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005491 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 else
5493 tcol = preedit_end_col;
5494 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5495 {
5496 if (feedback_old_attr < 0)
5497 {
5498 feedback_col = 0;
5499 feedback_old_attr = char_attr;
5500 }
5501 char_attr = im_get_feedback_attr(feedback_col);
5502 if (char_attr < 0)
5503 char_attr = feedback_old_attr;
5504 feedback_col++;
5505 }
5506 else if (feedback_old_attr >= 0)
5507 {
5508 char_attr = feedback_old_attr;
5509 feedback_old_attr = -1;
5510 feedback_col = 0;
5511 }
5512 }
5513#endif
5514 /*
5515 * Handle the case where we are in column 0 but not on the first
5516 * character of the line and the user wants us to show us a
5517 * special character (via 'listchars' option "precedes:<char>".
5518 */
5519 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005520 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5522#ifdef FEAT_DIFF
5523 && filler_todo <= 0
5524#endif
5525 && draw_state > WL_NR
5526 && c != NUL)
5527 {
5528 c = lcs_prec;
5529 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005530 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5531 {
5532 /* Double-width character being overwritten by the "precedes"
5533 * character, need to fill up half the character. */
5534 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005535 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005536 n_extra = 1;
5537 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005538 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005541 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 {
5543 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005544 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005545 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 }
5547 else
5548 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005549 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 {
5551 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005552 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 n_attr3 = 1;
5554 }
5555 }
5556
5557 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005558 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 */
Bram Moolenaarf914a332019-07-20 15:09:56 +02005560 if ((c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005561#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005562 || did_line_attr == 1
5563#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02005564 ) && eol_hl_off == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005566#ifdef FEAT_SEARCH_EXTRA
5567 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005568
5569 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005570 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005571 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005572#endif
5573
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005574 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 * highlight match at end of line. If it's beyond the last
5576 * char on the screen, just overwrite that one (tricky!) Not
5577 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005578#ifdef FEAT_SEARCH_EXTRA
5579 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005580 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005581 prevcol_hl_flag = TRUE;
5582 else
5583 {
5584 cur = wp->w_match_head;
5585 while (cur != NULL)
5586 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005587 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005588 {
5589 prevcol_hl_flag = TRUE;
5590 break;
5591 }
5592 cur = cur->next;
5593 }
5594 }
5595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005597 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005598 && (VIsual_mode != Ctrl_V
5599 || lnum == VIsual.lnum
5600 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005601 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602#ifdef FEAT_SEARCH_EXTRA
5603 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005604 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005605# ifdef FEAT_SYN_HL
5606 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5607 && !(wp == curwin && VIsual_active))
5608# endif
5609# ifdef FEAT_DIFF
5610 && diff_hlf == (hlf_T)0
5611# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005612# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005613 && did_line_attr <= 1
5614# endif
5615 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616#endif
5617 ))
5618 {
5619 int n = 0;
5620
5621#ifdef FEAT_RIGHTLEFT
5622 if (wp->w_p_rl)
5623 {
5624 if (col < 0)
5625 n = 1;
5626 }
5627 else
5628#endif
5629 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005630 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 n = -1;
5632 }
5633 if (n != 0)
5634 {
5635 /* At the window boundary, highlight the last character
5636 * instead (better than nothing). */
5637 off += n;
5638 col += n;
5639 }
5640 else
5641 {
5642 /* Add a blank character to highlight. */
5643 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 if (enc_utf8)
5645 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 }
5647#ifdef FEAT_SEARCH_EXTRA
5648 if (area_attr == 0)
5649 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005650 /* Use attributes from match with highest priority among
5651 * 'search_hl' and the match list. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005652 cur = wp->w_match_head;
5653 shl_flag = FALSE;
5654 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005655 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005656 if (shl_flag == FALSE
5657 && ((cur != NULL
5658 && cur->priority > SEARCH_HL_PRIORITY)
5659 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005660 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005661 shl = &search_hl;
5662 shl_flag = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02005663 if (screen_line_flags & SLF_POPUP)
5664 continue; // do not use search_hl
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005665 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005666 else
5667 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005668 if ((ptr - line) - 1 == (long)shl->startcol
5669 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005670 char_attr = shl->attr;
5671 if (shl != &search_hl && cur != NULL)
5672 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675#endif
5676 ScreenAttrs[off] = char_attr;
5677#ifdef FEAT_RIGHTLEFT
5678 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005681 --off;
5682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 else
5684#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005685 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005687 ++off;
5688 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005689 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005690 eol_hl_off = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693
Bram Moolenaar91170f82006-05-05 21:15:17 +00005694 /*
5695 * At end of the text line.
5696 */
5697 if (c == NUL)
5698 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005699#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005700 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005701 if (wp->w_p_wrap)
5702 v = wp->w_skipcol;
5703 else
5704 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005705
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005706 /* check if line ends before left margin */
5707 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005708 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005709#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005710 // Get rid of the boguscols now, we want to draw until the right
5711 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005712 col -= boguscols;
5713 boguscols = 0;
5714#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005715
5716 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005717 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005718
5719 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005720 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5721 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005722 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005723 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005724 || draw_color_col
5725 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005726# ifdef FEAT_RIGHTLEFT
5727 && !wp->w_p_rl
5728# endif
5729 )
5730 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005731 int rightmost_vcol = 0;
5732 int i;
5733
5734 if (wp->w_p_cuc)
5735 rightmost_vcol = wp->w_virtcol;
5736 if (draw_color_col)
5737 /* determine rightmost colorcolumn to possibly draw */
5738 for (i = 0; color_cols[i] >= 0; ++i)
5739 if (rightmost_vcol < color_cols[i])
5740 rightmost_vcol = color_cols[i];
5741
Bram Moolenaar02631462017-09-22 15:20:32 +02005742 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005743 {
5744 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005745 if (enc_utf8)
5746 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005747 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005748 if (draw_color_col)
5749 draw_color_col = advance_color_col(VCOL_HLC,
5750 &color_cols);
5751
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005752 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005753 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005754 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005755 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005756 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005757 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005758
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005759 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005760 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005761
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005762 ++vcol;
5763 }
5764 }
5765#endif
5766
Bram Moolenaar53f81742017-09-22 14:35:51 +02005767 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005768 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 row++;
5770
5771 /*
5772 * Update w_cline_height and w_cline_folded if the cursor line was
5773 * updated (saves a call to plines() later).
5774 */
5775 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5776 {
5777 curwin->w_cline_row = startrow;
5778 curwin->w_cline_height = row - startrow;
5779#ifdef FEAT_FOLDING
5780 curwin->w_cline_folded = FALSE;
5781#endif
5782 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5783 }
5784
5785 break;
5786 }
5787
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005788 // Show "extends" character from 'listchars' if beyond the line end and
5789 // 'list' is set.
5790 if (lcs_ext != NUL
5791 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 && !wp->w_p_wrap
5793#ifdef FEAT_DIFF
5794 && filler_todo <= 0
5795#endif
5796 && (
5797#ifdef FEAT_RIGHTLEFT
5798 wp->w_p_rl ? col == 0 :
5799#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005800 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005802 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5804 {
5805 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005806 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005808 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 {
5810 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005811 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005812 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 }
5814 else
5815 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 }
5817
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005818#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005819 /* advance to the next 'colorcolumn' */
5820 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005821 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005822
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005823 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005824 * highlight the cursor position itself.
5825 * Also highlight the 'colorcolumn' if it is different than
5826 * 'cursorcolumn' */
5827 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005828 if (draw_state == WL_LINE && !lnum_in_visual_area
5829 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005830 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005831 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005832 && lnum != wp->w_cursor.lnum)
5833 {
5834 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005835 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005836 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005837 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005838 {
5839 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005840 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005841 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005842 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005843#endif
5844
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 /*
5846 * Store character to be displayed.
5847 * Skip characters that are left of the screen for 'nowrap'.
5848 */
5849 vcol_prev = vcol;
5850 if (draw_state < WL_LINE || n_skip <= 0)
5851 {
5852 /*
5853 * Store the character.
5854 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005855#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5857 {
5858 /* A double-wide character is: put first halve in left cell. */
5859 --off;
5860 --col;
5861 }
5862#endif
5863 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005865 {
5866 if ((mb_c & 0xff00) == 0x8e00)
5867 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 else if (enc_utf8)
5871 {
5872 if (mb_utf8)
5873 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005874 int i;
5875
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005877 if ((c & 0xff) == 0)
5878 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005879 for (i = 0; i < Screen_mco; ++i)
5880 {
5881 ScreenLinesC[i][off] = u8cc[i];
5882 if (u8cc[i] == 0)
5883 break;
5884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 }
5886 else
5887 ScreenLinesUC[off] = 0;
5888 }
5889 if (multi_attr)
5890 {
5891 ScreenAttrs[off] = multi_attr;
5892 multi_attr = 0;
5893 }
5894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 ScreenAttrs[off] = char_attr;
5896
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5898 {
5899 /* Need to fill two screen columns. */
5900 ++off;
5901 ++col;
5902 if (enc_utf8)
5903 /* UTF-8: Put a 0 in the second screen char. */
5904 ScreenLines[off] = 0;
5905 else
5906 /* DBCS: Put second byte in the second screen char. */
5907 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005908 if (draw_state > WL_NR
5909#ifdef FEAT_DIFF
5910 && filler_todo <= 0
5911#endif
5912 )
5913 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 /* When "tocol" is halfway a character, set it to the end of
5915 * the character, otherwise highlighting won't stop. */
5916 if (tocol == vcol)
5917 ++tocol;
5918#ifdef FEAT_RIGHTLEFT
5919 if (wp->w_p_rl)
5920 {
5921 /* now it's time to backup one cell */
5922 --off;
5923 --col;
5924 }
5925#endif
5926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927#ifdef FEAT_RIGHTLEFT
5928 if (wp->w_p_rl)
5929 {
5930 --off;
5931 --col;
5932 }
5933 else
5934#endif
5935 {
5936 ++off;
5937 ++col;
5938 }
5939 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005940#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005941 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005942 {
5943 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005944 ++vcol_off;
5945 if (n_extra > 0)
5946 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005947 if (wp->w_p_wrap)
5948 {
5949 /*
5950 * Special voodoo required if 'wrap' is on.
5951 *
5952 * Advance the column indicator to force the line
5953 * drawing to wrap early. This will make the line
5954 * take up the same screen space when parts are concealed,
5955 * so that cursor line computations aren't messed up.
5956 *
5957 * To avoid the fictitious advance of 'col' causing
5958 * trailing junk to be written out of the screen line
5959 * we are building, 'boguscols' keeps track of the number
5960 * of bad columns we have advanced.
5961 */
5962 if (n_extra > 0)
5963 {
5964 vcol += n_extra;
5965# ifdef FEAT_RIGHTLEFT
5966 if (wp->w_p_rl)
5967 {
5968 col -= n_extra;
5969 boguscols -= n_extra;
5970 }
5971 else
5972# endif
5973 {
5974 col += n_extra;
5975 boguscols += n_extra;
5976 }
5977 n_extra = 0;
5978 n_attr = 0;
5979 }
5980
5981
Bram Moolenaar860cae12010-06-05 23:22:07 +02005982 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5983 {
5984 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005985# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005986 if (wp->w_p_rl)
5987 {
5988 --boguscols;
5989 --col;
5990 }
5991 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005992# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005993 {
5994 ++boguscols;
5995 ++col;
5996 }
5997 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005998
5999# ifdef FEAT_RIGHTLEFT
6000 if (wp->w_p_rl)
6001 {
6002 --boguscols;
6003 --col;
6004 }
6005 else
6006# endif
6007 {
6008 ++boguscols;
6009 ++col;
6010 }
6011 }
6012 else
6013 {
6014 if (n_extra > 0)
6015 {
6016 vcol += n_extra;
6017 n_extra = 0;
6018 n_attr = 0;
6019 }
6020 }
6021
6022 }
6023#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 else
6025 --n_skip;
6026
Bram Moolenaar64486672010-05-16 15:46:46 +02006027 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
6028 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00006029 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030#ifdef FEAT_DIFF
6031 && filler_todo <= 0
6032#endif
6033 )
6034 ++vcol;
6035
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006036#ifdef FEAT_SYN_HL
6037 if (vcol_save_attr >= 0)
6038 char_attr = vcol_save_attr;
6039#endif
6040
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 /* restore attributes after "predeces" in 'listchars' */
6042 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
6043 char_attr = saved_attr3;
6044
6045 /* restore attributes after last 'listchars' or 'number' char */
6046 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
6047 char_attr = saved_attr2;
6048
6049 /*
6050 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00006051 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 */
6053 if ((
6054#ifdef FEAT_RIGHTLEFT
6055 wp->w_p_rl ? (col < 0) :
6056#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006057 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 && (*ptr != NUL
6059#ifdef FEAT_DIFF
6060 || filler_todo > 0
6061#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006062 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6064 )
6065 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006066#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006067 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006068 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006069 boguscols = 0;
6070#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006071 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006072 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 ++row;
6075 ++screen_row;
6076
6077 /* When not wrapping and finished diff lines, or when displayed
6078 * '$' and highlighting until last column, break here. */
6079 if ((!wp->w_p_wrap
6080#ifdef FEAT_DIFF
6081 && filler_todo <= 0
6082#endif
6083 ) || lcs_eol_one == -1)
6084 break;
6085
6086 /* When the window is too narrow draw all "@" lines. */
6087 if (draw_state != WL_LINE
6088#ifdef FEAT_DIFF
6089 && filler_todo <= 0
6090#endif
6091 )
6092 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006093 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 row = endrow;
6096 }
6097
6098 /* When line got too long for screen break here. */
6099 if (row == endrow)
6100 {
6101 ++row;
6102 break;
6103 }
6104
6105 if (screen_cur_row == screen_row - 1
6106#ifdef FEAT_DIFF
6107 && filler_todo <= 0
6108#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006109 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 {
6111 /* Remember that the line wraps, used for modeless copy. */
6112 LineWraps[screen_row - 1] = TRUE;
6113
6114 /*
6115 * Special trick to make copy/paste of wrapped lines work with
6116 * xterm/screen: write an extra character beyond the end of
6117 * the line. This will work with all terminal types
6118 * (regardless of the xn,am settings).
6119 * Only do this on a fast tty.
6120 * Only do this if the cursor is on the current line
6121 * (something has been written in it).
6122 * Don't do this for the GUI.
6123 * Don't do this for double-width characters.
6124 * Don't do this for a window not at the right screen border.
6125 */
6126 if (p_tf
6127#ifdef FEAT_GUI
6128 && !gui.in_use
6129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006131 && ((*mb_off2cells)(LineOffset[screen_row],
6132 LineOffset[screen_row] + screen_Columns)
6133 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006135 + (int)Columns - 2,
6136 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006137 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 {
6139 /* First make sure we are at the end of the screen line,
6140 * then output the same character again to let the
6141 * terminal know about the wrap. If the terminal doesn't
6142 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006143 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 screen_char(LineOffset[screen_row - 1]
6145 + (unsigned)Columns - 1,
6146 screen_row - 1, (int)(Columns - 1));
6147
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 /* When there is a multi-byte character, just output a
6149 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006150 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6151 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 out_char(' ');
6153 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 out_char(ScreenLines[LineOffset[screen_row - 1]
6155 + (Columns - 1)]);
6156 /* force a redraw of the first char on the next line */
6157 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6158 screen_start(); /* don't know where cursor is now */
6159 }
6160 }
6161
6162 col = 0;
6163 off = (unsigned)(current_ScreenLine - ScreenLines);
6164#ifdef FEAT_RIGHTLEFT
6165 if (wp->w_p_rl)
6166 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006167 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 off += col;
6169 }
6170#endif
6171
6172 /* reset the drawing state for the start of a wrapped line */
6173 draw_state = WL_START;
6174 saved_n_extra = n_extra;
6175 saved_p_extra = p_extra;
6176 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006177 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 saved_char_attr = char_attr;
6179 n_extra = 0;
6180 lcs_prec_todo = lcs_prec;
6181#ifdef FEAT_LINEBREAK
6182# ifdef FEAT_DIFF
6183 if (filler_todo <= 0)
6184# endif
6185 need_showbreak = TRUE;
6186#endif
6187#ifdef FEAT_DIFF
6188 --filler_todo;
6189 /* When the filler lines are actually below the last line of the
6190 * file, don't draw the line itself, break here. */
6191 if (filler_todo == 0 && wp->w_botfill)
6192 break;
6193#endif
6194 }
6195
6196 } /* for every character in the line */
6197
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006198#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006199 /* After an empty line check first word for capital. */
6200 if (*skipwhite(line) == NUL)
6201 {
6202 capcol_lnum = lnum + 1;
6203 cap_col = 0;
6204 }
6205#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006206#ifdef FEAT_TEXT_PROP
6207 vim_free(text_props);
6208 vim_free(text_prop_idxs);
6209#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006210
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006211 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 return row;
6213}
6214
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006215/*
6216 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006217 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006218 */
6219 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006220comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006221{
6222 int i;
6223
6224 for (i = 0; i < Screen_mco; ++i)
6225 {
6226 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6227 return TRUE;
6228 if (ScreenLinesC[i][off_from] == 0)
6229 break;
6230 }
6231 return FALSE;
6232}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006233
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234/*
6235 * Check whether the given character needs redrawing:
6236 * - the (first byte of the) character is different
6237 * - the attributes are different
6238 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006239 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 */
6241 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006242char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
6244 if (cols > 0
6245 && ((ScreenLines[off_from] != ScreenLines[off_to]
6246 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 || (enc_dbcs != 0
6248 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6249 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6250 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6251 : (cols > 1 && ScreenLines[off_from + 1]
6252 != ScreenLines[off_to + 1])))
6253 || (enc_utf8
6254 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6255 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006256 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006257 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6258 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006259 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 return TRUE;
6261 return FALSE;
6262}
6263
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006264#if defined(FEAT_TERMINAL) || defined(PROTO)
6265/*
6266 * Return the index in ScreenLines[] for the current screen line.
6267 */
6268 int
6269screen_get_current_line_off()
6270{
6271 return (int)(current_ScreenLine - ScreenLines);
6272}
6273#endif
6274
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006275#ifdef FEAT_TEXT_PROP
6276/*
6277 * Return TRUE if this position has a higher level popup or this cell is
6278 * transparent in the current popup.
6279 */
6280 static int
6281blocked_by_popup(int row, int col)
6282{
6283 int off;
6284
6285 if (!popup_visible)
6286 return FALSE;
6287 off = row * screen_Columns + col;
6288 return popup_mask[off] > screen_zindex || popup_transparent[off];
6289}
6290#endif
6291
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292/*
6293 * Move one "cooked" screen line to the screen, but only the characters that
6294 * have actually changed. Handle insert/delete character.
6295 * "coloff" gives the first column on the screen for this line.
6296 * "endcol" gives the columns where valid characters are.
6297 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6298 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006299 * "flags" can have bits:
6300 * SLF_POPUP popup window
6301 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6303 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6304 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006305 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006306screen_line(
6307 int row,
6308 int coloff,
6309 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006310 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006311 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312{
6313 unsigned off_from;
6314 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006315 unsigned max_off_from;
6316 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 int force = FALSE; /* force update rest of the line */
6320 int redraw_this /* bool: does character need redraw? */
6321#ifdef FEAT_GUI
6322 = TRUE /* For GUI when while-loop empty */
6323#endif
6324 ;
6325 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 int clear_next = FALSE;
6327 int char_cells; /* 1: normal char */
6328 /* 2: occupies two display cells */
6329# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006331 /* Check for illegal row and col, just in case. */
6332 if (row >= Rows)
6333 row = Rows - 1;
6334 if (endcol > Columns)
6335 endcol = Columns;
6336
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337# ifdef FEAT_CLIPBOARD
6338 clip_may_clear_selection(row, row);
6339# endif
6340
6341 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6342 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006343 max_off_from = off_from + screen_Columns;
6344 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345
6346#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006347 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 {
6349 /* Clear rest first, because it's left of the text. */
6350 if (clear_width > 0)
6351 {
6352 while (col <= endcol && ScreenLines[off_to] == ' '
6353 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006354 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 {
6356 ++off_to;
6357 ++col;
6358 }
6359 if (col <= endcol)
6360 screen_fill(row, row + 1, col + coloff,
6361 endcol + coloff + 1, ' ', ' ', 0);
6362 }
6363 col = endcol + 1;
6364 off_to = LineOffset[row] + col + coloff;
6365 off_from += col;
6366 endcol = (clear_width > 0 ? clear_width : -clear_width);
6367 }
6368#endif /* FEAT_RIGHTLEFT */
6369
6370 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6371
6372 while (col < endcol)
6373 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006375 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 else
6377 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378
6379 redraw_this = redraw_next;
6380 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6381 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6382
6383#ifdef FEAT_GUI
6384 /* If the next character was bold, then redraw the current character to
6385 * remove any pixels that might have spilt over into us. This only
6386 * happens in the GUI.
6387 */
6388 if (redraw_next && gui.in_use)
6389 {
6390 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006391 if (hl > HL_ALL)
6392 hl = syn_attr2attr(hl);
6393 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 redraw_this = TRUE;
6395 }
6396#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006397#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006398 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +02006399 redraw_this = FALSE;
6400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 if (redraw_this)
6402 {
6403 /*
6404 * Special handling when 'xs' termcap flag set (hpterm):
6405 * Attributes for characters are stored at the position where the
6406 * cursor is when writing the highlighting code. The
6407 * start-highlighting code must be written with the cursor on the
6408 * first highlighted character. The stop-highlighting code must
6409 * be written with the cursor just after the last highlighted
6410 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006411 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 * to clear the rest of the line, and force redrawing it
6413 * completely.
6414 */
6415 if ( p_wiv
6416 && !force
6417#ifdef FEAT_GUI
6418 && !gui.in_use
6419#endif
6420 && ScreenAttrs[off_to] != 0
6421 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6422 {
6423 /*
6424 * Need to remove highlighting attributes here.
6425 */
6426 windgoto(row, col + coloff);
6427 out_str(T_CE); /* clear rest of this screen line */
6428 screen_start(); /* don't know where cursor is now */
6429 force = TRUE; /* force redraw of rest of the line */
6430 redraw_next = TRUE; /* or else next char would miss out */
6431
6432 /*
6433 * If the previous character was highlighted, need to stop
6434 * highlighting at this character.
6435 */
6436 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6437 {
6438 screen_attr = ScreenAttrs[off_to - 1];
6439 term_windgoto(row, col + coloff);
6440 screen_stop_highlight();
6441 }
6442 else
6443 screen_attr = 0; /* highlighting has stopped */
6444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 if (enc_dbcs != 0)
6446 {
6447 /* Check if overwriting a double-byte with a single-byte or
6448 * the other way around requires another character to be
6449 * redrawn. For UTF-8 this isn't needed, because comparing
6450 * ScreenLinesUC[] is sufficient. */
6451 if (char_cells == 1
6452 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006453 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 {
6455 /* Writing a single-cell character over a double-cell
6456 * character: need to redraw the next cell. */
6457 ScreenLines[off_to + 1] = 0;
6458 redraw_next = TRUE;
6459 }
6460 else if (char_cells == 2
6461 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006462 && (*mb_off2cells)(off_to, max_off_to) == 1
6463 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 {
6465 /* Writing the second half of a double-cell character over
6466 * a double-cell character: need to redraw the second
6467 * cell. */
6468 ScreenLines[off_to + 2] = 0;
6469 redraw_next = TRUE;
6470 }
6471
6472 if (enc_dbcs == DBCS_JPNU)
6473 ScreenLines2[off_to] = ScreenLines2[off_from];
6474 }
6475 /* When writing a single-width character over a double-width
6476 * character and at the end of the redrawn text, need to clear out
6477 * the right halve of the old character.
6478 * Also required when writing the right halve of a double-width
6479 * char over the left halve of an existing one. */
6480 if (has_mbyte && col + char_cells == endcol
6481 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006482 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006484 && (*mb_off2cells)(off_to, max_off_to) == 1
6485 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487
6488 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 if (enc_utf8)
6490 {
6491 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6492 if (ScreenLinesUC[off_from] != 0)
6493 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006494 int i;
6495
6496 for (i = 0; i < Screen_mco; ++i)
6497 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 }
6499 }
6500 if (char_cells == 2)
6501 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502
6503#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006504 /* The bold trick makes a single column of pixels appear in the
6505 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 * character should be redrawn too. This happens for our own GUI
6507 * and for some xterms. */
6508 if (
6509# ifdef FEAT_GUI
6510 gui.in_use
6511# endif
6512# if defined(FEAT_GUI) && defined(UNIX)
6513 ||
6514# endif
6515# ifdef UNIX
6516 term_is_xterm
6517# endif
6518 )
6519 {
6520 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006521 if (hl > HL_ALL)
6522 hl = syn_attr2attr(hl);
6523 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 redraw_next = TRUE;
6525 }
6526#endif
6527 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006528
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006529 /* For simplicity set the attributes of second half of a
6530 * double-wide character equal to the first half. */
6531 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006533
6534 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 screen_char(off_to, row, col + coloff);
6538 }
6539 else if ( p_wiv
6540#ifdef FEAT_GUI
6541 && !gui.in_use
6542#endif
6543 && col + coloff > 0)
6544 {
6545 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6546 {
6547 /*
6548 * Don't output stop-highlight when moving the cursor, it will
6549 * stop the highlighting when it should continue.
6550 */
6551 screen_attr = 0;
6552 }
6553 else if (screen_attr != 0)
6554 screen_stop_highlight();
6555 }
6556
6557 off_to += CHAR_CELLS;
6558 off_from += CHAR_CELLS;
6559 col += CHAR_CELLS;
6560 }
6561
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 if (clear_next)
6563 {
6564 /* Clear the second half of a double-wide character of which the left
6565 * half was overwritten with a single-wide character. */
6566 ScreenLines[off_to] = ' ';
6567 if (enc_utf8)
6568 ScreenLinesUC[off_to] = 0;
6569 screen_char(off_to, row, col + coloff);
6570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
6572 if (clear_width > 0
6573#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006574 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575#endif
6576 )
6577 {
6578#ifdef FEAT_GUI
6579 int startCol = col;
6580#endif
6581
6582 /* blank out the rest of the line */
6583 while (col < clear_width && ScreenLines[off_to] == ' '
6584 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006585 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 {
6587 ++off_to;
6588 ++col;
6589 }
6590 if (col < clear_width)
6591 {
6592#ifdef FEAT_GUI
6593 /*
6594 * In the GUI, clearing the rest of the line may leave pixels
6595 * behind if the first character cleared was bold. Some bold
6596 * fonts spill over the left. In this case we redraw the previous
6597 * character too. If we didn't skip any blanks above, then we
6598 * only redraw if the character wasn't already redrawn anyway.
6599 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006600 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 {
6602 hl = ScreenAttrs[off_to];
6603 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006604 {
6605 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006606
Bram Moolenaar9c697322006-10-09 20:11:17 +00006607 if (enc_utf8)
6608 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6609 * that its width is 2. */
6610 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6611 else if (enc_dbcs != 0)
6612 {
6613 /* find previous character by counting from first
6614 * column and get its width. */
6615 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006616 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006617
6618 while (off < off_to)
6619 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006620 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006621 off += prev_cells;
6622 }
6623 }
6624
6625 if (enc_dbcs != 0 && prev_cells > 1)
6626 screen_char_2(off_to - prev_cells, row,
6627 col + coloff - prev_cells);
6628 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006629 screen_char(off_to - prev_cells, row,
6630 col + coloff - prev_cells);
6631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 }
6633#endif
6634 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6635 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 off_to += clear_width - col;
6637 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 }
6639 }
6640
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006641 if (clear_width > 0
6642#ifdef FEAT_TEXT_PROP
6643 && !(flags & SLF_POPUP) // no separator for popup window
6644#endif
6645 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006647 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006648 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006649 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006651#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006652 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006655 int c;
6656
6657 c = fillchar_vsep(&hl);
6658 if (ScreenLines[off_to] != (schar_T)c
6659 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6660 != (c >= 0x80 ? c : 0))
6661 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006663 ScreenLines[off_to] = c;
6664 ScreenAttrs[off_to] = hl;
6665 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006667 if (c >= 0x80)
6668 {
6669 ScreenLinesUC[off_to] = c;
6670 ScreenLinesC[0][off_to] = 0;
6671 }
6672 else
6673 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006675 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 }
6678 }
6679 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 LineWraps[row] = FALSE;
6681 }
6682}
6683
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006684#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006686 * Mirror text "str" for right-left displaying.
6687 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006689 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006690rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691{
6692 char_u *p1, *p2;
6693 int t;
6694
6695 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6696 {
6697 t = *p1;
6698 *p1 = *p2;
6699 *p2 = t;
6700 }
6701}
6702#endif
6703
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704/*
6705 * mark all status lines for redraw; used after first :cd
6706 */
6707 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006708status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709{
6710 win_T *wp;
6711
Bram Moolenaar29323592016-07-24 22:04:11 +02006712 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 if (wp->w_status_height)
6714 {
6715 wp->w_redr_status = TRUE;
6716 redraw_later(VALID);
6717 }
6718}
6719
6720/*
6721 * mark all status lines of the current buffer for redraw
6722 */
6723 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006724status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725{
6726 win_T *wp;
6727
Bram Moolenaar29323592016-07-24 22:04:11 +02006728 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6730 {
6731 wp->w_redr_status = TRUE;
6732 redraw_later(VALID);
6733 }
6734}
6735
6736/*
6737 * Redraw all status lines that need to be redrawn.
6738 */
6739 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006740redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741{
6742 win_T *wp;
6743
Bram Moolenaar29323592016-07-24 22:04:11 +02006744 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006746 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006747 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006748 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750
Bram Moolenaar4033c552017-09-16 20:54:51 +02006751#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752/*
6753 * Redraw all status lines at the bottom of frame "frp".
6754 */
6755 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006756win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757{
6758 if (frp->fr_layout == FR_LEAF)
6759 frp->fr_win->w_redr_status = TRUE;
6760 else if (frp->fr_layout == FR_ROW)
6761 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006762 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 win_redraw_last_status(frp);
6764 }
6765 else /* frp->fr_layout == FR_COL */
6766 {
6767 frp = frp->fr_child;
6768 while (frp->fr_next != NULL)
6769 frp = frp->fr_next;
6770 win_redraw_last_status(frp);
6771 }
6772}
6773#endif
6774
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775/*
6776 * Draw the verticap separator right of window "wp" starting with line "row".
6777 */
6778 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006779draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780{
6781 int hl;
6782 int c;
6783
6784 if (wp->w_vsep_width)
6785 {
6786 /* draw the vertical separator right of this window */
6787 c = fillchar_vsep(&hl);
6788 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6789 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6790 c, ' ', hl);
6791 }
6792}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793
6794#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006795static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796
6797/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006798 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 */
6800 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006801status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802{
6803 int len = 0;
6804
6805#ifdef FEAT_MENU
6806 int emenu = (xp->xp_context == EXPAND_MENUS
6807 || xp->xp_context == EXPAND_MENUNAMES);
6808
6809 /* Check for menu separators - replace with '|'. */
6810 if (emenu && menu_is_separator(s))
6811 return 1;
6812#endif
6813
6814 while (*s != NUL)
6815 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006816 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006817 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006818 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 }
6820
6821 return len;
6822}
6823
6824/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006825 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006826 * These are backslashes used for escaping. Do show backslashes in help tags.
6827 */
6828 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006829skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006830{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006831 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006832#ifdef FEAT_MENU
6833 || ((xp->xp_context == EXPAND_MENUS
6834 || xp->xp_context == EXPAND_MENUNAMES)
6835 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6836#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006837 )
6838 {
6839#ifndef BACKSLASH_IN_FILENAME
6840 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6841 return 2;
6842#endif
6843 return 1;
6844 }
6845 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006846}
6847
6848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 * Show wildchar matches in the status line.
6850 * Show at least the "match" item.
6851 * We start at item 'first_match' in the list and show all matches that fit.
6852 *
6853 * If inversion is possible we use it. Else '=' characters are used.
6854 */
6855 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006856win_redr_status_matches(
6857 expand_T *xp,
6858 int num_matches,
6859 char_u **matches, /* list of matches */
6860 int match,
6861 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862{
6863#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6864 int row;
6865 char_u *buf;
6866 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006867 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 int fillchar;
6869 int attr;
6870 int i;
6871 int highlight = TRUE;
6872 char_u *selstart = NULL;
6873 int selstart_col = 0;
6874 char_u *selend = NULL;
6875 static int first_match = 0;
6876 int add_left = FALSE;
6877 char_u *s;
6878#ifdef FEAT_MENU
6879 int emenu;
6880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882
6883 if (matches == NULL) /* interrupted completion? */
6884 return;
6885
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006886 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006887 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006888 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006889 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 if (buf == NULL)
6891 return;
6892
6893 if (match == -1) /* don't show match but original text */
6894 {
6895 match = 0;
6896 highlight = FALSE;
6897 }
6898 /* count 1 for the ending ">" */
6899 clen = status_match_len(xp, L_MATCH(match)) + 3;
6900 if (match == 0)
6901 first_match = 0;
6902 else if (match < first_match)
6903 {
6904 /* jumping left, as far as we can go */
6905 first_match = match;
6906 add_left = TRUE;
6907 }
6908 else
6909 {
6910 /* check if match fits on the screen */
6911 for (i = first_match; i < match; ++i)
6912 clen += status_match_len(xp, L_MATCH(i)) + 2;
6913 if (first_match > 0)
6914 clen += 2;
6915 /* jumping right, put match at the left */
6916 if ((long)clen > Columns)
6917 {
6918 first_match = match;
6919 /* if showing the last match, we can add some on the left */
6920 clen = 2;
6921 for (i = match; i < num_matches; ++i)
6922 {
6923 clen += status_match_len(xp, L_MATCH(i)) + 2;
6924 if ((long)clen >= Columns)
6925 break;
6926 }
6927 if (i == num_matches)
6928 add_left = TRUE;
6929 }
6930 }
6931 if (add_left)
6932 while (first_match > 0)
6933 {
6934 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6935 if ((long)clen >= Columns)
6936 break;
6937 --first_match;
6938 }
6939
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006940 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941
6942 if (first_match == 0)
6943 {
6944 *buf = NUL;
6945 len = 0;
6946 }
6947 else
6948 {
6949 STRCPY(buf, "< ");
6950 len = 2;
6951 }
6952 clen = len;
6953
6954 i = first_match;
6955 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6956 {
6957 if (i == match)
6958 {
6959 selstart = buf + len;
6960 selstart_col = clen;
6961 }
6962
6963 s = L_MATCH(i);
6964 /* Check for menu separators - replace with '|' */
6965#ifdef FEAT_MENU
6966 emenu = (xp->xp_context == EXPAND_MENUS
6967 || xp->xp_context == EXPAND_MENUNAMES);
6968 if (emenu && menu_is_separator(s))
6969 {
6970 STRCPY(buf + len, transchar('|'));
6971 l = (int)STRLEN(buf + len);
6972 len += l;
6973 clen += l;
6974 }
6975 else
6976#endif
6977 for ( ; *s != NUL; ++s)
6978 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006979 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006981 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 {
6983 STRNCPY(buf + len, s, l);
6984 s += l - 1;
6985 len += l;
6986 }
6987 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 {
6989 STRCPY(buf + len, transchar_byte(*s));
6990 len += (int)STRLEN(buf + len);
6991 }
6992 }
6993 if (i == match)
6994 selend = buf + len;
6995
6996 *(buf + len++) = ' ';
6997 *(buf + len++) = ' ';
6998 clen += 2;
6999 if (++i == num_matches)
7000 break;
7001 }
7002
7003 if (i != num_matches)
7004 {
7005 *(buf + len++) = '>';
7006 ++clen;
7007 }
7008
7009 buf[len] = NUL;
7010
7011 row = cmdline_row - 1;
7012 if (row >= 0)
7013 {
7014 if (wild_menu_showing == 0)
7015 {
7016 if (msg_scrolled > 0)
7017 {
7018 /* Put the wildmenu just above the command line. If there is
7019 * no room, scroll the screen one line up. */
7020 if (cmdline_row == Rows - 1)
7021 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02007022 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 ++msg_scrolled;
7024 }
7025 else
7026 {
7027 ++cmdline_row;
7028 ++row;
7029 }
7030 wild_menu_showing = WM_SCROLLED;
7031 }
7032 else
7033 {
7034 /* Create status line if needed by setting 'laststatus' to 2.
7035 * Set 'winminheight' to zero to avoid that the window is
7036 * resized. */
7037 if (lastwin->w_status_height == 0)
7038 {
7039 save_p_ls = p_ls;
7040 save_p_wmh = p_wmh;
7041 p_ls = 2;
7042 p_wmh = 0;
7043 last_status(FALSE);
7044 }
7045 wild_menu_showing = WM_SHOWN;
7046 }
7047 }
7048
7049 screen_puts(buf, row, 0, attr);
7050 if (selstart != NULL && highlight)
7051 {
7052 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007053 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 }
7055
7056 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7057 }
7058
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 vim_free(buf);
7061}
7062#endif
7063
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064/*
7065 * Redraw the status line of window wp.
7066 *
7067 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007068 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7069 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007071 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007072win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073{
7074 int row;
7075 char_u *p;
7076 int len;
7077 int fillchar;
7078 int attr;
7079 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007080 static int busy = FALSE;
7081
7082 /* It's possible to get here recursively when 'statusline' (indirectly)
7083 * invokes ":redrawstatus". Simply ignore the call then. */
7084 if (busy)
7085 return;
7086 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087
7088 wp->w_redr_status = FALSE;
7089 if (wp->w_status_height == 0)
7090 {
7091 /* no status line, can only be last window */
7092 redraw_cmdline = TRUE;
7093 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007094 else if (!redrawing()
7095#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007096 // don't update status line when popup menu is visible and may be
7097 // drawn over it, unless it will be redrawn later
7098 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007099#endif
7100 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {
7102 /* Don't redraw right now, do it later. */
7103 wp->w_redr_status = TRUE;
7104 }
7105#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007106 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 {
7108 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007109 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 }
7111#endif
7112 else
7113 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007114 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007116 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 p = NameBuff;
7118 len = (int)STRLEN(p);
7119
Bram Moolenaard85f2712017-07-28 21:51:57 +02007120 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121#ifdef FEAT_QUICKFIX
7122 || wp->w_p_pvw
7123#endif
7124 || bufIsChanged(wp->w_buffer)
7125 || wp->w_buffer->b_p_ro)
7126 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007127 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007129 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 len += (int)STRLEN(p + len);
7131 }
7132#ifdef FEAT_QUICKFIX
7133 if (wp->w_p_pvw)
7134 {
7135 STRCPY(p + len, _("[Preview]"));
7136 len += (int)STRLEN(p + len);
7137 }
7138#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007139 if (bufIsChanged(wp->w_buffer)
7140#ifdef FEAT_TERMINAL
7141 && !bt_terminal(wp->w_buffer)
7142#endif
7143 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 {
7145 STRCPY(p + len, "[+]");
7146 len += 3;
7147 }
7148 if (wp->w_buffer->b_p_ro)
7149 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007150 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007151 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 }
7153
Bram Moolenaar02631462017-09-22 15:20:32 +02007154 this_ru_col = ru_col - (Columns - wp->w_width);
7155 if (this_ru_col < (wp->w_width + 1) / 2)
7156 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 if (this_ru_col <= 1)
7158 {
7159 p = (char_u *)"<"; /* No room for file name! */
7160 len = 1;
7161 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007162 else if (has_mbyte)
7163 {
7164 int clen = 0, i;
7165
7166 /* Count total number of display cells. */
7167 clen = mb_string2cells(p, -1);
7168
7169 /* Find first character that will fit.
7170 * Going from start to end is much faster for DBCS. */
7171 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7172 i += (*mb_ptr2len)(p + i))
7173 clen -= (*mb_ptr2cells)(p + i);
7174 len = clen;
7175 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007177 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007179 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 }
7181
Bram Moolenaara12a1612019-01-24 16:39:02 +01007182 }
7183 else if (len > this_ru_col - 1)
7184 {
7185 p += len - (this_ru_col - 1);
7186 *p = '<';
7187 len = this_ru_col - 1;
7188 }
7189
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007191 screen_puts(p, row, wp->w_wincol, attr);
7192 screen_fill(row, row + 1, len + wp->w_wincol,
7193 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007195 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7197 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007198 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199
7200#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007201 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202#endif
7203 }
7204
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 /*
7206 * May need to draw the character below the vertical separator.
7207 */
7208 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7209 {
7210 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007211 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 else
7213 fillchar = fillchar_vsep(&attr);
7214 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7215 attr);
7216 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007217 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218}
7219
Bram Moolenaar238a5642006-02-21 22:12:05 +00007220#ifdef FEAT_STL_OPT
7221/*
7222 * Redraw the status line according to 'statusline' and take care of any
7223 * errors encountered.
7224 */
7225 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007226redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007227{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007228 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007229 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007230
7231 /* When called recursively return. This can happen when the statusline
7232 * contains an expression that triggers a redraw. */
7233 if (entered)
7234 return;
7235 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007236
Bram Moolenaara742e082016-04-05 21:10:38 +02007237 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007238 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007239 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007240 {
7241 /* When there is an error disable the statusline, otherwise the
7242 * display is messed up with errors and a redraw triggers the problem
7243 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007244 set_string_option_direct((char_u *)"statusline", -1,
7245 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007246 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007247 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007248 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007249 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007250}
7251#endif
7252
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253/*
7254 * Return TRUE if the status line of window "wp" is connected to the status
7255 * line of the window right of it. If not, then it's a vertical separator.
7256 * Only call if (wp->w_vsep_width != 0).
7257 */
7258 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007259stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260{
7261 frame_T *fr;
7262
7263 fr = wp->w_frame;
7264 while (fr->fr_parent != NULL)
7265 {
7266 if (fr->fr_parent->fr_layout == FR_COL)
7267 {
7268 if (fr->fr_next != NULL)
7269 break;
7270 }
7271 else
7272 {
7273 if (fr->fr_next != NULL)
7274 return TRUE;
7275 }
7276 fr = fr->fr_parent;
7277 }
7278 return FALSE;
7279}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282/*
7283 * Get the value to show for the language mappings, active 'keymap'.
7284 */
7285 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007286get_keymap_str(
7287 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007288 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007289 char_u *buf, /* buffer for the result */
7290 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291{
7292 char_u *p;
7293
7294 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7295 return FALSE;
7296
7297 {
7298#ifdef FEAT_EVAL
7299 buf_T *old_curbuf = curbuf;
7300 win_T *old_curwin = curwin;
7301 char_u *s;
7302
7303 curbuf = wp->w_buffer;
7304 curwin = wp;
7305 STRCPY(buf, "b:keymap_name"); /* must be writable */
7306 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007307 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 --emsg_skip;
7309 curbuf = old_curbuf;
7310 curwin = old_curwin;
7311 if (p == NULL || *p == NUL)
7312#endif
7313 {
7314#ifdef FEAT_KEYMAP
7315 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7316 p = wp->w_buffer->b_p_keymap;
7317 else
7318#endif
7319 p = (char_u *)"lang";
7320 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007321 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 buf[0] = NUL;
7323#ifdef FEAT_EVAL
7324 vim_free(s);
7325#endif
7326 }
7327 return buf[0] != NUL;
7328}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329
7330#if defined(FEAT_STL_OPT) || defined(PROTO)
7331/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007332 * Redraw the status line or ruler of window "wp".
7333 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 */
7335 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007336win_redr_custom(
7337 win_T *wp,
7338 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007340 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 int attr;
7342 int curattr;
7343 int row;
7344 int col = 0;
7345 int maxwidth;
7346 int width;
7347 int n;
7348 int len;
7349 int fillchar;
7350 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007351 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007353 struct stl_hlrec hltab[STL_MAX_ITEM];
7354 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007355 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007356 win_T *ewp;
7357 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358
Bram Moolenaar1d633412013-12-11 15:52:01 +01007359 /* There is a tiny chance that this gets called recursively: When
7360 * redrawing a status line triggers redrawing the ruler or tabline.
7361 * Avoid trouble by not allowing recursion. */
7362 if (entered)
7363 return;
7364 entered = TRUE;
7365
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007367 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007369 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007370 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007371 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007372 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007373 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007374 maxwidth = Columns;
7375# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007376 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007377# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007379 else
7380 {
7381 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007382 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007383 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007384
7385 if (draw_ruler)
7386 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007387 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007388 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007389 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007390 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007391 if (*++stl == '-')
7392 stl++;
7393 if (atoi((char *)stl))
7394 while (VIM_ISDIGIT(*stl))
7395 stl++;
7396 if (*stl++ != '(')
7397 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007398 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007399 col = ru_col - (Columns - wp->w_width);
7400 if (col < (wp->w_width + 1) / 2)
7401 col = (wp->w_width + 1) / 2;
7402 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007403 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007404 {
7405 row = Rows - 1;
7406 --maxwidth; /* writing in last column may cause scrolling */
7407 fillchar = ' ';
7408 attr = 0;
7409 }
7410
7411# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007412 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007413# endif
7414 }
7415 else
7416 {
7417 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007418 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007419 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007420 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007421# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007422 use_sandbox = was_set_insecurely((char_u *)"statusline",
7423 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007424# endif
7425 }
7426
Bram Moolenaar53f81742017-09-22 14:35:51 +02007427 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007428 }
7429
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007431 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
Bram Moolenaar61452852011-02-01 18:01:11 +01007433 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7434 * the cursor away and back. */
7435 ewp = wp == NULL ? curwin : wp;
7436 p_crb_save = ewp->w_p_crb;
7437 ewp->w_p_crb = FALSE;
7438
Bram Moolenaar362f3562009-11-03 16:20:34 +00007439 /* Make a copy, because the statusline may include a function call that
7440 * might change the option value and free the memory. */
7441 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007442 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007443 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007444 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007445 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007446 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007448 /* Make all characters printable. */
7449 p = transstr(buf);
7450 if (p != NULL)
7451 {
7452 vim_strncpy(buf, p, sizeof(buf) - 1);
7453 vim_free(p);
7454 }
7455
7456 /* fill up with "fillchar" */
7457 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007458 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 ++width;
7462 }
7463 buf[len] = NUL;
7464
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007465 /*
7466 * Draw each snippet with the specified highlighting.
7467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 curattr = attr;
7469 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007470 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007472 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 screen_puts_len(p, len, row, col, curattr);
7474 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007475 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007477 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007479 else if (hltab[n].userhl < 0)
7480 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007481#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007482 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7483 && wp->w_status_height != 0)
7484 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007485 else if (wp != NULL && bt_terminal(wp->w_buffer)
7486 && wp->w_status_height != 0)
7487 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007488#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007489 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007490 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007492 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 }
7494 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007495
7496 if (wp == NULL)
7497 {
7498 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7499 col = 0;
7500 len = 0;
7501 p = buf;
7502 fillchar = 0;
7503 for (n = 0; tabtab[n].start != NULL; n++)
7504 {
7505 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7506 while (col < len)
7507 TabPageIdxs[col++] = fillchar;
7508 p = tabtab[n].start;
7509 fillchar = tabtab[n].userhl;
7510 }
7511 while (col < Columns)
7512 TabPageIdxs[col++] = fillchar;
7513 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007514
7515theend:
7516 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517}
7518
7519#endif /* FEAT_STL_OPT */
7520
7521/*
7522 * Output a single character directly to the screen and update ScreenLines.
7523 */
7524 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007525screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 char_u buf[MB_MAXBYTES + 1];
7528
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007529 if (has_mbyte)
7530 buf[(*mb_char2bytes)(c, buf)] = NUL;
7531 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007532 {
7533 buf[0] = c;
7534 buf[1] = NUL;
7535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 screen_puts(buf, row, col, attr);
7537}
7538
7539/*
7540 * Get a single character directly from ScreenLines into "bytes[]".
7541 * Also return its attribute in *attrp;
7542 */
7543 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007544screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545{
7546 unsigned off;
7547
7548 /* safety check */
7549 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7550 {
7551 off = LineOffset[row] + col;
7552 *attrp = ScreenAttrs[off];
7553 bytes[0] = ScreenLines[off];
7554 bytes[1] = NUL;
7555
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 if (enc_utf8 && ScreenLinesUC[off] != 0)
7557 bytes[utfc_char2bytes(off, bytes)] = NUL;
7558 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7559 {
7560 bytes[0] = ScreenLines[off];
7561 bytes[1] = ScreenLines2[off];
7562 bytes[2] = NUL;
7563 }
7564 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7565 {
7566 bytes[1] = ScreenLines[off + 1];
7567 bytes[2] = NUL;
7568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 }
7570}
7571
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007572/*
7573 * Return TRUE if composing characters for screen posn "off" differs from
7574 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007575 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007576 */
7577 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007578screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007579{
7580 int i;
7581
7582 for (i = 0; i < Screen_mco; ++i)
7583 {
7584 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7585 return TRUE;
7586 if (u8cc[i] == 0)
7587 break;
7588 }
7589 return FALSE;
7590}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007591
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592/*
7593 * Put string '*text' on the screen at position 'row' and 'col', with
7594 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7595 * Note: only outputs within one row, message is truncated at screen boundary!
7596 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7597 */
7598 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007599screen_puts(
7600 char_u *text,
7601 int row,
7602 int col,
7603 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604{
7605 screen_puts_len(text, -1, row, col, attr);
7606}
7607
7608/*
7609 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7610 * a NUL.
7611 */
7612 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007613screen_puts_len(
7614 char_u *text,
7615 int textlen,
7616 int row,
7617 int col,
7618 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619{
7620 unsigned off;
7621 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007622 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007624 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 int mbyte_blen = 1;
7626 int mbyte_cells = 1;
7627 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007628 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007630#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007632 int pc, nc, nc1;
7633 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007635 int force_redraw_this;
7636 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007637 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007639 // Safety check. The check for negative row and column is to fix issue
7640 // #4102. TODO: find out why row/col could be negative.
7641 if (ScreenLines == NULL
7642 || row >= screen_Rows || row < 0
7643 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007645 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646
Bram Moolenaarc236c162008-07-13 17:41:49 +00007647 /* When drawing over the right halve of a double-wide char clear out the
7648 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007649 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007650#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007651 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007652#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007653 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007654 {
7655 ScreenLines[off - 1] = ' ';
7656 ScreenAttrs[off - 1] = 0;
7657 if (enc_utf8)
7658 {
7659 ScreenLinesUC[off - 1] = 0;
7660 ScreenLinesC[0][off - 1] = 0;
7661 }
7662 /* redraw the previous cell, make it empty */
7663 screen_char(off - 1, row, col - 1);
7664 /* force the cell at "col" to be redrawn */
7665 force_redraw_next = TRUE;
7666 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007667
Bram Moolenaar367329b2007-08-30 11:53:22 +00007668 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007669 while (col < screen_Columns
7670 && (len < 0 || (int)(ptr - text) < len)
7671 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 {
7673 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 /* check if this is the first byte of a multibyte */
7675 if (has_mbyte)
7676 {
7677 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007678 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007680 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7682 mbyte_cells = 1;
7683 else if (enc_dbcs != 0)
7684 mbyte_cells = mbyte_blen;
7685 else /* enc_utf8 */
7686 {
7687 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007688 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 (int)((text + len) - ptr));
7690 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007691 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007693#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7695 {
7696 /* Do Arabic shaping. */
7697 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7698 {
7699 /* Past end of string to be displayed. */
7700 nc = NUL;
7701 nc1 = NUL;
7702 }
7703 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007704 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007705 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7706 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007707 nc1 = pcc[0];
7708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 pc = prev_c;
7710 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007711 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 }
7713 else
7714 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007715#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007716 if (col + mbyte_cells > screen_Columns)
7717 {
7718 /* Only 1 cell left, but character requires 2 cells:
7719 * display a '>' in the last column to avoid wrapping. */
7720 c = '>';
7721 mbyte_cells = 1;
7722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 }
7724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007726 force_redraw_this = force_redraw_next;
7727 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007728
7729 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 || (mbyte_cells == 2
7731 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7732 || (enc_dbcs == DBCS_JPNU
7733 && c == 0x8e
7734 && ScreenLines2[off] != ptr[1])
7735 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007736 && (ScreenLinesUC[off] !=
7737 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7738 || (ScreenLinesUC[off] != 0
7739 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007741 || exmode_active;
7742
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007743 if ((need_redraw || force_redraw_this)
7744#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007745 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007746#endif
7747 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 {
7749#if defined(FEAT_GUI) || defined(UNIX)
7750 /* The bold trick makes a single row of pixels appear in the next
7751 * character. When a bold character is removed, the next
7752 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007753 * and for some xterms. */
7754 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755# ifdef FEAT_GUI
7756 gui.in_use
7757# endif
7758# if defined(FEAT_GUI) && defined(UNIX)
7759 ||
7760# endif
7761# ifdef UNIX
7762 term_is_xterm
7763# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007764 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007766 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007768 if (n > HL_ALL)
7769 n = syn_attr2attr(n);
7770 if (n & HL_BOLD)
7771 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 }
7773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 /* When at the end of the text and overwriting a two-cell
7775 * character with a one-cell character, need to clear the next
7776 * cell. Also when overwriting the left halve of a two-cell char
7777 * with the right halve of a two-cell char. Do this only once
7778 * (mb_off2cells() may return 2 on the right halve). */
7779 if (clear_next_cell)
7780 clear_next_cell = FALSE;
7781 else if (has_mbyte
7782 && (len < 0 ? ptr[mbyte_blen] == NUL
7783 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007784 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007786 && (*mb_off2cells)(off, max_off) == 1
7787 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 clear_next_cell = TRUE;
7789
7790 /* Make sure we never leave a second byte of a double-byte behind,
7791 * it confuses mb_off2cells(). */
7792 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007793 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007795 && (*mb_off2cells)(off, max_off) == 1
7796 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 ScreenLines[off] = c;
7799 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 if (enc_utf8)
7801 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007802 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 ScreenLinesUC[off] = 0;
7804 else
7805 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007806 int i;
7807
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007809 for (i = 0; i < Screen_mco; ++i)
7810 {
7811 ScreenLinesC[i][off] = u8cc[i];
7812 if (u8cc[i] == 0)
7813 break;
7814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 }
7816 if (mbyte_cells == 2)
7817 {
7818 ScreenLines[off + 1] = 0;
7819 ScreenAttrs[off + 1] = attr;
7820 }
7821 screen_char(off, row, col);
7822 }
7823 else if (mbyte_cells == 2)
7824 {
7825 ScreenLines[off + 1] = ptr[1];
7826 ScreenAttrs[off + 1] = attr;
7827 screen_char_2(off, row, col);
7828 }
7829 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7830 {
7831 ScreenLines2[off] = ptr[1];
7832 screen_char(off, row, col);
7833 }
7834 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 screen_char(off, row, col);
7836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 if (has_mbyte)
7838 {
7839 off += mbyte_cells;
7840 col += mbyte_cells;
7841 ptr += mbyte_blen;
7842 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007843 {
7844 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007846 len = -1;
7847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 }
7849 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {
7851 ++off;
7852 ++col;
7853 ++ptr;
7854 }
7855 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007856
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007857 /* If we detected the next character needs to be redrawn, but the text
7858 * doesn't extend up to there, update the character here. */
7859 if (force_redraw_next && col < screen_Columns)
7860 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007861 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7862 screen_char_2(off, row, col);
7863 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007864 screen_char(off, row, col);
7865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866}
7867
7868#ifdef FEAT_SEARCH_EXTRA
7869/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007870 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 */
7872 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007873start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874{
7875 if (p_hls && !no_hlsearch)
7876 {
7877 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007878 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007879# ifdef FEAT_RELTIME
7880 /* Set the time limit to 'redrawtime'. */
7881 profile_setlimit(p_rdt, &search_hl.tm);
7882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 }
7884}
7885
7886/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007887 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 */
7889 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007890end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891{
7892 if (search_hl.rm.regprog != NULL)
7893 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007894 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 search_hl.rm.regprog = NULL;
7896 }
7897}
7898
7899/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007900 * Init for calling prepare_search_hl().
7901 */
7902 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007903init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007904{
7905 matchitem_T *cur;
7906
7907 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7908 * match */
7909 cur = wp->w_match_head;
7910 while (cur != NULL)
7911 {
7912 cur->hl.rm = cur->match;
7913 if (cur->hlg_id == 0)
7914 cur->hl.attr = 0;
7915 else
7916 cur->hl.attr = syn_id2attr(cur->hlg_id);
7917 cur->hl.buf = wp->w_buffer;
7918 cur->hl.lnum = 0;
7919 cur->hl.first_lnum = 0;
7920# ifdef FEAT_RELTIME
7921 /* Set the time limit to 'redrawtime'. */
7922 profile_setlimit(p_rdt, &(cur->hl.tm));
7923# endif
7924 cur = cur->next;
7925 }
7926 search_hl.buf = wp->w_buffer;
7927 search_hl.lnum = 0;
7928 search_hl.first_lnum = 0;
7929 /* time limit is set at the toplevel, for all windows */
7930}
7931
7932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 * Advance to the match in window "wp" line "lnum" or past it.
7934 */
7935 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007936prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007938 matchitem_T *cur; /* points to the match list */
7939 match_T *shl; /* points to search_hl or a match */
7940 int shl_flag; /* flag to indicate whether search_hl
7941 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007942 int pos_inprogress; /* marks that position match search is
7943 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 int n;
7945
7946 /*
7947 * When using a multi-line pattern, start searching at the top
7948 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007949 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007951 cur = wp->w_match_head;
7952 shl_flag = FALSE;
7953 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007955 if (shl_flag == FALSE)
7956 {
7957 shl = &search_hl;
7958 shl_flag = TRUE;
7959 }
7960 else
7961 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 if (shl->rm.regprog != NULL
7963 && shl->lnum == 0
7964 && re_multiline(shl->rm.regprog))
7965 {
7966 if (shl->first_lnum == 0)
7967 {
7968# ifdef FEAT_FOLDING
7969 for (shl->first_lnum = lnum;
7970 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7971 if (hasFoldingWin(wp, shl->first_lnum - 1,
7972 NULL, NULL, TRUE, NULL))
7973 break;
7974# else
7975 shl->first_lnum = wp->w_topline;
7976# endif
7977 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007978 if (cur != NULL)
7979 cur->pos.cur = 0;
7980 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007982 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7983 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007985 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7986 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007987 pos_inprogress = cur == NULL || cur->pos.cur == 0
7988 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 if (shl->lnum != 0)
7990 {
7991 shl->first_lnum = shl->lnum
7992 + shl->rm.endpos[0].lnum
7993 - shl->rm.startpos[0].lnum;
7994 n = shl->rm.endpos[0].col;
7995 }
7996 else
7997 {
7998 ++shl->first_lnum;
7999 n = 0;
8000 }
8001 }
8002 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008003 if (shl != &search_hl && cur != NULL)
8004 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 }
8006}
8007
8008/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008009 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 * Uses shl->buf.
8011 * Sets shl->lnum and shl->rm contents.
8012 * Note: Assumes a previous match is always before "lnum", unless
8013 * shl->lnum is zero.
8014 * Careful: Any pointers for buffer lines will become invalid.
8015 */
8016 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008017next_search_hl(
8018 win_T *win,
8019 match_T *shl, /* points to search_hl or a match */
8020 linenr_T lnum,
8021 colnr_T mincol, /* minimal column for a match */
8022 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023{
8024 linenr_T l;
8025 colnr_T matchcol;
8026 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008027 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008029 // for :{range}s/pat only highlight inside the range
8030 if (lnum < search_first_line || lnum > search_last_line)
8031 {
8032 shl->lnum = 0;
8033 return;
8034 }
8035
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 if (shl->lnum != 0)
8037 {
8038 /* Check for three situations:
8039 * 1. If the "lnum" is below a previous match, start a new search.
8040 * 2. If the previous match includes "mincol", use it.
8041 * 3. Continue after the previous match.
8042 */
8043 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8044 if (lnum > l)
8045 shl->lnum = 0;
8046 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8047 return;
8048 }
8049
8050 /*
8051 * Repeat searching for a match until one is found that includes "mincol"
8052 * or none is found in this line.
8053 */
8054 called_emsg = FALSE;
8055 for (;;)
8056 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008057#ifdef FEAT_RELTIME
8058 /* Stop searching after passing the time limit. */
8059 if (profile_passed_limit(&(shl->tm)))
8060 {
8061 shl->lnum = 0; /* no match found in time */
8062 break;
8063 }
8064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 /* Three situations:
8066 * 1. No useful previous match: search from start of line.
8067 * 2. Not Vi compatible or empty match: continue at next character.
8068 * Break the loop if this is beyond the end of the line.
8069 * 3. Vi compatible searching: continue at end of previous match.
8070 */
8071 if (shl->lnum == 0)
8072 matchcol = 0;
8073 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8074 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008075 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008077 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008078
8079 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008080 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008081 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008083 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 shl->lnum = 0;
8085 break;
8086 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008087 if (has_mbyte)
8088 matchcol += mb_ptr2len(ml);
8089 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008090 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 }
8092 else
8093 matchcol = shl->rm.endpos[0].col;
8094
8095 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008096 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008098 /* Remember whether shl->rm is using a copy of the regprog in
8099 * cur->match. */
8100 int regprog_is_copy = (shl != &search_hl && cur != NULL
8101 && shl == &cur->hl
8102 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008103 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008104
Bram Moolenaarb3414592014-06-17 17:48:32 +02008105 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8106 matchcol,
8107#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008108 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008109#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008110 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008111#endif
8112 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008113 /* Copy the regprog, in case it got freed and recompiled. */
8114 if (regprog_is_copy)
8115 cur->match.regprog = cur->hl.rm.regprog;
8116
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008117 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008118 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008119 /* Error while handling regexp: stop using this regexp. */
8120 if (shl == &search_hl)
8121 {
8122 /* don't free regprog in the match list, it's a copy */
8123 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008124 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008125 }
8126 shl->rm.regprog = NULL;
8127 shl->lnum = 0;
8128 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8129 message */
8130 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008131 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008132 }
8133 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008134 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008135 else
8136 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 if (nmatched == 0)
8138 {
8139 shl->lnum = 0; /* no match found */
8140 break;
8141 }
8142 if (shl->rm.startpos[0].lnum > 0
8143 || shl->rm.startpos[0].col >= mincol
8144 || nmatched > 1
8145 || shl->rm.endpos[0].col > mincol)
8146 {
8147 shl->lnum += shl->rm.startpos[0].lnum;
8148 break; /* useful match found */
8149 }
8150 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008151
8152 // Restore called_emsg for assert_fails().
8153 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155
Bram Moolenaar85077472016-10-16 14:35:48 +02008156/*
8157 * If there is a match fill "shl" and return one.
8158 * Return zero otherwise.
8159 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008160 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008161next_search_hl_pos(
8162 match_T *shl, /* points to a match */
8163 linenr_T lnum,
8164 posmatch_T *posmatch, /* match positions */
8165 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008166{
8167 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008168 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008169
Bram Moolenaarb3414592014-06-17 17:48:32 +02008170 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8171 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008172 llpos_T *pos = &posmatch->pos[i];
8173
8174 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008175 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008176 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008177 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008178 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008179 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008180 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008181 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008182 /* if this match comes before the one at "found" then swap
8183 * them */
8184 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008185 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008186 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008187
Bram Moolenaar85077472016-10-16 14:35:48 +02008188 *pos = posmatch->pos[found];
8189 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008190 }
8191 }
8192 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008193 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008194 }
8195 }
8196 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008197 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008198 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008199 colnr_T start = posmatch->pos[found].col == 0
8200 ? 0 : posmatch->pos[found].col - 1;
8201 colnr_T end = posmatch->pos[found].col == 0
8202 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008203
Bram Moolenaar85077472016-10-16 14:35:48 +02008204 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008205 shl->rm.startpos[0].lnum = 0;
8206 shl->rm.startpos[0].col = start;
8207 shl->rm.endpos[0].lnum = 0;
8208 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008209 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008210 posmatch->cur = found + 1;
8211 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008212 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008213 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008214}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008215#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008216
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008218screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219{
8220 attrentry_T *aep = NULL;
8221
8222 screen_attr = attr;
8223 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008224#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 && termcap_active
8226#endif
8227 )
8228 {
8229#ifdef FEAT_GUI
8230 if (gui.in_use)
8231 {
8232 char buf[20];
8233
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008234 /* The GUI handles this internally. */
8235 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 OUT_STR(buf);
8237 }
8238 else
8239#endif
8240 {
8241 if (attr > HL_ALL) /* special HL attr. */
8242 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008243 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 aep = syn_cterm_attr2entry(attr);
8245 else
8246 aep = syn_term_attr2entry(attr);
8247 if (aep == NULL) /* did ":syntax clear" */
8248 attr = 0;
8249 else
8250 attr = aep->ae_attr;
8251 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008252 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008254 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008255#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008256 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8257 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8258 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008259#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008260 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008261 /* If the Normal FG color has BOLD attribute and the new HL
8262 * has a FG color defined, clear BOLD. */
8263 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008264 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008266 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008267 out_str(T_UCS);
8268 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008269 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8270 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008272 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008274 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008276 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008277 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278
8279 /*
8280 * Output the color or start string after bold etc., in case the
8281 * bold etc. override the color setting.
8282 */
8283 if (aep != NULL)
8284 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008285#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008286 /* When 'termguicolors' is set but fg or bg is unset,
8287 * fall back to the cterm colors. This helps for SpellBad,
8288 * where the GUI uses a red undercurl. */
8289 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008291 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008292 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008293 }
8294 else
8295#endif
8296 if (t_colors > 1)
8297 {
8298 if (aep->ae_u.cterm.fg_color)
8299 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8300 }
8301#ifdef FEAT_TERMGUICOLORS
8302 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8303 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008304 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008305 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 }
8307 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008308#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008309 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008311 if (aep->ae_u.cterm.bg_color)
8312 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8313 }
8314
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008315 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008316 {
8317 if (aep->ae_u.term.start != NULL)
8318 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 }
8320 }
8321 }
8322 }
8323}
8324
8325 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008326screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327{
8328 int do_ME = FALSE; /* output T_ME code */
8329
8330 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008331#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 && termcap_active
8333#endif
8334 )
8335 {
8336#ifdef FEAT_GUI
8337 if (gui.in_use)
8338 {
8339 char buf[20];
8340
8341 /* use internal GUI code */
8342 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8343 OUT_STR(buf);
8344 }
8345 else
8346#endif
8347 {
8348 if (screen_attr > HL_ALL) /* special HL attr. */
8349 {
8350 attrentry_T *aep;
8351
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008352 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {
8354 /*
8355 * Assume that t_me restores the original colors!
8356 */
8357 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008358 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008359#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008360 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8361 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8362 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008363#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008364 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008365#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008366 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8367 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8368 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008369#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008370 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 do_ME = TRUE;
8372 }
8373 else
8374 {
8375 aep = syn_term_attr2entry(screen_attr);
8376 if (aep != NULL && aep->ae_u.term.stop != NULL)
8377 {
8378 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8379 do_ME = TRUE;
8380 else
8381 out_str(aep->ae_u.term.stop);
8382 }
8383 }
8384 if (aep == NULL) /* did ":syntax clear" */
8385 screen_attr = 0;
8386 else
8387 screen_attr = aep->ae_attr;
8388 }
8389
8390 /*
8391 * Often all ending-codes are equal to T_ME. Avoid outputting the
8392 * same sequence several times.
8393 */
8394 if (screen_attr & HL_STANDOUT)
8395 {
8396 if (STRCMP(T_SE, T_ME) == 0)
8397 do_ME = TRUE;
8398 else
8399 out_str(T_SE);
8400 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008401 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008402 {
8403 if (STRCMP(T_UCE, T_ME) == 0)
8404 do_ME = TRUE;
8405 else
8406 out_str(T_UCE);
8407 }
8408 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008409 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {
8411 if (STRCMP(T_UE, T_ME) == 0)
8412 do_ME = TRUE;
8413 else
8414 out_str(T_UE);
8415 }
8416 if (screen_attr & HL_ITALIC)
8417 {
8418 if (STRCMP(T_CZR, T_ME) == 0)
8419 do_ME = TRUE;
8420 else
8421 out_str(T_CZR);
8422 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008423 if (screen_attr & HL_STRIKETHROUGH)
8424 {
8425 if (STRCMP(T_STE, T_ME) == 0)
8426 do_ME = TRUE;
8427 else
8428 out_str(T_STE);
8429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8431 out_str(T_ME);
8432
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008433#ifdef FEAT_TERMGUICOLORS
8434 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008436 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008437 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008438 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008439 term_bg_rgb_color(cterm_normal_bg_gui_color);
8440 }
8441 else
8442#endif
8443 {
8444 if (t_colors > 1)
8445 {
8446 /* set Normal cterm colors */
8447 if (cterm_normal_fg_color != 0)
8448 term_fg_color(cterm_normal_fg_color - 1);
8449 if (cterm_normal_bg_color != 0)
8450 term_bg_color(cterm_normal_bg_color - 1);
8451 if (cterm_normal_fg_bold)
8452 out_str(T_MD);
8453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 }
8455 }
8456 }
8457 screen_attr = 0;
8458}
8459
8460/*
8461 * Reset the colors for a cterm. Used when leaving Vim.
8462 * The machine specific code may override this again.
8463 */
8464 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008465reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008467 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 {
8469 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008470#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008471 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8472 || cterm_normal_bg_gui_color != INVALCOLOR)
8473 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008474#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 {
8478 out_str(T_OP);
8479 screen_attr = -1;
8480 }
8481 if (cterm_normal_fg_bold)
8482 {
8483 out_str(T_ME);
8484 screen_attr = -1;
8485 }
8486 }
8487}
8488
8489/*
8490 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8491 * using the attributes from ScreenAttrs["off"].
8492 */
8493 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008494screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495{
8496 int attr;
8497
8498 /* Check for illegal values, just in case (could happen just after
8499 * resizing). */
8500 if (row >= screen_Rows || col >= screen_Columns)
8501 return;
8502
Bram Moolenaarae654382019-01-17 21:09:05 +01008503#ifdef FEAT_INS_EXPAND
Bram Moolenaar33796b32019-06-08 16:01:13 +02008504 // Skip if under the popup menu.
8505 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
8506 if (pum_under_menu(row, col)
8507# ifdef FEAT_TEXT_PROP
8508 && screen_zindex <= POPUPMENU_ZINDEX
8509# endif
8510 )
Bram Moolenaarae654382019-01-17 21:09:05 +01008511 return;
8512#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008513#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008514 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02008515 return;
8516#endif
8517
Bram Moolenaar494838a2015-02-10 19:20:37 +01008518 /* Outputting a character in the last cell on the screen may scroll the
8519 * screen up. Only do it when the "xn" termcap property is set, otherwise
8520 * mark the character invalid (update it when scrolled up). */
8521 if (*T_XN == NUL
8522 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523#ifdef FEAT_RIGHTLEFT
8524 /* account for first command-line character in rightleft mode */
8525 && !cmdmsg_rl
8526#endif
8527 )
8528 {
8529 ScreenAttrs[off] = (sattr_T)-1;
8530 return;
8531 }
8532
8533 /*
8534 * Stop highlighting first, so it's easier to move the cursor.
8535 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 if (screen_char_attr != 0)
8537 attr = screen_char_attr;
8538 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 attr = ScreenAttrs[off];
8540 if (screen_attr != attr)
8541 screen_stop_highlight();
8542
8543 windgoto(row, col);
8544
8545 if (screen_attr != attr)
8546 screen_start_highlight(attr);
8547
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 if (enc_utf8 && ScreenLinesUC[off] != 0)
8549 {
8550 char_u buf[MB_MAXBYTES + 1];
8551
Bram Moolenaarcb070082016-04-02 22:14:51 +02008552 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008553 {
8554 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008555#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008556 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008557#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008558 )
8559 {
8560 /* Clear the two screen cells. If the character is actually
8561 * single width it won't change the second cell. */
8562 out_str((char_u *)" ");
8563 term_windgoto(row, col);
8564 }
8565 /* not sure where the cursor is after drawing the ambiguous width
8566 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008567 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008568 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008569 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008571
8572 /* Convert the UTF-8 character to bytes and write it. */
8573 buf[utfc_char2bytes(off, buf)] = NUL;
8574 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 }
8576 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 /* double-byte character in single-width cell */
8581 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8582 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 }
8584
8585 screen_cur_col++;
8586}
8587
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588/*
8589 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8590 * on the screen at position 'row' and 'col'.
8591 * The attributes of the first byte is used for all. This is required to
8592 * output the two bytes of a double-byte character with nothing in between.
8593 */
8594 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008595screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
8597 /* Check for illegal values (could be wrong when screen was resized). */
8598 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8599 return;
8600
8601 /* Outputting the last character on the screen may scrollup the screen.
8602 * Don't to it! Mark the character invalid (update it when scrolled up) */
8603 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8604 {
8605 ScreenAttrs[off] = (sattr_T)-1;
8606 return;
8607 }
8608
8609 /* Output the first byte normally (positions the cursor), then write the
8610 * second byte directly. */
8611 screen_char(off, row, col);
8612 out_char(ScreenLines[off + 1]);
8613 ++screen_cur_col;
8614}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616/*
8617 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8618 * This uses the contents of ScreenLines[] and doesn't change it.
8619 */
8620 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008621screen_draw_rectangle(
8622 int row,
8623 int col,
8624 int height,
8625 int width,
8626 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627{
8628 int r, c;
8629 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008630 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008632 /* Can't use ScreenLines unless initialized */
8633 if (ScreenLines == NULL)
8634 return;
8635
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 if (invert)
8637 screen_char_attr = HL_INVERSE;
8638 for (r = row; r < row + height; ++r)
8639 {
8640 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008641 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 for (c = col; c < col + width; ++c)
8643 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008644 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 {
8646 screen_char_2(off + c, r, c);
8647 ++c;
8648 }
8649 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 {
8651 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008652 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 }
8655 }
8656 }
8657 screen_char_attr = 0;
8658}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660/*
8661 * Redraw the characters for a vertically split window.
8662 */
8663 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008664redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665{
8666 int col;
8667 int width;
8668
8669# ifdef FEAT_CLIPBOARD
8670 clip_may_clear_selection(row, end - 1);
8671# endif
8672
8673 if (wp == NULL)
8674 {
8675 col = 0;
8676 width = Columns;
8677 }
8678 else
8679 {
8680 col = wp->w_wincol;
8681 width = wp->w_width;
8682 }
8683 screen_draw_rectangle(row, col, end - row, width, FALSE);
8684}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008686 static void
8687space_to_screenline(int off, int attr)
8688{
8689 ScreenLines[off] = ' ';
8690 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008691 if (enc_utf8)
8692 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008693}
8694
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695/*
8696 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8697 * with character 'c1' in first column followed by 'c2' in the other columns.
8698 * Use attributes 'attr'.
8699 */
8700 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008701screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008702 int start_row,
8703 int end_row,
8704 int start_col,
8705 int end_col,
8706 int c1,
8707 int c2,
8708 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008710 int row;
8711 int col;
8712 int off;
8713 int end_off;
8714 int did_delete;
8715 int c;
8716 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008718 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719#endif
8720
8721 if (end_row > screen_Rows) /* safety check */
8722 end_row = screen_Rows;
8723 if (end_col > screen_Columns) /* safety check */
8724 end_col = screen_Columns;
8725 if (ScreenLines == NULL
8726 || start_row >= end_row
8727 || start_col >= end_col) /* nothing to do */
8728 return;
8729
8730 /* it's a "normal" terminal when not in a GUI or cterm */
8731 norm_term = (
8732#ifdef FEAT_GUI
8733 !gui.in_use &&
8734#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008735 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 for (row = start_row; row < end_row; ++row)
8737 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008738 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008739#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008740 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008741#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008742 )
8743 {
8744 /* When drawing over the right halve of a double-wide char clear
8745 * out the left halve. When drawing over the left halve of a
8746 * double wide-char clear out the right halve. Only needed in a
8747 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008748 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008749 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008750 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008751 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 /*
8754 * Try to use delete-line termcap code, when no attributes or in a
8755 * "normal" terminal, where a bold/italic space is just a
8756 * space.
8757 */
8758 did_delete = FALSE;
8759 if (c2 == ' '
8760 && end_col == Columns
8761 && can_clear(T_CE)
8762 && (attr == 0
8763 || (norm_term
8764 && attr <= HL_ALL
8765 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8766 {
8767 /*
8768 * check if we really need to clear something
8769 */
8770 col = start_col;
8771 if (c1 != ' ') /* don't clear first char */
8772 ++col;
8773
8774 off = LineOffset[row] + col;
8775 end_off = LineOffset[row] + end_col;
8776
8777 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 if (enc_utf8)
8779 while (off < end_off && ScreenLines[off] == ' '
8780 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8781 ++off;
8782 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 while (off < end_off && ScreenLines[off] == ' '
8784 && ScreenAttrs[off] == 0)
8785 ++off;
8786 if (off < end_off) /* something to be cleared */
8787 {
8788 col = off - LineOffset[row];
8789 screen_stop_highlight();
8790 term_windgoto(row, col);/* clear rest of this screen line */
8791 out_str(T_CE);
8792 screen_start(); /* don't know where cursor is now */
8793 col = end_col - col;
8794 while (col--) /* clear chars in ScreenLines */
8795 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008796 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 ++off;
8798 }
8799 }
8800 did_delete = TRUE; /* the chars are cleared now */
8801 }
8802
8803 off = LineOffset[row] + start_col;
8804 c = c1;
8805 for (col = start_col; col < end_col; ++col)
8806 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02008807 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008808 || (enc_utf8 && (int)ScreenLinesUC[off]
8809 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 || ScreenAttrs[off] != attr
8811#if defined(FEAT_GUI) || defined(UNIX)
8812 || force_next
8813#endif
8814 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02008815#ifdef FEAT_TEXT_PROP
8816 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008817 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008818#endif
8819 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 {
8821#if defined(FEAT_GUI) || defined(UNIX)
8822 /* The bold trick may make a single row of pixels appear in
8823 * the next character. When a bold character is removed, the
8824 * next character should be redrawn too. This happens for our
8825 * own GUI and for some xterms. */
8826 if (
8827# ifdef FEAT_GUI
8828 gui.in_use
8829# endif
8830# if defined(FEAT_GUI) && defined(UNIX)
8831 ||
8832# endif
8833# ifdef UNIX
8834 term_is_xterm
8835# endif
8836 )
8837 {
8838 if (ScreenLines[off] != ' '
8839 && (ScreenAttrs[off] > HL_ALL
8840 || ScreenAttrs[off] & HL_BOLD))
8841 force_next = TRUE;
8842 else
8843 force_next = FALSE;
8844 }
8845#endif
8846 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 if (enc_utf8)
8848 {
8849 if (c >= 0x80)
8850 {
8851 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008852 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 }
8854 else
8855 ScreenLinesUC[off] = 0;
8856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 ScreenAttrs[off] = attr;
8858 if (!did_delete || c != ' ')
8859 screen_char(off, row, col);
8860 }
8861 ++off;
8862 if (col == start_col)
8863 {
8864 if (did_delete)
8865 break;
8866 c = c2;
8867 }
8868 }
8869 if (end_col == Columns)
8870 LineWraps[row] = FALSE;
8871 if (row == Rows - 1) /* overwritten the command line */
8872 {
8873 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008874 if (start_col == 0 && end_col == Columns
8875 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008877 if (start_col == 0)
8878 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 }
8880 }
8881}
8882
8883/*
8884 * Check if there should be a delay. Used before clearing or redrawing the
8885 * screen or the command line.
8886 */
8887 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008888check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889{
8890 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8891 && !did_wait_return
8892 && emsg_silent == 0)
8893 {
8894 out_flush();
8895 ui_delay(1000L, TRUE);
8896 emsg_on_display = FALSE;
8897 if (check_msg_scroll)
8898 msg_scroll = FALSE;
8899 }
8900}
8901
8902/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008903 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8904 */
8905 static void
8906clear_TabPageIdxs(void)
8907{
8908 int scol;
8909
8910 for (scol = 0; scol < Columns; ++scol)
8911 TabPageIdxs[scol] = 0;
8912}
8913
8914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008916 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 * Returns TRUE if there is a valid screen to write to.
8918 * Returns FALSE when starting up and screen not initialized yet.
8919 */
8920 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008921screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008923 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 return (ScreenLines != NULL);
8925}
8926
8927/*
8928 * Resize the shell to Rows and Columns.
8929 * Allocate ScreenLines[] and associated items.
8930 *
8931 * There may be some time between setting Rows and Columns and (re)allocating
8932 * ScreenLines[]. This happens when starting up and when (manually) changing
8933 * the shell size. Always use screen_Rows and screen_Columns to access items
8934 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8935 * final size of the shell is needed.
8936 */
8937 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008938screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939{
8940 int new_row, old_row;
8941#ifdef FEAT_GUI
8942 int old_Rows;
8943#endif
8944 win_T *wp;
8945 int outofmem = FALSE;
8946 int len;
8947 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008949 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008951 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 sattr_T *new_ScreenAttrs;
8953 unsigned *new_LineOffset;
8954 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008955 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008956#ifdef FEAT_TEXT_PROP
8957 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008958 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008959 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008960#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00008961 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008963 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008964 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008966retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 /*
8968 * Allocation of the screen buffers is done only when the size changes and
8969 * when Rows and Columns have been set and we have started doing full
8970 * screen stuff.
8971 */
8972 if ((ScreenLines != NULL
8973 && Rows == screen_Rows
8974 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 && enc_utf8 == (ScreenLinesUC != NULL)
8976 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008977 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 || Rows == 0
8979 || Columns == 0
8980 || (!full_screen && ScreenLines == NULL))
8981 return;
8982
8983 /*
8984 * It's possible that we produce an out-of-memory message below, which
8985 * will cause this function to be called again. To break the loop, just
8986 * return here.
8987 */
8988 if (entered)
8989 return;
8990 entered = TRUE;
8991
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008992 /*
8993 * Note that the window sizes are updated before reallocating the arrays,
8994 * thus we must not redraw here!
8995 */
8996 ++RedrawingDisabled;
8997
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 win_new_shellsize(); /* fit the windows in the new sized shell */
8999
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 comp_col(); /* recompute columns for shown command and ruler */
9001
9002 /*
9003 * We're changing the size of the screen.
9004 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9005 * - Move lines from the old arrays into the new arrays, clear extra
9006 * lines (unless the screen is going to be cleared).
9007 * - Free the old arrays.
9008 *
9009 * If anything fails, make ScreenLines NULL, so we don't do anything!
9010 * Continuing with the old ScreenLines may result in a crash, because the
9011 * size is wrong.
9012 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009013 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009015 if (aucmd_win != NULL)
9016 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009017#ifdef FEAT_TEXT_PROP
9018 // global popup windows
9019 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9020 win_free_lsize(wp);
9021 // tab-local popup windows
9022 FOR_ALL_TABPAGES(tp)
9023 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9024 win_free_lsize(wp);
9025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009027 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01009028 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 if (enc_utf8)
9030 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009031 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009032 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009033 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
9034 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 }
9036 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009037 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
9038 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
9039 new_LineOffset = LALLOC_MULT(unsigned, Rows);
9040 new_LineWraps = LALLOC_MULT(char_u, Rows);
9041 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009042#ifdef FEAT_TEXT_PROP
9043 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009044 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02009045 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009048 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 {
9050 if (win_alloc_lines(wp) == FAIL)
9051 {
9052 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009053 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 }
9055 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009056 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9057 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009058 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009059#ifdef FEAT_TEXT_PROP
9060 // global popup windows
9061 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9062 if (win_alloc_lines(wp) == FAIL)
9063 {
9064 outofmem = TRUE;
9065 goto give_up;
9066 }
9067 // tab-local popup windows
9068 FOR_ALL_TABPAGES(tp)
9069 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9070 if (win_alloc_lines(wp) == FAIL)
9071 {
9072 outofmem = TRUE;
9073 goto give_up;
9074 }
9075#endif
9076
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009077give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009079 for (i = 0; i < p_mco; ++i)
9080 if (new_ScreenLinesC[i] == NULL)
9081 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009083 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 || new_ScreenAttrs == NULL
9086 || new_LineOffset == NULL
9087 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009088 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02009089#ifdef FEAT_TEXT_PROP
9090 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009091 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02009092 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02009093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 || outofmem)
9095 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009096 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009097 {
9098 /* guess the size */
9099 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9100
9101 /* Remember we did this to avoid getting outofmem messages over
9102 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009103 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009104 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009105 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009106 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009107 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009108 VIM_CLEAR(new_ScreenLinesC[i]);
9109 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009110 VIM_CLEAR(new_ScreenAttrs);
9111 VIM_CLEAR(new_LineOffset);
9112 VIM_CLEAR(new_LineWraps);
9113 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009114#ifdef FEAT_TEXT_PROP
9115 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009116 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02009117 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 }
9120 else
9121 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009122 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009123
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 for (new_row = 0; new_row < Rows; ++new_row)
9125 {
9126 new_LineOffset[new_row] = new_row * Columns;
9127 new_LineWraps[new_row] = FALSE;
9128
9129 /*
9130 * If the screen is not going to be cleared, copy as much as
9131 * possible from the old screen to the new one and clear the rest
9132 * (used when resizing the window at the "--more--" prompt or when
9133 * executing an external command, for the GUI).
9134 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009135 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 {
9137 (void)vim_memset(new_ScreenLines + new_row * Columns,
9138 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 if (enc_utf8)
9140 {
9141 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9142 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009143 for (i = 0; i < p_mco; ++i)
9144 (void)vim_memset(new_ScreenLinesC[i]
9145 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 0, (size_t)Columns * sizeof(u8char_T));
9147 }
9148 if (enc_dbcs == DBCS_JPNU)
9149 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9150 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9152 0, (size_t)Columns * sizeof(sattr_T));
9153 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009154 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 {
9156 if (screen_Columns < Columns)
9157 len = screen_Columns;
9158 else
9159 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009160 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009161 * may be invalid now. Also when p_mco changes. */
9162 if (!(enc_utf8 && ScreenLinesUC == NULL)
9163 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009164 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9165 ScreenLines + LineOffset[old_row],
9166 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009167 if (enc_utf8 && ScreenLinesUC != NULL
9168 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 {
9170 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9171 ScreenLinesUC + LineOffset[old_row],
9172 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009173 for (i = 0; i < p_mco; ++i)
9174 mch_memmove(new_ScreenLinesC[i]
9175 + new_LineOffset[new_row],
9176 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 (size_t)len * sizeof(u8char_T));
9178 }
9179 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9180 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9181 ScreenLines2 + LineOffset[old_row],
9182 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9184 ScreenAttrs + LineOffset[old_row],
9185 (size_t)len * sizeof(sattr_T));
9186 }
9187 }
9188 }
9189 /* Use the last line of the screen for the current line. */
9190 current_ScreenLine = new_ScreenLines + Rows * Columns;
9191 }
9192
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009193 free_screenlines();
9194
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009197 for (i = 0; i < p_mco; ++i)
9198 ScreenLinesC[i] = new_ScreenLinesC[i];
9199 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 ScreenAttrs = new_ScreenAttrs;
9202 LineOffset = new_LineOffset;
9203 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009204 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02009205#ifdef FEAT_TEXT_PROP
9206 popup_mask = new_popup_mask;
Bram Moolenaar1748c7f2019-06-08 16:55:15 +02009207 vim_memset(popup_mask, 0, Rows * Columns * sizeof(short));
Bram Moolenaarc662ec92019-06-23 00:15:57 +02009208 popup_mask_next = new_popup_mask_next;
9209 popup_transparent = new_popup_transparent;
9210 vim_memset(popup_transparent, 0, Rows * Columns * sizeof(char));
Bram Moolenaar33796b32019-06-08 16:01:13 +02009211 popup_mask_refresh = TRUE;
9212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213
9214 /* It's important that screen_Rows and screen_Columns reflect the actual
9215 * size of ScreenLines[]. Set them before calling anything. */
9216#ifdef FEAT_GUI
9217 old_Rows = screen_Rows;
9218#endif
9219 screen_Rows = Rows;
9220 screen_Columns = Columns;
9221
9222 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009223 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225#ifdef FEAT_GUI
9226 else if (gui.in_use
9227 && !gui.starting
9228 && ScreenLines != NULL
9229 && old_Rows != Rows)
9230 {
9231 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9232 /*
9233 * Adjust the position of the cursor, for when executing an external
9234 * command.
9235 */
9236 if (msg_row >= Rows) /* Rows got smaller */
9237 msg_row = Rows - 1; /* put cursor at last row */
9238 else if (Rows > old_Rows) /* Rows got bigger */
9239 msg_row += Rows - old_Rows; /* put cursor in same place */
9240 if (msg_col >= Columns) /* Columns got smaller */
9241 msg_col = Columns - 1; /* put cursor at last column */
9242 }
9243#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009244 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009247 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009248
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009249 /*
9250 * Do not apply autocommands more than 3 times to avoid an endless loop
9251 * in case applying autocommands always changes Rows or Columns.
9252 */
9253 if (starting == 0 && ++retry_count <= 3)
9254 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009255 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009256 /* In rare cases, autocommands may have altered Rows or Columns,
9257 * jump back to check if we need to allocate the screen again. */
9258 goto retry;
9259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260}
9261
9262 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009263free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009264{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009265 int i;
9266
Bram Moolenaar33796b32019-06-08 16:01:13 +02009267 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009268 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009269 VIM_CLEAR(ScreenLinesC[i]);
9270 VIM_CLEAR(ScreenLines2);
9271 VIM_CLEAR(ScreenLines);
9272 VIM_CLEAR(ScreenAttrs);
9273 VIM_CLEAR(LineOffset);
9274 VIM_CLEAR(LineWraps);
9275 VIM_CLEAR(TabPageIdxs);
9276#ifdef FEAT_TEXT_PROP
9277 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009278 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02009279 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009280#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009281}
9282
9283 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009284screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
9286 check_for_delay(FALSE);
9287 screenalloc(FALSE); /* allocate screen buffers if size changed */
9288 screenclear2(); /* clear the screen */
9289}
9290
9291 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009292screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293{
9294 int i;
9295
9296 if (starting == NO_SCREEN || ScreenLines == NULL
9297#ifdef FEAT_GUI
9298 || (gui.in_use && gui.starting)
9299#endif
9300 )
9301 return;
9302
9303#ifdef FEAT_GUI
9304 if (!gui.in_use)
9305#endif
9306 screen_attr = -1; /* force setting the Normal colors */
9307 screen_stop_highlight(); /* don't want highlighting here */
9308
9309#ifdef FEAT_CLIPBOARD
9310 /* disable selection without redrawing it */
9311 clip_scroll_selection(9999);
9312#endif
9313
9314 /* blank out ScreenLines */
9315 for (i = 0; i < Rows; ++i)
9316 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009317 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 LineWraps[i] = FALSE;
9319 }
9320
9321 if (can_clear(T_CL))
9322 {
9323 out_str(T_CL); /* clear the display */
9324 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009325 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 }
9327 else
9328 {
9329 /* can't clear the screen, mark all chars with invalid attributes */
9330 for (i = 0; i < Rows; ++i)
9331 lineinvalid(LineOffset[i], (int)Columns);
9332 clear_cmdline = TRUE;
9333 }
9334
9335 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9336
9337 win_rest_invalid(firstwin);
9338 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009339 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 if (must_redraw == CLEAR) /* no need to clear again */
9341 must_redraw = NOT_VALID;
9342 compute_cmdrow();
9343 msg_row = cmdline_row; /* put cursor on last line for messages */
9344 msg_col = 0;
9345 screen_start(); /* don't know where cursor is now */
9346 msg_scrolled = 0; /* can't scroll back */
9347 msg_didany = FALSE;
9348 msg_didout = FALSE;
9349}
9350
9351/*
9352 * Clear one line in ScreenLines.
9353 */
9354 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009355lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356{
9357 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 if (enc_utf8)
9359 (void)vim_memset(ScreenLinesUC + off, 0,
9360 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009361 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362}
9363
9364/*
9365 * Mark one line in ScreenLines invalid by setting the attributes to an
9366 * invalid value.
9367 */
9368 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009369lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370{
9371 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9372}
9373
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374/*
9375 * Copy part of a Screenline for vertically split window "wp".
9376 */
9377 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009378linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379{
9380 unsigned off_to = LineOffset[to] + wp->w_wincol;
9381 unsigned off_from = LineOffset[from] + wp->w_wincol;
9382
9383 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9384 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 if (enc_utf8)
9386 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009387 int i;
9388
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9390 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009391 for (i = 0; i < p_mco; ++i)
9392 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9393 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 }
9395 if (enc_dbcs == DBCS_JPNU)
9396 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9397 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9399 wp->w_width * sizeof(sattr_T));
9400}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401
9402/*
9403 * Return TRUE if clearing with term string "p" would work.
9404 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02009405 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 */
9407 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009408can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
9410 return (*p != NUL && (t_colors <= 1
9411#ifdef FEAT_GUI
9412 || gui.in_use
9413#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009414#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009415 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009416 || (!p_tgc && cterm_normal_bg_color == 0)
9417#else
9418 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009419#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009420 || *T_UT != NUL)
9421#ifdef FEAT_TEXT_PROP
9422 && !(p == T_CE && popup_visible)
9423#endif
9424 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425}
9426
9427/*
9428 * Reset cursor position. Use whenever cursor was moved because of outputting
9429 * something directly to the screen (shell commands) or a terminal control
9430 * code.
9431 */
9432 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009433screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434{
9435 screen_cur_row = screen_cur_col = 9999;
9436}
9437
9438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 * Move the cursor to position "row","col" in the screen.
9440 * This tries to find the most efficient way to move, minimizing the number of
9441 * characters sent to the terminal.
9442 */
9443 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009444windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009446 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 int i;
9448 int plan;
9449 int cost;
9450 int wouldbe_col;
9451 int noinvcurs;
9452 char_u *bs;
9453 int goto_cost;
9454 int attr;
9455
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009456#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9458
9459#define PLAN_LE 1
9460#define PLAN_CR 2
9461#define PLAN_NL 3
9462#define PLAN_WRITE 4
9463 /* Can't use ScreenLines unless initialized */
9464 if (ScreenLines == NULL)
9465 return;
9466
9467 if (col != screen_cur_col || row != screen_cur_row)
9468 {
9469 /* Check for valid position. */
9470 if (row < 0) /* window without text lines? */
9471 row = 0;
9472 if (row >= screen_Rows)
9473 row = screen_Rows - 1;
9474 if (col >= screen_Columns)
9475 col = screen_Columns - 1;
9476
9477 /* check if no cursor movement is allowed in highlight mode */
9478 if (screen_attr && *T_MS == NUL)
9479 noinvcurs = HIGHL_COST;
9480 else
9481 noinvcurs = 0;
9482 goto_cost = GOTO_COST + noinvcurs;
9483
9484 /*
9485 * Plan how to do the positioning:
9486 * 1. Use CR to move it to column 0, same row.
9487 * 2. Use T_LE to move it a few columns to the left.
9488 * 3. Use NL to move a few lines down, column 0.
9489 * 4. Move a few columns to the right with T_ND or by writing chars.
9490 *
9491 * Don't do this if the cursor went beyond the last column, the cursor
9492 * position is unknown then (some terminals wrap, some don't )
9493 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009494 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 * characters to move the cursor to the right.
9496 */
9497 if (row >= screen_cur_row && screen_cur_col < Columns)
9498 {
9499 /*
9500 * If the cursor is in the same row, bigger col, we can use CR
9501 * or T_LE.
9502 */
9503 bs = NULL; /* init for GCC */
9504 attr = screen_attr;
9505 if (row == screen_cur_row && col < screen_cur_col)
9506 {
9507 /* "le" is preferred over "bc", because "bc" is obsolete */
9508 if (*T_LE)
9509 bs = T_LE; /* "cursor left" */
9510 else
9511 bs = T_BC; /* "backspace character (old) */
9512 if (*bs)
9513 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9514 else
9515 cost = 999;
9516 if (col + 1 < cost) /* using CR is less characters */
9517 {
9518 plan = PLAN_CR;
9519 wouldbe_col = 0;
9520 cost = 1; /* CR is just one character */
9521 }
9522 else
9523 {
9524 plan = PLAN_LE;
9525 wouldbe_col = col;
9526 }
9527 if (noinvcurs) /* will stop highlighting */
9528 {
9529 cost += noinvcurs;
9530 attr = 0;
9531 }
9532 }
9533
9534 /*
9535 * If the cursor is above where we want to be, we can use CR LF.
9536 */
9537 else if (row > screen_cur_row)
9538 {
9539 plan = PLAN_NL;
9540 wouldbe_col = 0;
9541 cost = (row - screen_cur_row) * 2; /* CR LF */
9542 if (noinvcurs) /* will stop highlighting */
9543 {
9544 cost += noinvcurs;
9545 attr = 0;
9546 }
9547 }
9548
9549 /*
9550 * If the cursor is in the same row, smaller col, just use write.
9551 */
9552 else
9553 {
9554 plan = PLAN_WRITE;
9555 wouldbe_col = screen_cur_col;
9556 cost = 0;
9557 }
9558
9559 /*
9560 * Check if any characters that need to be written have the
9561 * correct attributes. Also avoid UTF-8 characters.
9562 */
9563 i = col - wouldbe_col;
9564 if (i > 0)
9565 cost += i;
9566 if (cost < goto_cost && i > 0)
9567 {
9568 /*
9569 * Check if the attributes are correct without additionally
9570 * stopping highlighting.
9571 */
9572 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9573 while (i && *p++ == attr)
9574 --i;
9575 if (i != 0)
9576 {
9577 /*
9578 * Try if it works when highlighting is stopped here.
9579 */
9580 if (*--p == 0)
9581 {
9582 cost += noinvcurs;
9583 while (i && *p++ == 0)
9584 --i;
9585 }
9586 if (i != 0)
9587 cost = 999; /* different attributes, don't do it */
9588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 if (enc_utf8)
9590 {
9591 /* Don't use an UTF-8 char for positioning, it's slow. */
9592 for (i = wouldbe_col; i < col; ++i)
9593 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9594 {
9595 cost = 999;
9596 break;
9597 }
9598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 }
9600
9601 /*
9602 * We can do it without term_windgoto()!
9603 */
9604 if (cost < goto_cost)
9605 {
9606 if (plan == PLAN_LE)
9607 {
9608 if (noinvcurs)
9609 screen_stop_highlight();
9610 while (screen_cur_col > col)
9611 {
9612 out_str(bs);
9613 --screen_cur_col;
9614 }
9615 }
9616 else if (plan == PLAN_CR)
9617 {
9618 if (noinvcurs)
9619 screen_stop_highlight();
9620 out_char('\r');
9621 screen_cur_col = 0;
9622 }
9623 else if (plan == PLAN_NL)
9624 {
9625 if (noinvcurs)
9626 screen_stop_highlight();
9627 while (screen_cur_row < row)
9628 {
9629 out_char('\n');
9630 ++screen_cur_row;
9631 }
9632 screen_cur_col = 0;
9633 }
9634
9635 i = col - screen_cur_col;
9636 if (i > 0)
9637 {
9638 /*
9639 * Use cursor-right if it's one character only. Avoids
9640 * removing a line of pixels from the last bold char, when
9641 * using the bold trick in the GUI.
9642 */
9643 if (T_ND[0] != NUL && T_ND[1] == NUL)
9644 {
9645 while (i-- > 0)
9646 out_char(*T_ND);
9647 }
9648 else
9649 {
9650 int off;
9651
9652 off = LineOffset[row] + screen_cur_col;
9653 while (i-- > 0)
9654 {
9655 if (ScreenAttrs[off] != screen_attr)
9656 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 if (enc_dbcs == DBCS_JPNU
9660 && ScreenLines[off] == 0x8e)
9661 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 ++off;
9663 }
9664 }
9665 }
9666 }
9667 }
9668 else
9669 cost = 999;
9670
9671 if (cost >= goto_cost)
9672 {
9673 if (noinvcurs)
9674 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009675 if (row == screen_cur_row && (col > screen_cur_col)
9676 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 term_cursor_right(col - screen_cur_col);
9678 else
9679 term_windgoto(row, col);
9680 }
9681 screen_cur_row = row;
9682 screen_cur_col = col;
9683 }
9684}
9685
9686/*
9687 * Set cursor to its position in the current window.
9688 */
9689 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009690setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009692 setcursor_mayforce(FALSE);
9693}
9694
9695/*
9696 * Set cursor to its position in the current window.
9697 * When "force" is TRUE also when not redrawing.
9698 */
9699 void
9700setcursor_mayforce(int force)
9701{
9702 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 {
9704 validate_cursor();
9705 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009706 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009708 /* With 'rightleft' set and the cursor on a double-wide
9709 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009710 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9711 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009712 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009713 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714#endif
9715 curwin->w_wcol));
9716 }
9717}
9718
9719
9720/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009721 * Insert 'line_count' lines at 'row' in window 'wp'.
9722 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9723 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 * scrolling.
9725 * Returns FAIL if the lines are not inserted, OK for success.
9726 */
9727 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009728win_ins_lines(
9729 win_T *wp,
9730 int row,
9731 int line_count,
9732 int invalid,
9733 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734{
9735 int did_delete;
9736 int nextrow;
9737 int lastrow;
9738 int retval;
9739
9740 if (invalid)
9741 wp->w_lines_valid = 0;
9742
9743 if (wp->w_height < 5)
9744 return FAIL;
9745
9746 if (line_count > wp->w_height - row)
9747 line_count = wp->w_height - row;
9748
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009749 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 if (retval != MAYBE)
9751 return retval;
9752
9753 /*
9754 * If there is a next window or a status line, we first try to delete the
9755 * lines at the bottom to avoid messing what is after the window.
9756 * If this fails and there are following windows, don't do anything to avoid
9757 * messing up those windows, better just redraw.
9758 */
9759 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 if (wp->w_next != NULL || wp->w_status_height)
9761 {
9762 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009763 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 did_delete = TRUE;
9765 else if (wp->w_next)
9766 return FAIL;
9767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 /*
9769 * if no lines deleted, blank the lines that will end up below the window
9770 */
9771 if (!did_delete)
9772 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009775 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 lastrow = nextrow + line_count;
9777 if (lastrow > Rows)
9778 lastrow = Rows;
9779 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009780 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 ' ', ' ', 0);
9782 }
9783
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009784 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 == FAIL)
9786 {
9787 /* deletion will have messed up other windows */
9788 if (did_delete)
9789 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 win_rest_invalid(W_NEXT(wp));
9792 }
9793 return FAIL;
9794 }
9795
9796 return OK;
9797}
9798
9799/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009800 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9802 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9803 * scrolling
9804 * Return OK for success, FAIL if the lines are not deleted.
9805 */
9806 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009807win_del_lines(
9808 win_T *wp,
9809 int row,
9810 int line_count,
9811 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009812 int mayclear,
9813 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814{
9815 int retval;
9816
9817 if (invalid)
9818 wp->w_lines_valid = 0;
9819
9820 if (line_count > wp->w_height - row)
9821 line_count = wp->w_height - row;
9822
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009823 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 if (retval != MAYBE)
9825 return retval;
9826
9827 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009828 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 return FAIL;
9830
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 /*
9832 * If there are windows or status lines below, try to put them at the
9833 * correct place. If we can't do that, they have to be redrawn.
9834 */
9835 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9836 {
9837 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009838 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 {
9840 wp->w_redr_status = TRUE;
9841 win_rest_invalid(wp->w_next);
9842 }
9843 }
9844 /*
9845 * If this is the last window and there is no status line, redraw the
9846 * command line later.
9847 */
9848 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 redraw_cmdline = TRUE;
9850 return OK;
9851}
9852
9853/*
9854 * Common code for win_ins_lines() and win_del_lines().
9855 * Returns OK or FAIL when the work has been done.
9856 * Returns MAYBE when not finished yet.
9857 */
9858 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009859win_do_lines(
9860 win_T *wp,
9861 int row,
9862 int line_count,
9863 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009864 int del,
9865 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866{
9867 int retval;
9868
9869 if (!redrawing() || line_count <= 0)
9870 return FAIL;
9871
Bram Moolenaar33796b32019-06-08 16:01:13 +02009872 // When inserting lines would result in loss of command output, just redraw
9873 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009874 if (no_win_do_lines_ins && !del)
9875 return FAIL;
9876
Bram Moolenaar33796b32019-06-08 16:01:13 +02009877 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02009878 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009880 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009881 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 return FAIL;
9883 }
9884
Bram Moolenaar33796b32019-06-08 16:01:13 +02009885#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009886 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02009887 if (popup_visible)
9888 return FAIL;
9889#endif
9890
9891 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 if (row + line_count >= wp->w_height)
9893 {
9894 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009895 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 ' ', ' ', 0);
9897 return OK;
9898 }
9899
9900 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009901 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009903 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009905 if (!no_win_do_lines_ins)
9906 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907
9908 /*
9909 * If the terminal can set a scroll region, use that.
9910 * Always do this in a vertically split window. This will redraw from
9911 * ScreenLines[] when t_CV isn't defined. That's faster than using
9912 * win_line().
9913 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009914 * a character in the lower right corner of the scroll region may cause a
9915 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009917 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 scroll_region_set(wp, row);
9921 if (del)
9922 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009923 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 else
9925 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009926 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 scroll_region_reset();
9929 return retval;
9930 }
9931
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9933 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934
9935 return MAYBE;
9936}
9937
9938/*
9939 * window 'wp' and everything after it is messed up, mark it for redraw
9940 */
9941 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009942win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 {
9946 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 wp->w_redr_status = TRUE;
9948 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 }
9950 redraw_cmdline = TRUE;
9951}
9952
9953/*
9954 * The rest of the routines in this file perform screen manipulations. The
9955 * given operation is performed physically on the screen. The corresponding
9956 * change is also made to the internal screen image. In this way, the editor
9957 * anticipates the effect of editing changes on the appearance of the screen.
9958 * That way, when we call screenupdate a complete redraw isn't usually
9959 * necessary. Another advantage is that we can keep adding code to anticipate
9960 * screen changes, and in the meantime, everything still works.
9961 */
9962
9963/*
9964 * types for inserting or deleting lines
9965 */
9966#define USE_T_CAL 1
9967#define USE_T_CDL 2
9968#define USE_T_AL 3
9969#define USE_T_CE 4
9970#define USE_T_DL 5
9971#define USE_T_SR 6
9972#define USE_NL 7
9973#define USE_T_CD 8
9974#define USE_REDRAW 9
9975
9976/*
9977 * insert lines on the screen and update ScreenLines[]
9978 * 'end' is the line after the scrolled part. Normally it is Rows.
9979 * When scrolling region used 'off' is the offset from the top for the region.
9980 * 'row' and 'end' are relative to the start of the region.
9981 *
9982 * return FAIL for failure, OK for success.
9983 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009984 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009985screen_ins_lines(
9986 int off,
9987 int row,
9988 int line_count,
9989 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009990 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009991 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992{
9993 int i;
9994 int j;
9995 unsigned temp;
9996 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009997 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 int type;
9999 int result_empty;
10000 int can_ce = can_clear(T_CE);
10001
10002 /*
10003 * FAIL if
10004 * - there is no valid screen
10005 * - the screen has to be redrawn completely
10006 * - the line count is less than one
10007 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010008 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +020010009 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 */
Bram Moolenaar33796b32019-06-08 16:01:13 +020010011 if (!screen_valid(TRUE)
10012 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010013#ifdef FEAT_CLIPBOARD
10014 || (clip_star.state != SELECT_CLEARED
10015 && redrawing_for_callback > 0)
10016#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +020010017#ifdef FEAT_TEXT_PROP
10018 || popup_visible
10019#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010020 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 return FAIL;
10022
10023 /*
10024 * There are seven ways to insert lines:
10025 * 0. When in a vertically split window and t_CV isn't set, redraw the
10026 * characters from ScreenLines[].
10027 * 1. Use T_CD (clear to end of display) if it exists and the result of
10028 * the insert is just empty lines
10029 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
10030 * present or line_count > 1. It looks better if we do all the inserts
10031 * at once.
10032 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
10033 * insert is just empty lines and T_CE is not present or line_count >
10034 * 1.
10035 * 4. Use T_AL (insert line) if it exists.
10036 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10037 * just empty lines.
10038 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10039 * just empty lines.
10040 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10041 * the 'da' flag is not set or we have clear line capability.
10042 * 8. redraw the characters from ScreenLines[].
10043 *
10044 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10045 * the scrollbar for the window. It does have insert line, use that if it
10046 * exists.
10047 */
10048 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10050 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010051 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 type = USE_T_CD;
10053 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10054 type = USE_T_CAL;
10055 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10056 type = USE_T_CDL;
10057 else if (*T_AL != NUL)
10058 type = USE_T_AL;
10059 else if (can_ce && result_empty)
10060 type = USE_T_CE;
10061 else if (*T_DL != NUL && result_empty)
10062 type = USE_T_DL;
10063 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10064 type = USE_T_SR;
10065 else
10066 return FAIL;
10067
10068 /*
10069 * For clearing the lines screen_del_lines() is used. This will also take
10070 * care of t_db if necessary.
10071 */
10072 if (type == USE_T_CD || type == USE_T_CDL ||
10073 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010074 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075
10076 /*
10077 * If text is retained below the screen, first clear or delete as many
10078 * lines at the bottom of the window as are about to be inserted so that
10079 * the deleted lines won't later surface during a screen_del_lines.
10080 */
10081 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010082 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083
10084#ifdef FEAT_CLIPBOARD
10085 /* Remove a modeless selection when inserting lines halfway the screen
10086 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010087 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010088 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 else
10090 clip_scroll_selection(-line_count);
10091#endif
10092
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093#ifdef FEAT_GUI
10094 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10095 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010096 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097#endif
10098
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010099 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10100 cursor_col = wp->w_wincol;
10101
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 if (*T_CCS != NUL) /* cursor relative to region */
10103 cursor_row = row;
10104 else
10105 cursor_row = row + off;
10106
10107 /*
10108 * Shift LineOffset[] line_count down to reflect the inserted lines.
10109 * Clear the inserted lines in ScreenLines[].
10110 */
10111 row += off;
10112 end += off;
10113 for (i = 0; i < line_count; ++i)
10114 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 if (wp != NULL && wp->w_width != Columns)
10116 {
10117 /* need to copy part of a line */
10118 j = end - 1 - i;
10119 while ((j -= line_count) >= row)
10120 linecopy(j + line_count, j, wp);
10121 j += line_count;
10122 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010123 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10124 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 else
10126 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10127 LineWraps[j] = FALSE;
10128 }
10129 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 {
10131 j = end - 1 - i;
10132 temp = LineOffset[j];
10133 while ((j -= line_count) >= row)
10134 {
10135 LineOffset[j + line_count] = LineOffset[j];
10136 LineWraps[j + line_count] = LineWraps[j];
10137 }
10138 LineOffset[j + line_count] = temp;
10139 LineWraps[j + line_count] = FALSE;
10140 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010141 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 else
10143 lineinvalid(temp, (int)Columns);
10144 }
10145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146
10147 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010148 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010149 if (clear_attr != 0)
10150 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 /* redraw the characters */
10153 if (type == USE_REDRAW)
10154 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010155 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 {
10157 term_append_lines(line_count);
10158 screen_start(); /* don't know where cursor is now */
10159 }
10160 else
10161 {
10162 for (i = 0; i < line_count; i++)
10163 {
10164 if (type == USE_T_AL)
10165 {
10166 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010167 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 out_str(T_AL);
10169 }
10170 else /* type == USE_T_SR */
10171 out_str(T_SR);
10172 screen_start(); /* don't know where cursor is now */
10173 }
10174 }
10175
10176 /*
10177 * With scroll-reverse and 'da' flag set we need to clear the lines that
10178 * have been scrolled down into the region.
10179 */
10180 if (type == USE_T_SR && *T_DA)
10181 {
10182 for (i = 0; i < line_count; ++i)
10183 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010184 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 out_str(T_CE);
10186 screen_start(); /* don't know where cursor is now */
10187 }
10188 }
10189
10190#ifdef FEAT_GUI
10191 gui_can_update_cursor();
10192 if (gui.in_use)
10193 out_flush(); /* always flush after a scroll */
10194#endif
10195 return OK;
10196}
10197
10198/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010199 * Delete lines on the screen and update ScreenLines[].
10200 * "end" is the line after the scrolled part. Normally it is Rows.
10201 * When scrolling region used "off" is the offset from the top for the region.
10202 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 *
10204 * Return OK for success, FAIL if the lines are not deleted.
10205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010207screen_del_lines(
10208 int off,
10209 int row,
10210 int line_count,
10211 int end,
10212 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010213 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010214 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215{
10216 int j;
10217 int i;
10218 unsigned temp;
10219 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010220 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 int cursor_end;
10222 int result_empty; /* result is empty until end of region */
10223 int can_delete; /* deleting line codes can be used */
10224 int type;
10225
10226 /*
10227 * FAIL if
10228 * - there is no valid screen
10229 * - the screen has to be redrawn completely
10230 * - the line count is less than one
10231 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010232 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010234 if (!screen_valid(TRUE) || line_count <= 0
10235 || (!force && line_count > p_ttyscroll)
10236#ifdef FEAT_CLIPBOARD
10237 || (clip_star.state != SELECT_CLEARED
10238 && redrawing_for_callback > 0)
10239#endif
10240 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 return FAIL;
10242
10243 /*
10244 * Check if the rest of the current region will become empty.
10245 */
10246 result_empty = row + line_count >= end;
10247
10248 /*
10249 * We can delete lines only when 'db' flag not set or when 'ce' option
10250 * available.
10251 */
10252 can_delete = (*T_DB == NUL || can_clear(T_CE));
10253
10254 /*
10255 * There are six ways to delete lines:
10256 * 0. When in a vertically split window and t_CV isn't set, redraw the
10257 * characters from ScreenLines[].
10258 * 1. Use T_CD if it exists and the result is empty.
10259 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10260 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10261 * none of the other ways work.
10262 * 4. Use T_CE (erase line) if the result is empty.
10263 * 5. Use T_DL (delete line) if it exists.
10264 * 6. redraw the characters from ScreenLines[].
10265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10267 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010268 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 type = USE_T_CD;
10270#if defined(__BEOS__) && defined(BEOS_DR8)
10271 /*
10272 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10273 * its internal termcap... this works okay for tests which test *T_DB !=
10274 * NUL. It has the disadvantage that the user cannot use any :set t_*
10275 * command to get T_DB (back) to empty_option, only :set term=... will do
10276 * the trick...
10277 * Anyway, this hack will hopefully go away with the next OS release.
10278 * (Olaf Seibert)
10279 */
10280 else if (row == 0 && T_DB == empty_option
10281 && (line_count == 1 || *T_CDL == NUL))
10282#else
10283 else if (row == 0 && (
10284#ifndef AMIGA
10285 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10286 * up, so use delete-line command */
10287 line_count == 1 ||
10288#endif
10289 *T_CDL == NUL))
10290#endif
10291 type = USE_NL;
10292 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10293 type = USE_T_CDL;
10294 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010295 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 type = USE_T_CE;
10297 else if (*T_DL != NUL && can_delete)
10298 type = USE_T_DL;
10299 else if (*T_CDL != NUL && can_delete)
10300 type = USE_T_CDL;
10301 else
10302 return FAIL;
10303
10304#ifdef FEAT_CLIPBOARD
10305 /* Remove a modeless selection when deleting lines halfway the screen or
10306 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010307 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010308 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 else
10310 clip_scroll_selection(line_count);
10311#endif
10312
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313#ifdef FEAT_GUI
10314 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10315 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010316 gui_dont_update_cursor(gui.cursor_row >= row + off
10317 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318#endif
10319
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010320 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10321 cursor_col = wp->w_wincol;
10322
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 if (*T_CCS != NUL) /* cursor relative to region */
10324 {
10325 cursor_row = row;
10326 cursor_end = end;
10327 }
10328 else
10329 {
10330 cursor_row = row + off;
10331 cursor_end = end + off;
10332 }
10333
10334 /*
10335 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10336 * Clear the inserted lines in ScreenLines[].
10337 */
10338 row += off;
10339 end += off;
10340 for (i = 0; i < line_count; ++i)
10341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 if (wp != NULL && wp->w_width != Columns)
10343 {
10344 /* need to copy part of a line */
10345 j = row + i;
10346 while ((j += line_count) <= end - 1)
10347 linecopy(j - line_count, j, wp);
10348 j -= line_count;
10349 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010350 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10351 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 else
10353 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10354 LineWraps[j] = FALSE;
10355 }
10356 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 {
10358 /* whole width, moving the line pointers is faster */
10359 j = row + i;
10360 temp = LineOffset[j];
10361 while ((j += line_count) <= end - 1)
10362 {
10363 LineOffset[j - line_count] = LineOffset[j];
10364 LineWraps[j - line_count] = LineWraps[j];
10365 }
10366 LineOffset[j - line_count] = temp;
10367 LineWraps[j - line_count] = FALSE;
10368 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010369 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 else
10371 lineinvalid(temp, (int)Columns);
10372 }
10373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010375 if (screen_attr != clear_attr)
10376 screen_stop_highlight();
10377 if (clear_attr != 0)
10378 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 /* redraw the characters */
10381 if (type == USE_REDRAW)
10382 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010383 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010385 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 out_str(T_CD);
10387 screen_start(); /* don't know where cursor is now */
10388 }
10389 else if (type == USE_T_CDL)
10390 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010391 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 term_delete_lines(line_count);
10393 screen_start(); /* don't know where cursor is now */
10394 }
10395 /*
10396 * Deleting lines at top of the screen or scroll region: Just scroll
10397 * the whole screen (scroll region) up by outputting newlines on the
10398 * last line.
10399 */
10400 else if (type == USE_NL)
10401 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010402 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 for (i = line_count; --i >= 0; )
10404 out_char('\n'); /* cursor will remain on same line */
10405 }
10406 else
10407 {
10408 for (i = line_count; --i >= 0; )
10409 {
10410 if (type == USE_T_DL)
10411 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010412 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 out_str(T_DL); /* delete a line */
10414 }
10415 else /* type == USE_T_CE */
10416 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010417 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 out_str(T_CE); /* erase a line */
10419 }
10420 screen_start(); /* don't know where cursor is now */
10421 }
10422 }
10423
10424 /*
10425 * If the 'db' flag is set, we need to clear the lines that have been
10426 * scrolled up at the bottom of the region.
10427 */
10428 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10429 {
10430 for (i = line_count; i > 0; --i)
10431 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010432 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 out_str(T_CE); /* erase a line */
10434 screen_start(); /* don't know where cursor is now */
10435 }
10436 }
10437
10438#ifdef FEAT_GUI
10439 gui_can_update_cursor();
10440 if (gui.in_use)
10441 out_flush(); /* always flush after a scroll */
10442#endif
10443
10444 return OK;
10445}
10446
10447/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010448 * Return TRUE when postponing displaying the mode message: when not redrawing
10449 * or inside a mapping.
10450 */
10451 int
10452skip_showmode()
10453{
10454 // Call char_avail() only when we are going to show something, because it
10455 // takes a bit of time. redrawing() may also call char_avail_avail().
10456 if (global_busy
10457 || msg_silent != 0
10458 || !redrawing()
10459 || (char_avail() && !KeyTyped))
10460 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010461 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010462 return TRUE;
10463 }
10464 return FALSE;
10465}
10466
10467/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010468 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 *
10470 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10471 * If clear_cmdline is FALSE there may be a message there that needs to be
10472 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010473 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 * Return the length of the message (0 if no message).
10475 */
10476 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010477showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478{
10479 int need_clear;
10480 int length = 0;
10481 int do_mode;
10482 int attr;
10483 int nwr_save;
10484#ifdef FEAT_INS_EXPAND
10485 int sub_attr;
10486#endif
10487
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010488 do_mode = ((p_smd && msg_silent == 0)
10489 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010490 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010491 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010492 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010494 if (skip_showmode())
10495 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496
10497 nwr_save = need_wait_return;
10498
10499 /* wait a bit before overwriting an important message */
10500 check_for_delay(FALSE);
10501
10502 /* if the cmdline is more than one line high, erase top lines */
10503 need_clear = clear_cmdline;
10504 if (clear_cmdline && cmdline_row < Rows - 1)
10505 msg_clr_cmdline(); /* will reset clear_cmdline */
10506
10507 /* Position on the last line in the window, column 0 */
10508 msg_pos_mode();
10509 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010510 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 if (do_mode)
10512 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010513 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010515 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010516# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010517 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010518# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010519 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010520# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010521 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010522# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010523 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010525 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526# endif
10527#endif
10528#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10529 if (gui.in_use)
10530 {
10531 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010532 {
10533 /* HANGUL */
10534 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010535 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010536 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010537 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 }
10540#endif
10541#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010542 /* CTRL-X in Insert mode */
10543 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 {
10545 /* These messages can get long, avoid a wrap in a narrow
10546 * window. Prefer showing edit_submode_extra. */
10547 length = (Rows - msg_row) * Columns - 3;
10548 if (edit_submode_extra != NULL)
10549 length -= vim_strsize(edit_submode_extra);
10550 if (length > 0)
10551 {
10552 if (edit_submode_pre != NULL)
10553 length -= vim_strsize(edit_submode_pre);
10554 if (length - vim_strsize(edit_submode) > 0)
10555 {
10556 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010557 msg_puts_attr((char *)edit_submode_pre, attr);
10558 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 }
10560 if (edit_submode_extra != NULL)
10561 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010562 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010564 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 else
10566 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010567 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 }
10569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 }
10571 else
10572#endif
10573 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010575 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010576 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010577 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 else if (State & INSERT)
10579 {
10580#ifdef FEAT_RIGHTLEFT
10581 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010582 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010584 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010586 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010587 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010589 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010591 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592#ifdef FEAT_RIGHTLEFT
10593 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010594 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595#endif
10596#ifdef FEAT_KEYMAP
10597 if (State & LANGMAP)
10598 {
10599# ifdef FEAT_ARABIC
10600 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010601 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 else
10603# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010604 if (get_keymap_str(curwin, (char_u *)" (%s)",
10605 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010606 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607 }
10608#endif
10609 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010610 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 if (VIsual_active)
10613 {
10614 char *p;
10615
10616 /* Don't concatenate separate words to avoid translation
10617 * problems. */
10618 switch ((VIsual_select ? 4 : 0)
10619 + (VIsual_mode == Ctrl_V) * 2
10620 + (VIsual_mode == 'V'))
10621 {
10622 case 0: p = N_(" VISUAL"); break;
10623 case 1: p = N_(" VISUAL LINE"); break;
10624 case 2: p = N_(" VISUAL BLOCK"); break;
10625 case 4: p = N_(" SELECT"); break;
10626 case 5: p = N_(" SELECT LINE"); break;
10627 default: p = N_(" SELECT BLOCK"); break;
10628 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010629 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010631 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010633
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 need_clear = TRUE;
10635 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010636 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637#ifdef FEAT_INS_EXPAND
10638 && edit_submode == NULL /* otherwise it gets too long */
10639#endif
10640 )
10641 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010642 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643 need_clear = TRUE;
10644 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010645
10646 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010647 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 msg_clr_eos();
10649 msg_didout = FALSE; /* overwrite this message */
10650 length = msg_col;
10651 msg_col = 0;
10652 need_wait_return = nwr_save; /* never ask for hit-return for this */
10653 }
10654 else if (clear_cmdline && msg_silent == 0)
10655 /* Clear the whole command line. Will reset "clear_cmdline". */
10656 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010657 else if (redraw_mode)
10658 {
10659 msg_pos_mode();
10660 msg_clr_eos();
10661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662
10663#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 /* In Visual mode the size of the selected area must be redrawn. */
10665 if (VIsual_active)
10666 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667
10668 /* If the last window has no status line, the ruler is after the mode
10669 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010670 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010671 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672#endif
10673 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010674 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 clear_cmdline = FALSE;
10676
10677 return length;
10678}
10679
10680/*
10681 * Position for a mode message.
10682 */
10683 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010684msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685{
10686 msg_col = 0;
10687 msg_row = Rows - 1;
10688}
10689
10690/*
10691 * Delete mode message. Used when ESC is typed which is expected to end
10692 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010693 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 */
10695 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010696unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697{
10698 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010699 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 */
10701 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10702 redraw_cmdline = TRUE; /* delete mode later */
10703 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010704 clearmode();
10705}
10706
10707/*
10708 * Clear the mode message.
10709 */
10710 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010711clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010712{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010713 int save_msg_row = msg_row;
10714 int save_msg_col = msg_col;
10715
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010716 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010717 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010718 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010719 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010720
10721 msg_col = save_msg_col;
10722 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723}
10724
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010725 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010726recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010727{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010728 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010729 if (!shortmess(SHM_RECORDING))
10730 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010731 char s[4];
10732
10733 sprintf(s, " @%c", reg_recording);
10734 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010735 }
10736}
10737
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010738/*
10739 * Draw the tab pages line at the top of the Vim window.
10740 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010741 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010742draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010743{
10744 int tabcount = 0;
10745 tabpage_T *tp;
10746 int tabwidth;
10747 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010748 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010749 int attr;
10750 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010751 win_T *cwp;
10752 int wincount;
10753 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010754 int c;
10755 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010756 int attr_sel = HL_ATTR(HLF_TPS);
10757 int attr_nosel = HL_ATTR(HLF_TP);
10758 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010759 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010760 int room;
10761 int use_sep_chars = (t_colors < 8
10762#ifdef FEAT_GUI
10763 && !gui.in_use
10764#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010765#ifdef FEAT_TERMGUICOLORS
10766 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010767#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010768 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010769
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010770 if (ScreenLines == NULL)
10771 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010772 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010773
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010774#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010775 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010776 if (gui_use_tabline())
10777 {
10778 gui_update_tabline();
10779 return;
10780 }
10781#endif
10782
10783 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010784 return;
10785
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010786#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010787 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010788
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010789 /* Use the 'tabline' option if it's set. */
10790 if (*p_tal != NUL)
10791 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010792 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010793
10794 /* Check for an error. If there is one we would loop in redrawing the
10795 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010796 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010797 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010798 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010799 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010800 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010801 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010802 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010803 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010804#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010805 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010806 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010807 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010808
Bram Moolenaar238a5642006-02-21 22:12:05 +000010809 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10810 if (tabwidth < 6)
10811 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010812
Bram Moolenaar238a5642006-02-21 22:12:05 +000010813 attr = attr_nosel;
10814 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010815 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10816 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010817 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010818 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010819
Bram Moolenaar238a5642006-02-21 22:12:05 +000010820 if (tp->tp_topframe == topframe)
10821 attr = attr_sel;
10822 if (use_sep_chars && col > 0)
10823 screen_putchar('|', 0, col++, attr);
10824
10825 if (tp->tp_topframe != topframe)
10826 attr = attr_nosel;
10827
10828 screen_putchar(' ', 0, col++, attr);
10829
10830 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010831 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010832 cwp = curwin;
10833 wp = firstwin;
10834 }
10835 else
10836 {
10837 cwp = tp->tp_curwin;
10838 wp = tp->tp_firstwin;
10839 }
10840
10841 modified = FALSE;
10842 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10843 if (bufIsChanged(wp->w_buffer))
10844 modified = TRUE;
10845 if (modified || wincount > 1)
10846 {
10847 if (wincount > 1)
10848 {
10849 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010850 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010851 if (col + len >= Columns - 3)
10852 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010853 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010854#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010855 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010856#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010857 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010858#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010859 );
10860 col += len;
10861 }
10862 if (modified)
10863 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10864 screen_putchar(' ', 0, col++, attr);
10865 }
10866
10867 room = scol - col + tabwidth - 1;
10868 if (room > 0)
10869 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010870 /* Get buffer name in NameBuff[] */
10871 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010872 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010873 len = vim_strsize(NameBuff);
10874 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010875 if (has_mbyte)
10876 while (len > room)
10877 {
10878 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010879 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010880 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010881 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010882 {
10883 p += len - room;
10884 len = room;
10885 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010886 if (len > Columns - col - 1)
10887 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010888
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010889 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010890 col += len;
10891 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010892 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010893
10894 /* Store the tab page number in TabPageIdxs[], so that
10895 * jump_to_mouse() knows where each one is. */
10896 ++tabcount;
10897 while (scol < col)
10898 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010899 }
10900
Bram Moolenaar238a5642006-02-21 22:12:05 +000010901 if (use_sep_chars)
10902 c = '_';
10903 else
10904 c = ' ';
10905 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010906
10907 /* Put an "X" for closing the current tab if there are several. */
10908 if (first_tabpage->tp_next != NULL)
10909 {
10910 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10911 TabPageIdxs[Columns - 1] = -999;
10912 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010913 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010914
10915 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10916 * set. */
10917 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010918}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010919
10920/*
10921 * Get buffer name for "buf" into NameBuff[].
10922 * Takes care of special buffer names and translates special characters.
10923 */
10924 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010925get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010926{
10927 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010928 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010929 else
10930 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10931 trans_characters(NameBuff, MAXPATHL);
10932}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010933
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934/*
10935 * Get the character to use in a status line. Get its attributes in "*attr".
10936 */
10937 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010938fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939{
10940 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010941
10942#ifdef FEAT_TERMINAL
10943 if (bt_terminal(wp->w_buffer))
10944 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010945 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010946 {
10947 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010948 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010949 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010950 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010951 {
10952 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010953 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010954 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010955 }
10956 else
10957#endif
10958 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010960 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 fill = fill_stl;
10962 }
10963 else
10964 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010965 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 fill = fill_stlnc;
10967 }
10968 /* Use fill when there is highlighting, and highlighting of current
10969 * window differs, or the fillchars differ, or this is not the
10970 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010971 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010972 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 || (fill_stl != fill_stlnc)))
10974 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010975 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 return '^';
10977 return '=';
10978}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980/*
10981 * Get the character to use in a separator between vertically split windows.
10982 * Get its attributes in "*attr".
10983 */
10984 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010985fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010987 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 if (*attr == 0 && fill_vert == ' ')
10989 return '|';
10990 else
10991 return fill_vert;
10992}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993
10994/*
10995 * Return TRUE if redrawing should currently be done.
10996 */
10997 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010998redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010011000#ifdef FEAT_EVAL
11001 if (disable_redraw_for_testing)
11002 return 0;
11003 else
11004#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020011005 return ((!RedrawingDisabled
11006#ifdef FEAT_EVAL
11007 || ignore_redraw_flag_for_testing
11008#endif
11009 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010}
11011
11012/*
11013 * Return TRUE if printing messages should currently be done.
11014 */
11015 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011016messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017{
11018 return (!(p_lz && char_avail() && !KeyTyped));
11019}
11020
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011021#ifdef FEAT_MENU
11022/*
11023 * Draw the window toolbar.
11024 */
11025 static void
11026redraw_win_toolbar(win_T *wp)
11027{
11028 vimmenu_T *menu;
11029 int item_idx = 0;
11030 int item_count = 0;
11031 int col = 0;
11032 int next_col;
11033 int off = (int)(current_ScreenLine - ScreenLines);
11034 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
11035 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
11036
11037 vim_free(wp->w_winbar_items);
11038 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
11039 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011040 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011041
11042 /* TODO: use fewer spaces if there is not enough room */
11043 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020011044 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011045 {
11046 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011047 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011048 break;
11049 if (col > 1)
11050 {
11051 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011052 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011053 break;
11054 }
11055
11056 wp->w_winbar_items[item_idx].wb_startcol = col;
11057 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011058 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011059 break;
11060
11061 next_col = text_to_screenline(wp, menu->name, col);
11062 while (col < next_col)
11063 {
11064 ScreenAttrs[off + col] = button_attr;
11065 ++col;
11066 }
11067 wp->w_winbar_items[item_idx].wb_endcol = col;
11068 wp->w_winbar_items[item_idx].wb_menu = menu;
11069 ++item_idx;
11070
Bram Moolenaar02631462017-09-22 15:20:32 +020011071 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011072 break;
11073 space_to_screenline(off + col, button_attr);
11074 ++col;
11075 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011076 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011077 {
11078 space_to_screenline(off + col, fill_attr);
11079 ++col;
11080 }
11081 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11082
Bram Moolenaar02631462017-09-22 15:20:32 +020011083 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020011084 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011085}
11086#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011087
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088/*
11089 * Show current status info in ruler and various other places
11090 * If always is FALSE, only show ruler if position has changed.
11091 */
11092 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011093showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094{
11095 if (!always && !redrawing())
11096 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011097#ifdef FEAT_INS_EXPAND
11098 if (pum_visible())
11099 {
11100 /* Don't redraw right now, do it later. */
11101 curwin->w_redr_status = TRUE;
11102 return;
11103 }
11104#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011105#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011106 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000011107 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 else
11109#endif
11110#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011111 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112#endif
11113
11114#ifdef FEAT_TITLE
11115 if (need_maketitle
11116# ifdef FEAT_STL_OPT
11117 || (p_icon && (stl_syntax & STL_IN_ICON))
11118 || (p_title && (stl_syntax & STL_IN_TITLE))
11119# endif
11120 )
11121 maketitle();
11122#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011123 /* Redraw the tab pages line if needed. */
11124 if (redraw_tabline)
11125 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126}
11127
11128#ifdef FEAT_CMDL_INFO
11129 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011130win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011132#define RULER_BUF_LEN 70
11133 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134 int row;
11135 int fillchar;
11136 int attr;
11137 int empty_line = FALSE;
11138 colnr_T virtcol;
11139 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011140 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 int this_ru_col;
11143 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010011144 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145
11146 /* If 'ruler' off or redrawing disabled, don't do anything */
11147 if (!p_ru)
11148 return;
11149
11150 /*
11151 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11152 * after deleting lines, before cursor.lnum is corrected.
11153 */
11154 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11155 return;
11156
11157#ifdef FEAT_INS_EXPAND
11158 /* Don't draw the ruler while doing insert-completion, it might overwrite
11159 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 if (edit_submode != NULL)
11162 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011163 // Don't draw the ruler when the popup menu is visible, it may overlap.
11164 // Except when the popup menu will be redrawn anyway.
11165 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011166 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167#endif
11168
11169#ifdef FEAT_STL_OPT
11170 if (*p_ruf)
11171 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011172 int save_called_emsg = called_emsg;
11173
11174 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011176 if (called_emsg)
11177 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011178 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011179 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 return;
11181 }
11182#endif
11183
11184 /*
11185 * Check if not in Insert mode and the line is empty (will show "0-1").
11186 */
11187 if (!(State & INSERT)
11188 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11189 empty_line = TRUE;
11190
11191 /*
11192 * Only draw the ruler when something changed.
11193 */
11194 validate_virtcol_win(wp);
11195 if ( redraw_cmdline
11196 || always
11197 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11198 || wp->w_cursor.col != wp->w_ru_cursor.col
11199 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201 || wp->w_topline != wp->w_ru_topline
11202 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11203#ifdef FEAT_DIFF
11204 || wp->w_topfill != wp->w_ru_topfill
11205#endif
11206 || empty_line != wp->w_ru_empty)
11207 {
11208 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209 if (wp->w_status_height)
11210 {
11211 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011212 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011213 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011214 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 }
11216 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 {
11218 row = Rows - 1;
11219 fillchar = ' ';
11220 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 width = Columns;
11222 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223 }
11224
11225 /* In list mode virtcol needs to be recomputed */
11226 virtcol = wp->w_virtcol;
11227 if (wp->w_p_list && lcs_tab1 == NUL)
11228 {
11229 wp->w_p_list = FALSE;
11230 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11231 wp->w_p_list = TRUE;
11232 }
11233
11234 /*
11235 * Some sprintfs return the length, some return a pointer.
11236 * To avoid portability problems we use strlen() here.
11237 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011238 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11240 ? 0L
11241 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011242 len = STRLEN(buffer);
11243 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11245 (int)virtcol + 1);
11246
11247 /*
11248 * Add a "50%" if there is room for it.
11249 * On the last line, don't print in the last column (scrolls the
11250 * screen up on some terminals).
11251 */
11252 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011253 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 this_ru_col = ru_col - (Columns - width);
11258 if (this_ru_col < 0)
11259 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 /* Never use more than half the window/screen width, leave the other
11261 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011262 if (this_ru_col < (width + 1) / 2)
11263 this_ru_col = (width + 1) / 2;
11264 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011266 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011267 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 if (has_mbyte)
11270 i += (*mb_char2bytes)(fillchar, buffer + i);
11271 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 buffer[i++] = fillchar;
11273 ++o;
11274 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011275 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276 }
11277 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278 if (has_mbyte)
11279 {
11280 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011281 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 {
11283 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011284 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 {
11286 buffer[i] = NUL;
11287 break;
11288 }
11289 }
11290 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011291 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011292 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293
Bram Moolenaar4033c552017-09-16 20:54:51 +020011294 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 i = redraw_cmdline;
11296 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011297 this_ru_col + off + (int)STRLEN(buffer),
11298 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299 fillchar, fillchar, attr);
11300 /* don't redraw the cmdline because of showing the ruler */
11301 redraw_cmdline = i;
11302 wp->w_ru_cursor = wp->w_cursor;
11303 wp->w_ru_virtcol = wp->w_virtcol;
11304 wp->w_ru_empty = empty_line;
11305 wp->w_ru_topline = wp->w_topline;
11306 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11307#ifdef FEAT_DIFF
11308 wp->w_ru_topfill = wp->w_topfill;
11309#endif
11310 }
11311}
11312#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011313
11314#if defined(FEAT_LINEBREAK) || defined(PROTO)
11315/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011316 * Return the width of the 'number' and 'relativenumber' column.
11317 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011318 * Otherwise it depends on 'numberwidth' and the line count.
11319 */
11320 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011321number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011322{
11323 int n;
11324 linenr_T lnum;
11325
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011326 if (wp->w_p_rnu && !wp->w_p_nu)
11327 /* cursor line shows "0" */
11328 lnum = wp->w_height;
11329 else
11330 /* cursor line shows absolute line number */
11331 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011332
Bram Moolenaar6b314672015-03-20 15:42:10 +010011333 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011334 return wp->w_nrwidth_width;
11335 wp->w_nrwidth_line_count = lnum;
11336
11337 n = 0;
11338 do
11339 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011340 lnum /= 10;
11341 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011342 } while (lnum > 0);
11343
11344 /* 'numberwidth' gives the minimal width plus one */
11345 if (n < wp->w_p_nuw - 1)
11346 n = wp->w_p_nuw - 1;
11347
Bram Moolenaare4b407f2019-07-04 11:59:28 +020011348# ifdef FEAT_SIGNS
11349 // If 'signcolumn' is set to 'number' and there is a sign to display, then
11350 // the minimal width for the number column is 2.
11351 if (n < 2 && (wp->w_buffer->b_signlist != NULL)
11352 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
11353 n = 2;
11354# endif
11355
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011356 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011357 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011358 return n;
11359}
11360#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011361
Bram Moolenaar113e1072019-01-20 15:30:40 +010011362#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011363/*
11364 * Return the current cursor column. This is the actual position on the
11365 * screen. First column is 0.
11366 */
11367 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011368screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011369{
11370 return screen_cur_col;
11371}
11372
11373/*
11374 * Return the current cursor row. This is the actual position on the screen.
11375 * First row is 0.
11376 */
11377 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011378screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011379{
11380 return screen_cur_row;
11381}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011382#endif