blob: 0a93f86add5d14d29b20a28ecbe881ede562469c [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 Moolenaar4d784b22019-05-25 19:51:39 +0200124#ifdef FEAT_TEXT_PROP
125static void update_popups(void);
126#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200128static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100129static 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 +0000130#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100131static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
132static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
133static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200135static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200141# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static void start_search_hl(void);
143static void end_search_hl(void);
144static void init_search_hl(win_T *wp);
145static void prepare_search_hl(win_T *wp, linenr_T lnum);
146static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
147static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static void screen_char(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200152static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200154static 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 +0100155static void win_rest_invalid(win_T *wp);
156static void msg_pos_mode(void);
157static void recording_mode(int attr);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200158static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100159static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200160#ifdef FEAT_MENU
161static void redraw_win_toolbar(win_T *wp);
162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200167static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170/* Ugly global: overrule attribute used by screen_char() */
171static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200173#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
174/* Can limit syntax highlight time to 'redrawtime'. */
175# define SYN_TIME_LIMIT 1
176#endif
177
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200178#ifdef FEAT_RIGHTLEFT
179# define HAS_RIGHTLEFT(x) x
180#else
181# define HAS_RIGHTLEFT(x) FALSE
182#endif
183
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200184// flags for screen_line()
185#define SLF_RIGHTLEFT 1
186#define SLF_POPUP 2
187
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188/*
189 * Redraw the current window later, with update_screen(type).
190 * Set must_redraw only if not already set to a higher value.
Bram Moolenaarae654382019-01-17 21:09:05 +0100191 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 */
193 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100194redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 redraw_win_later(curwin, type);
197}
198
199 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100200redraw_win_later(
201 win_T *wp,
202 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200204 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 {
206 wp->w_redr_type = type;
207 if (type >= NOT_VALID)
208 wp->w_lines_valid = 0;
209 if (must_redraw < type) /* must_redraw is the maximum of all windows */
210 must_redraw = type;
211 }
212}
213
214/*
215 * Force a complete redraw later. Also resets the highlighting. To be used
216 * after executing a shell command that messes up the screen.
217 */
218 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100219redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
221 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000222#ifdef FEAT_GUI
223 if (gui.in_use)
224 /* Use a code that will reset gui.highlight_mask in
225 * gui_stop_highlight(). */
226 screen_attr = HL_ALL + 1;
227 else
228#endif
229 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200230 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231}
232
233/*
234 * Mark all windows to be redrawn later.
235 */
236 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100237redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 redraw_win_later(wp, type);
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100243 // This may be needed when switching tabs.
244 if (must_redraw < type)
245 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246}
247
248/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000249 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 */
251 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100252redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253{
254 redraw_buf_later(curbuf, type);
255}
256
257 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100258redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259{
260 win_T *wp;
261
262 FOR_ALL_WINDOWS(wp)
263 {
264 if (wp->w_buffer == buf)
265 redraw_win_later(wp, type);
266 }
267}
268
Bram Moolenaar113e1072019-01-20 15:30:40 +0100269#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200270 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100271redraw_buf_line_later(buf_T *buf, linenr_T lnum)
272{
273 win_T *wp;
274
275 FOR_ALL_WINDOWS(wp)
276 if (wp->w_buffer == buf && lnum >= wp->w_topline
277 && lnum < wp->w_botline)
278 redrawWinline(wp, lnum);
279}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100280#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100281
Bram Moolenaar113e1072019-01-20 15:30:40 +0100282#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100283 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200284redraw_buf_and_status_later(buf_T *buf, int type)
285{
286 win_T *wp;
287
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200288#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200289 if (wild_menu_showing != 0)
290 /* Don't redraw while the command line completion is displayed, it
291 * would disappear. */
292 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200293#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200294 FOR_ALL_WINDOWS(wp)
295 {
296 if (wp->w_buffer == buf)
297 {
298 redraw_win_later(wp, type);
299 wp->w_redr_status = TRUE;
300 }
301 }
302}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100303#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200304
Bram Moolenaar113e1072019-01-20 15:30:40 +0100305#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200307 * Redraw as soon as possible. When the command line is not scrolled redraw
308 * right away and restore what was on the command line.
309 * Return a code indicating what happened.
310 */
311 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100312redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313{
314 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200315 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 int r;
317 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200318 schar_T *screenline; /* copy from ScreenLines[] */
319 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200321 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200323 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200324
325 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200326 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200327 return ret;
328
329 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200330 rows = screen_Rows - cmdline_row;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200331 screenline = LALLOC_MULT(schar_T, rows * cols);
332 screenattr = LALLOC_MULT(sattr_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200333 if (screenline == NULL || screenattr == NULL)
334 ret = 2;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200335 if (enc_utf8)
336 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200337 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200338 if (screenlineUC == NULL)
339 ret = 2;
340 for (i = 0; i < p_mco; ++i)
341 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200342 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200343 if (screenlineC[i] == NULL)
344 ret = 2;
345 }
346 }
347 if (enc_dbcs == DBCS_JPNU)
348 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200349 screenline2 = LALLOC_MULT(schar_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200350 if (screenline2 == NULL)
351 ret = 2;
352 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200353
354 if (ret != 2)
355 {
356 /* Save the text displayed in the command line area. */
357 for (r = 0; r < rows; ++r)
358 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200359 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200360 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 (size_t)cols * sizeof(schar_T));
362 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200364 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 if (enc_utf8)
366 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 mch_memmove(screenlineC[i] + r * cols,
372 ScreenLinesC[i] + LineOffset[cmdline_row + r],
373 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200374 }
375 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200376 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200377 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200379 }
380
381 update_screen(0);
382 ret = 3;
383
384 if (must_redraw == 0)
385 {
386 int off = (int)(current_ScreenLine - ScreenLines);
387
388 /* Restore the text displayed in the command line area. */
389 for (r = 0; r < rows; ++r)
390 {
391 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200392 screenline + r * cols,
393 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200394 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200395 screenattr + r * cols,
396 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200397 if (enc_utf8)
398 {
399 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200400 screenlineUC + r * cols,
401 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402 for (i = 0; i < p_mco; ++i)
403 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200404 screenlineC[i] + r * cols,
405 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200406 }
407 if (enc_dbcs == DBCS_JPNU)
408 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200409 screenline2 + r * cols,
410 (size_t)cols * sizeof(schar_T));
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200411 screen_line(cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200412 }
413 ret = 4;
414 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200415 }
416
417 vim_free(screenline);
418 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200419 if (enc_utf8)
420 {
421 vim_free(screenlineUC);
422 for (i = 0; i < p_mco; ++i)
423 vim_free(screenlineC[i]);
424 }
425 if (enc_dbcs == DBCS_JPNU)
426 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200427
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200428 /* Show the intro message when appropriate. */
429 maybe_intro_message();
430
431 setcursor();
432
Bram Moolenaar2951b772013-07-03 12:45:31 +0200433 return ret;
434}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100435#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200436
437/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100438 * Invoked after an asynchronous callback is called.
439 * If an echo command was used the cursor needs to be put back where
440 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200441 * If "call_update_screen" is FALSE don't call update_screen() when at the
442 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100443 */
444 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200445redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100446{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200447 ++redrawing_for_callback;
448
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100449 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200450 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100451 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200452 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200453 // Don't redraw when in prompt_for_number().
454 if (cmdline_row > 0)
455 {
456 // Redrawing only works when the screen didn't scroll. Don't clear
457 // wildmenu entries.
458 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200459#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200460 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200461#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200462 && call_update_screen)
463 update_screen(0);
464
465 // Redraw in the same position, so that the user can continue
466 // editing the command.
467 redrawcmdline_ex(FALSE);
468 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200469 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200470 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100471 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200472 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200473 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100474 setcursor();
475 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100476 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100477#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100478 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200479 // Don't update the cursor when it is blinking and off to avoid
480 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100481 out_flush_cursor(FALSE, FALSE);
482 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100483#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100484 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200485
486 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100487}
488
489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 * Changed something in the current window, at buffer line "lnum", that
491 * requires that line and possibly other lines to be redrawn.
492 * Used when entering/leaving Insert mode with the cursor on a folded line.
493 * Used to remove the "$" from a change command.
494 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
495 * may become invalid and the whole window will have to be redrawn.
496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100498redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200499 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100500 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200502 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
503 wp->w_redraw_top = lnum;
504 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
505 wp->w_redraw_bot = lnum;
506 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507}
508
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200509/*
510 * To be called when "updating_screen" was set before and now the postponed
511 * side effects may take place.
512 */
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200513 void
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200514after_updating_screen(int may_resize_shell UNUSED)
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200515{
516 updating_screen = FALSE;
517#ifdef FEAT_GUI
518 if (may_resize_shell)
519 gui_may_resize_shell();
520#endif
521#ifdef FEAT_TERMINAL
522 term_check_channel_closed_recently();
523#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200524
525#ifdef HAVE_DROP_FILE
526 // If handle_drop() was called while updating_screen was TRUE need to
527 // handle the drop now.
528 handle_any_postponed_drop();
529#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200530}
531
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200533 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 */
535 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100536update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
538 redraw_curbuf_later(type);
539 update_screen(type);
540}
541
542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 * Based on the current value of curwin->w_topline, transfer a screenfull
544 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200545 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200547 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200548update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200550 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 win_T *wp;
552 static int did_intro = FALSE;
553#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
554 int did_one;
555#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200556#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200557 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200558 int gui_cursor_col = 0;
559 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200560#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200561 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000563 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200565 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200567 if (type == VALID_NO_UPDATE)
568 {
569 no_update = TRUE;
570 type = 0;
571 }
572
Bram Moolenaara3347722019-05-11 21:14:24 +0200573#ifdef FEAT_EVAL
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200574 {
575 buf_T *buf;
576
577 // Before updating the screen, notify any listeners of changed text.
578 FOR_ALL_BUFFERS(buf)
579 invoke_listeners(buf);
580 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200581#endif
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (must_redraw)
584 {
585 if (type < must_redraw) /* use maximal type */
586 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000587
588 /* must_redraw is reset here, so that when we run into some weird
589 * reason to redraw while busy redrawing (e.g., asynchronous
590 * scrolling), or update_topline() in win_update() will cause a
591 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 must_redraw = 0;
593 }
594
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200595 /* May need to update w_lines[]. */
596 if (curwin->w_lines_valid == 0 && type < NOT_VALID
597#ifdef FEAT_TERMINAL
598 && !term_do_update_window(curwin)
599#endif
600 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 type = NOT_VALID;
602
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000603 /* Postpone the redrawing when it's not needed and when being called
604 * recursively. */
605 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 {
607 redraw_later(type); /* remember type for next time */
608 must_redraw = type;
609 if (type > INVERTED_ALL)
610 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200611 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200613
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200614#ifdef FEAT_TEXT_PROP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200615 // Update popup_mask if needed.
616 type = may_update_popup_mask(type);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618
619 updating_screen = TRUE;
620#ifdef FEAT_SYN_HL
621 ++display_tick; /* let syntax code know we're in a next round of
622 * display updating */
623#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200624 if (no_update)
625 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627 /*
628 * if the screen was scrolled up when displaying a message, scroll it down
629 */
630 if (msg_scrolled)
631 {
632 clear_cmdline = TRUE;
633 if (msg_scrolled > Rows - 5) /* clearing is faster */
634 type = CLEAR;
635 else if (type != CLEAR)
636 {
637 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200638 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
639 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 type = CLEAR;
641 FOR_ALL_WINDOWS(wp)
642 {
Bram Moolenaar98fb65c2019-06-02 20:33:32 +0200643 if (wp->w_winrow < msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 {
645 if (W_WINROW(wp) + wp->w_height > msg_scrolled
646 && wp->w_redr_type < REDRAW_TOP
647 && wp->w_lines_valid > 0
648 && wp->w_topline == wp->w_lines[0].wl_lnum)
649 {
650 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
651 wp->w_redr_type = REDRAW_TOP;
652 }
653 else
654 {
655 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200656 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
657 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
660 }
661 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200662 if (!no_update)
663 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000664 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
666 msg_scrolled = 0;
667 need_wait_return = FALSE;
668 }
669
670 /* reset cmdline_row now (may have been changed temporarily) */
671 compute_cmdrow();
672
673 /* Check for changed highlighting */
674 if (need_highlight_changed)
675 highlight_changed();
676
677 if (type == CLEAR) /* first clear screen */
678 {
679 screenclear(); /* will reset clear_cmdline */
680 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200681 /* must_redraw may be set indirectly, avoid another redraw later */
682 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 }
684
685 if (clear_cmdline) /* going to clear cmdline (done below) */
686 check_for_delay(FALSE);
687
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000688#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200689 /* Force redraw when width of 'number' or 'relativenumber' column
690 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000691 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200692 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
693 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000694 curwin->w_redr_type = NOT_VALID;
695#endif
696
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 /*
698 * Only start redrawing if there is really something to do.
699 */
700 if (type == INVERTED)
701 update_curswant();
702 if (curwin->w_redr_type < type
703 && !((type == VALID
704 && curwin->w_lines[0].wl_valid
705#ifdef FEAT_DIFF
706 && curwin->w_topfill == curwin->w_old_topfill
707 && curwin->w_botfill == curwin->w_old_botfill
708#endif
709 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000711 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
713 && curwin->w_old_visual_mode == VIsual_mode
714 && (curwin->w_valid & VALID_VIRTCOL)
715 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 ))
717 curwin->w_redr_type = type;
718
Bram Moolenaar5a305422006-04-28 22:38:25 +0000719 /* Redraw the tab pages line if needed. */
720 if (redraw_tabline || type >= NOT_VALID)
721 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000722
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723#ifdef FEAT_SYN_HL
724 /*
725 * Correct stored syntax highlighting info for changes in each displayed
726 * buffer. Each buffer must only be done once.
727 */
728 FOR_ALL_WINDOWS(wp)
729 {
730 if (wp->w_buffer->b_mod_set)
731 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 win_T *wwp;
733
734 /* Check if we already did this buffer. */
735 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
736 if (wwp->w_buffer == wp->w_buffer)
737 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200738 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 syn_stack_apply_changes(wp->w_buffer);
740 }
741 }
742#endif
743
744 /*
745 * Go from top to bottom through the windows, redrawing the ones that need
746 * it.
747 */
748#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
749 did_one = FALSE;
750#endif
751#ifdef FEAT_SEARCH_EXTRA
752 search_hl.rm.regprog = NULL;
753#endif
754 FOR_ALL_WINDOWS(wp)
755 {
756 if (wp->w_redr_type != 0)
757 {
758 cursor_off();
759#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
760 if (!did_one)
761 {
762 did_one = TRUE;
763# ifdef FEAT_SEARCH_EXTRA
764 start_search_hl();
765# endif
766# ifdef FEAT_CLIPBOARD
767 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200768 if (clip_star.available && clip_isautosel_star())
769 clip_update_selection(&clip_star);
770 if (clip_plus.available && clip_isautosel_plus())
771 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772# endif
773#ifdef FEAT_GUI
774 /* Remove the cursor before starting to do anything, because
775 * scrolling may make it difficult to redraw the text under
776 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200777 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200778 {
779 gui_cursor_col = gui.cursor_col;
780 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200782 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784#endif
785 }
786#endif
787 win_update(wp);
788 }
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 /* redraw status line after the window to minimize cursor movement */
791 if (wp->w_redr_status)
792 {
793 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200794 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 }
797#if defined(FEAT_SEARCH_EXTRA)
798 end_search_hl();
799#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100800#ifdef FEAT_INS_EXPAND
801 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200802 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 /* Reset b_mod_set flags. Going through all windows is probably faster
806 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200807 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200810 after_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812 /* Clear or redraw the command line. Done last, because scrolling may
813 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200814 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 showmode();
816
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200817 if (no_update)
818 --no_win_do_lines_ins;
819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200821 if (!did_intro)
822 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 did_intro = TRUE;
824
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200825#ifdef FEAT_TEXT_PROP
Bram Moolenaar988c4332019-06-02 14:12:11 +0200826 // Display popup windows on top of the windows.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200827 update_popups();
828#endif
829
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830#ifdef FEAT_GUI
831 /* Redraw the cursor and update the scrollbars when all screen updating is
832 * done. */
833 if (gui.in_use)
834 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200835 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200836 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100837 mch_disable_flush();
838 out_flush(); /* required before updating the cursor */
839 mch_enable_flush();
840
Bram Moolenaar144445d2016-07-08 21:41:54 +0200841 /* Put the GUI position where the cursor was, gui_update_cursor()
842 * uses that. */
843 gui.col = gui_cursor_col;
844 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200845 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100847 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200848 screen_cur_col = gui.col;
849 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200850 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100851 else
852 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 gui_update_scrollbars(FALSE);
854 }
855#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200856 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857}
858
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100859#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100860/*
861 * Prepare for updating one or more windows.
862 * Caller must check for "updating_screen" already set to avoid recursiveness.
863 */
864 static void
865update_prepare(void)
866{
867 cursor_off();
868 updating_screen = TRUE;
869#ifdef FEAT_GUI
870 /* Remove the cursor before starting to do anything, because scrolling may
871 * make it difficult to redraw the text under it. */
872 if (gui.in_use)
873 gui_undraw_cursor();
874#endif
875#ifdef FEAT_SEARCH_EXTRA
876 start_search_hl();
877#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +0200878#ifdef FEAT_TEXT_PROP
879 // Update popup_mask if needed.
880 may_update_popup_mask(0);
881#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100882}
883
884/*
885 * Finish updating one or more windows.
886 */
887 static void
888update_finish(void)
889{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200890 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100891 showmode();
892
893# ifdef FEAT_SEARCH_EXTRA
894 end_search_hl();
895# endif
896
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200897 after_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100898
899# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100900 /* Redraw the cursor and update the scrollbars when all screen updating is
901 * done. */
902 if (gui.in_use)
903 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100904 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100905 gui_update_scrollbars(FALSE);
906 }
907# endif
908}
909#endif
910
Bram Moolenaar860cae12010-06-05 23:22:07 +0200911#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200912/*
913 * Return TRUE if the cursor line in window "wp" may be concealed, according
914 * to the 'concealcursor' option.
915 */
916 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100917conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200918{
919 int c;
920
921 if (*wp->w_p_cocu == NUL)
922 return FALSE;
923 if (get_real_state() & VISUAL)
924 c = 'v';
925 else if (State & INSERT)
926 c = 'i';
927 else if (State & NORMAL)
928 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200929 else if (State & CMDLINE)
930 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200931 else
932 return FALSE;
933 return vim_strchr(wp->w_p_cocu, c) != NULL;
934}
935
936/*
937 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
938 */
939 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200940conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200941{
942 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
943 {
944 need_cursor_line_redraw = TRUE;
945 /* Need to recompute cursor column, e.g., when starting Visual mode
946 * without concealing. */
947 curs_columns(TRUE);
948 }
949}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950#endif
951
Bram Moolenaar113e1072019-01-20 15:30:40 +0100952#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100954update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955{
956 win_T *wp;
957 int doit = FALSE;
958
959# ifdef FEAT_FOLDING
960 win_foldinfo.fi_level = 0;
961# endif
962
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100963 // update/delete a specific sign
964 redraw_buf_line_later(buf, lnum);
965
966 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 if (wp->w_redr_type != 0)
969 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100971 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200972 * happening (recursive call), messages on the screen or still starting up.
973 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100974 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200975 || State == ASKMORE || State == HITRETURN
976 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100977#ifdef FEAT_GUI
978 || gui.starting
979#endif
980 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 return;
982
983 /* update all windows that need updating */
984 update_prepare();
985
Bram Moolenaar29323592016-07-24 22:04:11 +0200986 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 {
988 if (wp->w_redr_type != 0)
989 win_update(wp);
990 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200991 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994 update_finish();
995}
996#endif
997
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200998/*
999 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
1000 * window then get the "Pmenu" highlight attribute.
1001 */
1002 static int
1003get_wcr_attr(win_T *wp)
1004{
1005 int wcr_attr = 0;
1006
1007 if (*wp->w_p_wcr != NUL)
1008 wcr_attr = syn_name2attr(wp->w_p_wcr);
1009#ifdef FEAT_TEXT_PROP
1010 if (bt_popup(wp->w_buffer) && wcr_attr == 0)
1011 wcr_attr = HL_ATTR(HLF_PNI);
1012#endif
1013 return wcr_attr;
1014}
1015
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001016#ifdef FEAT_TEXT_PROP
1017/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001018 * Update "popup_mask" if needed.
1019 * Also recomputes the popup size and positions.
1020 * Also updates "popup_visible".
1021 * If more redrawing is needed than "type_arg" a higher value is returned.
1022 */
1023 int
1024may_update_popup_mask(int type_arg)
1025{
1026 int type = type_arg;
1027 win_T *wp;
1028
1029 if (popup_mask_tab != curtab)
1030 popup_mask_refresh = TRUE;
1031 if (!popup_mask_refresh)
1032 {
1033 // Check if any buffer has changed.
1034 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1035 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1036 popup_mask_refresh = TRUE;
1037 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1038 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1039 popup_mask_refresh = TRUE;
1040 if (!popup_mask_refresh)
1041 return type;
1042 }
1043
1044 popup_mask_refresh = FALSE;
1045 popup_mask_tab = curtab;
1046
1047 popup_visible = FALSE;
1048 vim_memset(popup_mask, 0, screen_Rows * screen_Columns * sizeof(short));
1049
1050 // Find the window with the lowest zindex that hasn't been handled yet,
1051 // so that the window with a higher zindex overwrites the value in
1052 // popup_mask.
1053 popup_reset_handled();
1054 while ((wp = find_next_popup(TRUE)) != NULL)
1055 {
1056 int top_off, bot_off;
1057 int left_off, right_off;
1058 short *p;
1059 int line, col;
1060
1061 popup_visible = TRUE;
1062
1063 // Recompute the position if the text changed.
1064 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1065 popup_adjust_position(wp);
1066
1067 // the position and size are for the inside, add the padding and
1068 // border
1069 top_off = wp->w_popup_padding[0] + wp->w_popup_border[0];
1070 bot_off = wp->w_popup_padding[2] + wp->w_popup_border[2];
1071 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
1072 right_off = wp->w_popup_padding[1] + wp->w_popup_border[1];
1073
1074 for (line = wp->w_winrow + top_off;
1075 line < wp->w_winrow + wp->w_height + bot_off
1076 && line < screen_Rows; ++line)
1077 for (col = wp->w_wincol + left_off;
1078 col < wp->w_wincol + wp->w_width + right_off
1079 && col < screen_Columns; ++col)
1080 {
1081 p = popup_mask + line * screen_Columns + col;
1082 if (*p != wp->w_zindex)
1083 {
1084 *p = wp->w_zindex;
1085 type = NOT_VALID;
1086 }
1087 }
1088 }
1089
1090 return type;
1091}
1092
1093/*
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001094 * Return a string of "len" spaces in IObuff.
1095 */
1096 static char_u *
1097get_spaces(int len)
1098{
1099 vim_memset(IObuff, ' ', (size_t)len);
1100 IObuff[len] = NUL;
1101 return IObuff;
1102}
1103
1104 static void
1105update_popups(void)
1106{
1107 win_T *wp;
1108 int top_off;
1109 int left_off;
1110 int total_width;
1111 int total_height;
1112 int popup_attr;
Bram Moolenaar790498b2019-06-01 22:15:29 +02001113 int border_attr[4];
Bram Moolenaar02e15072019-06-03 22:53:30 +02001114 int border_char[8];
Bram Moolenaar790498b2019-06-01 22:15:29 +02001115 char_u buf[MB_MAXBYTES];
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001116 int row;
Bram Moolenaar790498b2019-06-01 22:15:29 +02001117 int i;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001118
1119 // Find the window with the lowest zindex that hasn't been updated yet,
1120 // so that the window with a higher zindex is drawn later, thus goes on
1121 // top.
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001122 popup_reset_handled();
1123 while ((wp = find_next_popup(TRUE)) != NULL)
1124 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02001125 // This drawing uses the zindex of the popup window, so that it's on
1126 // top of the text but doesn't draw when another popup with higher
1127 // zindex is on top of the character.
1128 screen_zindex = wp->w_zindex;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001129
1130 // adjust w_winrow and w_wincol for border and padding, since
1131 // win_update() doesn't handle them.
1132 top_off = wp->w_popup_padding[0] + wp->w_popup_border[0];
1133 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
1134 wp->w_winrow += top_off;
1135 wp->w_wincol += left_off;
1136
1137 // Draw the popup text.
1138 win_update(wp);
1139
1140 wp->w_winrow -= top_off;
1141 wp->w_wincol -= left_off;
1142
1143 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
1144 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
1145 total_height = wp->w_popup_border[0] + wp->w_popup_padding[0]
1146 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
1147 popup_attr = get_wcr_attr(wp);
1148
Bram Moolenaar3f6aeba2019-06-03 22:21:27 +02001149 // We can only use these line drawing characters when 'encoding' is
1150 // "utf-8" and 'ambiwidth' is "single".
Bram Moolenaar02e15072019-06-03 22:53:30 +02001151 if (enc_utf8 && *p_ambw == 's')
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001152 {
Bram Moolenaar790498b2019-06-01 22:15:29 +02001153 border_char[0] = border_char[2] = 0x2550;
1154 border_char[1] = border_char[3] = 0x2551;
1155 border_char[4] = 0x2554;
1156 border_char[5] = 0x2557;
1157 border_char[6] = 0x255d;
1158 border_char[7] = 0x255a;
1159 }
Bram Moolenaar02e15072019-06-03 22:53:30 +02001160 else
1161 {
1162 border_char[0] = border_char[2] = '-';
1163 border_char[1] = border_char[3] = '|';
1164 for (i = 4; i < 8; ++i)
1165 border_char[i] = '+';
1166 }
Bram Moolenaar790498b2019-06-01 22:15:29 +02001167 for (i = 0; i < 8; ++i)
1168 if (wp->w_border_char[i] != 0)
1169 border_char[i] = wp->w_border_char[i];
1170
1171 for (i = 0; i < 4; ++i)
1172 {
1173 border_attr[i] = popup_attr;
1174 if (wp->w_border_highlight[i] != NULL)
1175 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001176 }
1177
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001178 if (wp->w_popup_border[0] > 0)
1179 {
1180 // top border
1181 screen_fill(wp->w_winrow, wp->w_winrow + 1,
1182 wp->w_wincol,
1183 wp->w_wincol + total_width,
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001184 wp->w_popup_border[3] != 0
Bram Moolenaar790498b2019-06-01 22:15:29 +02001185 ? border_char[4] : border_char[0],
1186 border_char[0], border_attr[0]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001187 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001188 {
1189 buf[mb_char2bytes(border_char[5], buf)] = NUL;
1190 screen_puts(buf, wp->w_winrow,
1191 wp->w_wincol + total_width - 1, border_attr[1]);
1192 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001193 }
1194
1195 if (wp->w_popup_padding[0] > 0)
1196 {
1197 // top padding
1198 row = wp->w_winrow + wp->w_popup_border[0];
1199 screen_fill(row, row + wp->w_popup_padding[0],
1200 wp->w_wincol + wp->w_popup_border[3],
1201 wp->w_wincol + total_width - wp->w_popup_border[1],
1202 ' ', ' ', popup_attr);
1203 }
1204
1205 for (row = wp->w_winrow + wp->w_popup_border[0];
1206 row < wp->w_winrow + total_height - wp->w_popup_border[2];
1207 ++row)
1208 {
1209 // left border
1210 if (wp->w_popup_border[3] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001211 {
1212 buf[mb_char2bytes(border_char[3], buf)] = NUL;
1213 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
1214 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001215 // left padding
1216 if (wp->w_popup_padding[3] > 0)
1217 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
1218 wp->w_wincol + wp->w_popup_border[3], popup_attr);
1219 // right border
1220 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001221 {
1222 buf[mb_char2bytes(border_char[1], buf)] = NUL;
1223 screen_puts(buf, row,
1224 wp->w_wincol + total_width - 1, border_attr[1]);
1225 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001226 // right padding
1227 if (wp->w_popup_padding[1] > 0)
1228 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
1229 wp->w_wincol + wp->w_popup_border[3]
1230 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
1231 }
1232
1233 if (wp->w_popup_padding[2] > 0)
1234 {
1235 // bottom padding
1236 row = wp->w_winrow + wp->w_popup_border[0]
1237 + wp->w_popup_padding[0] + wp->w_height;
1238 screen_fill(row, row + wp->w_popup_padding[2],
1239 wp->w_wincol + wp->w_popup_border[3],
1240 wp->w_wincol + total_width - wp->w_popup_border[1],
1241 ' ', ' ', popup_attr);
1242 }
1243
1244 if (wp->w_popup_border[2] > 0)
1245 {
1246 // bottom border
1247 row = wp->w_winrow + total_height - 1;
1248 screen_fill(row , row + 1,
1249 wp->w_wincol,
1250 wp->w_wincol + total_width,
Bram Moolenaar790498b2019-06-01 22:15:29 +02001251 wp->w_popup_border[3] != 0
1252 ? border_char[7] : border_char[2],
1253 border_char[2], border_attr[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001254 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001255 {
1256 buf[mb_char2bytes(border_char[6], buf)] = NUL;
1257 screen_puts(buf, row,
1258 wp->w_wincol + total_width - 1, border_attr[2]);
1259 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001260 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001261
1262 // Back to the normal zindex.
1263 screen_zindex = 0;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001264 }
1265}
1266#endif
1267
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268#if defined(FEAT_GUI) || defined(PROTO)
1269/*
1270 * Update a single window, its status line and maybe the command line msg.
1271 * Used for the GUI scrollbar.
1272 */
1273 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001274updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001276 /* return if already busy updating */
1277 if (updating_screen)
1278 return;
1279
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 update_prepare();
1281
1282#ifdef FEAT_CLIPBOARD
1283 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001284 if (clip_star.available && clip_isautosel_star())
1285 clip_update_selection(&clip_star);
1286 if (clip_plus.available && clip_isautosel_plus())
1287 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001289
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001291
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001292 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001293 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001294 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001295
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 if (wp->w_redr_status
1297# ifdef FEAT_CMDL_INFO
1298 || p_ru
1299# endif
1300# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001301 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302# endif
1303 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001304 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305
Bram Moolenaar988c4332019-06-02 14:12:11 +02001306#ifdef FEAT_TEXT_PROP
1307 // Display popup windows on top of everything.
1308 update_popups();
1309#endif
1310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 update_finish();
1312}
1313#endif
1314
1315/*
1316 * Update a single window.
1317 *
1318 * This may cause the windows below it also to be redrawn (when clearing the
1319 * screen or scrolling lines).
1320 *
1321 * How the window is redrawn depends on wp->w_redr_type. Each type also
1322 * implies the one below it.
1323 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001324 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1326 * INVERTED redraw the changed part of the Visual area
1327 * INVERTED_ALL redraw the whole Visual area
1328 * VALID 1. scroll up/down to adjust for a changed w_topline
1329 * 2. update lines at the top when scrolled down
1330 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001331 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 * b_mod_top and b_mod_bot.
1333 * - if wp->w_redraw_top non-zero, redraw lines between
1334 * wp->w_redraw_top and wp->w_redr_bot.
1335 * - continue redrawing when syntax status is invalid.
1336 * 4. if scrolled up, update lines at the bottom.
1337 * This results in three areas that may need updating:
1338 * top: from first row to top_end (when scrolled down)
1339 * mid: from mid_start to mid_end (update inversion or changed text)
1340 * bot: from bot_start to last row (when scrolled up)
1341 */
1342 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001343win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
1345 buf_T *buf = wp->w_buffer;
1346 int type;
1347 int top_end = 0; /* Below last row of the top area that needs
1348 updating. 0 when no top area updating. */
1349 int mid_start = 999;/* first row of the mid area that needs
1350 updating. 999 when no mid area updating. */
1351 int mid_end = 0; /* Below last row of the mid area that needs
1352 updating. 0 when no mid area updating. */
1353 int bot_start = 999;/* first row of the bot area that needs
1354 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 int scrolled_down = FALSE; /* TRUE when scrolled down when
1356 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001358 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 int top_to_mod = FALSE; /* redraw above mod_top */
1360#endif
1361
1362 int row; /* current window row to display */
1363 linenr_T lnum; /* current buffer lnum to display */
1364 int idx; /* current index in w_lines[] */
1365 int srow; /* starting row of the current line */
1366
1367 int eof = FALSE; /* if TRUE, we hit the end of the file */
1368 int didline = FALSE; /* if TRUE, we finished the last line */
1369 int i;
1370 long j;
1371 static int recursive = FALSE; /* being called recursively */
1372 int old_botline = wp->w_botline;
1373#ifdef FEAT_FOLDING
1374 long fold_count;
1375#endif
1376#ifdef FEAT_SYN_HL
1377 /* remember what happened to the previous line, to know if
1378 * check_visual_highlight() can be used */
1379#define DID_NONE 1 /* didn't update a line */
1380#define DID_LINE 2 /* updated a normal line */
1381#define DID_FOLD 3 /* updated a folded line */
1382 int did_update = DID_NONE;
1383 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1384#endif
1385 linenr_T mod_top = 0;
1386 linenr_T mod_bot = 0;
1387#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1388 int save_got_int;
1389#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001390#ifdef SYN_TIME_LIMIT
1391 proftime_T syntax_tm;
1392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
1394 type = wp->w_redr_type;
1395
1396 if (type == NOT_VALID)
1397 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 wp->w_lines_valid = 0;
1400 }
1401
1402 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001403 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 {
1405 wp->w_redr_type = 0;
1406 return;
1407 }
1408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 /* Window is zero-width: Only need to draw the separator. */
1410 if (wp->w_width == 0)
1411 {
1412 /* draw the vertical separator right of this window */
1413 draw_vsep_win(wp, 0);
1414 wp->w_redr_type = 0;
1415 return;
1416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001418#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001419 // If this window contains a terminal, redraw works completely differently.
1420 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001421 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001422 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001423# ifdef FEAT_MENU
1424 /* Draw the window toolbar, if there is one. */
1425 if (winbar_height(wp) > 0)
1426 redraw_win_toolbar(wp);
1427# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001428 wp->w_redr_type = 0;
1429 return;
1430 }
1431#endif
1432
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001434 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435#endif
1436
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001437#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001438 /* Force redraw when width of 'number' or 'relativenumber' column
1439 * changes. */
1440 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001441 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001442 {
1443 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001444 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001445 }
1446 else
1447#endif
1448
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1450 {
1451 /*
1452 * When there are both inserted/deleted lines and specific lines to be
1453 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1454 * everything (only happens when redrawing is off for while).
1455 */
1456 type = NOT_VALID;
1457 }
1458 else
1459 {
1460 /*
1461 * Set mod_top to the first line that needs displaying because of
1462 * changes. Set mod_bot to the first line after the changes.
1463 */
1464 mod_top = wp->w_redraw_top;
1465 if (wp->w_redraw_bot != 0)
1466 mod_bot = wp->w_redraw_bot + 1;
1467 else
1468 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (buf->b_mod_set)
1470 {
1471 if (mod_top == 0 || mod_top > buf->b_mod_top)
1472 {
1473 mod_top = buf->b_mod_top;
1474#ifdef FEAT_SYN_HL
1475 /* Need to redraw lines above the change that may be included
1476 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001477 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001479 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 if (mod_top < 1)
1481 mod_top = 1;
1482 }
1483#endif
1484 }
1485 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1486 mod_bot = buf->b_mod_bot;
1487
1488#ifdef FEAT_SEARCH_EXTRA
1489 /* When 'hlsearch' is on and using a multi-line search pattern, a
1490 * change in one line may make the Search highlighting in a
1491 * previous line invalid. Simple solution: redraw all visible
1492 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001493 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001495 if (search_hl.rm.regprog != NULL
1496 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001498 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001499 {
1500 cur = wp->w_match_head;
1501 while (cur != NULL)
1502 {
1503 if (cur->match.regprog != NULL
1504 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001505 {
1506 top_to_mod = TRUE;
1507 break;
1508 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001509 cur = cur->next;
1510 }
1511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512#endif
1513 }
1514#ifdef FEAT_FOLDING
1515 if (mod_top != 0 && hasAnyFolding(wp))
1516 {
1517 linenr_T lnumt, lnumb;
1518
1519 /*
1520 * A change in a line can cause lines above it to become folded or
1521 * unfolded. Find the top most buffer line that may be affected.
1522 * If the line was previously folded and displayed, get the first
1523 * line of that fold. If the line is folded now, get the first
1524 * folded line. Use the minimum of these two.
1525 */
1526
1527 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1528 * the line below it. If there is no valid entry, use w_topline.
1529 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1530 * to this line. If there is no valid entry, use MAXLNUM. */
1531 lnumt = wp->w_topline;
1532 lnumb = MAXLNUM;
1533 for (i = 0; i < wp->w_lines_valid; ++i)
1534 if (wp->w_lines[i].wl_valid)
1535 {
1536 if (wp->w_lines[i].wl_lastlnum < mod_top)
1537 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1538 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1539 {
1540 lnumb = wp->w_lines[i].wl_lnum;
1541 /* When there is a fold column it might need updating
1542 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001543 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 ++lnumb;
1545 }
1546 }
1547
1548 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1549 if (mod_top > lnumt)
1550 mod_top = lnumt;
1551
1552 /* Now do the same for the bottom line (one above mod_bot). */
1553 --mod_bot;
1554 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1555 ++mod_bot;
1556 if (mod_bot < lnumb)
1557 mod_bot = lnumb;
1558 }
1559#endif
1560
1561 /* When a change starts above w_topline and the end is below
1562 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001563 * If the end of the change is above w_topline: do like no change was
1564 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 if (mod_top != 0 && mod_top < wp->w_topline)
1566 {
1567 if (mod_bot > wp->w_topline)
1568 mod_top = wp->w_topline;
1569#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001570 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 top_end = 1;
1572#endif
1573 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001574
1575 /* When line numbers are displayed need to redraw all lines below
1576 * inserted/deleted lines. */
1577 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1578 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001580 wp->w_redraw_top = 0; // reset for next time
1581 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
1583 /*
1584 * When only displaying the lines at the top, set top_end. Used when
1585 * window has scrolled down for msg_scrolled.
1586 */
1587 if (type == REDRAW_TOP)
1588 {
1589 j = 0;
1590 for (i = 0; i < wp->w_lines_valid; ++i)
1591 {
1592 j += wp->w_lines[i].wl_size;
1593 if (j >= wp->w_upd_rows)
1594 {
1595 top_end = j;
1596 break;
1597 }
1598 }
1599 if (top_end == 0)
1600 /* not found (cannot happen?): redraw everything */
1601 type = NOT_VALID;
1602 else
1603 /* top area defined, the rest is VALID */
1604 type = VALID;
1605 }
1606
Bram Moolenaar367329b2007-08-30 11:53:22 +00001607 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001608 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1609 * non-zero and thus not FALSE) will indicate that screenclear() was not
1610 * called. */
1611 if (screen_cleared)
1612 screen_cleared = MAYBE;
1613
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 /*
1615 * If there are no changes on the screen that require a complete redraw,
1616 * handle three cases:
1617 * 1: we are off the top of the screen by a few lines: scroll down
1618 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1619 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1620 * w_lines[] that needs updating.
1621 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001622 if ((type == VALID || type == SOME_VALID
1623 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624#ifdef FEAT_DIFF
1625 && !wp->w_botfill && !wp->w_old_botfill
1626#endif
1627 )
1628 {
1629 if (mod_top != 0 && wp->w_topline == mod_top)
1630 {
1631 /*
1632 * w_topline is the first changed line, the scrolling will be done
1633 * further down.
1634 */
1635 }
1636 else if (wp->w_lines[0].wl_valid
1637 && (wp->w_topline < wp->w_lines[0].wl_lnum
1638#ifdef FEAT_DIFF
1639 || (wp->w_topline == wp->w_lines[0].wl_lnum
1640 && wp->w_topfill > wp->w_old_topfill)
1641#endif
1642 ))
1643 {
1644 /*
1645 * New topline is above old topline: May scroll down.
1646 */
1647#ifdef FEAT_FOLDING
1648 if (hasAnyFolding(wp))
1649 {
1650 linenr_T ln;
1651
1652 /* count the number of lines we are off, counting a sequence
1653 * of folded lines as one */
1654 j = 0;
1655 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1656 {
1657 ++j;
1658 if (j >= wp->w_height - 2)
1659 break;
1660 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1661 }
1662 }
1663 else
1664#endif
1665 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1666 if (j < wp->w_height - 2) /* not too far off */
1667 {
1668 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1669#ifdef FEAT_DIFF
1670 /* insert extra lines for previously invisible filler lines */
1671 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1672 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1673 - wp->w_old_topfill;
1674#endif
1675 if (i < wp->w_height - 2) /* less than a screen off */
1676 {
1677 /*
1678 * Try to insert the correct number of lines.
1679 * If not the last window, delete the lines at the bottom.
1680 * win_ins_lines may fail when the terminal can't do it.
1681 */
1682 if (i > 0)
1683 check_for_delay(FALSE);
1684 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1685 {
1686 if (wp->w_lines_valid != 0)
1687 {
1688 /* Need to update rows that are new, stop at the
1689 * first one that scrolled down. */
1690 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
1693 /* Move the entries that were scrolled, disable
1694 * the entries for the lines to be redrawn. */
1695 if ((wp->w_lines_valid += j) > wp->w_height)
1696 wp->w_lines_valid = wp->w_height;
1697 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1698 wp->w_lines[idx] = wp->w_lines[idx - j];
1699 while (idx >= 0)
1700 wp->w_lines[idx--].wl_valid = FALSE;
1701 }
1702 }
1703 else
1704 mid_start = 0; /* redraw all lines */
1705 }
1706 else
1707 mid_start = 0; /* redraw all lines */
1708 }
1709 else
1710 mid_start = 0; /* redraw all lines */
1711 }
1712 else
1713 {
1714 /*
1715 * New topline is at or below old topline: May scroll up.
1716 * When topline didn't change, find first entry in w_lines[] that
1717 * needs updating.
1718 */
1719
1720 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1721 j = -1;
1722 row = 0;
1723 for (i = 0; i < wp->w_lines_valid; i++)
1724 {
1725 if (wp->w_lines[i].wl_valid
1726 && wp->w_lines[i].wl_lnum == wp->w_topline)
1727 {
1728 j = i;
1729 break;
1730 }
1731 row += wp->w_lines[i].wl_size;
1732 }
1733 if (j == -1)
1734 {
1735 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1736 * lines */
1737 mid_start = 0;
1738 }
1739 else
1740 {
1741 /*
1742 * Try to delete the correct number of lines.
1743 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1744 */
1745#ifdef FEAT_DIFF
1746 /* If the topline didn't change, delete old filler lines,
1747 * otherwise delete filler lines of the new topline... */
1748 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1749 row += wp->w_old_topfill;
1750 else
1751 row += diff_check_fill(wp, wp->w_topline);
1752 /* ... but don't delete new filler lines. */
1753 row -= wp->w_topfill;
1754#endif
1755 if (row > 0)
1756 {
1757 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001758 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1759 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 bot_start = wp->w_height - row;
1761 else
1762 mid_start = 0; /* redraw all lines */
1763 }
1764 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1765 {
1766 /*
1767 * Skip the lines (below the deleted lines) that are still
1768 * valid and don't need redrawing. Copy their info
1769 * upwards, to compensate for the deleted lines. Set
1770 * bot_start to the first row that needs redrawing.
1771 */
1772 bot_start = 0;
1773 idx = 0;
1774 for (;;)
1775 {
1776 wp->w_lines[idx] = wp->w_lines[j];
1777 /* stop at line that didn't fit, unless it is still
1778 * valid (no lines deleted) */
1779 if (row > 0 && bot_start + row
1780 + (int)wp->w_lines[j].wl_size > wp->w_height)
1781 {
1782 wp->w_lines_valid = idx + 1;
1783 break;
1784 }
1785 bot_start += wp->w_lines[idx++].wl_size;
1786
1787 /* stop at the last valid entry in w_lines[].wl_size */
1788 if (++j >= wp->w_lines_valid)
1789 {
1790 wp->w_lines_valid = idx;
1791 break;
1792 }
1793 }
1794#ifdef FEAT_DIFF
1795 /* Correct the first entry for filler lines at the top
1796 * when it won't get updated below. */
1797 if (wp->w_p_diff && bot_start > 0)
1798 wp->w_lines[0].wl_size =
1799 plines_win_nofill(wp, wp->w_topline, TRUE)
1800 + wp->w_topfill;
1801#endif
1802 }
1803 }
1804 }
1805
1806 /* When starting redraw in the first line, redraw all lines. When
1807 * there is only one window it's probably faster to clear the screen
1808 * first. */
1809 if (mid_start == 0)
1810 {
1811 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001812 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001813 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001814 /* Clear the screen when it was not done by win_del_lines() or
1815 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1816 * then. */
1817 if (screen_cleared != TRUE)
1818 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001819 /* The screen was cleared, redraw the tab pages line. */
1820 if (redraw_tabline)
1821 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001824
1825 /* When win_del_lines() or win_ins_lines() caused the screen to be
1826 * cleared (only happens for the first window) or when screenclear()
1827 * was called directly above, "must_redraw" will have been set to
1828 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1829 if (screen_cleared == TRUE)
1830 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 }
1832 else
1833 {
1834 /* Not VALID or INVERTED: redraw all lines. */
1835 mid_start = 0;
1836 mid_end = wp->w_height;
1837 }
1838
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001839 if (type == SOME_VALID)
1840 {
1841 /* SOME_VALID: redraw all lines. */
1842 mid_start = 0;
1843 mid_end = wp->w_height;
1844 type = NOT_VALID;
1845 }
1846
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 /* check if we are updating or removing the inverted part */
1848 if ((VIsual_active && buf == curwin->w_buffer)
1849 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1850 {
1851 linenr_T from, to;
1852
1853 if (VIsual_active)
1854 {
1855 if (VIsual_active
1856 && (VIsual_mode != wp->w_old_visual_mode
1857 || type == INVERTED_ALL))
1858 {
1859 /*
1860 * If the type of Visual selection changed, redraw the whole
1861 * selection. Also when the ownership of the X selection is
1862 * gained or lost.
1863 */
1864 if (curwin->w_cursor.lnum < VIsual.lnum)
1865 {
1866 from = curwin->w_cursor.lnum;
1867 to = VIsual.lnum;
1868 }
1869 else
1870 {
1871 from = VIsual.lnum;
1872 to = curwin->w_cursor.lnum;
1873 }
1874 /* redraw more when the cursor moved as well */
1875 if (wp->w_old_cursor_lnum < from)
1876 from = wp->w_old_cursor_lnum;
1877 if (wp->w_old_cursor_lnum > to)
1878 to = wp->w_old_cursor_lnum;
1879 if (wp->w_old_visual_lnum < from)
1880 from = wp->w_old_visual_lnum;
1881 if (wp->w_old_visual_lnum > to)
1882 to = wp->w_old_visual_lnum;
1883 }
1884 else
1885 {
1886 /*
1887 * Find the line numbers that need to be updated: The lines
1888 * between the old cursor position and the current cursor
1889 * position. Also check if the Visual position changed.
1890 */
1891 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1892 {
1893 from = curwin->w_cursor.lnum;
1894 to = wp->w_old_cursor_lnum;
1895 }
1896 else
1897 {
1898 from = wp->w_old_cursor_lnum;
1899 to = curwin->w_cursor.lnum;
1900 if (from == 0) /* Visual mode just started */
1901 from = to;
1902 }
1903
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001904 if (VIsual.lnum != wp->w_old_visual_lnum
1905 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
1907 if (wp->w_old_visual_lnum < from
1908 && wp->w_old_visual_lnum != 0)
1909 from = wp->w_old_visual_lnum;
1910 if (wp->w_old_visual_lnum > to)
1911 to = wp->w_old_visual_lnum;
1912 if (VIsual.lnum < from)
1913 from = VIsual.lnum;
1914 if (VIsual.lnum > to)
1915 to = VIsual.lnum;
1916 }
1917 }
1918
1919 /*
1920 * If in block mode and changed column or curwin->w_curswant:
1921 * update all lines.
1922 * First compute the actual start and end column.
1923 */
1924 if (VIsual_mode == Ctrl_V)
1925 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001926 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001927#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001928 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
Bram Moolenaar404406a2014-10-09 13:24:43 +02001930 if (curwin->w_p_lbr)
1931 ve_flags = VE_ALL;
1932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001934#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001935 ve_flags = save_ve_flags;
1936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 ++toc;
1938 if (curwin->w_curswant == MAXCOL)
1939 toc = MAXCOL;
1940
1941 if (fromc != wp->w_old_cursor_fcol
1942 || toc != wp->w_old_cursor_lcol)
1943 {
1944 if (from > VIsual.lnum)
1945 from = VIsual.lnum;
1946 if (to < VIsual.lnum)
1947 to = VIsual.lnum;
1948 }
1949 wp->w_old_cursor_fcol = fromc;
1950 wp->w_old_cursor_lcol = toc;
1951 }
1952 }
1953 else
1954 {
1955 /* Use the line numbers of the old Visual area. */
1956 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1957 {
1958 from = wp->w_old_cursor_lnum;
1959 to = wp->w_old_visual_lnum;
1960 }
1961 else
1962 {
1963 from = wp->w_old_visual_lnum;
1964 to = wp->w_old_cursor_lnum;
1965 }
1966 }
1967
1968 /*
1969 * There is no need to update lines above the top of the window.
1970 */
1971 if (from < wp->w_topline)
1972 from = wp->w_topline;
1973
1974 /*
1975 * If we know the value of w_botline, use it to restrict the update to
1976 * the lines that are visible in the window.
1977 */
1978 if (wp->w_valid & VALID_BOTLINE)
1979 {
1980 if (from >= wp->w_botline)
1981 from = wp->w_botline - 1;
1982 if (to >= wp->w_botline)
1983 to = wp->w_botline - 1;
1984 }
1985
1986 /*
1987 * Find the minimal part to be updated.
1988 * Watch out for scrolling that made entries in w_lines[] invalid.
1989 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1990 * top_end; need to redraw from top_end to the "to" line.
1991 * A middle mouse click with a Visual selection may change the text
1992 * above the Visual area and reset wl_valid, do count these for
1993 * mid_end (in srow).
1994 */
1995 if (mid_start > 0)
1996 {
1997 lnum = wp->w_topline;
1998 idx = 0;
1999 srow = 0;
2000 if (scrolled_down)
2001 mid_start = top_end;
2002 else
2003 mid_start = 0;
2004 while (lnum < from && idx < wp->w_lines_valid) /* find start */
2005 {
2006 if (wp->w_lines[idx].wl_valid)
2007 mid_start += wp->w_lines[idx].wl_size;
2008 else if (!scrolled_down)
2009 srow += wp->w_lines[idx].wl_size;
2010 ++idx;
2011# ifdef FEAT_FOLDING
2012 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
2013 lnum = wp->w_lines[idx].wl_lnum;
2014 else
2015# endif
2016 ++lnum;
2017 }
2018 srow += mid_start;
2019 mid_end = wp->w_height;
2020 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
2021 {
2022 if (wp->w_lines[idx].wl_valid
2023 && wp->w_lines[idx].wl_lnum >= to + 1)
2024 {
2025 /* Only update until first row of this line */
2026 mid_end = srow;
2027 break;
2028 }
2029 srow += wp->w_lines[idx].wl_size;
2030 }
2031 }
2032 }
2033
2034 if (VIsual_active && buf == curwin->w_buffer)
2035 {
2036 wp->w_old_visual_mode = VIsual_mode;
2037 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
2038 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00002039 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 wp->w_old_curswant = curwin->w_curswant;
2041 }
2042 else
2043 {
2044 wp->w_old_visual_mode = 0;
2045 wp->w_old_cursor_lnum = 0;
2046 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00002047 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
2050#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2051 /* reset got_int, otherwise regexp won't work */
2052 save_got_int = got_int;
2053 got_int = 0;
2054#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002055#ifdef SYN_TIME_LIMIT
2056 /* Set the time limit to 'redrawtime'. */
2057 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002058 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060#ifdef FEAT_FOLDING
2061 win_foldinfo.fi_level = 0;
2062#endif
2063
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002064#ifdef FEAT_MENU
2065 /*
2066 * Draw the window toolbar, if there is one.
2067 * TODO: only when needed.
2068 */
2069 if (winbar_height(wp) > 0)
2070 redraw_win_toolbar(wp);
2071#endif
2072
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 /*
2074 * Update all the window rows.
2075 */
2076 idx = 0; /* first entry in w_lines[].wl_size */
2077 row = 0;
2078 srow = 0;
2079 lnum = wp->w_topline; /* first line shown in window */
2080 for (;;)
2081 {
2082 /* stop updating when reached the end of the window (check for _past_
2083 * the end of the window is at the end of the loop) */
2084 if (row == wp->w_height)
2085 {
2086 didline = TRUE;
2087 break;
2088 }
2089
2090 /* stop updating when hit the end of the file */
2091 if (lnum > buf->b_ml.ml_line_count)
2092 {
2093 eof = TRUE;
2094 break;
2095 }
2096
2097 /* Remember the starting row of the line that is going to be dealt
2098 * with. It is used further down when the line doesn't fit. */
2099 srow = row;
2100
2101 /*
2102 * Update a line when it is in an area that needs updating, when it
2103 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002104 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 * win_del_lines(), check if the current line includes it.
2106 * When syntax folding is being used, the saved syntax states will
2107 * already have been updated, we can't see where the syntax state is
2108 * the same again, just update until the end of the window.
2109 */
2110 if (row < top_end
2111 || (row >= mid_start && row < mid_end)
2112#ifdef FEAT_SEARCH_EXTRA
2113 || top_to_mod
2114#endif
2115 || idx >= wp->w_lines_valid
2116 || (row + wp->w_lines[idx].wl_size > bot_start)
2117 || (mod_top != 0
2118 && (lnum == mod_top
2119 || (lnum >= mod_top
2120 && (lnum < mod_bot
2121#ifdef FEAT_SYN_HL
2122 || did_update == DID_FOLD
2123 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02002124 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 && (
2126# ifdef FEAT_FOLDING
2127 (foldmethodIsSyntax(wp)
2128 && hasAnyFolding(wp)) ||
2129# endif
2130 syntax_check_changed(lnum)))
2131#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002132#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02002133 /* match in fixed position might need redraw
2134 * if lines were inserted or deleted */
2135 || (wp->w_match_head != NULL
2136 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 )))))
2139 {
2140#ifdef FEAT_SEARCH_EXTRA
2141 if (lnum == mod_top)
2142 top_to_mod = FALSE;
2143#endif
2144
2145 /*
2146 * When at start of changed lines: May scroll following lines
2147 * up or down to minimize redrawing.
2148 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002149 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 */
2151 if (lnum == mod_top
2152 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002153 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 {
2155 int old_rows = 0;
2156 int new_rows = 0;
2157 int xtra_rows;
2158 linenr_T l;
2159
2160 /* Count the old number of window rows, using w_lines[], which
2161 * should still contain the sizes for the lines as they are
2162 * currently displayed. */
2163 for (i = idx; i < wp->w_lines_valid; ++i)
2164 {
2165 /* Only valid lines have a meaningful wl_lnum. Invalid
2166 * lines are part of the changed area. */
2167 if (wp->w_lines[i].wl_valid
2168 && wp->w_lines[i].wl_lnum == mod_bot)
2169 break;
2170 old_rows += wp->w_lines[i].wl_size;
2171#ifdef FEAT_FOLDING
2172 if (wp->w_lines[i].wl_valid
2173 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2174 {
2175 /* Must have found the last valid entry above mod_bot.
2176 * Add following invalid entries. */
2177 ++i;
2178 while (i < wp->w_lines_valid
2179 && !wp->w_lines[i].wl_valid)
2180 old_rows += wp->w_lines[i++].wl_size;
2181 break;
2182 }
2183#endif
2184 }
2185
2186 if (i >= wp->w_lines_valid)
2187 {
2188 /* We can't find a valid line below the changed lines,
2189 * need to redraw until the end of the window.
2190 * Inserting/deleting lines has no use. */
2191 bot_start = 0;
2192 }
2193 else
2194 {
2195 /* Able to count old number of rows: Count new window
2196 * rows, and may insert/delete lines */
2197 j = idx;
2198 for (l = lnum; l < mod_bot; ++l)
2199 {
2200#ifdef FEAT_FOLDING
2201 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2202 ++new_rows;
2203 else
2204#endif
2205#ifdef FEAT_DIFF
2206 if (l == wp->w_topline)
2207 new_rows += plines_win_nofill(wp, l, TRUE)
2208 + wp->w_topfill;
2209 else
2210#endif
2211 new_rows += plines_win(wp, l, TRUE);
2212 ++j;
2213 if (new_rows > wp->w_height - row - 2)
2214 {
2215 /* it's getting too much, must redraw the rest */
2216 new_rows = 9999;
2217 break;
2218 }
2219 }
2220 xtra_rows = new_rows - old_rows;
2221 if (xtra_rows < 0)
2222 {
2223 /* May scroll text up. If there is not enough
2224 * remaining text or scrolling fails, must redraw the
2225 * rest. If scrolling works, must redraw the text
2226 * below the scrolled text. */
2227 if (row - xtra_rows >= wp->w_height - 2)
2228 mod_bot = MAXLNUM;
2229 else
2230 {
2231 check_for_delay(FALSE);
2232 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002233 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 mod_bot = MAXLNUM;
2235 else
2236 bot_start = wp->w_height + xtra_rows;
2237 }
2238 }
2239 else if (xtra_rows > 0)
2240 {
2241 /* May scroll text down. If there is not enough
2242 * remaining text of scrolling fails, must redraw the
2243 * rest. */
2244 if (row + xtra_rows >= wp->w_height - 2)
2245 mod_bot = MAXLNUM;
2246 else
2247 {
2248 check_for_delay(FALSE);
2249 if (win_ins_lines(wp, row + old_rows,
2250 xtra_rows, FALSE, FALSE) == FAIL)
2251 mod_bot = MAXLNUM;
2252 else if (top_end > row + old_rows)
2253 /* Scrolled the part at the top that requires
2254 * updating down. */
2255 top_end += xtra_rows;
2256 }
2257 }
2258
2259 /* When not updating the rest, may need to move w_lines[]
2260 * entries. */
2261 if (mod_bot != MAXLNUM && i != j)
2262 {
2263 if (j < i)
2264 {
2265 int x = row + new_rows;
2266
2267 /* move entries in w_lines[] upwards */
2268 for (;;)
2269 {
2270 /* stop at last valid entry in w_lines[] */
2271 if (i >= wp->w_lines_valid)
2272 {
2273 wp->w_lines_valid = j;
2274 break;
2275 }
2276 wp->w_lines[j] = wp->w_lines[i];
2277 /* stop at a line that won't fit */
2278 if (x + (int)wp->w_lines[j].wl_size
2279 > wp->w_height)
2280 {
2281 wp->w_lines_valid = j + 1;
2282 break;
2283 }
2284 x += wp->w_lines[j++].wl_size;
2285 ++i;
2286 }
2287 if (bot_start > x)
2288 bot_start = x;
2289 }
2290 else /* j > i */
2291 {
2292 /* move entries in w_lines[] downwards */
2293 j -= i;
2294 wp->w_lines_valid += j;
2295 if (wp->w_lines_valid > wp->w_height)
2296 wp->w_lines_valid = wp->w_height;
2297 for (i = wp->w_lines_valid; i - j >= idx; --i)
2298 wp->w_lines[i] = wp->w_lines[i - j];
2299
2300 /* The w_lines[] entries for inserted lines are
2301 * now invalid, but wl_size may be used above.
2302 * Reset to zero. */
2303 while (i >= idx)
2304 {
2305 wp->w_lines[i].wl_size = 0;
2306 wp->w_lines[i--].wl_valid = FALSE;
2307 }
2308 }
2309 }
2310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 }
2312
2313#ifdef FEAT_FOLDING
2314 /*
2315 * When lines are folded, display one line for all of them.
2316 * Otherwise, display normally (can be several display lines when
2317 * 'wrap' is on).
2318 */
2319 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2320 if (fold_count != 0)
2321 {
2322 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2323 ++row;
2324 --fold_count;
2325 wp->w_lines[idx].wl_folded = TRUE;
2326 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2327# ifdef FEAT_SYN_HL
2328 did_update = DID_FOLD;
2329# endif
2330 }
2331 else
2332#endif
2333 if (idx < wp->w_lines_valid
2334 && wp->w_lines[idx].wl_valid
2335 && wp->w_lines[idx].wl_lnum == lnum
2336 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002337 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar17146962019-05-30 00:12:11 +02002338#ifdef FEAT_TEXT_PROP
2339 && !bt_popup(wp->w_buffer)
2340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 && srow + wp->w_lines[idx].wl_size > wp->w_height
2342#ifdef FEAT_DIFF
2343 && diff_check_fill(wp, lnum) == 0
2344#endif
2345 )
2346 {
2347 /* This line is not going to fit. Don't draw anything here,
2348 * will draw "@ " lines below. */
2349 row = wp->w_height + 1;
2350 }
2351 else
2352 {
2353#ifdef FEAT_SEARCH_EXTRA
2354 prepare_search_hl(wp, lnum);
2355#endif
2356#ifdef FEAT_SYN_HL
2357 /* Let the syntax stuff know we skipped a few lines. */
2358 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002359 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 syntax_end_parsing(syntax_last_parsed + 1);
2361#endif
2362
2363 /*
2364 * Display one line.
2365 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002366 row = win_line(wp, lnum, srow, wp->w_height,
2367 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368
2369#ifdef FEAT_FOLDING
2370 wp->w_lines[idx].wl_folded = FALSE;
2371 wp->w_lines[idx].wl_lastlnum = lnum;
2372#endif
2373#ifdef FEAT_SYN_HL
2374 did_update = DID_LINE;
2375 syntax_last_parsed = lnum;
2376#endif
2377 }
2378
2379 wp->w_lines[idx].wl_lnum = lnum;
2380 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002381
2382 /* Past end of the window or end of the screen. Note that after
2383 * resizing wp->w_height may be end up too big. That's a problem
2384 * elsewhere, but prevent a crash here. */
2385 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
2387 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002388 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2390 ++idx;
2391 break;
2392 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002393 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 wp->w_lines[idx].wl_size = row - srow;
2395 ++idx;
2396#ifdef FEAT_FOLDING
2397 lnum += fold_count + 1;
2398#else
2399 ++lnum;
2400#endif
2401 }
2402 else
2403 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002404 if (wp->w_p_rnu)
2405 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002406#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002407 // 'relativenumber' set: The text doesn't need to be drawn, but
2408 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002409 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2410 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002411 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002412 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002413#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002414 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002415 }
2416
2417 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 row += wp->w_lines[idx++].wl_size;
2419 if (row > wp->w_height) /* past end of screen */
2420 break;
2421#ifdef FEAT_FOLDING
2422 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2423#else
2424 ++lnum;
2425#endif
2426#ifdef FEAT_SYN_HL
2427 did_update = DID_NONE;
2428#endif
2429 }
2430
2431 if (lnum > buf->b_ml.ml_line_count)
2432 {
2433 eof = TRUE;
2434 break;
2435 }
2436 }
2437 /*
2438 * End of loop over all window lines.
2439 */
2440
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002441#ifdef FEAT_VTP
2442 /* Rewrite the character at the end of the screen line. */
2443 if (use_vtp())
2444 {
2445 int i;
2446
2447 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002448 if (enc_utf8)
2449 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2450 LineOffset[i] + screen_Columns) > 1)
2451 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2452 else
2453 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2454 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002455 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2456 }
2457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
2459 if (idx > wp->w_lines_valid)
2460 wp->w_lines_valid = idx;
2461
2462#ifdef FEAT_SYN_HL
2463 /*
2464 * Let the syntax stuff know we stop parsing here.
2465 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002466 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 syntax_end_parsing(syntax_last_parsed + 1);
2468#endif
2469
2470 /*
2471 * If we didn't hit the end of the file, and we didn't finish the last
2472 * line we were working on, then the line didn't fit.
2473 */
2474 wp->w_empty_rows = 0;
2475#ifdef FEAT_DIFF
2476 wp->w_filler_rows = 0;
2477#endif
2478 if (!eof && !didline)
2479 {
2480 if (lnum == wp->w_topline)
2481 {
2482 /*
2483 * Single line that does not fit!
2484 * Don't overwrite it, it can be edited.
2485 */
2486 wp->w_botline = lnum + 1;
2487 }
2488#ifdef FEAT_DIFF
2489 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2490 {
2491 /* Window ends in filler lines. */
2492 wp->w_botline = lnum;
2493 wp->w_filler_rows = wp->w_height - srow;
2494 }
2495#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002496#ifdef FEAT_TEXT_PROP
2497 else if (bt_popup(wp->w_buffer))
2498 {
2499 // popup line that doesn't fit is left as-is
2500 wp->w_botline = lnum;
2501 }
2502#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002503 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2504 {
2505 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2506
2507 /*
2508 * Last line isn't finished: Display "@@@" in the last screen line.
2509 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002510 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002511 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002512 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002513 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002514 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002515 set_empty_rows(wp, srow);
2516 wp->w_botline = lnum;
2517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2519 {
2520 /*
2521 * Last line isn't finished: Display "@@@" at the end.
2522 */
2523 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2524 W_WINROW(wp) + wp->w_height,
2525 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002526 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 set_empty_rows(wp, srow);
2528 wp->w_botline = lnum;
2529 }
2530 else
2531 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002532 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 wp->w_botline = lnum;
2534 }
2535 }
2536 else
2537 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 if (eof) /* we hit the end of the file */
2540 {
2541 wp->w_botline = buf->b_ml.ml_line_count + 1;
2542#ifdef FEAT_DIFF
2543 j = diff_check_fill(wp, wp->w_botline);
2544 if (j > 0 && !wp->w_botfill)
2545 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002546 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (char2cells(fill_diff) > 1)
2548 i = '-';
2549 else
2550 i = fill_diff;
2551 if (row + j > wp->w_height)
2552 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002553 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 row += j;
2555 }
2556#endif
2557 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002558 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 wp->w_botline = lnum;
2560
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002561 // Make sure the rest of the screen is blank
2562 // put '~'s on rows that aren't part of the file.
Bram Moolenaar17146962019-05-30 00:12:11 +02002563 win_draw_end(wp,
2564#ifdef FEAT_TEXT_PROP
2565 bt_popup(wp->w_buffer) ? ' ' :
2566#endif
2567 '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
2569
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002570#ifdef SYN_TIME_LIMIT
2571 syn_set_timeout(NULL);
2572#endif
2573
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 /* Reset the type of redrawing required, the window has been updated. */
2575 wp->w_redr_type = 0;
2576#ifdef FEAT_DIFF
2577 wp->w_old_topfill = wp->w_topfill;
2578 wp->w_old_botfill = wp->w_botfill;
2579#endif
2580
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002581 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 {
2583 /*
2584 * There is a trick with w_botline. If we invalidate it on each
2585 * change that might modify it, this will cause a lot of expensive
2586 * calls to plines() in update_topline() each time. Therefore the
2587 * value of w_botline is often approximated, and this value is used to
2588 * compute the value of w_topline. If the value of w_botline was
2589 * wrong, check that the value of w_topline is correct (cursor is on
2590 * the visible part of the text). If it's not, we need to redraw
2591 * again. Mostly this just means scrolling up a few lines, so it
2592 * doesn't look too bad. Only do this for the current window (where
2593 * changes are relevant).
2594 */
2595 wp->w_valid |= VALID_BOTLINE;
2596 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2597 {
2598 recursive = TRUE;
2599 curwin->w_valid &= ~VALID_TOPLINE;
2600 update_topline(); /* may invalidate w_botline again */
2601 if (must_redraw != 0)
2602 {
2603 /* Don't update for changes in buffer again. */
2604 i = curbuf->b_mod_set;
2605 curbuf->b_mod_set = FALSE;
2606 win_update(curwin);
2607 must_redraw = 0;
2608 curbuf->b_mod_set = i;
2609 }
2610 recursive = FALSE;
2611 }
2612 }
2613
2614#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2615 /* restore got_int, unless CTRL-C was hit while redrawing */
2616 if (!got_int)
2617 got_int = save_got_int;
2618#endif
2619}
2620
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002622 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2623 * Return the new offset.
2624 */
2625 static int
2626screen_fill_end(
2627 win_T *wp,
2628 int c1,
2629 int c2,
2630 int off,
2631 int width,
2632 int row,
2633 int endrow,
2634 int attr)
2635{
2636 int nn = off + width;
2637
2638 if (nn > wp->w_width)
2639 nn = wp->w_width;
2640#ifdef FEAT_RIGHTLEFT
2641 if (wp->w_p_rl)
2642 {
2643 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2644 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2645 c1, c2, attr);
2646 }
2647 else
2648#endif
2649 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2650 wp->w_wincol + off, (int)wp->w_wincol + nn,
2651 c1, c2, attr);
2652 return nn;
2653}
2654
2655/*
2656 * Clear lines near the end the window and mark the unused lines with "c1".
2657 * use "c2" as the filler character.
2658 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 */
2660 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002661win_draw_end(
2662 win_T *wp,
2663 int c1,
2664 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002665 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002666 int row,
2667 int endrow,
2668 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002671 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002672 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002673
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002674 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002675
2676 if (draw_margin)
2677 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002678#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002679 int fdc = compute_foldcolumn(wp, 0);
2680
2681 if (fdc > 0)
2682 // draw the fold column
2683 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002684 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002685#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002686#ifdef FEAT_SIGNS
2687 if (signcolumn_on(wp))
2688 // draw the sign column
2689 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002690 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002691#endif
2692 if ((wp->w_p_nu || wp->w_p_rnu)
2693 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2694 // draw the number column
2695 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002696 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
2699#ifdef FEAT_RIGHTLEFT
2700 if (wp->w_p_rl)
2701 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002703 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002704 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002706 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002707 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
2709 else
2710#endif
2711 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002713 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002714 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002716
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 set_empty_rows(wp, row);
2718}
2719
Bram Moolenaar1a384422010-07-14 19:53:30 +02002720#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002721/*
2722 * Advance **color_cols and return TRUE when there are columns to draw.
2723 */
2724 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002725advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002726{
2727 while (**color_cols >= 0 && vcol > **color_cols)
2728 ++*color_cols;
2729 return (**color_cols >= 0);
2730}
2731#endif
2732
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002733#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2734/*
2735 * Copy "text" to ScreenLines using "attr".
2736 * Returns the next screen column.
2737 */
2738 static int
2739text_to_screenline(win_T *wp, char_u *text, int col)
2740{
2741 int off = (int)(current_ScreenLine - ScreenLines);
2742
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002743 if (has_mbyte)
2744 {
2745 int cells;
2746 int u8c, u8cc[MAX_MCO];
2747 int i;
2748 int idx;
2749 int c_len;
2750 char_u *p;
2751# ifdef FEAT_ARABIC
2752 int prev_c = 0; /* previous Arabic character */
2753 int prev_c1 = 0; /* first composing char for prev_c */
2754# endif
2755
2756# ifdef FEAT_RIGHTLEFT
2757 if (wp->w_p_rl)
2758 idx = off;
2759 else
2760# endif
2761 idx = off + col;
2762
2763 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2764 for (p = text; *p != NUL; )
2765 {
2766 cells = (*mb_ptr2cells)(p);
2767 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002768 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002769# ifdef FEAT_RIGHTLEFT
2770 - (wp->w_p_rl ? col : 0)
2771# endif
2772 )
2773 break;
2774 ScreenLines[idx] = *p;
2775 if (enc_utf8)
2776 {
2777 u8c = utfc_ptr2char(p, u8cc);
2778 if (*p < 0x80 && u8cc[0] == 0)
2779 {
2780 ScreenLinesUC[idx] = 0;
2781#ifdef FEAT_ARABIC
2782 prev_c = u8c;
2783#endif
2784 }
2785 else
2786 {
2787#ifdef FEAT_ARABIC
2788 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2789 {
2790 /* Do Arabic shaping. */
2791 int pc, pc1, nc;
2792 int pcc[MAX_MCO];
2793 int firstbyte = *p;
2794
2795 /* The idea of what is the previous and next
2796 * character depends on 'rightleft'. */
2797 if (wp->w_p_rl)
2798 {
2799 pc = prev_c;
2800 pc1 = prev_c1;
2801 nc = utf_ptr2char(p + c_len);
2802 prev_c1 = u8cc[0];
2803 }
2804 else
2805 {
2806 pc = utfc_ptr2char(p + c_len, pcc);
2807 nc = prev_c;
2808 pc1 = pcc[0];
2809 }
2810 prev_c = u8c;
2811
2812 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2813 pc, pc1, nc);
2814 ScreenLines[idx] = firstbyte;
2815 }
2816 else
2817 prev_c = u8c;
2818#endif
2819 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002820 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002821 for (i = 0; i < Screen_mco; ++i)
2822 {
2823 ScreenLinesC[i][idx] = u8cc[i];
2824 if (u8cc[i] == 0)
2825 break;
2826 }
2827 }
2828 if (cells > 1)
2829 ScreenLines[idx + 1] = 0;
2830 }
2831 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2832 /* double-byte single width character */
2833 ScreenLines2[idx] = p[1];
2834 else if (cells > 1)
2835 /* double-width character */
2836 ScreenLines[idx + 1] = p[1];
2837 col += cells;
2838 idx += cells;
2839 p += c_len;
2840 }
2841 }
2842 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002843 {
2844 int len = (int)STRLEN(text);
2845
Bram Moolenaar02631462017-09-22 15:20:32 +02002846 if (len > wp->w_width - col)
2847 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002848 if (len > 0)
2849 {
2850#ifdef FEAT_RIGHTLEFT
2851 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002852 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002853 else
2854#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002855 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002856 col += len;
2857 }
2858 }
2859 return col;
2860}
2861#endif
2862
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863#ifdef FEAT_FOLDING
2864/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002865 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2866 * space is available for window "wp", minus "col".
2867 */
2868 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002869compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002870{
2871 int fdc = wp->w_p_fdc;
2872 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002873 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002874
2875 if (fdc > wwidth - (col + wmw))
2876 fdc = wwidth - (col + wmw);
2877 return fdc;
2878}
2879
2880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 * Display one folded line.
2882 */
2883 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002884fold_line(
2885 win_T *wp,
2886 long fold_count,
2887 foldinfo_T *foldinfo,
2888 linenr_T lnum,
2889 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002891 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 pos_T *top, *bot;
2893 linenr_T lnume = lnum + fold_count - 1;
2894 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002895 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 int col;
2898 int txtcol;
2899 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002900 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901
2902 /* Build the fold line:
2903 * 1. Add the cmdwin_type for the command-line window
2904 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002905 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 * 4. Compose the text
2907 * 5. Add the text
2908 * 6. set highlighting for the Visual area an other text
2909 */
2910 col = 0;
2911
2912 /*
2913 * 1. Add the cmdwin_type for the command-line window
2914 * Ignores 'rightleft', this window is never right-left.
2915 */
2916#ifdef FEAT_CMDWIN
2917 if (cmdwin_type != 0 && wp == curwin)
2918 {
2919 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002920 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 if (enc_utf8)
2922 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 ++col;
2924 }
2925#endif
2926
2927 /*
2928 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002929 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002931 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 if (fdc > 0)
2933 {
2934 fill_foldcolumn(buf, wp, TRUE, lnum);
2935#ifdef FEAT_RIGHTLEFT
2936 if (wp->w_p_rl)
2937 {
2938 int i;
2939
Bram Moolenaar02631462017-09-22 15:20:32 +02002940 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002941 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 /* reverse the fold column */
2943 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002944 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 }
2946 else
2947#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002948 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 col += fdc;
2950 }
2951
2952#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002953# define RL_MEMSET(p, v, l) \
2954 do { \
2955 if (wp->w_p_rl) \
2956 for (ri = 0; ri < l; ++ri) \
2957 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2958 else \
2959 for (ri = 0; ri < l; ++ri) \
2960 ScreenAttrs[off + (p) + ri] = v; \
2961 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002963# define RL_MEMSET(p, v, l) \
2964 do { \
2965 for (ri = 0; ri < l; ++ri) \
2966 ScreenAttrs[off + (p) + ri] = v; \
2967 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968#endif
2969
Bram Moolenaar64486672010-05-16 15:46:46 +02002970 /* Set all attributes of the 'number' or 'relativenumber' column and the
2971 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002972 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974#ifdef FEAT_SIGNS
2975 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002976 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002978 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 if (len > 0)
2980 {
2981 if (len > 2)
2982 len = 2;
2983# ifdef FEAT_RIGHTLEFT
2984 if (wp->w_p_rl)
2985 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002986 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002987 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 else
2989# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002990 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 col += len;
2992 }
2993 }
2994#endif
2995
2996 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002997 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002999 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003001 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 if (len > 0)
3003 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003004 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02003005 long num;
3006 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003007
3008 if (len > w + 1)
3009 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02003010
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003011 if (wp->w_p_nu && !wp->w_p_rnu)
3012 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003013 num = (long)lnum;
3014 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003015 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003016 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003017 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003018 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003019 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003020 /* 'number' + 'relativenumber': cursor line shows absolute
3021 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003022 num = lnum;
3023 fmt = "%-*ld ";
3024 }
3025 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003026
Bram Moolenaar700e7342013-01-30 12:31:36 +01003027 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028#ifdef FEAT_RIGHTLEFT
3029 if (wp->w_p_rl)
3030 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02003031 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003032 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 else
3034#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01003035 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 col += len;
3037 }
3038 }
3039
3040 /*
3041 * 4. Compose the folded-line string with 'foldtext', if set.
3042 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003043 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044
3045 txtcol = col; /* remember where text starts */
3046
3047 /*
3048 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
3049 * Right-left text is put in columns 0 - number-col, normal text is put
3050 * in columns number-col - window-width.
3051 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003052 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
3054 /* Fill the rest of the line with the fold filler */
3055#ifdef FEAT_RIGHTLEFT
3056 if (wp->w_p_rl)
3057 col -= txtcol;
3058#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02003059 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060#ifdef FEAT_RIGHTLEFT
3061 - (wp->w_p_rl ? txtcol : 0)
3062#endif
3063 )
3064 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 if (enc_utf8)
3066 {
3067 if (fill_fold >= 0x80)
3068 {
3069 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003070 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01003071 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 }
3073 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02003074 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02003076 ScreenLines[off + col] = fill_fold;
3077 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02003078 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02003080 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02003081 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 }
3083
3084 if (text != buf)
3085 vim_free(text);
3086
3087 /*
3088 * 6. set highlighting for the Visual area an other text.
3089 * If all folded lines are in the Visual area, highlight the line.
3090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3092 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003093 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 {
3095 /* Visual is after curwin->w_cursor */
3096 top = &curwin->w_cursor;
3097 bot = &VIsual;
3098 }
3099 else
3100 {
3101 /* Visual is before curwin->w_cursor */
3102 top = &VIsual;
3103 bot = &curwin->w_cursor;
3104 }
3105 if (lnum >= top->lnum
3106 && lnume <= bot->lnum
3107 && (VIsual_mode != 'v'
3108 || ((lnum > top->lnum
3109 || (lnum == top->lnum
3110 && top->col == 0))
3111 && (lnume < bot->lnum
3112 || (lnume == bot->lnum
3113 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003114 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 {
3116 if (VIsual_mode == Ctrl_V)
3117 {
3118 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02003119 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02003121 if (wp->w_old_cursor_lcol != MAXCOL
3122 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02003123 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 len = wp->w_old_cursor_lcol;
3125 else
Bram Moolenaar02631462017-09-22 15:20:32 +02003126 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003127 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003128 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 }
3130 }
3131 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003132 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02003134 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 }
3137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003139#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003140 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
3141 if (wp->w_p_cc_cols)
3142 {
3143 int i = 0;
3144 int j = wp->w_p_cc_cols[i];
3145 int old_txtcol = txtcol;
3146
3147 while (j > -1)
3148 {
3149 txtcol += j;
3150 if (wp->w_p_wrap)
3151 txtcol -= wp->w_skipcol;
3152 else
3153 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003154 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003155 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003156 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003157 txtcol = old_txtcol;
3158 j = wp->w_p_cc_cols[++i];
3159 }
3160 }
3161
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003162 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00003163 if (wp->w_p_cuc)
3164 {
3165 txtcol += wp->w_virtcol;
3166 if (wp->w_p_wrap)
3167 txtcol -= wp->w_skipcol;
3168 else
3169 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003170 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00003171 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003172 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00003173 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
Bram Moolenaar02631462017-09-22 15:20:32 +02003176 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003177 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
3179 /*
3180 * Update w_cline_height and w_cline_folded if the cursor line was
3181 * updated (saves a call to plines() later).
3182 */
3183 if (wp == curwin
3184 && lnum <= curwin->w_cursor.lnum
3185 && lnume >= curwin->w_cursor.lnum)
3186 {
3187 curwin->w_cline_row = row;
3188 curwin->w_cline_height = 1;
3189 curwin->w_cline_folded = TRUE;
3190 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
3191 }
3192}
3193
3194/*
3195 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
3196 */
3197 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003198copy_text_attr(
3199 int off,
3200 char_u *buf,
3201 int len,
3202 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003204 int i;
3205
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 if (enc_utf8)
3208 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003209 for (i = 0; i < len; ++i)
3210 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211}
3212
3213/*
3214 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003215 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 */
3217 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003218fill_foldcolumn(
3219 char_u *p,
3220 win_T *wp,
3221 int closed, /* TRUE of FALSE */
3222 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
3224 int i = 0;
3225 int level;
3226 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003227 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003228 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
3230 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003231 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232
3233 level = win_foldinfo.fi_level;
3234 if (level > 0)
3235 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003236 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003237 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003238
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 /* If the column is too narrow, we start at the lowest level that
3240 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003241 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 if (first_level < 1)
3243 first_level = 1;
3244
Bram Moolenaar1c934292015-01-27 16:39:29 +01003245 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 {
3247 if (win_foldinfo.fi_lnum == lnum
3248 && first_level + i >= win_foldinfo.fi_low_level)
3249 p[i] = '-';
3250 else if (first_level == 1)
3251 p[i] = '|';
3252 else if (first_level + i <= 9)
3253 p[i] = '0' + first_level + i;
3254 else
3255 p[i] = '>';
3256 if (first_level + i == level)
3257 break;
3258 }
3259 }
3260 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003261 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262}
3263#endif /* FEAT_FOLDING */
3264
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003265#ifdef FEAT_TEXT_PROP
3266static textprop_T *current_text_props = NULL;
3267static buf_T *current_buf = NULL;
3268
3269 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003270text_prop_compare(const void *s1, const void *s2)
3271{
3272 int idx1, idx2;
3273 proptype_T *pt1, *pt2;
3274 colnr_T col1, col2;
3275
3276 idx1 = *(int *)s1;
3277 idx2 = *(int *)s2;
3278 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3279 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3280 if (pt1 == pt2)
3281 return 0;
3282 if (pt1 == NULL)
3283 return -1;
3284 if (pt2 == NULL)
3285 return 1;
3286 if (pt1->pt_priority != pt2->pt_priority)
3287 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3288 col1 = current_text_props[idx1].tp_col;
3289 col2 = current_text_props[idx2].tp_col;
3290 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3291}
3292#endif
3293
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294/*
3295 * Display line "lnum" of window 'wp' on the screen.
3296 * Start at row "startrow", stop when "endrow" is reached.
3297 * wp->w_virtcol needs to be valid.
3298 *
3299 * Return the number of last row the line occupies.
3300 */
3301 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003302win_line(
3303 win_T *wp,
3304 linenr_T lnum,
3305 int startrow,
3306 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003307 int nochange UNUSED, // not updating for changed text
3308 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003310 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3312 int c = 0; /* init for GCC */
3313 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003314#ifdef FEAT_LINEBREAK
3315 long vcol_sbr = -1; /* virtual column after showbreak */
3316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 long vcol_prev = -1; /* "vcol" of previous character */
3318 char_u *line; /* current line */
3319 char_u *ptr; /* current position in "line" */
3320 int row; /* row in the window, excl w_winrow */
3321 int screen_row; /* row on the screen, incl w_winrow */
3322
3323 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3324 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003325 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003326 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003328 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 int extra_attr = 0; /* attributes when n_extra != 0 */
3330 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3331 displaying lcs_eol at end-of-line */
3332 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3333 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3334
3335 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3336 int saved_n_extra = 0;
3337 char_u *saved_p_extra = NULL;
3338 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003339 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 int saved_char_attr = 0;
3341
3342 int n_attr = 0; /* chars with special attr */
3343 int saved_attr2 = 0; /* char_attr saved for n_attr */
3344 int n_attr3 = 0; /* chars with overruling special attr */
3345 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3346
3347 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3348
3349 int fromcol, tocol; /* start/end of inverting */
3350 int fromcol_prev = -2; /* start of inverting after cursor */
3351 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003353 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 pos_T pos;
3355 long v;
3356
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003357 int char_attr = 0; // attributes for next character
3358 int attr_pri = FALSE; // char_attr has priority
3359 int area_highlighting = FALSE; // Visual or incsearch highlighting
3360 // in this line
3361 int vi_attr = 0; // attributes for Visual and incsearch
3362 // highlighting
3363 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003364 int win_attr = 0; // background for whole window, except
3365 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003366 int area_attr = 0; // attributes desired by highlighting
3367 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003369 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 int syntax_attr = 0; /* attributes desired by syntax */
3371 int has_syntax = FALSE; /* this buffer has syntax highl. */
3372 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003373 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003374 int draw_color_col = FALSE; /* highlight colorcolumn */
3375 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003376#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003377#ifdef FEAT_TEXT_PROP
3378 int text_prop_count;
3379 int text_prop_next = 0; // next text property to use
3380 textprop_T *text_props = NULL;
3381 int *text_prop_idxs = NULL;
3382 int text_props_active = 0;
3383 proptype_T *text_prop_type = NULL;
3384 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003385 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003386#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003387#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003388 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003389# define SPWORDLEN 150
3390 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003391 int nextlinecol = 0; /* column where nextline[] starts */
3392 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003393 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003394 int spell_attr = 0; /* attributes desired by spelling */
3395 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003396 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3397 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003398 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003399 static int cap_col = -1; /* column to check for Cap word */
3400 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003401 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003403 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 int multi_attr = 0; /* attributes desired by multibyte */
3405 int mb_l = 1; /* multi-byte byte length */
3406 int mb_c = 0; /* decoded multi-byte character */
3407 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003408 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409#ifdef FEAT_DIFF
3410 int filler_lines; /* nr of filler lines to be drawn */
3411 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003412 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 int change_start = MAXCOL; /* first col of changed area */
3414 int change_end = -1; /* last col of changed area */
3415#endif
3416 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3417#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003418 int need_showbreak = FALSE; /* overlong line, skipping first x
3419 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003421#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003422 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003424 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425#endif
3426#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003427 matchitem_T *cur; /* points to the match list */
3428 match_T *shl; /* points to search_hl or a match */
3429 int shl_flag; /* flag to indicate whether search_hl
3430 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003431 int pos_inprogress; /* marks that position match search is
3432 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003433 int prevcol_hl_flag; /* flag to indicate whether prevcol
3434 equals startcol of search_hl or one
3435 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436#endif
3437#ifdef FEAT_ARABIC
3438 int prev_c = 0; /* previous Arabic character */
3439 int prev_c1 = 0; /* first composing char for prev_c */
3440#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003441#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003442 int did_line_attr = 0;
3443#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003444#ifdef FEAT_TERMINAL
3445 int get_term_attr = FALSE;
3446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
3448 /* draw_state: items that are drawn in sequence: */
3449#define WL_START 0 /* nothing done yet */
3450#ifdef FEAT_CMDWIN
3451# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3452#else
3453# define WL_CMDLINE WL_START
3454#endif
3455#ifdef FEAT_FOLDING
3456# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3457#else
3458# define WL_FOLD WL_CMDLINE
3459#endif
3460#ifdef FEAT_SIGNS
3461# define WL_SIGN WL_FOLD + 1 /* column for signs */
3462#else
3463# define WL_SIGN WL_FOLD /* column for signs */
3464#endif
3465#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003466#ifdef FEAT_LINEBREAK
3467# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003469# define WL_BRI WL_NR
3470#endif
3471#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3472# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3473#else
3474# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475#endif
3476#define WL_LINE WL_SBR + 1 /* text in the line */
3477 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003478#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 int feedback_col = 0;
3480 int feedback_old_attr = -1;
3481#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003482 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483
Bram Moolenaar860cae12010-06-05 23:22:07 +02003484#ifdef FEAT_CONCEAL
3485 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003486 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003487 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003488 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003489 int is_concealing = FALSE;
3490 int boguscols = 0; /* nonexistent columns added to force
3491 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003492 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003493 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003494 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003495 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003496# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003497# define FIX_FOR_BOGUSCOLS \
3498 { \
3499 n_extra += vcol_off; \
3500 vcol -= vcol_off; \
3501 vcol_off = 0; \
3502 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003503 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003504 boguscols = 0; \
3505 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003506#else
3507# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
3510 if (startrow > endrow) /* past the end already! */
3511 return startrow;
3512
3513 row = startrow;
3514 screen_row = row + W_WINROW(wp);
3515
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003516 if (!number_only)
3517 {
3518 /*
3519 * To speed up the loop below, set extra_check when there is linebreak,
3520 * trailing white space and/or syntax processing to be done.
3521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003523 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524#endif
3525#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003526 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003527# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003528 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003529# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003530 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003532 /* Prepare for syntax highlighting in this line. When there is an
3533 * error, stop syntax highlighting. */
3534 save_did_emsg = did_emsg;
3535 did_emsg = FALSE;
3536 syntax_start(wp, lnum);
3537 if (did_emsg)
3538 wp->w_s->b_syn_error = TRUE;
3539 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003540 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003541 did_emsg = save_did_emsg;
3542#ifdef SYN_TIME_LIMIT
3543 if (!wp->w_s->b_syn_slow)
3544#endif
3545 {
3546 has_syntax = TRUE;
3547 extra_check = TRUE;
3548 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003551
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003552 /* Check for columns to display for 'colorcolumn'. */
3553 color_cols = wp->w_p_cc_cols;
3554 if (color_cols != NULL)
3555 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003556#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003557
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003558#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003559 if (term_show_buffer(wp->w_buffer))
3560 {
3561 extra_check = TRUE;
3562 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003563 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003564 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003565#endif
3566
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003567#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003568 if (wp->w_p_spell
3569 && *wp->w_s->b_p_spl != NUL
3570 && wp->w_s->b_langp.ga_len > 0
3571 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003572 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003573 /* Prepare for spell checking. */
3574 has_spell = TRUE;
3575 extra_check = TRUE;
3576
Bram Moolenaar7701f302018-10-02 21:20:32 +02003577 /* Get the start of the next line, so that words that wrap to the
3578 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003579 * Trick: skip a few chars for C/shell/Vim comments */
3580 nextline[SPWORDLEN] = NUL;
3581 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3582 {
3583 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3584 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3585 }
3586
Bram Moolenaar7701f302018-10-02 21:20:32 +02003587 /* When a word wrapped from the previous line the start of the
3588 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003589 if (lnum == checked_lnum)
3590 cur_checked_col = checked_col;
3591 checked_lnum = 0;
3592
3593 /* When there was a sentence end in the previous line may require a
3594 * word starting with capital in this line. In line 1 always check
3595 * the first word. */
3596 if (lnum != capcol_lnum)
3597 cap_col = -1;
3598 if (lnum == 1)
3599 cap_col = 0;
3600 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602#endif
3603
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003604 /*
3605 * handle visual active in this window
3606 */
3607 fromcol = -10;
3608 tocol = MAXCOL;
3609 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003611 /* Visual is after curwin->w_cursor */
3612 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003614 top = &curwin->w_cursor;
3615 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003617 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003619 top = &VIsual;
3620 bot = &curwin->w_cursor;
3621 }
3622 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3623 if (VIsual_mode == Ctrl_V) /* block mode */
3624 {
3625 if (lnum_in_visual_area)
3626 {
3627 fromcol = wp->w_old_cursor_fcol;
3628 tocol = wp->w_old_cursor_lcol;
3629 }
3630 }
3631 else /* non-block mode */
3632 {
3633 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003635 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003637 if (VIsual_mode == 'V') /* linewise */
3638 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 else
3640 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003641 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3642 if (gchar_pos(top) == NUL)
3643 tocol = fromcol + 1;
3644 }
3645 }
3646 if (VIsual_mode != 'V' && lnum == bot->lnum)
3647 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003648 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003649 {
3650 fromcol = -10;
3651 tocol = MAXCOL;
3652 }
3653 else if (bot->col == MAXCOL)
3654 tocol = MAXCOL;
3655 else
3656 {
3657 pos = *bot;
3658 if (*p_sel == 'e')
3659 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3660 else
3661 {
3662 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3663 ++tocol;
3664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 }
3666 }
3667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003669 /* Check if the character under the cursor should not be inverted */
3670 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003671#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003672 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003673#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003674 )
3675 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003677 /* if inverting in this line set area_highlighting */
3678 if (fromcol >= 0)
3679 {
3680 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003681 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003683 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003684 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003685 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003686 && clip_isautosel_plus()))
3687 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003692 /*
3693 * handle 'incsearch' and ":s///c" highlighting
3694 */
3695 else if (highlight_match
3696 && wp == curwin
3697 && lnum >= curwin->w_cursor.lnum
3698 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003700 if (lnum == curwin->w_cursor.lnum)
3701 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003702 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003703 else
3704 fromcol = 0;
3705 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3706 {
3707 pos.lnum = lnum;
3708 pos.col = search_match_endcol;
3709 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3710 }
3711 else
3712 tocol = MAXCOL;
3713 /* do at least one character; happens when past end of line */
3714 if (fromcol == tocol)
3715 tocol = fromcol + 1;
3716 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003717 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720
3721#ifdef FEAT_DIFF
3722 filler_lines = diff_check(wp, lnum);
3723 if (filler_lines < 0)
3724 {
3725 if (filler_lines == -1)
3726 {
3727 if (diff_find_change(wp, lnum, &change_start, &change_end))
3728 diff_hlf = HLF_ADD; /* added line */
3729 else if (change_start == 0)
3730 diff_hlf = HLF_TXD; /* changed text */
3731 else
3732 diff_hlf = HLF_CHD; /* changed line */
3733 }
3734 else
3735 diff_hlf = HLF_ADD; /* added line */
3736 filler_lines = 0;
3737 area_highlighting = TRUE;
3738 }
3739 if (lnum == wp->w_topline)
3740 filler_lines = wp->w_topfill;
3741 filler_todo = filler_lines;
3742#endif
3743
3744#ifdef LINE_ATTR
3745# ifdef FEAT_SIGNS
3746 /* If this line has a sign with line highlighting set line_attr. */
3747 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3748 if (v != 0)
3749 line_attr = sign_get_attr((int)v, TRUE);
3750# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003751# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003753 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003754 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755# endif
3756 if (line_attr != 0)
3757 area_highlighting = TRUE;
3758#endif
3759
3760 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3761 ptr = line;
3762
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003763#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003764 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003765 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003766 /* For checking first word with a capital skip white space. */
3767 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003768 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003769
Bram Moolenaar30abd282005-06-22 22:35:10 +00003770 /* To be able to spell-check over line boundaries copy the end of the
3771 * current line into nextline[]. Above the start of the next line was
3772 * copied to nextline[SPWORDLEN]. */
3773 if (nextline[SPWORDLEN] == NUL)
3774 {
3775 /* No next line or it is empty. */
3776 nextlinecol = MAXCOL;
3777 nextline_idx = 0;
3778 }
3779 else
3780 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003781 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003782 if (v < SPWORDLEN)
3783 {
3784 /* Short line, use it completely and append the start of the
3785 * next line. */
3786 nextlinecol = 0;
3787 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003788 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003789 nextline_idx = v + 1;
3790 }
3791 else
3792 {
3793 /* Long line, use only the last SPWORDLEN bytes. */
3794 nextlinecol = v - SPWORDLEN;
3795 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3796 nextline_idx = SPWORDLEN + 1;
3797 }
3798 }
3799 }
3800#endif
3801
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003802 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003804 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003805 extra_check = TRUE;
3806 /* find start of trailing whitespace */
3807 if (lcs_trail)
3808 {
3809 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003810 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003811 --trailcol;
3812 trailcol += (colnr_T) (ptr - line);
3813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 }
3815
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003816 wcr_attr = get_wcr_attr(wp);
3817 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003818 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003819 win_attr = wcr_attr;
3820 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003821 }
3822#ifdef FEAT_TEXT_PROP
3823 if (bt_popup(wp->w_buffer))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003824 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003825#endif
3826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 /*
3828 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3829 * first character to be displayed.
3830 */
3831 if (wp->w_p_wrap)
3832 v = wp->w_skipcol;
3833 else
3834 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003835 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003838
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 while (vcol < v && *ptr != NUL)
3840 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003841 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003844 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 }
3846
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003847 /* When:
3848 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003849 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003850 * - 'virtualedit' is set, or
3851 * - the visual mode is active,
3852 * the end of the line may be before the start of the displayed part.
3853 */
3854 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003855#ifdef FEAT_SYN_HL
3856 wp->w_p_cuc || draw_color_col ||
3857#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003858 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003859 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
3862 /* Handle a character that's not completely on the screen: Put ptr at
3863 * that character but skip the first few screen characters. */
3864 if (vcol > v)
3865 {
3866 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003868 /* If the character fits on the screen, don't need to skip it.
3869 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003870 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003871 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
3873
3874 /*
3875 * Adjust for when the inverted text is before the screen,
3876 * and when the start of the inverted text is before the screen.
3877 */
3878 if (tocol <= vcol)
3879 fromcol = 0;
3880 else if (fromcol >= 0 && fromcol < vcol)
3881 fromcol = vcol;
3882
3883#ifdef FEAT_LINEBREAK
3884 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3885 if (wp->w_p_wrap)
3886 need_showbreak = TRUE;
3887#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003888#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003889 /* When spell checking a word we need to figure out the start of the
3890 * word and if it's badly spelled or not. */
3891 if (has_spell)
3892 {
3893 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003894 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003895 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003896
3897 pos = wp->w_cursor;
3898 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003899 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003900 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003901
3902 /* spell_move_to() may call ml_get() and make "line" invalid */
3903 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3904 ptr = line + linecol;
3905
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003906 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003907 {
3908 /* no bad word found at line start, don't check until end of a
3909 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003910 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003911 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003912 }
3913 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003914 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003915 /* bad word found, use attributes until end of word */
3916 word_end = wp->w_cursor.col + len + 1;
3917
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003918 /* Turn index into actual attributes. */
3919 if (spell_hlf != HLF_COUNT)
3920 spell_attr = highlight_attr[spell_hlf];
3921 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003922 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003923
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003924# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003925 /* Need to restart syntax highlighting for this line. */
3926 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003927 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003928# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003929 }
3930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
3932
3933 /*
3934 * Correct highlighting for cursor that can't be disabled.
3935 * Avoids having to check this for each character.
3936 */
3937 if (fromcol >= 0)
3938 {
3939 if (noinvcur)
3940 {
3941 if ((colnr_T)fromcol == wp->w_virtcol)
3942 {
3943 /* highlighting starts at cursor, let it start just after the
3944 * cursor */
3945 fromcol_prev = fromcol;
3946 fromcol = -1;
3947 }
3948 else if ((colnr_T)fromcol < wp->w_virtcol)
3949 /* restart highlighting after the cursor */
3950 fromcol_prev = wp->w_virtcol;
3951 }
3952 if (fromcol >= tocol)
3953 fromcol = -1;
3954 }
3955
3956#ifdef FEAT_SEARCH_EXTRA
3957 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003958 * Handle highlighting the last used search pattern and matches.
3959 * Do this for both search_hl and the match list.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003960 * Not in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003962 cur = wp->w_match_head;
3963 shl_flag = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003964 while ((cur != NULL || shl_flag == FALSE) && !number_only
3965 && !(screen_line_flags & SLF_POPUP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003967 if (shl_flag == FALSE)
3968 {
3969 shl = &search_hl;
3970 shl_flag = TRUE;
3971 }
3972 else
3973 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003974 shl->startcol = MAXCOL;
3975 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003977 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003978 v = (long)(ptr - line);
3979 if (cur != NULL)
3980 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003981 next_search_hl(wp, shl, lnum, (colnr_T)v,
3982 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003983
3984 /* Need to get the line again, a multi-line regexp may have made it
3985 * invalid. */
3986 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3987 ptr = line + v;
3988
3989 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003991 if (shl->lnum == lnum)
3992 shl->startcol = shl->rm.startpos[0].col;
3993 else
3994 shl->startcol = 0;
3995 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3996 - shl->rm.startpos[0].lnum)
3997 shl->endcol = shl->rm.endpos[0].col;
3998 else
3999 shl->endcol = MAXCOL;
4000 /* Highlight one character for an empty match. */
4001 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02004003 if (has_mbyte && line[shl->endcol] != NUL)
4004 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
4005 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02004006 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02004008 if ((long)shl->startcol < v) /* match at leftcol */
4009 {
4010 shl->attr_cur = shl->attr;
4011 search_attr = shl->attr;
4012 }
4013 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004015 if (shl != &search_hl && cur != NULL)
4016 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 }
4018#endif
4019
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004020#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01004021 // Cursor line highlighting for 'cursorline' in the current window.
4022 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004023 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01004024 // Do not show the cursor line when Visual mode is active, because it's
4025 // not clear what is selected then. Do update w_last_cursorline.
4026 if (!(wp == curwin && VIsual_active))
4027 {
4028 line_attr = HL_ATTR(HLF_CUL);
4029 area_highlighting = TRUE;
4030 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01004031 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004032 }
4033#endif
4034
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004035#ifdef FEAT_TEXT_PROP
4036 {
4037 char_u *prop_start;
4038
4039 text_prop_count = get_text_props(wp->w_buffer, lnum,
4040 &prop_start, FALSE);
4041 if (text_prop_count > 0)
4042 {
4043 // Make a copy of the properties, so that they are properly
4044 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004045 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004046 if (text_props != NULL)
4047 mch_memmove(text_props, prop_start,
4048 text_prop_count * sizeof(textprop_T));
4049
4050 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004051 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004052 area_highlighting = TRUE;
4053 extra_check = TRUE;
4054 }
4055 }
4056#endif
4057
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004058 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004060
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061#ifdef FEAT_RIGHTLEFT
4062 if (wp->w_p_rl)
4063 {
4064 /* Rightleft window: process the text in the normal direction, but put
4065 * it in current_ScreenLine[] from right to left. Start at the
4066 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02004067 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004069 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
4071#endif
4072
4073 /*
4074 * Repeat for the whole displayed line.
4075 */
4076 for (;;)
4077 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02004078#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004079 int has_match_conc = 0; // match wants to conceal
4080 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 /* Skip this quickly when working on the text. */
4083 if (draw_state != WL_LINE)
4084 {
4085#ifdef FEAT_CMDWIN
4086 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
4087 {
4088 draw_state = WL_CMDLINE;
4089 if (cmdwin_type != 0 && wp == curwin)
4090 {
4091 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00004093 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004094 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004095 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097 }
4098#endif
4099
4100#ifdef FEAT_FOLDING
4101 if (draw_state == WL_FOLD - 1 && n_extra == 0)
4102 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01004103 int fdc = compute_foldcolumn(wp, 0);
4104
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01004106 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 {
Bram Moolenaar62706602016-12-09 19:28:48 +01004108 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004109 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004110 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01004111 p_extra_free = alloc(12 + 1);
4112
4113 if (p_extra_free != NULL)
4114 {
4115 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
4116 n_extra = fdc;
4117 p_extra_free[n_extra] = NUL;
4118 p_extra = p_extra_free;
4119 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004120 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004121 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01004122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124 }
4125#endif
4126
4127#ifdef FEAT_SIGNS
4128 if (draw_state == WL_SIGN - 1 && n_extra == 0)
4129 {
4130 draw_state = WL_SIGN;
4131 /* Show the sign column when there are any signs in this
4132 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02004133 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004135 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004137 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138# endif
4139
4140 /* Draw two cells with the sign value or blank. */
4141 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004142 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004143 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 n_extra = 2;
4145
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02004146 if (row == startrow
4147#ifdef FEAT_DIFF
4148 + filler_lines && filler_todo <= 0
4149#endif
4150 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 {
4152 text_sign = buf_getsigntype(wp->w_buffer, lnum,
4153 SIGN_TEXT);
4154# ifdef FEAT_SIGN_ICONS
4155 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
4156 SIGN_ICON);
4157 if (gui.in_use && icon_sign != 0)
4158 {
4159 /* Use the image in this position. */
4160 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004161 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162# ifdef FEAT_NETBEANS_INTG
4163 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004164 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004166 c_final = NUL;
4167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168# endif
4169 char_attr = icon_sign;
4170 }
4171 else
4172# endif
4173 if (text_sign != 0)
4174 {
4175 p_extra = sign_get_text(text_sign);
4176 if (p_extra != NULL)
4177 {
4178 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004179 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004180 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
4182 char_attr = sign_get_attr(text_sign, FALSE);
4183 }
4184 }
4185 }
4186 }
4187#endif
4188
4189 if (draw_state == WL_NR - 1 && n_extra == 0)
4190 {
4191 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02004192 /* Display the absolute or relative line number. After the
4193 * first fill with blanks when the 'n' flag isn't in 'cpo' */
4194 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 && (row == startrow
4196#ifdef FEAT_DIFF
4197 + filler_lines
4198#endif
4199 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
4200 {
4201 /* Draw the line number (empty space after wrapping). */
4202 if (row == startrow
4203#ifdef FEAT_DIFF
4204 + filler_lines
4205#endif
4206 )
4207 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004208 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01004209 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02004210
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004211 if (wp->w_p_nu && !wp->w_p_rnu)
4212 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02004213 num = (long)lnum;
4214 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01004215 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004216 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01004217 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004218 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004219 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004220 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004221 num = lnum;
4222 fmt = "%-*ld ";
4223 }
4224 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004225
Bram Moolenaar700e7342013-01-30 12:31:36 +01004226 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004227 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 if (wp->w_skipcol > 0)
4229 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4230 *p_extra = '-';
4231#ifdef FEAT_RIGHTLEFT
4232 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004233 {
4234 char_u *p1, *p2;
4235 int t;
4236
4237 // like rl_mirror(), but keep the space at the end
4238 p2 = skiptowhite(extra) - 1;
4239 for (p1 = extra; p1 < p2; ++p1, --p2)
4240 {
4241 t = *p1;
4242 *p1 = *p2;
4243 *p2 = t;
4244 }
4245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246#endif
4247 p_extra = extra;
4248 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004249 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
4251 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004254 c_final = NUL;
4255 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004256 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004257 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004258#ifdef FEAT_SYN_HL
4259 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004260 * the current line differently.
4261 * TODO: Can we use CursorLine instead of CursorLineNr
4262 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004263 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004264 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004265 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
4268 }
4269
Bram Moolenaar597a4222014-06-25 14:39:50 +02004270#ifdef FEAT_LINEBREAK
4271 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4272 && n_extra == 0 && *p_sbr != NUL)
4273 /* draw indent after showbreak value */
4274 draw_state = WL_BRI;
4275 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4276 /* After the showbreak, draw the breakindent */
4277 draw_state = WL_BRI - 1;
4278
4279 /* draw 'breakindent': indent wrapped text accordingly */
4280 if (draw_state == WL_BRI - 1 && n_extra == 0)
4281 {
4282 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004283 /* if need_showbreak is set, breakindent also applies */
4284 if (wp->w_p_bri && n_extra == 0
4285 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004286# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004287 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004288# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004289 )
4290 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004291 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004292# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004293 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004294 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004295 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004296# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004297 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4298 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004299 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004300# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004301 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004302# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004303 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004304 c_extra = ' ';
4305 n_extra = get_breakindent_win(wp,
4306 ml_get_buf(wp->w_buffer, lnum, FALSE));
4307 /* Correct end of highlighted area for 'breakindent',
4308 * required when 'linebreak' is also set. */
4309 if (tocol == vcol)
4310 tocol += n_extra;
4311 }
4312 }
4313#endif
4314
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4316 if (draw_state == WL_SBR - 1 && n_extra == 0)
4317 {
4318 draw_state = WL_SBR;
4319# ifdef FEAT_DIFF
4320 if (filler_todo > 0)
4321 {
4322 /* Draw "deleted" diff line(s). */
4323 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004324 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004326 c_final = NUL;
4327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004331 c_final = NUL;
4332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333# ifdef FEAT_RIGHTLEFT
4334 if (wp->w_p_rl)
4335 n_extra = col + 1;
4336 else
4337# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004338 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004339 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 }
4341# endif
4342# ifdef FEAT_LINEBREAK
4343 if (*p_sbr != NUL && need_showbreak)
4344 {
4345 /* Draw 'showbreak' at the start of each broken line. */
4346 p_extra = p_sbr;
4347 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004348 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004350 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004352 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 /* Correct end of highlighted area for 'showbreak',
4354 * required when 'linebreak' is also set. */
4355 if (tocol == vcol)
4356 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004357#ifdef FEAT_SYN_HL
4358 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004359 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004360 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004361 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
4364# endif
4365 }
4366#endif
4367
4368 if (draw_state == WL_LINE - 1 && n_extra == 0)
4369 {
4370 draw_state = WL_LINE;
4371 if (saved_n_extra)
4372 {
4373 /* Continue item from end of wrapped line. */
4374 n_extra = saved_n_extra;
4375 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004376 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 p_extra = saved_p_extra;
4378 char_attr = saved_char_attr;
4379 }
4380 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004381 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
4383 }
4384
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004385 // When still displaying '$' of change command, stop at cursor.
4386 // When only displaying the (relative) line number and that's done,
4387 // stop here.
4388 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004389 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390#ifdef FEAT_DIFF
4391 && filler_todo <= 0
4392#endif
4393 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004394 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004396 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004397 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004398 /* Pretend we have finished updating the window. Except when
4399 * 'cursorcolumn' is set. */
4400#ifdef FEAT_SYN_HL
4401 if (wp->w_p_cuc)
4402 row = wp->w_cline_row + wp->w_cline_height;
4403 else
4404#endif
4405 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 break;
4407 }
4408
Bram Moolenaar637532b2019-01-03 21:44:40 +01004409 if (draw_state == WL_LINE && (area_highlighting
4410#ifdef FEAT_SPELL
4411 || has_spell
4412#endif
4413 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 {
4415 /* handle Visual or match highlighting in this line */
4416 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4418 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004420 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004422 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 else if (area_attr != 0
4424 && (vcol == tocol
4425 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
4428#ifdef FEAT_SEARCH_EXTRA
4429 if (!n_extra)
4430 {
4431 /*
4432 * Check for start/end of search pattern match.
4433 * After end, check for start/end of next match.
4434 * When another match, have to check for start again.
4435 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004436 * Do this for 'search_hl' and the match list (ordered by
4437 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004439 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004440 cur = wp->w_match_head;
4441 shl_flag = FALSE;
4442 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004444 if (shl_flag == FALSE
4445 && ((cur != NULL
4446 && cur->priority > SEARCH_HL_PRIORITY)
4447 || cur == NULL))
4448 {
4449 shl = &search_hl;
4450 shl_flag = TRUE;
4451 }
4452 else
4453 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004454 if (cur != NULL)
4455 cur->pos.cur = 0;
4456 pos_inprogress = TRUE;
4457 while (shl->rm.regprog != NULL
4458 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004460 if (shl->startcol != MAXCOL
4461 && v >= (long)shl->startcol
4462 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004464 int tmp_col = v + MB_PTR2LEN(ptr);
4465
4466 if (shl->endcol < tmp_col)
4467 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004469#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004470 // Match with the "Conceal" group results in hiding
4471 // the match.
4472 if (cur != NULL
4473 && shl != &search_hl
4474 && syn_name2id((char_u *)"Conceal")
4475 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004476 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004477 has_match_conc =
4478 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004479 match_conc = cur->conceal_char;
4480 }
4481 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004482 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004485 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 {
4487 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004488 next_search_hl(wp, shl, lnum, (colnr_T)v,
4489 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004490 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004491 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492
4493 /* Need to get the line again, a multi-line regexp
4494 * may have made it invalid. */
4495 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4496 ptr = line + v;
4497
4498 if (shl->lnum == lnum)
4499 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004500 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004502 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004504 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004506 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 {
4508 /* highlight empty match, try again after
4509 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004511 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004512 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004514 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 }
4516
4517 /* Loop to check if the match starts at the
4518 * current position */
4519 continue;
4520 }
4521 }
4522 break;
4523 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004524 if (shl != &search_hl && cur != NULL)
4525 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004527
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004528 /* Use attributes from match with highest priority among
4529 * 'search_hl' and the match list. */
4530 search_attr = search_hl.attr_cur;
4531 cur = wp->w_match_head;
4532 shl_flag = FALSE;
4533 while (cur != NULL || shl_flag == FALSE)
4534 {
4535 if (shl_flag == FALSE
4536 && ((cur != NULL
4537 && cur->priority > SEARCH_HL_PRIORITY)
4538 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004539 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004540 shl = &search_hl;
4541 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004542 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004543 else
4544 shl = &cur->hl;
4545 if (shl->attr_cur != 0)
4546 search_attr = shl->attr_cur;
4547 if (shl != &search_hl && cur != NULL)
4548 cur = cur->next;
4549 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004550 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004551 if (*ptr == NUL && (did_line_attr >= 1
4552 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004553 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
4555#endif
4556
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004558 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004560 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4561 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004563 if (diff_hlf == HLF_TXD && ptr - line > change_end
4564 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004566 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004567 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004568 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 }
4570#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004571
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004572#ifdef FEAT_TEXT_PROP
4573 if (text_props != NULL)
4574 {
4575 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004576 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004577
4578 // Check if any active property ends.
4579 for (pi = 0; pi < text_props_active; ++pi)
4580 {
4581 int tpi = text_prop_idxs[pi];
4582
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004583 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004584 + text_props[tpi].tp_len)
4585 {
4586 if (pi + 1 < text_props_active)
4587 mch_memmove(text_prop_idxs + pi,
4588 text_prop_idxs + pi + 1,
4589 sizeof(int)
4590 * (text_props_active - (pi + 1)));
4591 --text_props_active;
4592 --pi;
4593 }
4594 }
4595
4596 // Add any text property that starts in this column.
4597 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004598 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004599 text_prop_idxs[text_props_active++] = text_prop_next++;
4600
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004601 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004602 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004603 if (text_props_active > 0)
4604 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004605 // Sort the properties on priority and/or starting last.
4606 // Then combine the attributes, highest priority last.
4607 current_text_props = text_props;
4608 current_buf = wp->w_buffer;
4609 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4610 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004611
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004612 for (pi = 0; pi < text_props_active; ++pi)
4613 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004614 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004615 proptype_T *pt = text_prop_type_by_id(
4616 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004617
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004618 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004619 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004620 int pt_attr = syn_id2attr(pt->pt_hl_id);
4621
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004622 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004623 text_prop_attr =
4624 hl_combine_attr(text_prop_attr, pt_attr);
4625 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004626 }
4627 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004628 }
4629 }
4630#endif
4631
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004632 /* Decide which of the highlight attributes to use. */
4633 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004634#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004635 if (area_attr != 0)
4636 char_attr = hl_combine_attr(line_attr, area_attr);
4637 else if (search_attr != 0)
4638 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004639# ifdef FEAT_TEXT_PROP
4640 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004641 {
4642 char_attr = hl_combine_attr(
4643 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4644 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004645# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004646 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004647 || vcol < fromcol || vcol_prev < fromcol_prev
4648 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004649 // Use line_attr when not in the Visual or 'incsearch' area
4650 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004651 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004652#else
4653 if (area_attr != 0)
4654 char_attr = area_attr;
4655 else if (search_attr != 0)
4656 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004657#endif
4658 else
4659 {
4660 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004661#ifdef FEAT_TEXT_PROP
4662 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004663 {
4664 if (text_prop_combine)
4665 char_attr = hl_combine_attr(
4666 syntax_attr, text_prop_attr);
4667 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004668 char_attr = hl_combine_attr(
4669 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004670 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004671 else
4672#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004673#ifdef FEAT_SYN_HL
4674 if (has_syntax)
4675 char_attr = syntax_attr;
4676 else
4677#endif
4678 char_attr = 0;
4679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004681 if (char_attr == 0)
4682 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683
4684 /*
4685 * Get the next character to put on the screen.
4686 */
4687 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004688 * The "p_extra" points to the extra stuff that is inserted to
4689 * represent special characters (non-printable stuff) and other
4690 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004691 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004692 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4693 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4695 */
4696 if (n_extra > 0)
4697 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004698 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004700 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004702 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
4704 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004705 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004706 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 }
4708 else
4709 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 }
4711 else
4712 {
4713 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 if (has_mbyte)
4715 {
4716 mb_c = c;
4717 if (enc_utf8)
4718 {
4719 /* If the UTF-8 character is more than one byte:
4720 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004721 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 mb_utf8 = FALSE;
4723 if (mb_l > n_extra)
4724 mb_l = 1;
4725 else if (mb_l > 1)
4726 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004727 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004729 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
4731 }
4732 else
4733 {
4734 /* if this is a DBCS character, put it in "mb_c" */
4735 mb_l = MB_BYTE2LEN(c);
4736 if (mb_l >= n_extra)
4737 mb_l = 1;
4738 else if (mb_l > 1)
4739 mb_c = (c << 8) + p_extra[1];
4740 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004741 if (mb_l == 0) /* at the NUL at end-of-line */
4742 mb_l = 1;
4743
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 /* If a double-width char doesn't fit display a '>' in the
4745 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004746 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747# ifdef FEAT_RIGHTLEFT
4748 wp->w_p_rl ? (col <= 0) :
4749# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004750 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 && (*mb_char2cells)(mb_c) == 2)
4752 {
4753 c = '>';
4754 mb_c = c;
4755 mb_l = 1;
4756 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004757 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 /* put the pointer back to output the double-width
4759 * character at the start of the next line. */
4760 ++n_extra;
4761 --p_extra;
4762 }
4763 else
4764 {
4765 n_extra -= mb_l - 1;
4766 p_extra += mb_l - 1;
4767 }
4768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 ++p_extra;
4770 }
4771 --n_extra;
4772 }
4773 else
4774 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004775#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004776 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004777#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004778
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004779 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004780 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 /*
4782 * Get a character from the line itself.
4783 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004784 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004785#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004786 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 if (has_mbyte)
4789 {
4790 mb_c = c;
4791 if (enc_utf8)
4792 {
4793 /* If the UTF-8 character is more than one byte: Decode it
4794 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004795 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 mb_utf8 = FALSE;
4797 if (mb_l > 1)
4798 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004799 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 /* Overlong encoded ASCII or ASCII with composing char
4801 * is displayed normally, except a NUL. */
4802 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004803 {
4804 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004805#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004806 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004807#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004810
4811 /* At start of the line we can have a composing char.
4812 * Draw it as a space with a composing char. */
4813 if (utf_iscomposing(mb_c))
4814 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004815 int i;
4816
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004817 for (i = Screen_mco - 1; i > 0; --i)
4818 u8cc[i] = u8cc[i - 1];
4819 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004820 mb_c = ' ';
4821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 }
4823
4824 if ((mb_l == 1 && c >= 0x80)
4825 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004826 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 {
4828 /*
4829 * Illegal UTF-8 byte: display as <xx>.
4830 * Non-BMP character : display as ? or fullwidth ?.
4831 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004832 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004833# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004834 if (wp->w_p_rl) /* reverse */
4835 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004836# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 p_extra = extra;
4838 c = *p_extra;
4839 mb_c = mb_ptr2char_adv(&p_extra);
4840 mb_utf8 = (c >= 0x80);
4841 n_extra = (int)STRLEN(p_extra);
4842 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004843 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 if (area_attr == 0 && search_attr == 0)
4845 {
4846 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004847 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 saved_attr2 = char_attr; /* save current attr */
4849 }
4850 }
4851 else if (mb_l == 0) /* at the NUL at end-of-line */
4852 mb_l = 1;
4853#ifdef FEAT_ARABIC
4854 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4855 {
4856 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004857 int pc, pc1, nc;
4858 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
4860 /* The idea of what is the previous and next
4861 * character depends on 'rightleft'. */
4862 if (wp->w_p_rl)
4863 {
4864 pc = prev_c;
4865 pc1 = prev_c1;
4866 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004867 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 }
4869 else
4870 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004871 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004873 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 }
4875 prev_c = mb_c;
4876
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004877 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879 else
4880 prev_c = mb_c;
4881#endif
4882 }
4883 else /* enc_dbcs */
4884 {
4885 mb_l = MB_BYTE2LEN(c);
4886 if (mb_l == 0) /* at the NUL at end-of-line */
4887 mb_l = 1;
4888 else if (mb_l > 1)
4889 {
4890 /* We assume a second byte below 32 is illegal.
4891 * Hopefully this is OK for all double-byte encodings!
4892 */
4893 if (ptr[1] >= 32)
4894 mb_c = (c << 8) + ptr[1];
4895 else
4896 {
4897 if (ptr[1] == NUL)
4898 {
4899 /* head byte at end of line */
4900 mb_l = 1;
4901 transchar_nonprint(extra, c);
4902 }
4903 else
4904 {
4905 /* illegal tail byte */
4906 mb_l = 2;
4907 STRCPY(extra, "XX");
4908 }
4909 p_extra = extra;
4910 n_extra = (int)STRLEN(extra) - 1;
4911 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004912 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 c = *p_extra++;
4914 if (area_attr == 0 && search_attr == 0)
4915 {
4916 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004917 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 saved_attr2 = char_attr; /* save current attr */
4919 }
4920 mb_c = c;
4921 }
4922 }
4923 }
4924 /* If a double-width char doesn't fit display a '>' in the
4925 * last column; the character is displayed at the start of the
4926 * next line. */
4927 if ((
4928# ifdef FEAT_RIGHTLEFT
4929 wp->w_p_rl ? (col <= 0) :
4930# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004931 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 && (*mb_char2cells)(mb_c) == 2)
4933 {
4934 c = '>';
4935 mb_c = c;
4936 mb_utf8 = FALSE;
4937 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004938 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004939 // Put pointer back so that the character will be
4940 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004942#ifdef FEAT_CONCEAL
4943 did_decrement_ptr = TRUE;
4944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946 else if (*ptr != NUL)
4947 ptr += mb_l - 1;
4948
4949 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004950 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004951 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004952 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004955 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004956 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 c = ' ';
4958 if (area_attr == 0 && search_attr == 0)
4959 {
4960 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004961 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 saved_attr2 = char_attr; /* save current attr */
4963 }
4964 mb_c = c;
4965 mb_utf8 = FALSE;
4966 mb_l = 1;
4967 }
4968
4969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 ++ptr;
4971
4972 if (extra_check)
4973 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004974#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004975 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004976#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004977
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004978#ifdef FEAT_TERMINAL
4979 if (get_term_attr)
4980 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004981 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004982
4983 if (!attr_pri)
4984 char_attr = syntax_attr;
4985 else
4986 char_attr = hl_combine_attr(syntax_attr, char_attr);
4987 }
4988#endif
4989
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004990#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004991 // Get syntax attribute, unless still at the start of the line
4992 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004993 v = (long)(ptr - line);
4994 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 {
4996 /* Get the syntax attribute for the character. If there
4997 * is an error, disable syntax highlighting. */
4998 save_did_emsg = did_emsg;
4999 did_emsg = FALSE;
5000
Bram Moolenaar217ad922005-03-20 22:37:15 +00005001 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02005002# ifdef FEAT_SPELL
5003 has_spell ? &can_spell :
5004# endif
5005 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006
5007 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005008 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005009 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005010 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005011 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 else
5014 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005015
5016 // combine syntax attribute with 'wincolor'
5017 if (win_attr != 0)
5018 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
5019
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005020#ifdef SYN_TIME_LIMIT
5021 if (wp->w_s->b_syn_slow)
5022 has_syntax = FALSE;
5023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024
5025 /* Need to get the line again, a multi-line regexp may
5026 * have made it invalid. */
5027 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
5028 ptr = line + v;
5029
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005030# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02005031 // Text properties overrule syntax highlighting or combine.
5032 if (text_prop_attr == 0 || text_prop_combine)
5033# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005034 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02005035 int comb_attr = syntax_attr;
5036# ifdef FEAT_TEXT_PROP
5037 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
5038# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005039 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02005040 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005041 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02005042 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005043 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02005044# ifdef FEAT_CONCEAL
5045 /* no concealing past the end of the line, it interferes
5046 * with line highlighting */
5047 if (c == NUL)
5048 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005049 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005050 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02005051# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005053#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00005054
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005055#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005056 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00005057 * Only do this when there is no syntax highlighting, the
5058 * @Spell cluster is not used or the current syntax item
5059 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00005060 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00005061 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005062 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005063 if (c != 0 && (
5064# ifdef FEAT_SYN_HL
5065 !has_syntax ||
5066# endif
5067 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00005068 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00005069 char_u *prev_ptr, *p;
5070 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005071 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00005072 if (has_mbyte)
5073 {
5074 prev_ptr = ptr - mb_l;
5075 v -= mb_l - 1;
5076 }
5077 else
Bram Moolenaare7566042005-06-17 22:00:15 +00005078 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00005079
5080 /* Use nextline[] if possible, it has the start of the
5081 * next line concatenated. */
5082 if ((prev_ptr - line) - nextlinecol >= 0)
5083 p = nextline + (prev_ptr - line) - nextlinecol;
5084 else
5085 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005086 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005087 len = spell_check(wp, p, &spell_hlf, &cap_col,
5088 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005089 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005090
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005091 /* In Insert mode only highlight a word that
5092 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005093 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005094 && (State & INSERT) != 0
5095 && wp->w_cursor.lnum == lnum
5096 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00005097 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005098 && wp->w_cursor.col < (colnr_T)word_end)
5099 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005100 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005101 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005102 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00005103
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005104 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00005105 && (p - nextline) + len > nextline_idx)
5106 {
5107 /* Remember that the good word continues at the
5108 * start of the next line. */
5109 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005110 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005111 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005112
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005113 /* Turn index into actual attributes. */
5114 if (spell_hlf != HLF_COUNT)
5115 spell_attr = highlight_attr[spell_hlf];
5116
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005117 if (cap_col > 0)
5118 {
5119 if (p != prev_ptr
5120 && (p - nextline) + cap_col >= nextline_idx)
5121 {
5122 /* Remember that the word in the next line
5123 * must start with a capital. */
5124 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005125 cap_col = (int)((p - nextline) + cap_col
5126 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005127 }
5128 else
5129 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005130 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005131 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005132 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005133 }
5134 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005135 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005136 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005137 char_attr = hl_combine_attr(char_attr, spell_attr);
5138 else
5139 char_attr = hl_combine_attr(spell_attr, char_attr);
5140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141#endif
5142#ifdef FEAT_LINEBREAK
5143 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00005144 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01005146 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01005147 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01005149 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005150 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01005151
Bram Moolenaar597a4222014-06-25 14:39:50 +02005152 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005153 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02005154 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02005155 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005156# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02005157 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005158 wp->w_buffer->b_p_vts_array) - 1;
5159# else
Bram Moolenaara3650912014-11-19 13:21:57 +01005160 n_extra = (int)wp->w_buffer->b_p_ts
5161 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005162# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01005163
Bram Moolenaar4df70292015-03-21 14:20:16 +01005164 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01005165 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005166 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005167 {
5168#ifdef FEAT_CONCEAL
5169 if (c == TAB)
5170 /* See "Tab alignment" below. */
5171 FIX_FOR_BOGUSCOLS;
5172#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005173 if (!wp->w_p_list)
5174 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
5177#endif
5178
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005179 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
5180 // But not when the character is followed by a composing
5181 // character (use mb_l to check that).
5182 if (wp->w_p_list
5183 && ((((c == 160 && mb_l == 1)
5184 || (mb_utf8
5185 && ((mb_c == 160 && mb_l == 2)
5186 || (mb_c == 0x202f && mb_l == 3))))
5187 && lcs_nbsp)
5188 || (c == ' '
5189 && mb_l == 1
5190 && lcs_space
5191 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005192 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005193 c = (c == ' ') ? lcs_space : lcs_nbsp;
5194 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005195 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005196 n_attr = 1;
5197 extra_attr = HL_ATTR(HLF_8);
5198 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005199 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005200 mb_c = c;
5201 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005202 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005203 mb_utf8 = TRUE;
5204 u8cc[0] = 0;
5205 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005206 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005207 else
5208 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005209 }
5210
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
5212 {
5213 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005214 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
5216 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005217 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 saved_attr2 = char_attr; /* save current attr */
5219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005221 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 {
5223 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005224 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005225 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
5227 else
5228 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
5230 }
5231
5232 /*
5233 * Handling of non-printable characters.
5234 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005235 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 {
5237 /*
5238 * when getting a character from the file, we may have to
5239 * turn it into something else on the way to putting it
5240 * into "ScreenLines".
5241 */
5242 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5243 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005244 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005245 long vcol_adjusted = vcol; /* removed showbreak length */
5246#ifdef FEAT_LINEBREAK
5247 /* only adjust the tab_len, when at the first column
5248 * after the showbreak value was drawn */
5249 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5250 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005253#ifdef FEAT_VARTABS
5254 tab_len = tabstop_padding(vcol_adjusted,
5255 wp->w_buffer->b_p_ts,
5256 wp->w_buffer->b_p_vts_array) - 1;
5257#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005258 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005259 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5260#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005261
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005262#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005263 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005264#endif
5265 /* tab amount depends on current column */
5266 n_extra = tab_len;
5267#ifdef FEAT_LINEBREAK
5268 else
5269 {
5270 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005271 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005272 int i;
5273 int saved_nextra = n_extra;
5274
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005275#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005276 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005277 /* there are characters to conceal */
5278 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005279 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5280 */
5281 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5282 && n_extra > tab_len)
5283 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005284#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005285
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005286 /* if n_extra > 0, it gives the number of chars, to
5287 * use for a tab, else we need to calculate the width
5288 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005289 len = (tab_len * mb_char2len(lcs_tab2));
5290 if (n_extra > 0)
5291 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005292 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005293 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005294 vim_memset(p, ' ', len);
5295 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005296 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005297 p_extra_free = p;
5298 for (i = 0; i < tab_len; i++)
5299 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005300 if (*p == NUL)
5301 {
5302 tab_len = i;
5303 break;
5304 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005305 mb_char2bytes(lcs_tab2, p);
5306 p += mb_char2len(lcs_tab2);
5307 n_extra += mb_char2len(lcs_tab2)
5308 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005309 }
5310 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005311#ifdef FEAT_CONCEAL
5312 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5313 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005314 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005315 n_extra -= vcol_off;
5316#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005317 }
5318#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005319#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005320 {
5321 int vc_saved = vcol_off;
5322
5323 /* Tab alignment should be identical regardless of
5324 * 'conceallevel' value. So tab compensates of all
5325 * previous concealed characters, and thus resets
5326 * vcol_off and boguscols accumulated so far in the
5327 * line. Note that the tab can be longer than
5328 * 'tabstop' when there are concealed characters. */
5329 FIX_FOR_BOGUSCOLS;
5330
5331 /* Make sure, the highlighting for the tab char will be
5332 * correctly set further below (effectively reverts the
5333 * FIX_FOR_BOGSUCOLS macro */
5334 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005335 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005336 tab_len += vc_saved;
5337 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 if (wp->w_p_list)
5341 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005342 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005343#ifdef FEAT_LINEBREAK
5344 if (wp->w_p_lbr)
5345 c_extra = NUL; /* using p_extra from above */
5346 else
5347#endif
5348 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005349 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005350 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005351 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005354 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 {
5356 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005357 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005358 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
5361 else
5362 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005363 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 c_extra = ' ';
5365 c = ' ';
5366 }
5367 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005368 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005369 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005370 || ((fromcol >= 0 || fromcol_prev >= 0)
5371 && tocol > vcol
5372 && VIsual_mode != Ctrl_V
5373 && (
5374# ifdef FEAT_RIGHTLEFT
5375 wp->w_p_rl ? (col >= 0) :
5376# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005377 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005378 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005379 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005380 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005381 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005383 /* Display a '$' after the line or highlight an extra
5384 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5386 /* For a diff line the highlighting continues after the
5387 * "$". */
5388 if (
5389# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005390 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391# ifdef LINE_ATTR
5392 &&
5393# endif
5394# endif
5395# ifdef LINE_ATTR
5396 line_attr == 0
5397# endif
5398 )
5399#endif
5400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 /* In virtualedit, visual selections may extend
5402 * beyond end of line. */
5403 if (area_highlighting && virtual_active()
5404 && tocol != MAXCOL && vcol < tocol)
5405 n_extra = 0;
5406 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 {
5408 p_extra = at_end_str;
5409 n_extra = 1;
5410 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005411 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 }
5413 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005414 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005415 c = lcs_eol;
5416 else
5417 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 lcs_eol_one = -1;
5419 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005420 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005422 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 n_attr = 1;
5424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005426 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 {
5428 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005429 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005430 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 }
5432 else
5433 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
5435 else if (c != NUL)
5436 {
5437 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005438 if (n_extra == 0)
5439 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440#ifdef FEAT_RIGHTLEFT
5441 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5442 rl_mirror(p_extra); /* reverse "<12>" */
5443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005445 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005446#ifdef FEAT_LINEBREAK
5447 if (wp->w_p_lbr)
5448 {
5449 char_u *p;
5450
5451 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005452 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005453 vim_memset(p, ' ', n_extra);
5454 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5455 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005456 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005457 p_extra_free = p_extra = p;
5458 }
5459 else
5460#endif
5461 {
5462 n_extra = byte2cells(c) - 1;
5463 c = *p_extra++;
5464 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005465 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 {
5467 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005468 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 saved_attr2 = char_attr; /* save current attr */
5470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 else if (VIsual_active
5474 && (VIsual_mode == Ctrl_V
5475 || VIsual_mode == 'v')
5476 && virtual_active()
5477 && tocol != MAXCOL
5478 && vcol < tocol
5479 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005480#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005482#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005483 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 {
5485 c = ' ';
5486 --ptr; /* put it back at the NUL */
5487 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005488#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 else if ((
5490# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005491 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005493# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005494 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005495# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 ) && (
5498# ifdef FEAT_RIGHTLEFT
5499 wp->w_p_rl ? (col >= 0) :
5500# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005501 (col
5502# ifdef FEAT_CONCEAL
5503 - boguscols
5504# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005505 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 {
5507 /* Highlight until the right side of the window */
5508 c = ' ';
5509 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005510
5511 /* Remember we do the char for line highlighting. */
5512 ++did_line_attr;
5513
5514 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005515 if (line_attr != 0 && char_attr == search_attr
5516 && (did_line_attr > 1
5517 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005518 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519# ifdef FEAT_DIFF
5520 if (diff_hlf == HLF_TXD)
5521 {
5522 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005523 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005524 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005525 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005526 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5527 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005528 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 }
5531# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005532# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005533 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005534 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005535 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005536 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5537 char_attr = hl_combine_attr(char_attr,
5538 HL_ATTR(HLF_CUL));
5539 }
5540# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 }
5542#endif
5543 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005544
5545#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005546 if ( wp->w_p_cole > 0
5547 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005548 conceal_cursor_line(wp))
5549 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005550 && !(lnum_in_visual_area
5551 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005552 {
5553 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005554 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005555 && (syn_get_sub_char() != NUL || match_conc
5556 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005557 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005558 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005559 /* First time at this concealed item: display one
5560 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005561 if (match_conc)
5562 c = match_conc;
5563 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005564 c = syn_get_sub_char();
5565 else if (lcs_conceal != NUL)
5566 c = lcs_conceal;
5567 else
5568 c = ' ';
5569
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005570 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005571
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005572 if (n_extra > 0)
5573 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005574 vcol += n_extra;
5575 if (wp->w_p_wrap && n_extra > 0)
5576 {
5577# ifdef FEAT_RIGHTLEFT
5578 if (wp->w_p_rl)
5579 {
5580 col -= n_extra;
5581 boguscols -= n_extra;
5582 }
5583 else
5584# endif
5585 {
5586 boguscols += n_extra;
5587 col += n_extra;
5588 }
5589 }
5590 n_extra = 0;
5591 n_attr = 0;
5592 }
5593 else if (n_skip == 0)
5594 {
5595 is_concealing = TRUE;
5596 n_skip = 1;
5597 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005598 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005599 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005600 {
5601 mb_utf8 = TRUE;
5602 u8cc[0] = 0;
5603 c = 0xc0;
5604 }
5605 else
5606 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005607 }
5608 else
5609 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005610 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005611 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005612 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005613
5614 if (n_skip > 0 && did_decrement_ptr)
5615 // not showing the '>', put pointer back to avoid getting stuck
5616 ++ptr;
5617
5618#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 }
5620
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005621#ifdef FEAT_CONCEAL
5622 /* In the cursor line and we may be concealing characters: correct
5623 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005624 if (!did_wcol && draw_state == WL_LINE
5625 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005626 && conceal_cursor_line(wp)
5627 && (int)wp->w_virtcol <= vcol + n_skip)
5628 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005629# ifdef FEAT_RIGHTLEFT
5630 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005631 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005632 else
5633# endif
5634 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005635 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005636 did_wcol = TRUE;
5637 }
5638#endif
5639
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 /* Don't override visual selection highlighting. */
5641 if (n_attr > 0
5642 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005643 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 char_attr = extra_attr;
5645
Bram Moolenaar81695252004-12-29 20:58:21 +00005646#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 /* XIM don't send preedit_start and preedit_end, but they send
5648 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5649 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005650 if (p_imst == IM_ON_THE_SPOT
5651 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005652 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 && (State & INSERT)
5654 && !p_imdisable
5655 && im_is_preediting()
5656 && draw_state == WL_LINE)
5657 {
5658 colnr_T tcol;
5659
5660 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005661 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 else
5663 tcol = preedit_end_col;
5664 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5665 {
5666 if (feedback_old_attr < 0)
5667 {
5668 feedback_col = 0;
5669 feedback_old_attr = char_attr;
5670 }
5671 char_attr = im_get_feedback_attr(feedback_col);
5672 if (char_attr < 0)
5673 char_attr = feedback_old_attr;
5674 feedback_col++;
5675 }
5676 else if (feedback_old_attr >= 0)
5677 {
5678 char_attr = feedback_old_attr;
5679 feedback_old_attr = -1;
5680 feedback_col = 0;
5681 }
5682 }
5683#endif
5684 /*
5685 * Handle the case where we are in column 0 but not on the first
5686 * character of the line and the user wants us to show us a
5687 * special character (via 'listchars' option "precedes:<char>".
5688 */
5689 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005690 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5692#ifdef FEAT_DIFF
5693 && filler_todo <= 0
5694#endif
5695 && draw_state > WL_NR
5696 && c != NUL)
5697 {
5698 c = lcs_prec;
5699 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005700 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5701 {
5702 /* Double-width character being overwritten by the "precedes"
5703 * character, need to fill up half the character. */
5704 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005705 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005706 n_extra = 1;
5707 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005708 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005711 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 {
5713 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005714 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005715 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
5717 else
5718 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005719 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
5721 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005722 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 n_attr3 = 1;
5724 }
5725 }
5726
5727 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005728 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005730 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005731#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005732 || did_line_attr == 1
5733#endif
5734 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005736#ifdef FEAT_SEARCH_EXTRA
5737 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005738
5739 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005740 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005741 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005742#endif
5743
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005744 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 * highlight match at end of line. If it's beyond the last
5746 * char on the screen, just overwrite that one (tricky!) Not
5747 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005748#ifdef FEAT_SEARCH_EXTRA
5749 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005750 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005751 prevcol_hl_flag = TRUE;
5752 else
5753 {
5754 cur = wp->w_match_head;
5755 while (cur != NULL)
5756 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005757 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005758 {
5759 prevcol_hl_flag = TRUE;
5760 break;
5761 }
5762 cur = cur->next;
5763 }
5764 }
5765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005767 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005768 && (VIsual_mode != Ctrl_V
5769 || lnum == VIsual.lnum
5770 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005771 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772#ifdef FEAT_SEARCH_EXTRA
5773 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005774 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005775# ifdef FEAT_SYN_HL
5776 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5777 && !(wp == curwin && VIsual_active))
5778# endif
5779# ifdef FEAT_DIFF
5780 && diff_hlf == (hlf_T)0
5781# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005782# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005783 && did_line_attr <= 1
5784# endif
5785 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786#endif
5787 ))
5788 {
5789 int n = 0;
5790
5791#ifdef FEAT_RIGHTLEFT
5792 if (wp->w_p_rl)
5793 {
5794 if (col < 0)
5795 n = 1;
5796 }
5797 else
5798#endif
5799 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005800 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 n = -1;
5802 }
5803 if (n != 0)
5804 {
5805 /* At the window boundary, highlight the last character
5806 * instead (better than nothing). */
5807 off += n;
5808 col += n;
5809 }
5810 else
5811 {
5812 /* Add a blank character to highlight. */
5813 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 if (enc_utf8)
5815 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 }
5817#ifdef FEAT_SEARCH_EXTRA
5818 if (area_attr == 0)
5819 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005820 /* Use attributes from match with highest priority among
5821 * 'search_hl' and the match list. */
5822 char_attr = search_hl.attr;
5823 cur = wp->w_match_head;
5824 shl_flag = FALSE;
5825 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005826 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005827 if (shl_flag == FALSE
5828 && ((cur != NULL
5829 && cur->priority > SEARCH_HL_PRIORITY)
5830 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005831 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005832 shl = &search_hl;
5833 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005834 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005835 else
5836 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005837 if ((ptr - line) - 1 == (long)shl->startcol
5838 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005839 char_attr = shl->attr;
5840 if (shl != &search_hl && cur != NULL)
5841 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 }
5844#endif
5845 ScreenAttrs[off] = char_attr;
5846#ifdef FEAT_RIGHTLEFT
5847 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005848 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005850 --off;
5851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 else
5853#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005854 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005856 ++off;
5857 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005858 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005859#ifdef FEAT_SYN_HL
5860 eol_hl_off = 1;
5861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864
Bram Moolenaar91170f82006-05-05 21:15:17 +00005865 /*
5866 * At end of the text line.
5867 */
5868 if (c == NUL)
5869 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005870#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005871 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005872 if (wp->w_p_wrap)
5873 v = wp->w_skipcol;
5874 else
5875 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005876
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005877 /* check if line ends before left margin */
5878 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005879 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005880#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005881 // Get rid of the boguscols now, we want to draw until the right
5882 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005883 col -= boguscols;
5884 boguscols = 0;
5885#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005886
5887 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005888 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005889
5890 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005891 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5892 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005893 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005894 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005895 || draw_color_col
5896 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005897# ifdef FEAT_RIGHTLEFT
5898 && !wp->w_p_rl
5899# endif
5900 )
5901 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005902 int rightmost_vcol = 0;
5903 int i;
5904
5905 if (wp->w_p_cuc)
5906 rightmost_vcol = wp->w_virtcol;
5907 if (draw_color_col)
5908 /* determine rightmost colorcolumn to possibly draw */
5909 for (i = 0; color_cols[i] >= 0; ++i)
5910 if (rightmost_vcol < color_cols[i])
5911 rightmost_vcol = color_cols[i];
5912
Bram Moolenaar02631462017-09-22 15:20:32 +02005913 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005914 {
5915 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005916 if (enc_utf8)
5917 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005918 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005919 if (draw_color_col)
5920 draw_color_col = advance_color_col(VCOL_HLC,
5921 &color_cols);
5922
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005923 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005924 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005925 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005926 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005927 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005928 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005929
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005930 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005931 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005932
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005933 ++vcol;
5934 }
5935 }
5936#endif
5937
Bram Moolenaar53f81742017-09-22 14:35:51 +02005938 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005939 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 row++;
5941
5942 /*
5943 * Update w_cline_height and w_cline_folded if the cursor line was
5944 * updated (saves a call to plines() later).
5945 */
5946 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5947 {
5948 curwin->w_cline_row = startrow;
5949 curwin->w_cline_height = row - startrow;
5950#ifdef FEAT_FOLDING
5951 curwin->w_cline_folded = FALSE;
5952#endif
5953 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5954 }
5955
5956 break;
5957 }
5958
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005959 // Show "extends" character from 'listchars' if beyond the line end and
5960 // 'list' is set.
5961 if (lcs_ext != NUL
5962 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 && !wp->w_p_wrap
5964#ifdef FEAT_DIFF
5965 && filler_todo <= 0
5966#endif
5967 && (
5968#ifdef FEAT_RIGHTLEFT
5969 wp->w_p_rl ? col == 0 :
5970#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005971 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005973 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5975 {
5976 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005977 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005979 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 {
5981 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005982 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005983 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 }
5985 else
5986 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 }
5988
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005989#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005990 /* advance to the next 'colorcolumn' */
5991 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005992 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005993
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005994 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005995 * highlight the cursor position itself.
5996 * Also highlight the 'colorcolumn' if it is different than
5997 * 'cursorcolumn' */
5998 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005999 if (draw_state == WL_LINE && !lnum_in_visual_area
6000 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006001 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02006002 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02006003 && lnum != wp->w_cursor.lnum)
6004 {
6005 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006006 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02006007 }
Bram Moolenaard160c342010-07-18 23:30:34 +02006008 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02006009 {
6010 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006011 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02006012 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006013 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006014#endif
6015
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 /*
6017 * Store character to be displayed.
6018 * Skip characters that are left of the screen for 'nowrap'.
6019 */
6020 vcol_prev = vcol;
6021 if (draw_state < WL_LINE || n_skip <= 0)
6022 {
6023 /*
6024 * Store the character.
6025 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01006026#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
6028 {
6029 /* A double-wide character is: put first halve in left cell. */
6030 --off;
6031 --col;
6032 }
6033#endif
6034 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01006036 {
6037 if ((mb_c & 0xff00) == 0x8e00)
6038 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01006040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 else if (enc_utf8)
6042 {
6043 if (mb_utf8)
6044 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006045 int i;
6046
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006048 if ((c & 0xff) == 0)
6049 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006050 for (i = 0; i < Screen_mco; ++i)
6051 {
6052 ScreenLinesC[i][off] = u8cc[i];
6053 if (u8cc[i] == 0)
6054 break;
6055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 }
6057 else
6058 ScreenLinesUC[off] = 0;
6059 }
6060 if (multi_attr)
6061 {
6062 ScreenAttrs[off] = multi_attr;
6063 multi_attr = 0;
6064 }
6065 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 ScreenAttrs[off] = char_attr;
6067
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
6069 {
6070 /* Need to fill two screen columns. */
6071 ++off;
6072 ++col;
6073 if (enc_utf8)
6074 /* UTF-8: Put a 0 in the second screen char. */
6075 ScreenLines[off] = 0;
6076 else
6077 /* DBCS: Put second byte in the second screen char. */
6078 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01006079 if (draw_state > WL_NR
6080#ifdef FEAT_DIFF
6081 && filler_todo <= 0
6082#endif
6083 )
6084 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 /* When "tocol" is halfway a character, set it to the end of
6086 * the character, otherwise highlighting won't stop. */
6087 if (tocol == vcol)
6088 ++tocol;
6089#ifdef FEAT_RIGHTLEFT
6090 if (wp->w_p_rl)
6091 {
6092 /* now it's time to backup one cell */
6093 --off;
6094 --col;
6095 }
6096#endif
6097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098#ifdef FEAT_RIGHTLEFT
6099 if (wp->w_p_rl)
6100 {
6101 --off;
6102 --col;
6103 }
6104 else
6105#endif
6106 {
6107 ++off;
6108 ++col;
6109 }
6110 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006111#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006112 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02006113 {
6114 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02006115 ++vcol_off;
6116 if (n_extra > 0)
6117 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02006118 if (wp->w_p_wrap)
6119 {
6120 /*
6121 * Special voodoo required if 'wrap' is on.
6122 *
6123 * Advance the column indicator to force the line
6124 * drawing to wrap early. This will make the line
6125 * take up the same screen space when parts are concealed,
6126 * so that cursor line computations aren't messed up.
6127 *
6128 * To avoid the fictitious advance of 'col' causing
6129 * trailing junk to be written out of the screen line
6130 * we are building, 'boguscols' keeps track of the number
6131 * of bad columns we have advanced.
6132 */
6133 if (n_extra > 0)
6134 {
6135 vcol += n_extra;
6136# ifdef FEAT_RIGHTLEFT
6137 if (wp->w_p_rl)
6138 {
6139 col -= n_extra;
6140 boguscols -= n_extra;
6141 }
6142 else
6143# endif
6144 {
6145 col += n_extra;
6146 boguscols += n_extra;
6147 }
6148 n_extra = 0;
6149 n_attr = 0;
6150 }
6151
6152
Bram Moolenaar860cae12010-06-05 23:22:07 +02006153 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
6154 {
6155 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01006156# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02006157 if (wp->w_p_rl)
6158 {
6159 --boguscols;
6160 --col;
6161 }
6162 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01006163# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02006164 {
6165 ++boguscols;
6166 ++col;
6167 }
6168 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006169
6170# ifdef FEAT_RIGHTLEFT
6171 if (wp->w_p_rl)
6172 {
6173 --boguscols;
6174 --col;
6175 }
6176 else
6177# endif
6178 {
6179 ++boguscols;
6180 ++col;
6181 }
6182 }
6183 else
6184 {
6185 if (n_extra > 0)
6186 {
6187 vcol += n_extra;
6188 n_extra = 0;
6189 n_attr = 0;
6190 }
6191 }
6192
6193 }
6194#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 else
6196 --n_skip;
6197
Bram Moolenaar64486672010-05-16 15:46:46 +02006198 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
6199 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00006200 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201#ifdef FEAT_DIFF
6202 && filler_todo <= 0
6203#endif
6204 )
6205 ++vcol;
6206
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006207#ifdef FEAT_SYN_HL
6208 if (vcol_save_attr >= 0)
6209 char_attr = vcol_save_attr;
6210#endif
6211
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 /* restore attributes after "predeces" in 'listchars' */
6213 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
6214 char_attr = saved_attr3;
6215
6216 /* restore attributes after last 'listchars' or 'number' char */
6217 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
6218 char_attr = saved_attr2;
6219
6220 /*
6221 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00006222 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 */
6224 if ((
6225#ifdef FEAT_RIGHTLEFT
6226 wp->w_p_rl ? (col < 0) :
6227#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006228 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 && (*ptr != NUL
6230#ifdef FEAT_DIFF
6231 || filler_todo > 0
6232#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006233 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6235 )
6236 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006237#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006238 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006239 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006240 boguscols = 0;
6241#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006242 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006243 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 ++row;
6246 ++screen_row;
6247
6248 /* When not wrapping and finished diff lines, or when displayed
6249 * '$' and highlighting until last column, break here. */
6250 if ((!wp->w_p_wrap
6251#ifdef FEAT_DIFF
6252 && filler_todo <= 0
6253#endif
6254 ) || lcs_eol_one == -1)
6255 break;
6256
6257 /* When the window is too narrow draw all "@" lines. */
6258 if (draw_state != WL_LINE
6259#ifdef FEAT_DIFF
6260 && filler_todo <= 0
6261#endif
6262 )
6263 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006264 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 row = endrow;
6267 }
6268
6269 /* When line got too long for screen break here. */
6270 if (row == endrow)
6271 {
6272 ++row;
6273 break;
6274 }
6275
6276 if (screen_cur_row == screen_row - 1
6277#ifdef FEAT_DIFF
6278 && filler_todo <= 0
6279#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006280 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 {
6282 /* Remember that the line wraps, used for modeless copy. */
6283 LineWraps[screen_row - 1] = TRUE;
6284
6285 /*
6286 * Special trick to make copy/paste of wrapped lines work with
6287 * xterm/screen: write an extra character beyond the end of
6288 * the line. This will work with all terminal types
6289 * (regardless of the xn,am settings).
6290 * Only do this on a fast tty.
6291 * Only do this if the cursor is on the current line
6292 * (something has been written in it).
6293 * Don't do this for the GUI.
6294 * Don't do this for double-width characters.
6295 * Don't do this for a window not at the right screen border.
6296 */
6297 if (p_tf
6298#ifdef FEAT_GUI
6299 && !gui.in_use
6300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006302 && ((*mb_off2cells)(LineOffset[screen_row],
6303 LineOffset[screen_row] + screen_Columns)
6304 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006306 + (int)Columns - 2,
6307 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006308 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 {
6310 /* First make sure we are at the end of the screen line,
6311 * then output the same character again to let the
6312 * terminal know about the wrap. If the terminal doesn't
6313 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006314 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 screen_char(LineOffset[screen_row - 1]
6316 + (unsigned)Columns - 1,
6317 screen_row - 1, (int)(Columns - 1));
6318
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 /* When there is a multi-byte character, just output a
6320 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006321 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6322 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 out_char(' ');
6324 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 out_char(ScreenLines[LineOffset[screen_row - 1]
6326 + (Columns - 1)]);
6327 /* force a redraw of the first char on the next line */
6328 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6329 screen_start(); /* don't know where cursor is now */
6330 }
6331 }
6332
6333 col = 0;
6334 off = (unsigned)(current_ScreenLine - ScreenLines);
6335#ifdef FEAT_RIGHTLEFT
6336 if (wp->w_p_rl)
6337 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006338 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 off += col;
6340 }
6341#endif
6342
6343 /* reset the drawing state for the start of a wrapped line */
6344 draw_state = WL_START;
6345 saved_n_extra = n_extra;
6346 saved_p_extra = p_extra;
6347 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006348 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 saved_char_attr = char_attr;
6350 n_extra = 0;
6351 lcs_prec_todo = lcs_prec;
6352#ifdef FEAT_LINEBREAK
6353# ifdef FEAT_DIFF
6354 if (filler_todo <= 0)
6355# endif
6356 need_showbreak = TRUE;
6357#endif
6358#ifdef FEAT_DIFF
6359 --filler_todo;
6360 /* When the filler lines are actually below the last line of the
6361 * file, don't draw the line itself, break here. */
6362 if (filler_todo == 0 && wp->w_botfill)
6363 break;
6364#endif
6365 }
6366
6367 } /* for every character in the line */
6368
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006369#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006370 /* After an empty line check first word for capital. */
6371 if (*skipwhite(line) == NUL)
6372 {
6373 capcol_lnum = lnum + 1;
6374 cap_col = 0;
6375 }
6376#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006377#ifdef FEAT_TEXT_PROP
6378 vim_free(text_props);
6379 vim_free(text_prop_idxs);
6380#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006381
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006382 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 return row;
6384}
6385
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006386/*
6387 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006388 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006389 */
6390 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006391comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006392{
6393 int i;
6394
6395 for (i = 0; i < Screen_mco; ++i)
6396 {
6397 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6398 return TRUE;
6399 if (ScreenLinesC[i][off_from] == 0)
6400 break;
6401 }
6402 return FALSE;
6403}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006404
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405/*
6406 * Check whether the given character needs redrawing:
6407 * - the (first byte of the) character is different
6408 * - the attributes are different
6409 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006410 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 */
6412 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006413char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414{
6415 if (cols > 0
6416 && ((ScreenLines[off_from] != ScreenLines[off_to]
6417 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 || (enc_dbcs != 0
6419 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6420 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6421 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6422 : (cols > 1 && ScreenLines[off_from + 1]
6423 != ScreenLines[off_to + 1])))
6424 || (enc_utf8
6425 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6426 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006427 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006428 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6429 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006430 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 return TRUE;
6432 return FALSE;
6433}
6434
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006435#if defined(FEAT_TERMINAL) || defined(PROTO)
6436/*
6437 * Return the index in ScreenLines[] for the current screen line.
6438 */
6439 int
6440screen_get_current_line_off()
6441{
6442 return (int)(current_ScreenLine - ScreenLines);
6443}
6444#endif
6445
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446/*
6447 * Move one "cooked" screen line to the screen, but only the characters that
6448 * have actually changed. Handle insert/delete character.
6449 * "coloff" gives the first column on the screen for this line.
6450 * "endcol" gives the columns where valid characters are.
6451 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6452 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006453 * "flags" can have bits:
6454 * SLF_POPUP popup window
6455 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6457 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6458 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006459 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006460screen_line(
6461 int row,
6462 int coloff,
6463 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006464 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006465 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466{
6467 unsigned off_from;
6468 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006469 unsigned max_off_from;
6470 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 int force = FALSE; /* force update rest of the line */
6474 int redraw_this /* bool: does character need redraw? */
6475#ifdef FEAT_GUI
6476 = TRUE /* For GUI when while-loop empty */
6477#endif
6478 ;
6479 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 int clear_next = FALSE;
6481 int char_cells; /* 1: normal char */
6482 /* 2: occupies two display cells */
6483# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006485 /* Check for illegal row and col, just in case. */
6486 if (row >= Rows)
6487 row = Rows - 1;
6488 if (endcol > Columns)
6489 endcol = Columns;
6490
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491# ifdef FEAT_CLIPBOARD
6492 clip_may_clear_selection(row, row);
6493# endif
6494
6495 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6496 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006497 max_off_from = off_from + screen_Columns;
6498 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499
6500#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006501 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 {
6503 /* Clear rest first, because it's left of the text. */
6504 if (clear_width > 0)
6505 {
6506 while (col <= endcol && ScreenLines[off_to] == ' '
6507 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006508 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 {
6510 ++off_to;
6511 ++col;
6512 }
6513 if (col <= endcol)
6514 screen_fill(row, row + 1, col + coloff,
6515 endcol + coloff + 1, ' ', ' ', 0);
6516 }
6517 col = endcol + 1;
6518 off_to = LineOffset[row] + col + coloff;
6519 off_from += col;
6520 endcol = (clear_width > 0 ? clear_width : -clear_width);
6521 }
6522#endif /* FEAT_RIGHTLEFT */
6523
6524 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6525
6526 while (col < endcol)
6527 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006529 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 else
6531 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532
6533 redraw_this = redraw_next;
6534 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6535 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6536
6537#ifdef FEAT_GUI
6538 /* If the next character was bold, then redraw the current character to
6539 * remove any pixels that might have spilt over into us. This only
6540 * happens in the GUI.
6541 */
6542 if (redraw_next && gui.in_use)
6543 {
6544 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006545 if (hl > HL_ALL)
6546 hl = syn_attr2attr(hl);
6547 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 redraw_this = TRUE;
6549 }
6550#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006551#ifdef FEAT_TEXT_PROP
6552 // Skip if under a(nother) popup.
6553 if (popup_mask[row * screen_Columns + col + coloff] > screen_zindex)
6554 redraw_this = FALSE;
6555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556
6557 if (redraw_this)
6558 {
6559 /*
6560 * Special handling when 'xs' termcap flag set (hpterm):
6561 * Attributes for characters are stored at the position where the
6562 * cursor is when writing the highlighting code. The
6563 * start-highlighting code must be written with the cursor on the
6564 * first highlighted character. The stop-highlighting code must
6565 * be written with the cursor just after the last highlighted
6566 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006567 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 * to clear the rest of the line, and force redrawing it
6569 * completely.
6570 */
6571 if ( p_wiv
6572 && !force
6573#ifdef FEAT_GUI
6574 && !gui.in_use
6575#endif
6576 && ScreenAttrs[off_to] != 0
6577 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6578 {
6579 /*
6580 * Need to remove highlighting attributes here.
6581 */
6582 windgoto(row, col + coloff);
6583 out_str(T_CE); /* clear rest of this screen line */
6584 screen_start(); /* don't know where cursor is now */
6585 force = TRUE; /* force redraw of rest of the line */
6586 redraw_next = TRUE; /* or else next char would miss out */
6587
6588 /*
6589 * If the previous character was highlighted, need to stop
6590 * highlighting at this character.
6591 */
6592 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6593 {
6594 screen_attr = ScreenAttrs[off_to - 1];
6595 term_windgoto(row, col + coloff);
6596 screen_stop_highlight();
6597 }
6598 else
6599 screen_attr = 0; /* highlighting has stopped */
6600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 if (enc_dbcs != 0)
6602 {
6603 /* Check if overwriting a double-byte with a single-byte or
6604 * the other way around requires another character to be
6605 * redrawn. For UTF-8 this isn't needed, because comparing
6606 * ScreenLinesUC[] is sufficient. */
6607 if (char_cells == 1
6608 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006609 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 {
6611 /* Writing a single-cell character over a double-cell
6612 * character: need to redraw the next cell. */
6613 ScreenLines[off_to + 1] = 0;
6614 redraw_next = TRUE;
6615 }
6616 else if (char_cells == 2
6617 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006618 && (*mb_off2cells)(off_to, max_off_to) == 1
6619 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 {
6621 /* Writing the second half of a double-cell character over
6622 * a double-cell character: need to redraw the second
6623 * cell. */
6624 ScreenLines[off_to + 2] = 0;
6625 redraw_next = TRUE;
6626 }
6627
6628 if (enc_dbcs == DBCS_JPNU)
6629 ScreenLines2[off_to] = ScreenLines2[off_from];
6630 }
6631 /* When writing a single-width character over a double-width
6632 * character and at the end of the redrawn text, need to clear out
6633 * the right halve of the old character.
6634 * Also required when writing the right halve of a double-width
6635 * char over the left halve of an existing one. */
6636 if (has_mbyte && col + char_cells == endcol
6637 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006638 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006640 && (*mb_off2cells)(off_to, max_off_to) == 1
6641 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643
6644 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 if (enc_utf8)
6646 {
6647 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6648 if (ScreenLinesUC[off_from] != 0)
6649 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006650 int i;
6651
6652 for (i = 0; i < Screen_mco; ++i)
6653 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 }
6655 }
6656 if (char_cells == 2)
6657 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658
6659#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006660 /* The bold trick makes a single column of pixels appear in the
6661 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 * character should be redrawn too. This happens for our own GUI
6663 * and for some xterms. */
6664 if (
6665# ifdef FEAT_GUI
6666 gui.in_use
6667# endif
6668# if defined(FEAT_GUI) && defined(UNIX)
6669 ||
6670# endif
6671# ifdef UNIX
6672 term_is_xterm
6673# endif
6674 )
6675 {
6676 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006677 if (hl > HL_ALL)
6678 hl = syn_attr2attr(hl);
6679 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 redraw_next = TRUE;
6681 }
6682#endif
6683 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006684
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006685 /* For simplicity set the attributes of second half of a
6686 * double-wide character equal to the first half. */
6687 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006689
6690 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 screen_char(off_to, row, col + coloff);
6694 }
6695 else if ( p_wiv
6696#ifdef FEAT_GUI
6697 && !gui.in_use
6698#endif
6699 && col + coloff > 0)
6700 {
6701 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6702 {
6703 /*
6704 * Don't output stop-highlight when moving the cursor, it will
6705 * stop the highlighting when it should continue.
6706 */
6707 screen_attr = 0;
6708 }
6709 else if (screen_attr != 0)
6710 screen_stop_highlight();
6711 }
6712
6713 off_to += CHAR_CELLS;
6714 off_from += CHAR_CELLS;
6715 col += CHAR_CELLS;
6716 }
6717
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 if (clear_next)
6719 {
6720 /* Clear the second half of a double-wide character of which the left
6721 * half was overwritten with a single-wide character. */
6722 ScreenLines[off_to] = ' ';
6723 if (enc_utf8)
6724 ScreenLinesUC[off_to] = 0;
6725 screen_char(off_to, row, col + coloff);
6726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727
6728 if (clear_width > 0
6729#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006730 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731#endif
6732 )
6733 {
6734#ifdef FEAT_GUI
6735 int startCol = col;
6736#endif
6737
6738 /* blank out the rest of the line */
6739 while (col < clear_width && ScreenLines[off_to] == ' '
6740 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006741 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 {
6743 ++off_to;
6744 ++col;
6745 }
6746 if (col < clear_width)
6747 {
6748#ifdef FEAT_GUI
6749 /*
6750 * In the GUI, clearing the rest of the line may leave pixels
6751 * behind if the first character cleared was bold. Some bold
6752 * fonts spill over the left. In this case we redraw the previous
6753 * character too. If we didn't skip any blanks above, then we
6754 * only redraw if the character wasn't already redrawn anyway.
6755 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006756 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 {
6758 hl = ScreenAttrs[off_to];
6759 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006760 {
6761 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006762
Bram Moolenaar9c697322006-10-09 20:11:17 +00006763 if (enc_utf8)
6764 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6765 * that its width is 2. */
6766 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6767 else if (enc_dbcs != 0)
6768 {
6769 /* find previous character by counting from first
6770 * column and get its width. */
6771 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006772 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006773
6774 while (off < off_to)
6775 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006776 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006777 off += prev_cells;
6778 }
6779 }
6780
6781 if (enc_dbcs != 0 && prev_cells > 1)
6782 screen_char_2(off_to - prev_cells, row,
6783 col + coloff - prev_cells);
6784 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006785 screen_char(off_to - prev_cells, row,
6786 col + coloff - prev_cells);
6787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 }
6789#endif
6790 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6791 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 off_to += clear_width - col;
6793 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 }
6795 }
6796
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006797 if (clear_width > 0
6798#ifdef FEAT_TEXT_PROP
6799 && !(flags & SLF_POPUP) // no separator for popup window
6800#endif
6801 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006803 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006804 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006805 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006807#ifdef FEAT_TEXT_PROP
6808 if (popup_mask[row * screen_Columns + col + coloff]
6809 <= screen_zindex)
6810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006812 int c;
6813
6814 c = fillchar_vsep(&hl);
6815 if (ScreenLines[off_to] != (schar_T)c
6816 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6817 != (c >= 0x80 ? c : 0))
6818 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006820 ScreenLines[off_to] = c;
6821 ScreenAttrs[off_to] = hl;
6822 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006824 if (c >= 0x80)
6825 {
6826 ScreenLinesUC[off_to] = c;
6827 ScreenLinesC[0][off_to] = 0;
6828 }
6829 else
6830 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006832 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 }
6835 }
6836 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 LineWraps[row] = FALSE;
6838 }
6839}
6840
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006841#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006843 * Mirror text "str" for right-left displaying.
6844 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006846 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006847rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848{
6849 char_u *p1, *p2;
6850 int t;
6851
6852 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6853 {
6854 t = *p1;
6855 *p1 = *p2;
6856 *p2 = t;
6857 }
6858}
6859#endif
6860
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861/*
6862 * mark all status lines for redraw; used after first :cd
6863 */
6864 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006865status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866{
6867 win_T *wp;
6868
Bram Moolenaar29323592016-07-24 22:04:11 +02006869 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 if (wp->w_status_height)
6871 {
6872 wp->w_redr_status = TRUE;
6873 redraw_later(VALID);
6874 }
6875}
6876
6877/*
6878 * mark all status lines of the current buffer for redraw
6879 */
6880 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006881status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882{
6883 win_T *wp;
6884
Bram Moolenaar29323592016-07-24 22:04:11 +02006885 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6887 {
6888 wp->w_redr_status = TRUE;
6889 redraw_later(VALID);
6890 }
6891}
6892
6893/*
6894 * Redraw all status lines that need to be redrawn.
6895 */
6896 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006897redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898{
6899 win_T *wp;
6900
Bram Moolenaar29323592016-07-24 22:04:11 +02006901 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006903 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006904 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006905 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907
Bram Moolenaar4033c552017-09-16 20:54:51 +02006908#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909/*
6910 * Redraw all status lines at the bottom of frame "frp".
6911 */
6912 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006913win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914{
6915 if (frp->fr_layout == FR_LEAF)
6916 frp->fr_win->w_redr_status = TRUE;
6917 else if (frp->fr_layout == FR_ROW)
6918 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006919 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 win_redraw_last_status(frp);
6921 }
6922 else /* frp->fr_layout == FR_COL */
6923 {
6924 frp = frp->fr_child;
6925 while (frp->fr_next != NULL)
6926 frp = frp->fr_next;
6927 win_redraw_last_status(frp);
6928 }
6929}
6930#endif
6931
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932/*
6933 * Draw the verticap separator right of window "wp" starting with line "row".
6934 */
6935 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006936draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937{
6938 int hl;
6939 int c;
6940
6941 if (wp->w_vsep_width)
6942 {
6943 /* draw the vertical separator right of this window */
6944 c = fillchar_vsep(&hl);
6945 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6946 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6947 c, ' ', hl);
6948 }
6949}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950
6951#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006952static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
6954/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006955 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 */
6957 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006958status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959{
6960 int len = 0;
6961
6962#ifdef FEAT_MENU
6963 int emenu = (xp->xp_context == EXPAND_MENUS
6964 || xp->xp_context == EXPAND_MENUNAMES);
6965
6966 /* Check for menu separators - replace with '|'. */
6967 if (emenu && menu_is_separator(s))
6968 return 1;
6969#endif
6970
6971 while (*s != NUL)
6972 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006973 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006974 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006975 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 }
6977
6978 return len;
6979}
6980
6981/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006982 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006983 * These are backslashes used for escaping. Do show backslashes in help tags.
6984 */
6985 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006986skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006987{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006988 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006989#ifdef FEAT_MENU
6990 || ((xp->xp_context == EXPAND_MENUS
6991 || xp->xp_context == EXPAND_MENUNAMES)
6992 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6993#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006994 )
6995 {
6996#ifndef BACKSLASH_IN_FILENAME
6997 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6998 return 2;
6999#endif
7000 return 1;
7001 }
7002 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007003}
7004
7005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 * Show wildchar matches in the status line.
7007 * Show at least the "match" item.
7008 * We start at item 'first_match' in the list and show all matches that fit.
7009 *
7010 * If inversion is possible we use it. Else '=' characters are used.
7011 */
7012 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007013win_redr_status_matches(
7014 expand_T *xp,
7015 int num_matches,
7016 char_u **matches, /* list of matches */
7017 int match,
7018 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019{
7020#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
7021 int row;
7022 char_u *buf;
7023 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007024 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 int fillchar;
7026 int attr;
7027 int i;
7028 int highlight = TRUE;
7029 char_u *selstart = NULL;
7030 int selstart_col = 0;
7031 char_u *selend = NULL;
7032 static int first_match = 0;
7033 int add_left = FALSE;
7034 char_u *s;
7035#ifdef FEAT_MENU
7036 int emenu;
7037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039
7040 if (matches == NULL) /* interrupted completion? */
7041 return;
7042
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007043 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02007044 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007045 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02007046 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 if (buf == NULL)
7048 return;
7049
7050 if (match == -1) /* don't show match but original text */
7051 {
7052 match = 0;
7053 highlight = FALSE;
7054 }
7055 /* count 1 for the ending ">" */
7056 clen = status_match_len(xp, L_MATCH(match)) + 3;
7057 if (match == 0)
7058 first_match = 0;
7059 else if (match < first_match)
7060 {
7061 /* jumping left, as far as we can go */
7062 first_match = match;
7063 add_left = TRUE;
7064 }
7065 else
7066 {
7067 /* check if match fits on the screen */
7068 for (i = first_match; i < match; ++i)
7069 clen += status_match_len(xp, L_MATCH(i)) + 2;
7070 if (first_match > 0)
7071 clen += 2;
7072 /* jumping right, put match at the left */
7073 if ((long)clen > Columns)
7074 {
7075 first_match = match;
7076 /* if showing the last match, we can add some on the left */
7077 clen = 2;
7078 for (i = match; i < num_matches; ++i)
7079 {
7080 clen += status_match_len(xp, L_MATCH(i)) + 2;
7081 if ((long)clen >= Columns)
7082 break;
7083 }
7084 if (i == num_matches)
7085 add_left = TRUE;
7086 }
7087 }
7088 if (add_left)
7089 while (first_match > 0)
7090 {
7091 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
7092 if ((long)clen >= Columns)
7093 break;
7094 --first_match;
7095 }
7096
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007097 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098
7099 if (first_match == 0)
7100 {
7101 *buf = NUL;
7102 len = 0;
7103 }
7104 else
7105 {
7106 STRCPY(buf, "< ");
7107 len = 2;
7108 }
7109 clen = len;
7110
7111 i = first_match;
7112 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
7113 {
7114 if (i == match)
7115 {
7116 selstart = buf + len;
7117 selstart_col = clen;
7118 }
7119
7120 s = L_MATCH(i);
7121 /* Check for menu separators - replace with '|' */
7122#ifdef FEAT_MENU
7123 emenu = (xp->xp_context == EXPAND_MENUS
7124 || xp->xp_context == EXPAND_MENUNAMES);
7125 if (emenu && menu_is_separator(s))
7126 {
7127 STRCPY(buf + len, transchar('|'));
7128 l = (int)STRLEN(buf + len);
7129 len += l;
7130 clen += l;
7131 }
7132 else
7133#endif
7134 for ( ; *s != NUL; ++s)
7135 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007136 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007138 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {
7140 STRNCPY(buf + len, s, l);
7141 s += l - 1;
7142 len += l;
7143 }
7144 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 {
7146 STRCPY(buf + len, transchar_byte(*s));
7147 len += (int)STRLEN(buf + len);
7148 }
7149 }
7150 if (i == match)
7151 selend = buf + len;
7152
7153 *(buf + len++) = ' ';
7154 *(buf + len++) = ' ';
7155 clen += 2;
7156 if (++i == num_matches)
7157 break;
7158 }
7159
7160 if (i != num_matches)
7161 {
7162 *(buf + len++) = '>';
7163 ++clen;
7164 }
7165
7166 buf[len] = NUL;
7167
7168 row = cmdline_row - 1;
7169 if (row >= 0)
7170 {
7171 if (wild_menu_showing == 0)
7172 {
7173 if (msg_scrolled > 0)
7174 {
7175 /* Put the wildmenu just above the command line. If there is
7176 * no room, scroll the screen one line up. */
7177 if (cmdline_row == Rows - 1)
7178 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02007179 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 ++msg_scrolled;
7181 }
7182 else
7183 {
7184 ++cmdline_row;
7185 ++row;
7186 }
7187 wild_menu_showing = WM_SCROLLED;
7188 }
7189 else
7190 {
7191 /* Create status line if needed by setting 'laststatus' to 2.
7192 * Set 'winminheight' to zero to avoid that the window is
7193 * resized. */
7194 if (lastwin->w_status_height == 0)
7195 {
7196 save_p_ls = p_ls;
7197 save_p_wmh = p_wmh;
7198 p_ls = 2;
7199 p_wmh = 0;
7200 last_status(FALSE);
7201 }
7202 wild_menu_showing = WM_SHOWN;
7203 }
7204 }
7205
7206 screen_puts(buf, row, 0, attr);
7207 if (selstart != NULL && highlight)
7208 {
7209 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007210 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 }
7212
7213 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7214 }
7215
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 vim_free(buf);
7218}
7219#endif
7220
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221/*
7222 * Redraw the status line of window wp.
7223 *
7224 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007225 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7226 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007228 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007229win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230{
7231 int row;
7232 char_u *p;
7233 int len;
7234 int fillchar;
7235 int attr;
7236 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007237 static int busy = FALSE;
7238
7239 /* It's possible to get here recursively when 'statusline' (indirectly)
7240 * invokes ":redrawstatus". Simply ignore the call then. */
7241 if (busy)
7242 return;
7243 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244
7245 wp->w_redr_status = FALSE;
7246 if (wp->w_status_height == 0)
7247 {
7248 /* no status line, can only be last window */
7249 redraw_cmdline = TRUE;
7250 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007251 else if (!redrawing()
7252#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007253 // don't update status line when popup menu is visible and may be
7254 // drawn over it, unless it will be redrawn later
7255 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007256#endif
7257 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 {
7259 /* Don't redraw right now, do it later. */
7260 wp->w_redr_status = TRUE;
7261 }
7262#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007263 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 {
7265 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007266 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 }
7268#endif
7269 else
7270 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007271 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007273 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 p = NameBuff;
7275 len = (int)STRLEN(p);
7276
Bram Moolenaard85f2712017-07-28 21:51:57 +02007277 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278#ifdef FEAT_QUICKFIX
7279 || wp->w_p_pvw
7280#endif
7281 || bufIsChanged(wp->w_buffer)
7282 || wp->w_buffer->b_p_ro)
7283 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007284 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007286 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 len += (int)STRLEN(p + len);
7288 }
7289#ifdef FEAT_QUICKFIX
7290 if (wp->w_p_pvw)
7291 {
7292 STRCPY(p + len, _("[Preview]"));
7293 len += (int)STRLEN(p + len);
7294 }
7295#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007296 if (bufIsChanged(wp->w_buffer)
7297#ifdef FEAT_TERMINAL
7298 && !bt_terminal(wp->w_buffer)
7299#endif
7300 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 {
7302 STRCPY(p + len, "[+]");
7303 len += 3;
7304 }
7305 if (wp->w_buffer->b_p_ro)
7306 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007307 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007308 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 }
7310
Bram Moolenaar02631462017-09-22 15:20:32 +02007311 this_ru_col = ru_col - (Columns - wp->w_width);
7312 if (this_ru_col < (wp->w_width + 1) / 2)
7313 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 if (this_ru_col <= 1)
7315 {
7316 p = (char_u *)"<"; /* No room for file name! */
7317 len = 1;
7318 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007319 else if (has_mbyte)
7320 {
7321 int clen = 0, i;
7322
7323 /* Count total number of display cells. */
7324 clen = mb_string2cells(p, -1);
7325
7326 /* Find first character that will fit.
7327 * Going from start to end is much faster for DBCS. */
7328 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7329 i += (*mb_ptr2len)(p + i))
7330 clen -= (*mb_ptr2cells)(p + i);
7331 len = clen;
7332 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007334 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007336 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 }
7338
Bram Moolenaara12a1612019-01-24 16:39:02 +01007339 }
7340 else if (len > this_ru_col - 1)
7341 {
7342 p += len - (this_ru_col - 1);
7343 *p = '<';
7344 len = this_ru_col - 1;
7345 }
7346
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007348 screen_puts(p, row, wp->w_wincol, attr);
7349 screen_fill(row, row + 1, len + wp->w_wincol,
7350 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007352 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7354 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007355 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356
7357#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007358 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359#endif
7360 }
7361
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 /*
7363 * May need to draw the character below the vertical separator.
7364 */
7365 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7366 {
7367 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007368 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 else
7370 fillchar = fillchar_vsep(&attr);
7371 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7372 attr);
7373 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007374 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375}
7376
Bram Moolenaar238a5642006-02-21 22:12:05 +00007377#ifdef FEAT_STL_OPT
7378/*
7379 * Redraw the status line according to 'statusline' and take care of any
7380 * errors encountered.
7381 */
7382 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007383redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007384{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007385 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007386 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007387
7388 /* When called recursively return. This can happen when the statusline
7389 * contains an expression that triggers a redraw. */
7390 if (entered)
7391 return;
7392 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007393
Bram Moolenaara742e082016-04-05 21:10:38 +02007394 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007395 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007396 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007397 {
7398 /* When there is an error disable the statusline, otherwise the
7399 * display is messed up with errors and a redraw triggers the problem
7400 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007401 set_string_option_direct((char_u *)"statusline", -1,
7402 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007403 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007404 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007405 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007406 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007407}
7408#endif
7409
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410/*
7411 * Return TRUE if the status line of window "wp" is connected to the status
7412 * line of the window right of it. If not, then it's a vertical separator.
7413 * Only call if (wp->w_vsep_width != 0).
7414 */
7415 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007416stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417{
7418 frame_T *fr;
7419
7420 fr = wp->w_frame;
7421 while (fr->fr_parent != NULL)
7422 {
7423 if (fr->fr_parent->fr_layout == FR_COL)
7424 {
7425 if (fr->fr_next != NULL)
7426 break;
7427 }
7428 else
7429 {
7430 if (fr->fr_next != NULL)
7431 return TRUE;
7432 }
7433 fr = fr->fr_parent;
7434 }
7435 return FALSE;
7436}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439/*
7440 * Get the value to show for the language mappings, active 'keymap'.
7441 */
7442 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007443get_keymap_str(
7444 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007445 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007446 char_u *buf, /* buffer for the result */
7447 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448{
7449 char_u *p;
7450
7451 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7452 return FALSE;
7453
7454 {
7455#ifdef FEAT_EVAL
7456 buf_T *old_curbuf = curbuf;
7457 win_T *old_curwin = curwin;
7458 char_u *s;
7459
7460 curbuf = wp->w_buffer;
7461 curwin = wp;
7462 STRCPY(buf, "b:keymap_name"); /* must be writable */
7463 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007464 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 --emsg_skip;
7466 curbuf = old_curbuf;
7467 curwin = old_curwin;
7468 if (p == NULL || *p == NUL)
7469#endif
7470 {
7471#ifdef FEAT_KEYMAP
7472 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7473 p = wp->w_buffer->b_p_keymap;
7474 else
7475#endif
7476 p = (char_u *)"lang";
7477 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007478 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 buf[0] = NUL;
7480#ifdef FEAT_EVAL
7481 vim_free(s);
7482#endif
7483 }
7484 return buf[0] != NUL;
7485}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486
7487#if defined(FEAT_STL_OPT) || defined(PROTO)
7488/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007489 * Redraw the status line or ruler of window "wp".
7490 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 */
7492 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007493win_redr_custom(
7494 win_T *wp,
7495 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007497 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 int attr;
7499 int curattr;
7500 int row;
7501 int col = 0;
7502 int maxwidth;
7503 int width;
7504 int n;
7505 int len;
7506 int fillchar;
7507 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007508 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007510 struct stl_hlrec hltab[STL_MAX_ITEM];
7511 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007512 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007513 win_T *ewp;
7514 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515
Bram Moolenaar1d633412013-12-11 15:52:01 +01007516 /* There is a tiny chance that this gets called recursively: When
7517 * redrawing a status line triggers redrawing the ruler or tabline.
7518 * Avoid trouble by not allowing recursion. */
7519 if (entered)
7520 return;
7521 entered = TRUE;
7522
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007524 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007526 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007527 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007528 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007529 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007530 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007531 maxwidth = Columns;
7532# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007533 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007534# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007536 else
7537 {
7538 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007539 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007540 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007541
7542 if (draw_ruler)
7543 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007544 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007545 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007546 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007547 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007548 if (*++stl == '-')
7549 stl++;
7550 if (atoi((char *)stl))
7551 while (VIM_ISDIGIT(*stl))
7552 stl++;
7553 if (*stl++ != '(')
7554 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007555 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007556 col = ru_col - (Columns - wp->w_width);
7557 if (col < (wp->w_width + 1) / 2)
7558 col = (wp->w_width + 1) / 2;
7559 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007560 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007561 {
7562 row = Rows - 1;
7563 --maxwidth; /* writing in last column may cause scrolling */
7564 fillchar = ' ';
7565 attr = 0;
7566 }
7567
7568# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007569 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007570# endif
7571 }
7572 else
7573 {
7574 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007575 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007576 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007577 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007578# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007579 use_sandbox = was_set_insecurely((char_u *)"statusline",
7580 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007581# endif
7582 }
7583
Bram Moolenaar53f81742017-09-22 14:35:51 +02007584 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007585 }
7586
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007588 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589
Bram Moolenaar61452852011-02-01 18:01:11 +01007590 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7591 * the cursor away and back. */
7592 ewp = wp == NULL ? curwin : wp;
7593 p_crb_save = ewp->w_p_crb;
7594 ewp->w_p_crb = FALSE;
7595
Bram Moolenaar362f3562009-11-03 16:20:34 +00007596 /* Make a copy, because the statusline may include a function call that
7597 * might change the option value and free the memory. */
7598 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007599 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007600 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007601 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007602 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007603 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007605 /* Make all characters printable. */
7606 p = transstr(buf);
7607 if (p != NULL)
7608 {
7609 vim_strncpy(buf, p, sizeof(buf) - 1);
7610 vim_free(p);
7611 }
7612
7613 /* fill up with "fillchar" */
7614 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007615 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 ++width;
7619 }
7620 buf[len] = NUL;
7621
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007622 /*
7623 * Draw each snippet with the specified highlighting.
7624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 curattr = attr;
7626 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007627 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007629 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 screen_puts_len(p, len, row, col, curattr);
7631 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007632 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007634 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007636 else if (hltab[n].userhl < 0)
7637 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007638#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007639 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7640 && wp->w_status_height != 0)
7641 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007642 else if (wp != NULL && bt_terminal(wp->w_buffer)
7643 && wp->w_status_height != 0)
7644 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007645#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007646 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007647 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007649 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 }
7651 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007652
7653 if (wp == NULL)
7654 {
7655 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7656 col = 0;
7657 len = 0;
7658 p = buf;
7659 fillchar = 0;
7660 for (n = 0; tabtab[n].start != NULL; n++)
7661 {
7662 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7663 while (col < len)
7664 TabPageIdxs[col++] = fillchar;
7665 p = tabtab[n].start;
7666 fillchar = tabtab[n].userhl;
7667 }
7668 while (col < Columns)
7669 TabPageIdxs[col++] = fillchar;
7670 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007671
7672theend:
7673 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674}
7675
7676#endif /* FEAT_STL_OPT */
7677
7678/*
7679 * Output a single character directly to the screen and update ScreenLines.
7680 */
7681 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007682screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 char_u buf[MB_MAXBYTES + 1];
7685
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007686 if (has_mbyte)
7687 buf[(*mb_char2bytes)(c, buf)] = NUL;
7688 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007689 {
7690 buf[0] = c;
7691 buf[1] = NUL;
7692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 screen_puts(buf, row, col, attr);
7694}
7695
7696/*
7697 * Get a single character directly from ScreenLines into "bytes[]".
7698 * Also return its attribute in *attrp;
7699 */
7700 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007701screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702{
7703 unsigned off;
7704
7705 /* safety check */
7706 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7707 {
7708 off = LineOffset[row] + col;
7709 *attrp = ScreenAttrs[off];
7710 bytes[0] = ScreenLines[off];
7711 bytes[1] = NUL;
7712
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 if (enc_utf8 && ScreenLinesUC[off] != 0)
7714 bytes[utfc_char2bytes(off, bytes)] = NUL;
7715 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7716 {
7717 bytes[0] = ScreenLines[off];
7718 bytes[1] = ScreenLines2[off];
7719 bytes[2] = NUL;
7720 }
7721 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7722 {
7723 bytes[1] = ScreenLines[off + 1];
7724 bytes[2] = NUL;
7725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 }
7727}
7728
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007729/*
7730 * Return TRUE if composing characters for screen posn "off" differs from
7731 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007732 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007733 */
7734 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007735screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007736{
7737 int i;
7738
7739 for (i = 0; i < Screen_mco; ++i)
7740 {
7741 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7742 return TRUE;
7743 if (u8cc[i] == 0)
7744 break;
7745 }
7746 return FALSE;
7747}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007748
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749/*
7750 * Put string '*text' on the screen at position 'row' and 'col', with
7751 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7752 * Note: only outputs within one row, message is truncated at screen boundary!
7753 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7754 */
7755 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007756screen_puts(
7757 char_u *text,
7758 int row,
7759 int col,
7760 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761{
7762 screen_puts_len(text, -1, row, col, attr);
7763}
7764
7765/*
7766 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7767 * a NUL.
7768 */
7769 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007770screen_puts_len(
7771 char_u *text,
7772 int textlen,
7773 int row,
7774 int col,
7775 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776{
7777 unsigned off;
7778 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007779 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007781 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 int mbyte_blen = 1;
7783 int mbyte_cells = 1;
7784 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007785 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007787#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007789 int pc, nc, nc1;
7790 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007792 int force_redraw_this;
7793 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007794 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007796 // Safety check. The check for negative row and column is to fix issue
7797 // #4102. TODO: find out why row/col could be negative.
7798 if (ScreenLines == NULL
7799 || row >= screen_Rows || row < 0
7800 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007802 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803
Bram Moolenaarc236c162008-07-13 17:41:49 +00007804 /* When drawing over the right halve of a double-wide char clear out the
7805 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007806 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007807#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007808 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007809#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007810 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007811 {
7812 ScreenLines[off - 1] = ' ';
7813 ScreenAttrs[off - 1] = 0;
7814 if (enc_utf8)
7815 {
7816 ScreenLinesUC[off - 1] = 0;
7817 ScreenLinesC[0][off - 1] = 0;
7818 }
7819 /* redraw the previous cell, make it empty */
7820 screen_char(off - 1, row, col - 1);
7821 /* force the cell at "col" to be redrawn */
7822 force_redraw_next = TRUE;
7823 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007824
Bram Moolenaar367329b2007-08-30 11:53:22 +00007825 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007826 while (col < screen_Columns
7827 && (len < 0 || (int)(ptr - text) < len)
7828 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 {
7830 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 /* check if this is the first byte of a multibyte */
7832 if (has_mbyte)
7833 {
7834 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007835 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007837 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7839 mbyte_cells = 1;
7840 else if (enc_dbcs != 0)
7841 mbyte_cells = mbyte_blen;
7842 else /* enc_utf8 */
7843 {
7844 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007845 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 (int)((text + len) - ptr));
7847 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007848 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007850#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7852 {
7853 /* Do Arabic shaping. */
7854 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7855 {
7856 /* Past end of string to be displayed. */
7857 nc = NUL;
7858 nc1 = NUL;
7859 }
7860 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007861 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007862 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7863 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007864 nc1 = pcc[0];
7865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 pc = prev_c;
7867 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007868 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 }
7870 else
7871 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007872#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007873 if (col + mbyte_cells > screen_Columns)
7874 {
7875 /* Only 1 cell left, but character requires 2 cells:
7876 * display a '>' in the last column to avoid wrapping. */
7877 c = '>';
7878 mbyte_cells = 1;
7879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 }
7881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007883 force_redraw_this = force_redraw_next;
7884 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007885
7886 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 || (mbyte_cells == 2
7888 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7889 || (enc_dbcs == DBCS_JPNU
7890 && c == 0x8e
7891 && ScreenLines2[off] != ptr[1])
7892 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007893 && (ScreenLinesUC[off] !=
7894 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7895 || (ScreenLinesUC[off] != 0
7896 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007898 || exmode_active;
7899
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007900 if ((need_redraw || force_redraw_this)
7901#ifdef FEAT_TEXT_PROP
7902 && popup_mask[row * screen_Columns + col] <= screen_zindex
7903#endif
7904 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 {
7906#if defined(FEAT_GUI) || defined(UNIX)
7907 /* The bold trick makes a single row of pixels appear in the next
7908 * character. When a bold character is removed, the next
7909 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007910 * and for some xterms. */
7911 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912# ifdef FEAT_GUI
7913 gui.in_use
7914# endif
7915# if defined(FEAT_GUI) && defined(UNIX)
7916 ||
7917# endif
7918# ifdef UNIX
7919 term_is_xterm
7920# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007921 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007923 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007925 if (n > HL_ALL)
7926 n = syn_attr2attr(n);
7927 if (n & HL_BOLD)
7928 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 }
7930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 /* When at the end of the text and overwriting a two-cell
7932 * character with a one-cell character, need to clear the next
7933 * cell. Also when overwriting the left halve of a two-cell char
7934 * with the right halve of a two-cell char. Do this only once
7935 * (mb_off2cells() may return 2 on the right halve). */
7936 if (clear_next_cell)
7937 clear_next_cell = FALSE;
7938 else if (has_mbyte
7939 && (len < 0 ? ptr[mbyte_blen] == NUL
7940 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007941 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007943 && (*mb_off2cells)(off, max_off) == 1
7944 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 clear_next_cell = TRUE;
7946
7947 /* Make sure we never leave a second byte of a double-byte behind,
7948 * it confuses mb_off2cells(). */
7949 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007950 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007952 && (*mb_off2cells)(off, max_off) == 1
7953 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 ScreenLines[off] = c;
7956 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 if (enc_utf8)
7958 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007959 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 ScreenLinesUC[off] = 0;
7961 else
7962 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007963 int i;
7964
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007966 for (i = 0; i < Screen_mco; ++i)
7967 {
7968 ScreenLinesC[i][off] = u8cc[i];
7969 if (u8cc[i] == 0)
7970 break;
7971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 }
7973 if (mbyte_cells == 2)
7974 {
7975 ScreenLines[off + 1] = 0;
7976 ScreenAttrs[off + 1] = attr;
7977 }
7978 screen_char(off, row, col);
7979 }
7980 else if (mbyte_cells == 2)
7981 {
7982 ScreenLines[off + 1] = ptr[1];
7983 ScreenAttrs[off + 1] = attr;
7984 screen_char_2(off, row, col);
7985 }
7986 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7987 {
7988 ScreenLines2[off] = ptr[1];
7989 screen_char(off, row, col);
7990 }
7991 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 screen_char(off, row, col);
7993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 if (has_mbyte)
7995 {
7996 off += mbyte_cells;
7997 col += mbyte_cells;
7998 ptr += mbyte_blen;
7999 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02008000 {
8001 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02008003 len = -1;
8004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 }
8006 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 {
8008 ++off;
8009 ++col;
8010 ++ptr;
8011 }
8012 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00008013
Bram Moolenaar2bea2912009-03-11 16:58:40 +00008014 /* If we detected the next character needs to be redrawn, but the text
8015 * doesn't extend up to there, update the character here. */
8016 if (force_redraw_next && col < screen_Columns)
8017 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00008018 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
8019 screen_char_2(off, row, col);
8020 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00008021 screen_char(off, row, col);
8022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023}
8024
8025#ifdef FEAT_SEARCH_EXTRA
8026/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008027 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 */
8029 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008030start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031{
8032 if (p_hls && !no_hlsearch)
8033 {
8034 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008035 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008036# ifdef FEAT_RELTIME
8037 /* Set the time limit to 'redrawtime'. */
8038 profile_setlimit(p_rdt, &search_hl.tm);
8039# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 }
8041}
8042
8043/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008044 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 */
8046 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008047end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048{
8049 if (search_hl.rm.regprog != NULL)
8050 {
Bram Moolenaar473de612013-06-08 18:19:48 +02008051 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 search_hl.rm.regprog = NULL;
8053 }
8054}
8055
8056/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02008057 * Init for calling prepare_search_hl().
8058 */
8059 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008060init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02008061{
8062 matchitem_T *cur;
8063
8064 /* Setup for match and 'hlsearch' highlighting. Disable any previous
8065 * match */
8066 cur = wp->w_match_head;
8067 while (cur != NULL)
8068 {
8069 cur->hl.rm = cur->match;
8070 if (cur->hlg_id == 0)
8071 cur->hl.attr = 0;
8072 else
8073 cur->hl.attr = syn_id2attr(cur->hlg_id);
8074 cur->hl.buf = wp->w_buffer;
8075 cur->hl.lnum = 0;
8076 cur->hl.first_lnum = 0;
8077# ifdef FEAT_RELTIME
8078 /* Set the time limit to 'redrawtime'. */
8079 profile_setlimit(p_rdt, &(cur->hl.tm));
8080# endif
8081 cur = cur->next;
8082 }
8083 search_hl.buf = wp->w_buffer;
8084 search_hl.lnum = 0;
8085 search_hl.first_lnum = 0;
8086 /* time limit is set at the toplevel, for all windows */
8087}
8088
8089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 * Advance to the match in window "wp" line "lnum" or past it.
8091 */
8092 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008093prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008095 matchitem_T *cur; /* points to the match list */
8096 match_T *shl; /* points to search_hl or a match */
8097 int shl_flag; /* flag to indicate whether search_hl
8098 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008099 int pos_inprogress; /* marks that position match search is
8100 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 int n;
8102
8103 /*
8104 * When using a multi-line pattern, start searching at the top
8105 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008106 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008108 cur = wp->w_match_head;
8109 shl_flag = FALSE;
8110 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008112 if (shl_flag == FALSE)
8113 {
8114 shl = &search_hl;
8115 shl_flag = TRUE;
8116 }
8117 else
8118 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 if (shl->rm.regprog != NULL
8120 && shl->lnum == 0
8121 && re_multiline(shl->rm.regprog))
8122 {
8123 if (shl->first_lnum == 0)
8124 {
8125# ifdef FEAT_FOLDING
8126 for (shl->first_lnum = lnum;
8127 shl->first_lnum > wp->w_topline; --shl->first_lnum)
8128 if (hasFoldingWin(wp, shl->first_lnum - 1,
8129 NULL, NULL, TRUE, NULL))
8130 break;
8131# else
8132 shl->first_lnum = wp->w_topline;
8133# endif
8134 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008135 if (cur != NULL)
8136 cur->pos.cur = 0;
8137 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008139 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
8140 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02008142 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
8143 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008144 pos_inprogress = cur == NULL || cur->pos.cur == 0
8145 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 if (shl->lnum != 0)
8147 {
8148 shl->first_lnum = shl->lnum
8149 + shl->rm.endpos[0].lnum
8150 - shl->rm.startpos[0].lnum;
8151 n = shl->rm.endpos[0].col;
8152 }
8153 else
8154 {
8155 ++shl->first_lnum;
8156 n = 0;
8157 }
8158 }
8159 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008160 if (shl != &search_hl && cur != NULL)
8161 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 }
8163}
8164
8165/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008166 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 * Uses shl->buf.
8168 * Sets shl->lnum and shl->rm contents.
8169 * Note: Assumes a previous match is always before "lnum", unless
8170 * shl->lnum is zero.
8171 * Careful: Any pointers for buffer lines will become invalid.
8172 */
8173 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008174next_search_hl(
8175 win_T *win,
8176 match_T *shl, /* points to search_hl or a match */
8177 linenr_T lnum,
8178 colnr_T mincol, /* minimal column for a match */
8179 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180{
8181 linenr_T l;
8182 colnr_T matchcol;
8183 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008184 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008186 // for :{range}s/pat only highlight inside the range
8187 if (lnum < search_first_line || lnum > search_last_line)
8188 {
8189 shl->lnum = 0;
8190 return;
8191 }
8192
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 if (shl->lnum != 0)
8194 {
8195 /* Check for three situations:
8196 * 1. If the "lnum" is below a previous match, start a new search.
8197 * 2. If the previous match includes "mincol", use it.
8198 * 3. Continue after the previous match.
8199 */
8200 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8201 if (lnum > l)
8202 shl->lnum = 0;
8203 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8204 return;
8205 }
8206
8207 /*
8208 * Repeat searching for a match until one is found that includes "mincol"
8209 * or none is found in this line.
8210 */
8211 called_emsg = FALSE;
8212 for (;;)
8213 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008214#ifdef FEAT_RELTIME
8215 /* Stop searching after passing the time limit. */
8216 if (profile_passed_limit(&(shl->tm)))
8217 {
8218 shl->lnum = 0; /* no match found in time */
8219 break;
8220 }
8221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 /* Three situations:
8223 * 1. No useful previous match: search from start of line.
8224 * 2. Not Vi compatible or empty match: continue at next character.
8225 * Break the loop if this is beyond the end of the line.
8226 * 3. Vi compatible searching: continue at end of previous match.
8227 */
8228 if (shl->lnum == 0)
8229 matchcol = 0;
8230 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8231 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008232 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008234 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008235
8236 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008237 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008238 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008240 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 shl->lnum = 0;
8242 break;
8243 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008244 if (has_mbyte)
8245 matchcol += mb_ptr2len(ml);
8246 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008247 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 }
8249 else
8250 matchcol = shl->rm.endpos[0].col;
8251
8252 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008253 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008255 /* Remember whether shl->rm is using a copy of the regprog in
8256 * cur->match. */
8257 int regprog_is_copy = (shl != &search_hl && cur != NULL
8258 && shl == &cur->hl
8259 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008260 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008261
Bram Moolenaarb3414592014-06-17 17:48:32 +02008262 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8263 matchcol,
8264#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008265 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008266#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008267 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008268#endif
8269 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008270 /* Copy the regprog, in case it got freed and recompiled. */
8271 if (regprog_is_copy)
8272 cur->match.regprog = cur->hl.rm.regprog;
8273
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008274 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008275 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008276 /* Error while handling regexp: stop using this regexp. */
8277 if (shl == &search_hl)
8278 {
8279 /* don't free regprog in the match list, it's a copy */
8280 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008281 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008282 }
8283 shl->rm.regprog = NULL;
8284 shl->lnum = 0;
8285 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8286 message */
8287 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008288 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008289 }
8290 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008291 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008292 else
8293 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 if (nmatched == 0)
8295 {
8296 shl->lnum = 0; /* no match found */
8297 break;
8298 }
8299 if (shl->rm.startpos[0].lnum > 0
8300 || shl->rm.startpos[0].col >= mincol
8301 || nmatched > 1
8302 || shl->rm.endpos[0].col > mincol)
8303 {
8304 shl->lnum += shl->rm.startpos[0].lnum;
8305 break; /* useful match found */
8306 }
8307 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008308
8309 // Restore called_emsg for assert_fails().
8310 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312
Bram Moolenaar85077472016-10-16 14:35:48 +02008313/*
8314 * If there is a match fill "shl" and return one.
8315 * Return zero otherwise.
8316 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008317 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008318next_search_hl_pos(
8319 match_T *shl, /* points to a match */
8320 linenr_T lnum,
8321 posmatch_T *posmatch, /* match positions */
8322 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008323{
8324 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008325 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008326
Bram Moolenaarb3414592014-06-17 17:48:32 +02008327 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8328 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008329 llpos_T *pos = &posmatch->pos[i];
8330
8331 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008332 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008333 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008334 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008335 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008336 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008337 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008338 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008339 /* if this match comes before the one at "found" then swap
8340 * them */
8341 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008342 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008343 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008344
Bram Moolenaar85077472016-10-16 14:35:48 +02008345 *pos = posmatch->pos[found];
8346 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008347 }
8348 }
8349 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008350 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008351 }
8352 }
8353 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008354 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008355 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008356 colnr_T start = posmatch->pos[found].col == 0
8357 ? 0 : posmatch->pos[found].col - 1;
8358 colnr_T end = posmatch->pos[found].col == 0
8359 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008360
Bram Moolenaar85077472016-10-16 14:35:48 +02008361 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008362 shl->rm.startpos[0].lnum = 0;
8363 shl->rm.startpos[0].col = start;
8364 shl->rm.endpos[0].lnum = 0;
8365 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008366 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008367 posmatch->cur = found + 1;
8368 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008369 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008370 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008371}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008372#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008373
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008375screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376{
8377 attrentry_T *aep = NULL;
8378
8379 screen_attr = attr;
8380 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008381#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 && termcap_active
8383#endif
8384 )
8385 {
8386#ifdef FEAT_GUI
8387 if (gui.in_use)
8388 {
8389 char buf[20];
8390
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008391 /* The GUI handles this internally. */
8392 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 OUT_STR(buf);
8394 }
8395 else
8396#endif
8397 {
8398 if (attr > HL_ALL) /* special HL attr. */
8399 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008400 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 aep = syn_cterm_attr2entry(attr);
8402 else
8403 aep = syn_term_attr2entry(attr);
8404 if (aep == NULL) /* did ":syntax clear" */
8405 attr = 0;
8406 else
8407 attr = aep->ae_attr;
8408 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008409 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008411 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008412#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008413 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8414 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8415 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008416#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008417 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008418 /* If the Normal FG color has BOLD attribute and the new HL
8419 * has a FG color defined, clear BOLD. */
8420 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008421 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008423 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008424 out_str(T_UCS);
8425 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008426 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8427 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008429 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008431 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008433 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008434 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435
8436 /*
8437 * Output the color or start string after bold etc., in case the
8438 * bold etc. override the color setting.
8439 */
8440 if (aep != NULL)
8441 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008442#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008443 /* When 'termguicolors' is set but fg or bg is unset,
8444 * fall back to the cterm colors. This helps for SpellBad,
8445 * where the GUI uses a red undercurl. */
8446 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008448 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008449 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008450 }
8451 else
8452#endif
8453 if (t_colors > 1)
8454 {
8455 if (aep->ae_u.cterm.fg_color)
8456 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8457 }
8458#ifdef FEAT_TERMGUICOLORS
8459 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8460 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008461 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008462 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 }
8464 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008465#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008466 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008468 if (aep->ae_u.cterm.bg_color)
8469 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8470 }
8471
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008472 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008473 {
8474 if (aep->ae_u.term.start != NULL)
8475 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 }
8477 }
8478 }
8479 }
8480}
8481
8482 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008483screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
8485 int do_ME = FALSE; /* output T_ME code */
8486
8487 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008488#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 && termcap_active
8490#endif
8491 )
8492 {
8493#ifdef FEAT_GUI
8494 if (gui.in_use)
8495 {
8496 char buf[20];
8497
8498 /* use internal GUI code */
8499 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8500 OUT_STR(buf);
8501 }
8502 else
8503#endif
8504 {
8505 if (screen_attr > HL_ALL) /* special HL attr. */
8506 {
8507 attrentry_T *aep;
8508
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008509 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 {
8511 /*
8512 * Assume that t_me restores the original colors!
8513 */
8514 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008515 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008516#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008517 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8518 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8519 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008520#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008521 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008522#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008523 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8524 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8525 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008526#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008527 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 do_ME = TRUE;
8529 }
8530 else
8531 {
8532 aep = syn_term_attr2entry(screen_attr);
8533 if (aep != NULL && aep->ae_u.term.stop != NULL)
8534 {
8535 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8536 do_ME = TRUE;
8537 else
8538 out_str(aep->ae_u.term.stop);
8539 }
8540 }
8541 if (aep == NULL) /* did ":syntax clear" */
8542 screen_attr = 0;
8543 else
8544 screen_attr = aep->ae_attr;
8545 }
8546
8547 /*
8548 * Often all ending-codes are equal to T_ME. Avoid outputting the
8549 * same sequence several times.
8550 */
8551 if (screen_attr & HL_STANDOUT)
8552 {
8553 if (STRCMP(T_SE, T_ME) == 0)
8554 do_ME = TRUE;
8555 else
8556 out_str(T_SE);
8557 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008558 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008559 {
8560 if (STRCMP(T_UCE, T_ME) == 0)
8561 do_ME = TRUE;
8562 else
8563 out_str(T_UCE);
8564 }
8565 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008566 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 {
8568 if (STRCMP(T_UE, T_ME) == 0)
8569 do_ME = TRUE;
8570 else
8571 out_str(T_UE);
8572 }
8573 if (screen_attr & HL_ITALIC)
8574 {
8575 if (STRCMP(T_CZR, T_ME) == 0)
8576 do_ME = TRUE;
8577 else
8578 out_str(T_CZR);
8579 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008580 if (screen_attr & HL_STRIKETHROUGH)
8581 {
8582 if (STRCMP(T_STE, T_ME) == 0)
8583 do_ME = TRUE;
8584 else
8585 out_str(T_STE);
8586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8588 out_str(T_ME);
8589
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008590#ifdef FEAT_TERMGUICOLORS
8591 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008593 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008594 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008595 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008596 term_bg_rgb_color(cterm_normal_bg_gui_color);
8597 }
8598 else
8599#endif
8600 {
8601 if (t_colors > 1)
8602 {
8603 /* set Normal cterm colors */
8604 if (cterm_normal_fg_color != 0)
8605 term_fg_color(cterm_normal_fg_color - 1);
8606 if (cterm_normal_bg_color != 0)
8607 term_bg_color(cterm_normal_bg_color - 1);
8608 if (cterm_normal_fg_bold)
8609 out_str(T_MD);
8610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 }
8612 }
8613 }
8614 screen_attr = 0;
8615}
8616
8617/*
8618 * Reset the colors for a cterm. Used when leaving Vim.
8619 * The machine specific code may override this again.
8620 */
8621 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008622reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008624 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 {
8626 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008627#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008628 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8629 || cterm_normal_bg_gui_color != INVALCOLOR)
8630 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008631#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 {
8635 out_str(T_OP);
8636 screen_attr = -1;
8637 }
8638 if (cterm_normal_fg_bold)
8639 {
8640 out_str(T_ME);
8641 screen_attr = -1;
8642 }
8643 }
8644}
8645
8646/*
8647 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8648 * using the attributes from ScreenAttrs["off"].
8649 */
8650 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008651screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652{
8653 int attr;
8654
8655 /* Check for illegal values, just in case (could happen just after
8656 * resizing). */
8657 if (row >= screen_Rows || col >= screen_Columns)
8658 return;
8659
Bram Moolenaarae654382019-01-17 21:09:05 +01008660#ifdef FEAT_INS_EXPAND
Bram Moolenaar33796b32019-06-08 16:01:13 +02008661 // Skip if under the popup menu.
8662 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
8663 if (pum_under_menu(row, col)
8664# ifdef FEAT_TEXT_PROP
8665 && screen_zindex <= POPUPMENU_ZINDEX
8666# endif
8667 )
Bram Moolenaarae654382019-01-17 21:09:05 +01008668 return;
8669#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008670#ifdef FEAT_TEXT_PROP
8671 // Skip if under a(nother) popup.
8672 if (popup_mask[row * screen_Columns + col] > screen_zindex)
8673 return;
8674#endif
8675
Bram Moolenaar494838a2015-02-10 19:20:37 +01008676 /* Outputting a character in the last cell on the screen may scroll the
8677 * screen up. Only do it when the "xn" termcap property is set, otherwise
8678 * mark the character invalid (update it when scrolled up). */
8679 if (*T_XN == NUL
8680 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681#ifdef FEAT_RIGHTLEFT
8682 /* account for first command-line character in rightleft mode */
8683 && !cmdmsg_rl
8684#endif
8685 )
8686 {
8687 ScreenAttrs[off] = (sattr_T)-1;
8688 return;
8689 }
8690
8691 /*
8692 * Stop highlighting first, so it's easier to move the cursor.
8693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 if (screen_char_attr != 0)
8695 attr = screen_char_attr;
8696 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 attr = ScreenAttrs[off];
8698 if (screen_attr != attr)
8699 screen_stop_highlight();
8700
8701 windgoto(row, col);
8702
8703 if (screen_attr != attr)
8704 screen_start_highlight(attr);
8705
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 if (enc_utf8 && ScreenLinesUC[off] != 0)
8707 {
8708 char_u buf[MB_MAXBYTES + 1];
8709
Bram Moolenaarcb070082016-04-02 22:14:51 +02008710 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008711 {
8712 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008713#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008714 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008715#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008716 )
8717 {
8718 /* Clear the two screen cells. If the character is actually
8719 * single width it won't change the second cell. */
8720 out_str((char_u *)" ");
8721 term_windgoto(row, col);
8722 }
8723 /* not sure where the cursor is after drawing the ambiguous width
8724 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008725 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008726 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008727 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008729
8730 /* Convert the UTF-8 character to bytes and write it. */
8731 buf[utfc_char2bytes(off, buf)] = NUL;
8732 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 }
8734 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 /* double-byte character in single-width cell */
8739 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8740 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 }
8742
8743 screen_cur_col++;
8744}
8745
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746/*
8747 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8748 * on the screen at position 'row' and 'col'.
8749 * The attributes of the first byte is used for all. This is required to
8750 * output the two bytes of a double-byte character with nothing in between.
8751 */
8752 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008753screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754{
8755 /* Check for illegal values (could be wrong when screen was resized). */
8756 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8757 return;
8758
8759 /* Outputting the last character on the screen may scrollup the screen.
8760 * Don't to it! Mark the character invalid (update it when scrolled up) */
8761 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8762 {
8763 ScreenAttrs[off] = (sattr_T)-1;
8764 return;
8765 }
8766
8767 /* Output the first byte normally (positions the cursor), then write the
8768 * second byte directly. */
8769 screen_char(off, row, col);
8770 out_char(ScreenLines[off + 1]);
8771 ++screen_cur_col;
8772}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774/*
8775 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8776 * This uses the contents of ScreenLines[] and doesn't change it.
8777 */
8778 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008779screen_draw_rectangle(
8780 int row,
8781 int col,
8782 int height,
8783 int width,
8784 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785{
8786 int r, c;
8787 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008788 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008790 /* Can't use ScreenLines unless initialized */
8791 if (ScreenLines == NULL)
8792 return;
8793
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 if (invert)
8795 screen_char_attr = HL_INVERSE;
8796 for (r = row; r < row + height; ++r)
8797 {
8798 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008799 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 for (c = col; c < col + width; ++c)
8801 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008802 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 {
8804 screen_char_2(off + c, r, c);
8805 ++c;
8806 }
8807 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 {
8809 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008810 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 }
8813 }
8814 }
8815 screen_char_attr = 0;
8816}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818/*
8819 * Redraw the characters for a vertically split window.
8820 */
8821 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008822redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823{
8824 int col;
8825 int width;
8826
8827# ifdef FEAT_CLIPBOARD
8828 clip_may_clear_selection(row, end - 1);
8829# endif
8830
8831 if (wp == NULL)
8832 {
8833 col = 0;
8834 width = Columns;
8835 }
8836 else
8837 {
8838 col = wp->w_wincol;
8839 width = wp->w_width;
8840 }
8841 screen_draw_rectangle(row, col, end - row, width, FALSE);
8842}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008844 static void
8845space_to_screenline(int off, int attr)
8846{
8847 ScreenLines[off] = ' ';
8848 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008849 if (enc_utf8)
8850 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008851}
8852
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853/*
8854 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8855 * with character 'c1' in first column followed by 'c2' in the other columns.
8856 * Use attributes 'attr'.
8857 */
8858 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008859screen_fill(
8860 int start_row,
8861 int end_row,
8862 int start_col,
8863 int end_col,
8864 int c1,
8865 int c2,
8866 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867{
8868 int row;
8869 int col;
8870 int off;
8871 int end_off;
8872 int did_delete;
8873 int c;
8874 int norm_term;
8875#if defined(FEAT_GUI) || defined(UNIX)
8876 int force_next = FALSE;
8877#endif
8878
8879 if (end_row > screen_Rows) /* safety check */
8880 end_row = screen_Rows;
8881 if (end_col > screen_Columns) /* safety check */
8882 end_col = screen_Columns;
8883 if (ScreenLines == NULL
8884 || start_row >= end_row
8885 || start_col >= end_col) /* nothing to do */
8886 return;
8887
8888 /* it's a "normal" terminal when not in a GUI or cterm */
8889 norm_term = (
8890#ifdef FEAT_GUI
8891 !gui.in_use &&
8892#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008893 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 for (row = start_row; row < end_row; ++row)
8895 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008896 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008897#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008898 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008899#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008900 )
8901 {
8902 /* When drawing over the right halve of a double-wide char clear
8903 * out the left halve. When drawing over the left halve of a
8904 * double wide-char clear out the right halve. Only needed in a
8905 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008906 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008907 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008908 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008909 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 /*
8912 * Try to use delete-line termcap code, when no attributes or in a
8913 * "normal" terminal, where a bold/italic space is just a
8914 * space.
8915 */
8916 did_delete = FALSE;
8917 if (c2 == ' '
8918 && end_col == Columns
8919 && can_clear(T_CE)
8920 && (attr == 0
8921 || (norm_term
8922 && attr <= HL_ALL
8923 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8924 {
8925 /*
8926 * check if we really need to clear something
8927 */
8928 col = start_col;
8929 if (c1 != ' ') /* don't clear first char */
8930 ++col;
8931
8932 off = LineOffset[row] + col;
8933 end_off = LineOffset[row] + end_col;
8934
8935 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 if (enc_utf8)
8937 while (off < end_off && ScreenLines[off] == ' '
8938 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8939 ++off;
8940 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 while (off < end_off && ScreenLines[off] == ' '
8942 && ScreenAttrs[off] == 0)
8943 ++off;
8944 if (off < end_off) /* something to be cleared */
8945 {
8946 col = off - LineOffset[row];
8947 screen_stop_highlight();
8948 term_windgoto(row, col);/* clear rest of this screen line */
8949 out_str(T_CE);
8950 screen_start(); /* don't know where cursor is now */
8951 col = end_col - col;
8952 while (col--) /* clear chars in ScreenLines */
8953 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008954 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 ++off;
8956 }
8957 }
8958 did_delete = TRUE; /* the chars are cleared now */
8959 }
8960
8961 off = LineOffset[row] + start_col;
8962 c = c1;
8963 for (col = start_col; col < end_col; ++col)
8964 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02008965 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008966 || (enc_utf8 && (int)ScreenLinesUC[off]
8967 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 || ScreenAttrs[off] != attr
8969#if defined(FEAT_GUI) || defined(UNIX)
8970 || force_next
8971#endif
8972 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02008973#ifdef FEAT_TEXT_PROP
8974 // Skip if under a(nother) popup.
8975 && popup_mask[row * screen_Columns + col] <= screen_zindex
8976#endif
8977 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 {
8979#if defined(FEAT_GUI) || defined(UNIX)
8980 /* The bold trick may make a single row of pixels appear in
8981 * the next character. When a bold character is removed, the
8982 * next character should be redrawn too. This happens for our
8983 * own GUI and for some xterms. */
8984 if (
8985# ifdef FEAT_GUI
8986 gui.in_use
8987# endif
8988# if defined(FEAT_GUI) && defined(UNIX)
8989 ||
8990# endif
8991# ifdef UNIX
8992 term_is_xterm
8993# endif
8994 )
8995 {
8996 if (ScreenLines[off] != ' '
8997 && (ScreenAttrs[off] > HL_ALL
8998 || ScreenAttrs[off] & HL_BOLD))
8999 force_next = TRUE;
9000 else
9001 force_next = FALSE;
9002 }
9003#endif
9004 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 if (enc_utf8)
9006 {
9007 if (c >= 0x80)
9008 {
9009 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009010 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 }
9012 else
9013 ScreenLinesUC[off] = 0;
9014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 ScreenAttrs[off] = attr;
9016 if (!did_delete || c != ' ')
9017 screen_char(off, row, col);
9018 }
9019 ++off;
9020 if (col == start_col)
9021 {
9022 if (did_delete)
9023 break;
9024 c = c2;
9025 }
9026 }
9027 if (end_col == Columns)
9028 LineWraps[row] = FALSE;
9029 if (row == Rows - 1) /* overwritten the command line */
9030 {
9031 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02009032 if (start_col == 0 && end_col == Columns
9033 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009035 if (start_col == 0)
9036 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 }
9038 }
9039}
9040
9041/*
9042 * Check if there should be a delay. Used before clearing or redrawing the
9043 * screen or the command line.
9044 */
9045 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009046check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047{
9048 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
9049 && !did_wait_return
9050 && emsg_silent == 0)
9051 {
9052 out_flush();
9053 ui_delay(1000L, TRUE);
9054 emsg_on_display = FALSE;
9055 if (check_msg_scroll)
9056 msg_scroll = FALSE;
9057 }
9058}
9059
9060/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009061 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
9062 */
9063 static void
9064clear_TabPageIdxs(void)
9065{
9066 int scol;
9067
9068 for (scol = 0; scol < Columns; ++scol)
9069 TabPageIdxs[scol] = 0;
9070}
9071
9072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009074 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 * Returns TRUE if there is a valid screen to write to.
9076 * Returns FALSE when starting up and screen not initialized yet.
9077 */
9078 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009079screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009081 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 return (ScreenLines != NULL);
9083}
9084
9085/*
9086 * Resize the shell to Rows and Columns.
9087 * Allocate ScreenLines[] and associated items.
9088 *
9089 * There may be some time between setting Rows and Columns and (re)allocating
9090 * ScreenLines[]. This happens when starting up and when (manually) changing
9091 * the shell size. Always use screen_Rows and screen_Columns to access items
9092 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
9093 * final size of the shell is needed.
9094 */
9095 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009096screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097{
9098 int new_row, old_row;
9099#ifdef FEAT_GUI
9100 int old_Rows;
9101#endif
9102 win_T *wp;
9103 int outofmem = FALSE;
9104 int len;
9105 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009107 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009109 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 sattr_T *new_ScreenAttrs;
9111 unsigned *new_LineOffset;
9112 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009113 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02009114#ifdef FEAT_TEXT_PROP
9115 short *new_popup_mask;
9116#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00009117 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009119 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009120 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009122retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 /*
9124 * Allocation of the screen buffers is done only when the size changes and
9125 * when Rows and Columns have been set and we have started doing full
9126 * screen stuff.
9127 */
9128 if ((ScreenLines != NULL
9129 && Rows == screen_Rows
9130 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 && enc_utf8 == (ScreenLinesUC != NULL)
9132 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01009133 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 || Rows == 0
9135 || Columns == 0
9136 || (!full_screen && ScreenLines == NULL))
9137 return;
9138
9139 /*
9140 * It's possible that we produce an out-of-memory message below, which
9141 * will cause this function to be called again. To break the loop, just
9142 * return here.
9143 */
9144 if (entered)
9145 return;
9146 entered = TRUE;
9147
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009148 /*
9149 * Note that the window sizes are updated before reallocating the arrays,
9150 * thus we must not redraw here!
9151 */
9152 ++RedrawingDisabled;
9153
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 win_new_shellsize(); /* fit the windows in the new sized shell */
9155
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 comp_col(); /* recompute columns for shown command and ruler */
9157
9158 /*
9159 * We're changing the size of the screen.
9160 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9161 * - Move lines from the old arrays into the new arrays, clear extra
9162 * lines (unless the screen is going to be cleared).
9163 * - Free the old arrays.
9164 *
9165 * If anything fails, make ScreenLines NULL, so we don't do anything!
9166 * Continuing with the old ScreenLines may result in a crash, because the
9167 * size is wrong.
9168 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009169 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009171 if (aucmd_win != NULL)
9172 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009173#ifdef FEAT_TEXT_PROP
9174 // global popup windows
9175 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9176 win_free_lsize(wp);
9177 // tab-local popup windows
9178 FOR_ALL_TABPAGES(tp)
9179 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9180 win_free_lsize(wp);
9181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009183 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01009184 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 if (enc_utf8)
9186 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009187 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009188 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009189 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
9190 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 }
9192 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009193 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
9194 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
9195 new_LineOffset = LALLOC_MULT(unsigned, Rows);
9196 new_LineWraps = LALLOC_MULT(char_u, Rows);
9197 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009198#ifdef FEAT_TEXT_PROP
9199 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
9200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009202 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 {
9204 if (win_alloc_lines(wp) == FAIL)
9205 {
9206 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009207 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 }
9209 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009210 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9211 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009212 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009213#ifdef FEAT_TEXT_PROP
9214 // global popup windows
9215 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9216 if (win_alloc_lines(wp) == FAIL)
9217 {
9218 outofmem = TRUE;
9219 goto give_up;
9220 }
9221 // tab-local popup windows
9222 FOR_ALL_TABPAGES(tp)
9223 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9224 if (win_alloc_lines(wp) == FAIL)
9225 {
9226 outofmem = TRUE;
9227 goto give_up;
9228 }
9229#endif
9230
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009231give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009233 for (i = 0; i < p_mco; ++i)
9234 if (new_ScreenLinesC[i] == NULL)
9235 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009237 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 || new_ScreenAttrs == NULL
9240 || new_LineOffset == NULL
9241 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009242 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02009243#ifdef FEAT_TEXT_PROP
9244 || new_popup_mask == NULL
9245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 || outofmem)
9247 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009248 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009249 {
9250 /* guess the size */
9251 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9252
9253 /* Remember we did this to avoid getting outofmem messages over
9254 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009255 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009256 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009257 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009258 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009259 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009260 VIM_CLEAR(new_ScreenLinesC[i]);
9261 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009262 VIM_CLEAR(new_ScreenAttrs);
9263 VIM_CLEAR(new_LineOffset);
9264 VIM_CLEAR(new_LineWraps);
9265 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009266#ifdef FEAT_TEXT_PROP
9267 VIM_CLEAR(new_popup_mask);
9268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 }
9270 else
9271 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009272 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009273
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 for (new_row = 0; new_row < Rows; ++new_row)
9275 {
9276 new_LineOffset[new_row] = new_row * Columns;
9277 new_LineWraps[new_row] = FALSE;
9278
9279 /*
9280 * If the screen is not going to be cleared, copy as much as
9281 * possible from the old screen to the new one and clear the rest
9282 * (used when resizing the window at the "--more--" prompt or when
9283 * executing an external command, for the GUI).
9284 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009285 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 {
9287 (void)vim_memset(new_ScreenLines + new_row * Columns,
9288 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 if (enc_utf8)
9290 {
9291 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9292 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009293 for (i = 0; i < p_mco; ++i)
9294 (void)vim_memset(new_ScreenLinesC[i]
9295 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 0, (size_t)Columns * sizeof(u8char_T));
9297 }
9298 if (enc_dbcs == DBCS_JPNU)
9299 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9300 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9302 0, (size_t)Columns * sizeof(sattr_T));
9303 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009304 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 {
9306 if (screen_Columns < Columns)
9307 len = screen_Columns;
9308 else
9309 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009310 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009311 * may be invalid now. Also when p_mco changes. */
9312 if (!(enc_utf8 && ScreenLinesUC == NULL)
9313 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009314 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9315 ScreenLines + LineOffset[old_row],
9316 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009317 if (enc_utf8 && ScreenLinesUC != NULL
9318 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 {
9320 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9321 ScreenLinesUC + LineOffset[old_row],
9322 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009323 for (i = 0; i < p_mco; ++i)
9324 mch_memmove(new_ScreenLinesC[i]
9325 + new_LineOffset[new_row],
9326 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 (size_t)len * sizeof(u8char_T));
9328 }
9329 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9330 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9331 ScreenLines2 + LineOffset[old_row],
9332 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9334 ScreenAttrs + LineOffset[old_row],
9335 (size_t)len * sizeof(sattr_T));
9336 }
9337 }
9338 }
9339 /* Use the last line of the screen for the current line. */
9340 current_ScreenLine = new_ScreenLines + Rows * Columns;
9341 }
9342
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009343 free_screenlines();
9344
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009347 for (i = 0; i < p_mco; ++i)
9348 ScreenLinesC[i] = new_ScreenLinesC[i];
9349 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 ScreenAttrs = new_ScreenAttrs;
9352 LineOffset = new_LineOffset;
9353 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009354 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02009355#ifdef FEAT_TEXT_PROP
9356 popup_mask = new_popup_mask;
Bram Moolenaar1748c7f2019-06-08 16:55:15 +02009357 vim_memset(popup_mask, 0, Rows * Columns * sizeof(short));
Bram Moolenaar33796b32019-06-08 16:01:13 +02009358 popup_mask_refresh = TRUE;
9359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360
9361 /* It's important that screen_Rows and screen_Columns reflect the actual
9362 * size of ScreenLines[]. Set them before calling anything. */
9363#ifdef FEAT_GUI
9364 old_Rows = screen_Rows;
9365#endif
9366 screen_Rows = Rows;
9367 screen_Columns = Columns;
9368
9369 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009370 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372#ifdef FEAT_GUI
9373 else if (gui.in_use
9374 && !gui.starting
9375 && ScreenLines != NULL
9376 && old_Rows != Rows)
9377 {
9378 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9379 /*
9380 * Adjust the position of the cursor, for when executing an external
9381 * command.
9382 */
9383 if (msg_row >= Rows) /* Rows got smaller */
9384 msg_row = Rows - 1; /* put cursor at last row */
9385 else if (Rows > old_Rows) /* Rows got bigger */
9386 msg_row += Rows - old_Rows; /* put cursor in same place */
9387 if (msg_col >= Columns) /* Columns got smaller */
9388 msg_col = Columns - 1; /* put cursor at last column */
9389 }
9390#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009391 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009394 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009395
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009396 /*
9397 * Do not apply autocommands more than 3 times to avoid an endless loop
9398 * in case applying autocommands always changes Rows or Columns.
9399 */
9400 if (starting == 0 && ++retry_count <= 3)
9401 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009402 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009403 /* In rare cases, autocommands may have altered Rows or Columns,
9404 * jump back to check if we need to allocate the screen again. */
9405 goto retry;
9406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407}
9408
9409 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009410free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009411{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009412 int i;
9413
Bram Moolenaar33796b32019-06-08 16:01:13 +02009414 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009415 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009416 VIM_CLEAR(ScreenLinesC[i]);
9417 VIM_CLEAR(ScreenLines2);
9418 VIM_CLEAR(ScreenLines);
9419 VIM_CLEAR(ScreenAttrs);
9420 VIM_CLEAR(LineOffset);
9421 VIM_CLEAR(LineWraps);
9422 VIM_CLEAR(TabPageIdxs);
9423#ifdef FEAT_TEXT_PROP
9424 VIM_CLEAR(popup_mask);
9425#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009426}
9427
9428 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009429screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430{
9431 check_for_delay(FALSE);
9432 screenalloc(FALSE); /* allocate screen buffers if size changed */
9433 screenclear2(); /* clear the screen */
9434}
9435
9436 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009437screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438{
9439 int i;
9440
9441 if (starting == NO_SCREEN || ScreenLines == NULL
9442#ifdef FEAT_GUI
9443 || (gui.in_use && gui.starting)
9444#endif
9445 )
9446 return;
9447
9448#ifdef FEAT_GUI
9449 if (!gui.in_use)
9450#endif
9451 screen_attr = -1; /* force setting the Normal colors */
9452 screen_stop_highlight(); /* don't want highlighting here */
9453
9454#ifdef FEAT_CLIPBOARD
9455 /* disable selection without redrawing it */
9456 clip_scroll_selection(9999);
9457#endif
9458
9459 /* blank out ScreenLines */
9460 for (i = 0; i < Rows; ++i)
9461 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009462 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 LineWraps[i] = FALSE;
9464 }
9465
9466 if (can_clear(T_CL))
9467 {
9468 out_str(T_CL); /* clear the display */
9469 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009470 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 }
9472 else
9473 {
9474 /* can't clear the screen, mark all chars with invalid attributes */
9475 for (i = 0; i < Rows; ++i)
9476 lineinvalid(LineOffset[i], (int)Columns);
9477 clear_cmdline = TRUE;
9478 }
9479
9480 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9481
9482 win_rest_invalid(firstwin);
9483 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009484 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 if (must_redraw == CLEAR) /* no need to clear again */
9486 must_redraw = NOT_VALID;
9487 compute_cmdrow();
9488 msg_row = cmdline_row; /* put cursor on last line for messages */
9489 msg_col = 0;
9490 screen_start(); /* don't know where cursor is now */
9491 msg_scrolled = 0; /* can't scroll back */
9492 msg_didany = FALSE;
9493 msg_didout = FALSE;
9494}
9495
9496/*
9497 * Clear one line in ScreenLines.
9498 */
9499 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009500lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
9502 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 if (enc_utf8)
9504 (void)vim_memset(ScreenLinesUC + off, 0,
9505 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009506 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507}
9508
9509/*
9510 * Mark one line in ScreenLines invalid by setting the attributes to an
9511 * invalid value.
9512 */
9513 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009514lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515{
9516 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9517}
9518
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519/*
9520 * Copy part of a Screenline for vertically split window "wp".
9521 */
9522 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009523linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524{
9525 unsigned off_to = LineOffset[to] + wp->w_wincol;
9526 unsigned off_from = LineOffset[from] + wp->w_wincol;
9527
9528 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9529 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 if (enc_utf8)
9531 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009532 int i;
9533
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9535 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009536 for (i = 0; i < p_mco; ++i)
9537 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9538 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 }
9540 if (enc_dbcs == DBCS_JPNU)
9541 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9542 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9544 wp->w_width * sizeof(sattr_T));
9545}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546
9547/*
9548 * Return TRUE if clearing with term string "p" would work.
9549 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02009550 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 */
9552 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009553can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554{
9555 return (*p != NUL && (t_colors <= 1
9556#ifdef FEAT_GUI
9557 || gui.in_use
9558#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009559#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009560 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009561 || (!p_tgc && cterm_normal_bg_color == 0)
9562#else
9563 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009564#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009565 || *T_UT != NUL)
9566#ifdef FEAT_TEXT_PROP
9567 && !(p == T_CE && popup_visible)
9568#endif
9569 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570}
9571
9572/*
9573 * Reset cursor position. Use whenever cursor was moved because of outputting
9574 * something directly to the screen (shell commands) or a terminal control
9575 * code.
9576 */
9577 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009578screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579{
9580 screen_cur_row = screen_cur_col = 9999;
9581}
9582
9583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 * Move the cursor to position "row","col" in the screen.
9585 * This tries to find the most efficient way to move, minimizing the number of
9586 * characters sent to the terminal.
9587 */
9588 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009589windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009591 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 int i;
9593 int plan;
9594 int cost;
9595 int wouldbe_col;
9596 int noinvcurs;
9597 char_u *bs;
9598 int goto_cost;
9599 int attr;
9600
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009601#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9603
9604#define PLAN_LE 1
9605#define PLAN_CR 2
9606#define PLAN_NL 3
9607#define PLAN_WRITE 4
9608 /* Can't use ScreenLines unless initialized */
9609 if (ScreenLines == NULL)
9610 return;
9611
9612 if (col != screen_cur_col || row != screen_cur_row)
9613 {
9614 /* Check for valid position. */
9615 if (row < 0) /* window without text lines? */
9616 row = 0;
9617 if (row >= screen_Rows)
9618 row = screen_Rows - 1;
9619 if (col >= screen_Columns)
9620 col = screen_Columns - 1;
9621
9622 /* check if no cursor movement is allowed in highlight mode */
9623 if (screen_attr && *T_MS == NUL)
9624 noinvcurs = HIGHL_COST;
9625 else
9626 noinvcurs = 0;
9627 goto_cost = GOTO_COST + noinvcurs;
9628
9629 /*
9630 * Plan how to do the positioning:
9631 * 1. Use CR to move it to column 0, same row.
9632 * 2. Use T_LE to move it a few columns to the left.
9633 * 3. Use NL to move a few lines down, column 0.
9634 * 4. Move a few columns to the right with T_ND or by writing chars.
9635 *
9636 * Don't do this if the cursor went beyond the last column, the cursor
9637 * position is unknown then (some terminals wrap, some don't )
9638 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009639 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 * characters to move the cursor to the right.
9641 */
9642 if (row >= screen_cur_row && screen_cur_col < Columns)
9643 {
9644 /*
9645 * If the cursor is in the same row, bigger col, we can use CR
9646 * or T_LE.
9647 */
9648 bs = NULL; /* init for GCC */
9649 attr = screen_attr;
9650 if (row == screen_cur_row && col < screen_cur_col)
9651 {
9652 /* "le" is preferred over "bc", because "bc" is obsolete */
9653 if (*T_LE)
9654 bs = T_LE; /* "cursor left" */
9655 else
9656 bs = T_BC; /* "backspace character (old) */
9657 if (*bs)
9658 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9659 else
9660 cost = 999;
9661 if (col + 1 < cost) /* using CR is less characters */
9662 {
9663 plan = PLAN_CR;
9664 wouldbe_col = 0;
9665 cost = 1; /* CR is just one character */
9666 }
9667 else
9668 {
9669 plan = PLAN_LE;
9670 wouldbe_col = col;
9671 }
9672 if (noinvcurs) /* will stop highlighting */
9673 {
9674 cost += noinvcurs;
9675 attr = 0;
9676 }
9677 }
9678
9679 /*
9680 * If the cursor is above where we want to be, we can use CR LF.
9681 */
9682 else if (row > screen_cur_row)
9683 {
9684 plan = PLAN_NL;
9685 wouldbe_col = 0;
9686 cost = (row - screen_cur_row) * 2; /* CR LF */
9687 if (noinvcurs) /* will stop highlighting */
9688 {
9689 cost += noinvcurs;
9690 attr = 0;
9691 }
9692 }
9693
9694 /*
9695 * If the cursor is in the same row, smaller col, just use write.
9696 */
9697 else
9698 {
9699 plan = PLAN_WRITE;
9700 wouldbe_col = screen_cur_col;
9701 cost = 0;
9702 }
9703
9704 /*
9705 * Check if any characters that need to be written have the
9706 * correct attributes. Also avoid UTF-8 characters.
9707 */
9708 i = col - wouldbe_col;
9709 if (i > 0)
9710 cost += i;
9711 if (cost < goto_cost && i > 0)
9712 {
9713 /*
9714 * Check if the attributes are correct without additionally
9715 * stopping highlighting.
9716 */
9717 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9718 while (i && *p++ == attr)
9719 --i;
9720 if (i != 0)
9721 {
9722 /*
9723 * Try if it works when highlighting is stopped here.
9724 */
9725 if (*--p == 0)
9726 {
9727 cost += noinvcurs;
9728 while (i && *p++ == 0)
9729 --i;
9730 }
9731 if (i != 0)
9732 cost = 999; /* different attributes, don't do it */
9733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 if (enc_utf8)
9735 {
9736 /* Don't use an UTF-8 char for positioning, it's slow. */
9737 for (i = wouldbe_col; i < col; ++i)
9738 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9739 {
9740 cost = 999;
9741 break;
9742 }
9743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 }
9745
9746 /*
9747 * We can do it without term_windgoto()!
9748 */
9749 if (cost < goto_cost)
9750 {
9751 if (plan == PLAN_LE)
9752 {
9753 if (noinvcurs)
9754 screen_stop_highlight();
9755 while (screen_cur_col > col)
9756 {
9757 out_str(bs);
9758 --screen_cur_col;
9759 }
9760 }
9761 else if (plan == PLAN_CR)
9762 {
9763 if (noinvcurs)
9764 screen_stop_highlight();
9765 out_char('\r');
9766 screen_cur_col = 0;
9767 }
9768 else if (plan == PLAN_NL)
9769 {
9770 if (noinvcurs)
9771 screen_stop_highlight();
9772 while (screen_cur_row < row)
9773 {
9774 out_char('\n');
9775 ++screen_cur_row;
9776 }
9777 screen_cur_col = 0;
9778 }
9779
9780 i = col - screen_cur_col;
9781 if (i > 0)
9782 {
9783 /*
9784 * Use cursor-right if it's one character only. Avoids
9785 * removing a line of pixels from the last bold char, when
9786 * using the bold trick in the GUI.
9787 */
9788 if (T_ND[0] != NUL && T_ND[1] == NUL)
9789 {
9790 while (i-- > 0)
9791 out_char(*T_ND);
9792 }
9793 else
9794 {
9795 int off;
9796
9797 off = LineOffset[row] + screen_cur_col;
9798 while (i-- > 0)
9799 {
9800 if (ScreenAttrs[off] != screen_attr)
9801 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 if (enc_dbcs == DBCS_JPNU
9805 && ScreenLines[off] == 0x8e)
9806 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 ++off;
9808 }
9809 }
9810 }
9811 }
9812 }
9813 else
9814 cost = 999;
9815
9816 if (cost >= goto_cost)
9817 {
9818 if (noinvcurs)
9819 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009820 if (row == screen_cur_row && (col > screen_cur_col)
9821 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 term_cursor_right(col - screen_cur_col);
9823 else
9824 term_windgoto(row, col);
9825 }
9826 screen_cur_row = row;
9827 screen_cur_col = col;
9828 }
9829}
9830
9831/*
9832 * Set cursor to its position in the current window.
9833 */
9834 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009835setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009837 setcursor_mayforce(FALSE);
9838}
9839
9840/*
9841 * Set cursor to its position in the current window.
9842 * When "force" is TRUE also when not redrawing.
9843 */
9844 void
9845setcursor_mayforce(int force)
9846{
9847 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 {
9849 validate_cursor();
9850 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009851 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009853 /* With 'rightleft' set and the cursor on a double-wide
9854 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009855 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9856 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009857 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009858 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859#endif
9860 curwin->w_wcol));
9861 }
9862}
9863
9864
9865/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009866 * Insert 'line_count' lines at 'row' in window 'wp'.
9867 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9868 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 * scrolling.
9870 * Returns FAIL if the lines are not inserted, OK for success.
9871 */
9872 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009873win_ins_lines(
9874 win_T *wp,
9875 int row,
9876 int line_count,
9877 int invalid,
9878 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879{
9880 int did_delete;
9881 int nextrow;
9882 int lastrow;
9883 int retval;
9884
9885 if (invalid)
9886 wp->w_lines_valid = 0;
9887
9888 if (wp->w_height < 5)
9889 return FAIL;
9890
9891 if (line_count > wp->w_height - row)
9892 line_count = wp->w_height - row;
9893
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009894 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 if (retval != MAYBE)
9896 return retval;
9897
9898 /*
9899 * If there is a next window or a status line, we first try to delete the
9900 * lines at the bottom to avoid messing what is after the window.
9901 * If this fails and there are following windows, don't do anything to avoid
9902 * messing up those windows, better just redraw.
9903 */
9904 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 if (wp->w_next != NULL || wp->w_status_height)
9906 {
9907 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009908 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 did_delete = TRUE;
9910 else if (wp->w_next)
9911 return FAIL;
9912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 /*
9914 * if no lines deleted, blank the lines that will end up below the window
9915 */
9916 if (!did_delete)
9917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009920 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 lastrow = nextrow + line_count;
9922 if (lastrow > Rows)
9923 lastrow = Rows;
9924 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009925 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 ' ', ' ', 0);
9927 }
9928
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009929 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 == FAIL)
9931 {
9932 /* deletion will have messed up other windows */
9933 if (did_delete)
9934 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 win_rest_invalid(W_NEXT(wp));
9937 }
9938 return FAIL;
9939 }
9940
9941 return OK;
9942}
9943
9944/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009945 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9947 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9948 * scrolling
9949 * Return OK for success, FAIL if the lines are not deleted.
9950 */
9951 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009952win_del_lines(
9953 win_T *wp,
9954 int row,
9955 int line_count,
9956 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009957 int mayclear,
9958 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959{
9960 int retval;
9961
9962 if (invalid)
9963 wp->w_lines_valid = 0;
9964
9965 if (line_count > wp->w_height - row)
9966 line_count = wp->w_height - row;
9967
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009968 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 if (retval != MAYBE)
9970 return retval;
9971
9972 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009973 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 return FAIL;
9975
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 /*
9977 * If there are windows or status lines below, try to put them at the
9978 * correct place. If we can't do that, they have to be redrawn.
9979 */
9980 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9981 {
9982 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009983 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 {
9985 wp->w_redr_status = TRUE;
9986 win_rest_invalid(wp->w_next);
9987 }
9988 }
9989 /*
9990 * If this is the last window and there is no status line, redraw the
9991 * command line later.
9992 */
9993 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 redraw_cmdline = TRUE;
9995 return OK;
9996}
9997
9998/*
9999 * Common code for win_ins_lines() and win_del_lines().
10000 * Returns OK or FAIL when the work has been done.
10001 * Returns MAYBE when not finished yet.
10002 */
10003 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010004win_do_lines(
10005 win_T *wp,
10006 int row,
10007 int line_count,
10008 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010009 int del,
10010 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011{
10012 int retval;
10013
10014 if (!redrawing() || line_count <= 0)
10015 return FAIL;
10016
Bram Moolenaar33796b32019-06-08 16:01:13 +020010017 // When inserting lines would result in loss of command output, just redraw
10018 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010019 if (no_win_do_lines_ins && !del)
10020 return FAIL;
10021
Bram Moolenaar33796b32019-06-08 16:01:13 +020010022 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +020010023 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010025 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +020010026 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 return FAIL;
10028 }
10029
Bram Moolenaar33796b32019-06-08 16:01:13 +020010030#ifdef FEAT_TEXT_PROP
10031 // this doesn't work when tere are popups visible
10032 if (popup_visible)
10033 return FAIL;
10034#endif
10035
10036 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 if (row + line_count >= wp->w_height)
10038 {
10039 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +020010040 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 ' ', ' ', 0);
10042 return OK;
10043 }
10044
10045 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010046 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010048 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010050 if (!no_win_do_lines_ins)
10051 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052
10053 /*
10054 * If the terminal can set a scroll region, use that.
10055 * Always do this in a vertically split window. This will redraw from
10056 * ScreenLines[] when t_CV isn't defined. That's faster than using
10057 * win_line().
10058 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010059 * a character in the lower right corner of the scroll region may cause a
10060 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 */
Bram Moolenaar02631462017-09-22 15:20:32 +020010062 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 scroll_region_set(wp, row);
10066 if (del)
10067 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010068 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 else
10070 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010071 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 scroll_region_reset();
10074 return retval;
10075 }
10076
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
10078 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079
10080 return MAYBE;
10081}
10082
10083/*
10084 * window 'wp' and everything after it is messed up, mark it for redraw
10085 */
10086 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010087win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 {
10091 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 wp->w_redr_status = TRUE;
10093 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 }
10095 redraw_cmdline = TRUE;
10096}
10097
10098/*
10099 * The rest of the routines in this file perform screen manipulations. The
10100 * given operation is performed physically on the screen. The corresponding
10101 * change is also made to the internal screen image. In this way, the editor
10102 * anticipates the effect of editing changes on the appearance of the screen.
10103 * That way, when we call screenupdate a complete redraw isn't usually
10104 * necessary. Another advantage is that we can keep adding code to anticipate
10105 * screen changes, and in the meantime, everything still works.
10106 */
10107
10108/*
10109 * types for inserting or deleting lines
10110 */
10111#define USE_T_CAL 1
10112#define USE_T_CDL 2
10113#define USE_T_AL 3
10114#define USE_T_CE 4
10115#define USE_T_DL 5
10116#define USE_T_SR 6
10117#define USE_NL 7
10118#define USE_T_CD 8
10119#define USE_REDRAW 9
10120
10121/*
10122 * insert lines on the screen and update ScreenLines[]
10123 * 'end' is the line after the scrolled part. Normally it is Rows.
10124 * When scrolling region used 'off' is the offset from the top for the region.
10125 * 'row' and 'end' are relative to the start of the region.
10126 *
10127 * return FAIL for failure, OK for success.
10128 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000010129 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010130screen_ins_lines(
10131 int off,
10132 int row,
10133 int line_count,
10134 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010135 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +010010136 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137{
10138 int i;
10139 int j;
10140 unsigned temp;
10141 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010142 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 int type;
10144 int result_empty;
10145 int can_ce = can_clear(T_CE);
10146
10147 /*
10148 * FAIL if
10149 * - there is no valid screen
10150 * - the screen has to be redrawn completely
10151 * - the line count is less than one
10152 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010153 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +020010154 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 */
Bram Moolenaar33796b32019-06-08 16:01:13 +020010156 if (!screen_valid(TRUE)
10157 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010158#ifdef FEAT_CLIPBOARD
10159 || (clip_star.state != SELECT_CLEARED
10160 && redrawing_for_callback > 0)
10161#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +020010162#ifdef FEAT_TEXT_PROP
10163 || popup_visible
10164#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010165 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 return FAIL;
10167
10168 /*
10169 * There are seven ways to insert lines:
10170 * 0. When in a vertically split window and t_CV isn't set, redraw the
10171 * characters from ScreenLines[].
10172 * 1. Use T_CD (clear to end of display) if it exists and the result of
10173 * the insert is just empty lines
10174 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
10175 * present or line_count > 1. It looks better if we do all the inserts
10176 * at once.
10177 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
10178 * insert is just empty lines and T_CE is not present or line_count >
10179 * 1.
10180 * 4. Use T_AL (insert line) if it exists.
10181 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10182 * just empty lines.
10183 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10184 * just empty lines.
10185 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10186 * the 'da' flag is not set or we have clear line capability.
10187 * 8. redraw the characters from ScreenLines[].
10188 *
10189 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10190 * the scrollbar for the window. It does have insert line, use that if it
10191 * exists.
10192 */
10193 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10195 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010196 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 type = USE_T_CD;
10198 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10199 type = USE_T_CAL;
10200 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10201 type = USE_T_CDL;
10202 else if (*T_AL != NUL)
10203 type = USE_T_AL;
10204 else if (can_ce && result_empty)
10205 type = USE_T_CE;
10206 else if (*T_DL != NUL && result_empty)
10207 type = USE_T_DL;
10208 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10209 type = USE_T_SR;
10210 else
10211 return FAIL;
10212
10213 /*
10214 * For clearing the lines screen_del_lines() is used. This will also take
10215 * care of t_db if necessary.
10216 */
10217 if (type == USE_T_CD || type == USE_T_CDL ||
10218 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010219 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220
10221 /*
10222 * If text is retained below the screen, first clear or delete as many
10223 * lines at the bottom of the window as are about to be inserted so that
10224 * the deleted lines won't later surface during a screen_del_lines.
10225 */
10226 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010227 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228
10229#ifdef FEAT_CLIPBOARD
10230 /* Remove a modeless selection when inserting lines halfway the screen
10231 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010232 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010233 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 else
10235 clip_scroll_selection(-line_count);
10236#endif
10237
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238#ifdef FEAT_GUI
10239 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10240 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010241 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242#endif
10243
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010244 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10245 cursor_col = wp->w_wincol;
10246
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 if (*T_CCS != NUL) /* cursor relative to region */
10248 cursor_row = row;
10249 else
10250 cursor_row = row + off;
10251
10252 /*
10253 * Shift LineOffset[] line_count down to reflect the inserted lines.
10254 * Clear the inserted lines in ScreenLines[].
10255 */
10256 row += off;
10257 end += off;
10258 for (i = 0; i < line_count; ++i)
10259 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 if (wp != NULL && wp->w_width != Columns)
10261 {
10262 /* need to copy part of a line */
10263 j = end - 1 - i;
10264 while ((j -= line_count) >= row)
10265 linecopy(j + line_count, j, wp);
10266 j += line_count;
10267 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010268 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10269 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 else
10271 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10272 LineWraps[j] = FALSE;
10273 }
10274 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 {
10276 j = end - 1 - i;
10277 temp = LineOffset[j];
10278 while ((j -= line_count) >= row)
10279 {
10280 LineOffset[j + line_count] = LineOffset[j];
10281 LineWraps[j + line_count] = LineWraps[j];
10282 }
10283 LineOffset[j + line_count] = temp;
10284 LineWraps[j + line_count] = FALSE;
10285 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010286 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 else
10288 lineinvalid(temp, (int)Columns);
10289 }
10290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291
10292 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010293 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010294 if (clear_attr != 0)
10295 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 /* redraw the characters */
10298 if (type == USE_REDRAW)
10299 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010300 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 {
10302 term_append_lines(line_count);
10303 screen_start(); /* don't know where cursor is now */
10304 }
10305 else
10306 {
10307 for (i = 0; i < line_count; i++)
10308 {
10309 if (type == USE_T_AL)
10310 {
10311 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010312 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 out_str(T_AL);
10314 }
10315 else /* type == USE_T_SR */
10316 out_str(T_SR);
10317 screen_start(); /* don't know where cursor is now */
10318 }
10319 }
10320
10321 /*
10322 * With scroll-reverse and 'da' flag set we need to clear the lines that
10323 * have been scrolled down into the region.
10324 */
10325 if (type == USE_T_SR && *T_DA)
10326 {
10327 for (i = 0; i < line_count; ++i)
10328 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010329 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 out_str(T_CE);
10331 screen_start(); /* don't know where cursor is now */
10332 }
10333 }
10334
10335#ifdef FEAT_GUI
10336 gui_can_update_cursor();
10337 if (gui.in_use)
10338 out_flush(); /* always flush after a scroll */
10339#endif
10340 return OK;
10341}
10342
10343/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010344 * Delete lines on the screen and update ScreenLines[].
10345 * "end" is the line after the scrolled part. Normally it is Rows.
10346 * When scrolling region used "off" is the offset from the top for the region.
10347 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 *
10349 * Return OK for success, FAIL if the lines are not deleted.
10350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010352screen_del_lines(
10353 int off,
10354 int row,
10355 int line_count,
10356 int end,
10357 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010358 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010359 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360{
10361 int j;
10362 int i;
10363 unsigned temp;
10364 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010365 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 int cursor_end;
10367 int result_empty; /* result is empty until end of region */
10368 int can_delete; /* deleting line codes can be used */
10369 int type;
10370
10371 /*
10372 * FAIL if
10373 * - there is no valid screen
10374 * - the screen has to be redrawn completely
10375 * - the line count is less than one
10376 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010377 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010379 if (!screen_valid(TRUE) || line_count <= 0
10380 || (!force && line_count > p_ttyscroll)
10381#ifdef FEAT_CLIPBOARD
10382 || (clip_star.state != SELECT_CLEARED
10383 && redrawing_for_callback > 0)
10384#endif
10385 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 return FAIL;
10387
10388 /*
10389 * Check if the rest of the current region will become empty.
10390 */
10391 result_empty = row + line_count >= end;
10392
10393 /*
10394 * We can delete lines only when 'db' flag not set or when 'ce' option
10395 * available.
10396 */
10397 can_delete = (*T_DB == NUL || can_clear(T_CE));
10398
10399 /*
10400 * There are six ways to delete lines:
10401 * 0. When in a vertically split window and t_CV isn't set, redraw the
10402 * characters from ScreenLines[].
10403 * 1. Use T_CD if it exists and the result is empty.
10404 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10405 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10406 * none of the other ways work.
10407 * 4. Use T_CE (erase line) if the result is empty.
10408 * 5. Use T_DL (delete line) if it exists.
10409 * 6. redraw the characters from ScreenLines[].
10410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10412 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010413 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 type = USE_T_CD;
10415#if defined(__BEOS__) && defined(BEOS_DR8)
10416 /*
10417 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10418 * its internal termcap... this works okay for tests which test *T_DB !=
10419 * NUL. It has the disadvantage that the user cannot use any :set t_*
10420 * command to get T_DB (back) to empty_option, only :set term=... will do
10421 * the trick...
10422 * Anyway, this hack will hopefully go away with the next OS release.
10423 * (Olaf Seibert)
10424 */
10425 else if (row == 0 && T_DB == empty_option
10426 && (line_count == 1 || *T_CDL == NUL))
10427#else
10428 else if (row == 0 && (
10429#ifndef AMIGA
10430 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10431 * up, so use delete-line command */
10432 line_count == 1 ||
10433#endif
10434 *T_CDL == NUL))
10435#endif
10436 type = USE_NL;
10437 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10438 type = USE_T_CDL;
10439 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010440 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 type = USE_T_CE;
10442 else if (*T_DL != NUL && can_delete)
10443 type = USE_T_DL;
10444 else if (*T_CDL != NUL && can_delete)
10445 type = USE_T_CDL;
10446 else
10447 return FAIL;
10448
10449#ifdef FEAT_CLIPBOARD
10450 /* Remove a modeless selection when deleting lines halfway the screen or
10451 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010452 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010453 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 else
10455 clip_scroll_selection(line_count);
10456#endif
10457
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458#ifdef FEAT_GUI
10459 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10460 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010461 gui_dont_update_cursor(gui.cursor_row >= row + off
10462 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463#endif
10464
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010465 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10466 cursor_col = wp->w_wincol;
10467
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 if (*T_CCS != NUL) /* cursor relative to region */
10469 {
10470 cursor_row = row;
10471 cursor_end = end;
10472 }
10473 else
10474 {
10475 cursor_row = row + off;
10476 cursor_end = end + off;
10477 }
10478
10479 /*
10480 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10481 * Clear the inserted lines in ScreenLines[].
10482 */
10483 row += off;
10484 end += off;
10485 for (i = 0; i < line_count; ++i)
10486 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 if (wp != NULL && wp->w_width != Columns)
10488 {
10489 /* need to copy part of a line */
10490 j = row + i;
10491 while ((j += line_count) <= end - 1)
10492 linecopy(j - line_count, j, wp);
10493 j -= line_count;
10494 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010495 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10496 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 else
10498 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10499 LineWraps[j] = FALSE;
10500 }
10501 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 {
10503 /* whole width, moving the line pointers is faster */
10504 j = row + i;
10505 temp = LineOffset[j];
10506 while ((j += line_count) <= end - 1)
10507 {
10508 LineOffset[j - line_count] = LineOffset[j];
10509 LineWraps[j - line_count] = LineWraps[j];
10510 }
10511 LineOffset[j - line_count] = temp;
10512 LineWraps[j - line_count] = FALSE;
10513 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010514 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 else
10516 lineinvalid(temp, (int)Columns);
10517 }
10518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010520 if (screen_attr != clear_attr)
10521 screen_stop_highlight();
10522 if (clear_attr != 0)
10523 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 /* redraw the characters */
10526 if (type == USE_REDRAW)
10527 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010528 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010530 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 out_str(T_CD);
10532 screen_start(); /* don't know where cursor is now */
10533 }
10534 else if (type == USE_T_CDL)
10535 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010536 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 term_delete_lines(line_count);
10538 screen_start(); /* don't know where cursor is now */
10539 }
10540 /*
10541 * Deleting lines at top of the screen or scroll region: Just scroll
10542 * the whole screen (scroll region) up by outputting newlines on the
10543 * last line.
10544 */
10545 else if (type == USE_NL)
10546 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010547 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 for (i = line_count; --i >= 0; )
10549 out_char('\n'); /* cursor will remain on same line */
10550 }
10551 else
10552 {
10553 for (i = line_count; --i >= 0; )
10554 {
10555 if (type == USE_T_DL)
10556 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010557 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 out_str(T_DL); /* delete a line */
10559 }
10560 else /* type == USE_T_CE */
10561 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010562 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 out_str(T_CE); /* erase a line */
10564 }
10565 screen_start(); /* don't know where cursor is now */
10566 }
10567 }
10568
10569 /*
10570 * If the 'db' flag is set, we need to clear the lines that have been
10571 * scrolled up at the bottom of the region.
10572 */
10573 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10574 {
10575 for (i = line_count; i > 0; --i)
10576 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010577 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 out_str(T_CE); /* erase a line */
10579 screen_start(); /* don't know where cursor is now */
10580 }
10581 }
10582
10583#ifdef FEAT_GUI
10584 gui_can_update_cursor();
10585 if (gui.in_use)
10586 out_flush(); /* always flush after a scroll */
10587#endif
10588
10589 return OK;
10590}
10591
10592/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010593 * Return TRUE when postponing displaying the mode message: when not redrawing
10594 * or inside a mapping.
10595 */
10596 int
10597skip_showmode()
10598{
10599 // Call char_avail() only when we are going to show something, because it
10600 // takes a bit of time. redrawing() may also call char_avail_avail().
10601 if (global_busy
10602 || msg_silent != 0
10603 || !redrawing()
10604 || (char_avail() && !KeyTyped))
10605 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010606 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010607 return TRUE;
10608 }
10609 return FALSE;
10610}
10611
10612/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010613 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 *
10615 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10616 * If clear_cmdline is FALSE there may be a message there that needs to be
10617 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010618 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619 * Return the length of the message (0 if no message).
10620 */
10621 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010622showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623{
10624 int need_clear;
10625 int length = 0;
10626 int do_mode;
10627 int attr;
10628 int nwr_save;
10629#ifdef FEAT_INS_EXPAND
10630 int sub_attr;
10631#endif
10632
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010633 do_mode = ((p_smd && msg_silent == 0)
10634 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010635 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010636 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010637 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010639 if (skip_showmode())
10640 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641
10642 nwr_save = need_wait_return;
10643
10644 /* wait a bit before overwriting an important message */
10645 check_for_delay(FALSE);
10646
10647 /* if the cmdline is more than one line high, erase top lines */
10648 need_clear = clear_cmdline;
10649 if (clear_cmdline && cmdline_row < Rows - 1)
10650 msg_clr_cmdline(); /* will reset clear_cmdline */
10651
10652 /* Position on the last line in the window, column 0 */
10653 msg_pos_mode();
10654 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010655 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 if (do_mode)
10657 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010658 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010660 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010661# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010662 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010663# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010664 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010665# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010666 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010667# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010668 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010670 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671# endif
10672#endif
10673#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10674 if (gui.in_use)
10675 {
10676 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010677 {
10678 /* HANGUL */
10679 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010680 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010681 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010682 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 }
10685#endif
10686#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010687 /* CTRL-X in Insert mode */
10688 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 {
10690 /* These messages can get long, avoid a wrap in a narrow
10691 * window. Prefer showing edit_submode_extra. */
10692 length = (Rows - msg_row) * Columns - 3;
10693 if (edit_submode_extra != NULL)
10694 length -= vim_strsize(edit_submode_extra);
10695 if (length > 0)
10696 {
10697 if (edit_submode_pre != NULL)
10698 length -= vim_strsize(edit_submode_pre);
10699 if (length - vim_strsize(edit_submode) > 0)
10700 {
10701 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010702 msg_puts_attr((char *)edit_submode_pre, attr);
10703 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 }
10705 if (edit_submode_extra != NULL)
10706 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010707 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010709 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 else
10711 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010712 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713 }
10714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 }
10716 else
10717#endif
10718 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010720 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010721 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010722 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 else if (State & INSERT)
10724 {
10725#ifdef FEAT_RIGHTLEFT
10726 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010727 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010729 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010731 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010732 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010734 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010736 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737#ifdef FEAT_RIGHTLEFT
10738 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010739 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740#endif
10741#ifdef FEAT_KEYMAP
10742 if (State & LANGMAP)
10743 {
10744# ifdef FEAT_ARABIC
10745 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010746 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 else
10748# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010749 if (get_keymap_str(curwin, (char_u *)" (%s)",
10750 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010751 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 }
10753#endif
10754 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010755 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 if (VIsual_active)
10758 {
10759 char *p;
10760
10761 /* Don't concatenate separate words to avoid translation
10762 * problems. */
10763 switch ((VIsual_select ? 4 : 0)
10764 + (VIsual_mode == Ctrl_V) * 2
10765 + (VIsual_mode == 'V'))
10766 {
10767 case 0: p = N_(" VISUAL"); break;
10768 case 1: p = N_(" VISUAL LINE"); break;
10769 case 2: p = N_(" VISUAL BLOCK"); break;
10770 case 4: p = N_(" SELECT"); break;
10771 case 5: p = N_(" SELECT LINE"); break;
10772 default: p = N_(" SELECT BLOCK"); break;
10773 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010774 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010776 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010778
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 need_clear = TRUE;
10780 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010781 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782#ifdef FEAT_INS_EXPAND
10783 && edit_submode == NULL /* otherwise it gets too long */
10784#endif
10785 )
10786 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010787 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 need_clear = TRUE;
10789 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010790
10791 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010792 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 msg_clr_eos();
10794 msg_didout = FALSE; /* overwrite this message */
10795 length = msg_col;
10796 msg_col = 0;
10797 need_wait_return = nwr_save; /* never ask for hit-return for this */
10798 }
10799 else if (clear_cmdline && msg_silent == 0)
10800 /* Clear the whole command line. Will reset "clear_cmdline". */
10801 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010802 else if (redraw_mode)
10803 {
10804 msg_pos_mode();
10805 msg_clr_eos();
10806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807
10808#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 /* In Visual mode the size of the selected area must be redrawn. */
10810 if (VIsual_active)
10811 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812
10813 /* If the last window has no status line, the ruler is after the mode
10814 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010815 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010816 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817#endif
10818 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010819 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 clear_cmdline = FALSE;
10821
10822 return length;
10823}
10824
10825/*
10826 * Position for a mode message.
10827 */
10828 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010829msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830{
10831 msg_col = 0;
10832 msg_row = Rows - 1;
10833}
10834
10835/*
10836 * Delete mode message. Used when ESC is typed which is expected to end
10837 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010838 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 */
10840 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010841unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842{
10843 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010844 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 */
10846 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10847 redraw_cmdline = TRUE; /* delete mode later */
10848 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010849 clearmode();
10850}
10851
10852/*
10853 * Clear the mode message.
10854 */
10855 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010856clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010857{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010858 int save_msg_row = msg_row;
10859 int save_msg_col = msg_col;
10860
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010861 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010862 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010863 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010864 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010865
10866 msg_col = save_msg_col;
10867 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868}
10869
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010870 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010871recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010872{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010873 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010874 if (!shortmess(SHM_RECORDING))
10875 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010876 char s[4];
10877
10878 sprintf(s, " @%c", reg_recording);
10879 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010880 }
10881}
10882
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010883/*
10884 * Draw the tab pages line at the top of the Vim window.
10885 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010886 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010887draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010888{
10889 int tabcount = 0;
10890 tabpage_T *tp;
10891 int tabwidth;
10892 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010893 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010894 int attr;
10895 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010896 win_T *cwp;
10897 int wincount;
10898 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010899 int c;
10900 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010901 int attr_sel = HL_ATTR(HLF_TPS);
10902 int attr_nosel = HL_ATTR(HLF_TP);
10903 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010904 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010905 int room;
10906 int use_sep_chars = (t_colors < 8
10907#ifdef FEAT_GUI
10908 && !gui.in_use
10909#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010910#ifdef FEAT_TERMGUICOLORS
10911 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010912#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010913 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010914
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010915 if (ScreenLines == NULL)
10916 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010917 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010918
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010919#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010920 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010921 if (gui_use_tabline())
10922 {
10923 gui_update_tabline();
10924 return;
10925 }
10926#endif
10927
10928 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010929 return;
10930
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010931#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010932 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010933
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010934 /* Use the 'tabline' option if it's set. */
10935 if (*p_tal != NUL)
10936 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010937 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010938
10939 /* Check for an error. If there is one we would loop in redrawing the
10940 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010941 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010942 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010943 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010944 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010945 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010946 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010947 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010948 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010949#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010950 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010951 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010952 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010953
Bram Moolenaar238a5642006-02-21 22:12:05 +000010954 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10955 if (tabwidth < 6)
10956 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010957
Bram Moolenaar238a5642006-02-21 22:12:05 +000010958 attr = attr_nosel;
10959 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010960 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10961 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010962 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010963 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010964
Bram Moolenaar238a5642006-02-21 22:12:05 +000010965 if (tp->tp_topframe == topframe)
10966 attr = attr_sel;
10967 if (use_sep_chars && col > 0)
10968 screen_putchar('|', 0, col++, attr);
10969
10970 if (tp->tp_topframe != topframe)
10971 attr = attr_nosel;
10972
10973 screen_putchar(' ', 0, col++, attr);
10974
10975 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010976 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010977 cwp = curwin;
10978 wp = firstwin;
10979 }
10980 else
10981 {
10982 cwp = tp->tp_curwin;
10983 wp = tp->tp_firstwin;
10984 }
10985
10986 modified = FALSE;
10987 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10988 if (bufIsChanged(wp->w_buffer))
10989 modified = TRUE;
10990 if (modified || wincount > 1)
10991 {
10992 if (wincount > 1)
10993 {
10994 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010995 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010996 if (col + len >= Columns - 3)
10997 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010998 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010999#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010011000 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000011001#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020011002 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000011003#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000011004 );
11005 col += len;
11006 }
11007 if (modified)
11008 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
11009 screen_putchar(' ', 0, col++, attr);
11010 }
11011
11012 room = scol - col + tabwidth - 1;
11013 if (room > 0)
11014 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000011015 /* Get buffer name in NameBuff[] */
11016 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011017 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011018 len = vim_strsize(NameBuff);
11019 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000011020 if (has_mbyte)
11021 while (len > room)
11022 {
11023 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011024 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011025 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011026 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011027 {
11028 p += len - room;
11029 len = room;
11030 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000011031 if (len > Columns - col - 1)
11032 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000011033
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011034 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000011035 col += len;
11036 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000011037 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011038
11039 /* Store the tab page number in TabPageIdxs[], so that
11040 * jump_to_mouse() knows where each one is. */
11041 ++tabcount;
11042 while (scol < col)
11043 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000011044 }
11045
Bram Moolenaar238a5642006-02-21 22:12:05 +000011046 if (use_sep_chars)
11047 c = '_';
11048 else
11049 c = ' ';
11050 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000011051
11052 /* Put an "X" for closing the current tab if there are several. */
11053 if (first_tabpage->tp_next != NULL)
11054 {
11055 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
11056 TabPageIdxs[Columns - 1] = -999;
11057 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000011058 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000011059
11060 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
11061 * set. */
11062 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000011063}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000011064
11065/*
11066 * Get buffer name for "buf" into NameBuff[].
11067 * Takes care of special buffer names and translates special characters.
11068 */
11069 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011070get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000011071{
11072 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020011073 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000011074 else
11075 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
11076 trans_characters(NameBuff, MAXPATHL);
11077}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000011078
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079/*
11080 * Get the character to use in a status line. Get its attributes in "*attr".
11081 */
11082 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011083fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084{
11085 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011086
11087#ifdef FEAT_TERMINAL
11088 if (bt_terminal(wp->w_buffer))
11089 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011090 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020011091 {
11092 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011093 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020011094 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011095 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020011096 {
11097 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011098 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020011099 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011100 }
11101 else
11102#endif
11103 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010011105 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 fill = fill_stl;
11107 }
11108 else
11109 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010011110 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 fill = fill_stlnc;
11112 }
11113 /* Use fill when there is highlighting, and highlighting of current
11114 * window differs, or the fillchars differ, or this is not the
11115 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010011116 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011117 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 || (fill_stl != fill_stlnc)))
11119 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011120 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 return '^';
11122 return '=';
11123}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125/*
11126 * Get the character to use in a separator between vertically split windows.
11127 * Get its attributes in "*attr".
11128 */
11129 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010011130fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131{
Bram Moolenaar8820b482017-03-16 17:23:31 +010011132 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 if (*attr == 0 && fill_vert == ' ')
11134 return '|';
11135 else
11136 return fill_vert;
11137}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138
11139/*
11140 * Return TRUE if redrawing should currently be done.
11141 */
11142 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011143redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010011145#ifdef FEAT_EVAL
11146 if (disable_redraw_for_testing)
11147 return 0;
11148 else
11149#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020011150 return ((!RedrawingDisabled
11151#ifdef FEAT_EVAL
11152 || ignore_redraw_flag_for_testing
11153#endif
11154 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155}
11156
11157/*
11158 * Return TRUE if printing messages should currently be done.
11159 */
11160 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011161messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162{
11163 return (!(p_lz && char_avail() && !KeyTyped));
11164}
11165
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011166#ifdef FEAT_MENU
11167/*
11168 * Draw the window toolbar.
11169 */
11170 static void
11171redraw_win_toolbar(win_T *wp)
11172{
11173 vimmenu_T *menu;
11174 int item_idx = 0;
11175 int item_count = 0;
11176 int col = 0;
11177 int next_col;
11178 int off = (int)(current_ScreenLine - ScreenLines);
11179 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
11180 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
11181
11182 vim_free(wp->w_winbar_items);
11183 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
11184 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011185 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011186
11187 /* TODO: use fewer spaces if there is not enough room */
11188 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020011189 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011190 {
11191 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011192 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011193 break;
11194 if (col > 1)
11195 {
11196 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011197 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011198 break;
11199 }
11200
11201 wp->w_winbar_items[item_idx].wb_startcol = col;
11202 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011203 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011204 break;
11205
11206 next_col = text_to_screenline(wp, menu->name, col);
11207 while (col < next_col)
11208 {
11209 ScreenAttrs[off + col] = button_attr;
11210 ++col;
11211 }
11212 wp->w_winbar_items[item_idx].wb_endcol = col;
11213 wp->w_winbar_items[item_idx].wb_menu = menu;
11214 ++item_idx;
11215
Bram Moolenaar02631462017-09-22 15:20:32 +020011216 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011217 break;
11218 space_to_screenline(off + col, button_attr);
11219 ++col;
11220 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011221 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011222 {
11223 space_to_screenline(off + col, fill_attr);
11224 ++col;
11225 }
11226 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11227
Bram Moolenaar02631462017-09-22 15:20:32 +020011228 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020011229 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011230}
11231#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011232
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233/*
11234 * Show current status info in ruler and various other places
11235 * If always is FALSE, only show ruler if position has changed.
11236 */
11237 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011238showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239{
11240 if (!always && !redrawing())
11241 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011242#ifdef FEAT_INS_EXPAND
11243 if (pum_visible())
11244 {
11245 /* Don't redraw right now, do it later. */
11246 curwin->w_redr_status = TRUE;
11247 return;
11248 }
11249#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011250#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011251 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000011252 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 else
11254#endif
11255#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011256 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257#endif
11258
11259#ifdef FEAT_TITLE
11260 if (need_maketitle
11261# ifdef FEAT_STL_OPT
11262 || (p_icon && (stl_syntax & STL_IN_ICON))
11263 || (p_title && (stl_syntax & STL_IN_TITLE))
11264# endif
11265 )
11266 maketitle();
11267#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011268 /* Redraw the tab pages line if needed. */
11269 if (redraw_tabline)
11270 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271}
11272
11273#ifdef FEAT_CMDL_INFO
11274 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011275win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011277#define RULER_BUF_LEN 70
11278 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279 int row;
11280 int fillchar;
11281 int attr;
11282 int empty_line = FALSE;
11283 colnr_T virtcol;
11284 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011285 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 int this_ru_col;
11288 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010011289 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290
11291 /* If 'ruler' off or redrawing disabled, don't do anything */
11292 if (!p_ru)
11293 return;
11294
11295 /*
11296 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11297 * after deleting lines, before cursor.lnum is corrected.
11298 */
11299 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11300 return;
11301
11302#ifdef FEAT_INS_EXPAND
11303 /* Don't draw the ruler while doing insert-completion, it might overwrite
11304 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306 if (edit_submode != NULL)
11307 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011308 // Don't draw the ruler when the popup menu is visible, it may overlap.
11309 // Except when the popup menu will be redrawn anyway.
11310 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011311 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312#endif
11313
11314#ifdef FEAT_STL_OPT
11315 if (*p_ruf)
11316 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011317 int save_called_emsg = called_emsg;
11318
11319 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011321 if (called_emsg)
11322 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011323 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011324 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 return;
11326 }
11327#endif
11328
11329 /*
11330 * Check if not in Insert mode and the line is empty (will show "0-1").
11331 */
11332 if (!(State & INSERT)
11333 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11334 empty_line = TRUE;
11335
11336 /*
11337 * Only draw the ruler when something changed.
11338 */
11339 validate_virtcol_win(wp);
11340 if ( redraw_cmdline
11341 || always
11342 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11343 || wp->w_cursor.col != wp->w_ru_cursor.col
11344 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 || wp->w_topline != wp->w_ru_topline
11347 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11348#ifdef FEAT_DIFF
11349 || wp->w_topfill != wp->w_ru_topfill
11350#endif
11351 || empty_line != wp->w_ru_empty)
11352 {
11353 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 if (wp->w_status_height)
11355 {
11356 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011357 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011358 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011359 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 }
11361 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362 {
11363 row = Rows - 1;
11364 fillchar = ' ';
11365 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366 width = Columns;
11367 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 }
11369
11370 /* In list mode virtcol needs to be recomputed */
11371 virtcol = wp->w_virtcol;
11372 if (wp->w_p_list && lcs_tab1 == NUL)
11373 {
11374 wp->w_p_list = FALSE;
11375 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11376 wp->w_p_list = TRUE;
11377 }
11378
11379 /*
11380 * Some sprintfs return the length, some return a pointer.
11381 * To avoid portability problems we use strlen() here.
11382 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011383 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11385 ? 0L
11386 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011387 len = STRLEN(buffer);
11388 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11390 (int)virtcol + 1);
11391
11392 /*
11393 * Add a "50%" if there is room for it.
11394 * On the last line, don't print in the last column (scrolls the
11395 * screen up on some terminals).
11396 */
11397 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011398 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 this_ru_col = ru_col - (Columns - width);
11403 if (this_ru_col < 0)
11404 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405 /* Never use more than half the window/screen width, leave the other
11406 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011407 if (this_ru_col < (width + 1) / 2)
11408 this_ru_col = (width + 1) / 2;
11409 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011411 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011412 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 if (has_mbyte)
11415 i += (*mb_char2bytes)(fillchar, buffer + i);
11416 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 buffer[i++] = fillchar;
11418 ++o;
11419 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011420 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 }
11422 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423 if (has_mbyte)
11424 {
11425 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011426 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427 {
11428 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011429 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430 {
11431 buffer[i] = NUL;
11432 break;
11433 }
11434 }
11435 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011436 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011437 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438
Bram Moolenaar4033c552017-09-16 20:54:51 +020011439 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440 i = redraw_cmdline;
11441 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011442 this_ru_col + off + (int)STRLEN(buffer),
11443 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 fillchar, fillchar, attr);
11445 /* don't redraw the cmdline because of showing the ruler */
11446 redraw_cmdline = i;
11447 wp->w_ru_cursor = wp->w_cursor;
11448 wp->w_ru_virtcol = wp->w_virtcol;
11449 wp->w_ru_empty = empty_line;
11450 wp->w_ru_topline = wp->w_topline;
11451 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11452#ifdef FEAT_DIFF
11453 wp->w_ru_topfill = wp->w_topfill;
11454#endif
11455 }
11456}
11457#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011458
11459#if defined(FEAT_LINEBREAK) || defined(PROTO)
11460/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011461 * Return the width of the 'number' and 'relativenumber' column.
11462 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011463 * Otherwise it depends on 'numberwidth' and the line count.
11464 */
11465 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011466number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011467{
11468 int n;
11469 linenr_T lnum;
11470
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011471 if (wp->w_p_rnu && !wp->w_p_nu)
11472 /* cursor line shows "0" */
11473 lnum = wp->w_height;
11474 else
11475 /* cursor line shows absolute line number */
11476 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011477
Bram Moolenaar6b314672015-03-20 15:42:10 +010011478 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011479 return wp->w_nrwidth_width;
11480 wp->w_nrwidth_line_count = lnum;
11481
11482 n = 0;
11483 do
11484 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011485 lnum /= 10;
11486 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011487 } while (lnum > 0);
11488
11489 /* 'numberwidth' gives the minimal width plus one */
11490 if (n < wp->w_p_nuw - 1)
11491 n = wp->w_p_nuw - 1;
11492
11493 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011494 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011495 return n;
11496}
11497#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011498
Bram Moolenaar113e1072019-01-20 15:30:40 +010011499#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011500/*
11501 * Return the current cursor column. This is the actual position on the
11502 * screen. First column is 0.
11503 */
11504 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011505screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011506{
11507 return screen_cur_col;
11508}
11509
11510/*
11511 * Return the current cursor row. This is the actual position on the screen.
11512 * First row is 0.
11513 */
11514 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011515screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011516{
11517 return screen_cur_row;
11518}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011519#endif