blob: fae9fe7eb835f540f7ed1030d51d1b492b39e52f [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 Moolenaarac2450a2019-06-09 18:04:28 +02003960 * Do not use search_hl 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;
Bram Moolenaarac2450a2019-06-09 18:04:28 +02003963 shl_flag = (screen_line_flags & SLF_POPUP);
3964 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003966 if (shl_flag == FALSE)
3967 {
3968 shl = &search_hl;
3969 shl_flag = TRUE;
3970 }
3971 else
3972 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003973 shl->startcol = MAXCOL;
3974 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003976 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003977 v = (long)(ptr - line);
3978 if (cur != NULL)
3979 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003980 next_search_hl(wp, shl, lnum, (colnr_T)v,
3981 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003982
3983 /* Need to get the line again, a multi-line regexp may have made it
3984 * invalid. */
3985 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3986 ptr = line + v;
3987
3988 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003990 if (shl->lnum == lnum)
3991 shl->startcol = shl->rm.startpos[0].col;
3992 else
3993 shl->startcol = 0;
3994 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3995 - shl->rm.startpos[0].lnum)
3996 shl->endcol = shl->rm.endpos[0].col;
3997 else
3998 shl->endcol = MAXCOL;
3999 /* Highlight one character for an empty match. */
4000 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02004002 if (has_mbyte && line[shl->endcol] != NUL)
4003 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
4004 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02004005 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02004007 if ((long)shl->startcol < v) /* match at leftcol */
4008 {
4009 shl->attr_cur = shl->attr;
4010 search_attr = shl->attr;
4011 }
4012 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004014 if (shl != &search_hl && cur != NULL)
4015 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017#endif
4018
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004019#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01004020 // Cursor line highlighting for 'cursorline' in the current window.
4021 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004022 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01004023 // Do not show the cursor line when Visual mode is active, because it's
4024 // not clear what is selected then. Do update w_last_cursorline.
4025 if (!(wp == curwin && VIsual_active))
4026 {
4027 line_attr = HL_ATTR(HLF_CUL);
4028 area_highlighting = TRUE;
4029 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01004030 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004031 }
4032#endif
4033
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004034#ifdef FEAT_TEXT_PROP
4035 {
4036 char_u *prop_start;
4037
4038 text_prop_count = get_text_props(wp->w_buffer, lnum,
4039 &prop_start, FALSE);
4040 if (text_prop_count > 0)
4041 {
4042 // Make a copy of the properties, so that they are properly
4043 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004044 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004045 if (text_props != NULL)
4046 mch_memmove(text_props, prop_start,
4047 text_prop_count * sizeof(textprop_T));
4048
4049 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004050 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004051 area_highlighting = TRUE;
4052 extra_check = TRUE;
4053 }
4054 }
4055#endif
4056
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004057 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004059
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060#ifdef FEAT_RIGHTLEFT
4061 if (wp->w_p_rl)
4062 {
4063 /* Rightleft window: process the text in the normal direction, but put
4064 * it in current_ScreenLine[] from right to left. Start at the
4065 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02004066 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004068 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
4070#endif
4071
4072 /*
4073 * Repeat for the whole displayed line.
4074 */
4075 for (;;)
4076 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02004077#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004078 int has_match_conc = 0; // match wants to conceal
4079 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 /* Skip this quickly when working on the text. */
4082 if (draw_state != WL_LINE)
4083 {
4084#ifdef FEAT_CMDWIN
4085 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
4086 {
4087 draw_state = WL_CMDLINE;
4088 if (cmdwin_type != 0 && wp == curwin)
4089 {
4090 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00004092 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004093 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004094 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
4096 }
4097#endif
4098
4099#ifdef FEAT_FOLDING
4100 if (draw_state == WL_FOLD - 1 && n_extra == 0)
4101 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01004102 int fdc = compute_foldcolumn(wp, 0);
4103
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01004105 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
Bram Moolenaar62706602016-12-09 19:28:48 +01004107 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004108 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004109 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01004110 p_extra_free = alloc(12 + 1);
4111
4112 if (p_extra_free != NULL)
4113 {
4114 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
4115 n_extra = fdc;
4116 p_extra_free[n_extra] = NUL;
4117 p_extra = p_extra_free;
4118 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004119 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004120 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01004121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 }
4123 }
4124#endif
4125
4126#ifdef FEAT_SIGNS
4127 if (draw_state == WL_SIGN - 1 && n_extra == 0)
4128 {
4129 draw_state = WL_SIGN;
4130 /* Show the sign column when there are any signs in this
4131 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02004132 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004134 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004136 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137# endif
4138
4139 /* Draw two cells with the sign value or blank. */
4140 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004141 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004142 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 n_extra = 2;
4144
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02004145 if (row == startrow
4146#ifdef FEAT_DIFF
4147 + filler_lines && filler_todo <= 0
4148#endif
4149 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 {
4151 text_sign = buf_getsigntype(wp->w_buffer, lnum,
4152 SIGN_TEXT);
4153# ifdef FEAT_SIGN_ICONS
4154 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
4155 SIGN_ICON);
4156 if (gui.in_use && icon_sign != 0)
4157 {
4158 /* Use the image in this position. */
4159 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004160 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161# ifdef FEAT_NETBEANS_INTG
4162 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004163 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004165 c_final = NUL;
4166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167# endif
4168 char_attr = icon_sign;
4169 }
4170 else
4171# endif
4172 if (text_sign != 0)
4173 {
4174 p_extra = sign_get_text(text_sign);
4175 if (p_extra != NULL)
4176 {
4177 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004178 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004179 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181 char_attr = sign_get_attr(text_sign, FALSE);
4182 }
4183 }
4184 }
4185 }
4186#endif
4187
4188 if (draw_state == WL_NR - 1 && n_extra == 0)
4189 {
4190 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02004191 /* Display the absolute or relative line number. After the
4192 * first fill with blanks when the 'n' flag isn't in 'cpo' */
4193 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 && (row == startrow
4195#ifdef FEAT_DIFF
4196 + filler_lines
4197#endif
4198 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
4199 {
4200 /* Draw the line number (empty space after wrapping). */
4201 if (row == startrow
4202#ifdef FEAT_DIFF
4203 + filler_lines
4204#endif
4205 )
4206 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004207 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01004208 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02004209
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004210 if (wp->w_p_nu && !wp->w_p_rnu)
4211 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02004212 num = (long)lnum;
4213 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01004214 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004215 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01004216 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004217 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004218 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004219 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004220 num = lnum;
4221 fmt = "%-*ld ";
4222 }
4223 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004224
Bram Moolenaar700e7342013-01-30 12:31:36 +01004225 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004226 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 if (wp->w_skipcol > 0)
4228 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4229 *p_extra = '-';
4230#ifdef FEAT_RIGHTLEFT
4231 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004232 {
4233 char_u *p1, *p2;
4234 int t;
4235
4236 // like rl_mirror(), but keep the space at the end
4237 p2 = skiptowhite(extra) - 1;
4238 for (p1 = extra; p1 < p2; ++p1, --p2)
4239 {
4240 t = *p1;
4241 *p1 = *p2;
4242 *p2 = t;
4243 }
4244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245#endif
4246 p_extra = extra;
4247 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004248 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004251 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004253 c_final = NUL;
4254 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004255 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004256 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004257#ifdef FEAT_SYN_HL
4258 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004259 * the current line differently.
4260 * TODO: Can we use CursorLine instead of CursorLineNr
4261 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004262 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004263 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004264 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267 }
4268
Bram Moolenaar597a4222014-06-25 14:39:50 +02004269#ifdef FEAT_LINEBREAK
4270 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4271 && n_extra == 0 && *p_sbr != NUL)
4272 /* draw indent after showbreak value */
4273 draw_state = WL_BRI;
4274 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4275 /* After the showbreak, draw the breakindent */
4276 draw_state = WL_BRI - 1;
4277
4278 /* draw 'breakindent': indent wrapped text accordingly */
4279 if (draw_state == WL_BRI - 1 && n_extra == 0)
4280 {
4281 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004282 /* if need_showbreak is set, breakindent also applies */
4283 if (wp->w_p_bri && n_extra == 0
4284 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004285# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004286 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004287# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004288 )
4289 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004290 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004291# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004292 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004293 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004294 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004295# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004296 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4297 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004298 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004299# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004300 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004301# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004302 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004303 c_extra = ' ';
4304 n_extra = get_breakindent_win(wp,
4305 ml_get_buf(wp->w_buffer, lnum, FALSE));
4306 /* Correct end of highlighted area for 'breakindent',
4307 * required when 'linebreak' is also set. */
4308 if (tocol == vcol)
4309 tocol += n_extra;
4310 }
4311 }
4312#endif
4313
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4315 if (draw_state == WL_SBR - 1 && n_extra == 0)
4316 {
4317 draw_state = WL_SBR;
4318# ifdef FEAT_DIFF
4319 if (filler_todo > 0)
4320 {
4321 /* Draw "deleted" diff line(s). */
4322 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004325 c_final = NUL;
4326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004330 c_final = NUL;
4331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332# ifdef FEAT_RIGHTLEFT
4333 if (wp->w_p_rl)
4334 n_extra = col + 1;
4335 else
4336# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004337 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004338 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 }
4340# endif
4341# ifdef FEAT_LINEBREAK
4342 if (*p_sbr != NUL && need_showbreak)
4343 {
4344 /* Draw 'showbreak' at the start of each broken line. */
4345 p_extra = p_sbr;
4346 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004347 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004349 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004351 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 /* Correct end of highlighted area for 'showbreak',
4353 * required when 'linebreak' is also set. */
4354 if (tocol == vcol)
4355 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004356#ifdef FEAT_SYN_HL
4357 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004358 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004359 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004360 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363# endif
4364 }
4365#endif
4366
4367 if (draw_state == WL_LINE - 1 && n_extra == 0)
4368 {
4369 draw_state = WL_LINE;
4370 if (saved_n_extra)
4371 {
4372 /* Continue item from end of wrapped line. */
4373 n_extra = saved_n_extra;
4374 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004375 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 p_extra = saved_p_extra;
4377 char_attr = saved_char_attr;
4378 }
4379 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004380 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 }
4382 }
4383
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004384 // When still displaying '$' of change command, stop at cursor.
4385 // When only displaying the (relative) line number and that's done,
4386 // stop here.
4387 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004388 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389#ifdef FEAT_DIFF
4390 && filler_todo <= 0
4391#endif
4392 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004393 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004395 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004396 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004397 /* Pretend we have finished updating the window. Except when
4398 * 'cursorcolumn' is set. */
4399#ifdef FEAT_SYN_HL
4400 if (wp->w_p_cuc)
4401 row = wp->w_cline_row + wp->w_cline_height;
4402 else
4403#endif
4404 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 break;
4406 }
4407
Bram Moolenaar637532b2019-01-03 21:44:40 +01004408 if (draw_state == WL_LINE && (area_highlighting
4409#ifdef FEAT_SPELL
4410 || has_spell
4411#endif
4412 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 {
4414 /* handle Visual or match highlighting in this line */
4415 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4417 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004419 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004421 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 else if (area_attr != 0
4423 && (vcol == tocol
4424 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
4427#ifdef FEAT_SEARCH_EXTRA
4428 if (!n_extra)
4429 {
4430 /*
4431 * Check for start/end of search pattern match.
4432 * After end, check for start/end of next match.
4433 * When another match, have to check for start again.
4434 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004435 * Do this for 'search_hl' and the match list (ordered by
4436 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004438 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004439 cur = wp->w_match_head;
Bram Moolenaarac2450a2019-06-09 18:04:28 +02004440 shl_flag = (screen_line_flags & SLF_POPUP);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004441 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004443 if (shl_flag == FALSE
4444 && ((cur != NULL
4445 && cur->priority > SEARCH_HL_PRIORITY)
4446 || cur == NULL))
4447 {
4448 shl = &search_hl;
4449 shl_flag = TRUE;
4450 }
4451 else
4452 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004453 if (cur != NULL)
4454 cur->pos.cur = 0;
4455 pos_inprogress = TRUE;
4456 while (shl->rm.regprog != NULL
4457 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004459 if (shl->startcol != MAXCOL
4460 && v >= (long)shl->startcol
4461 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004463 int tmp_col = v + MB_PTR2LEN(ptr);
4464
4465 if (shl->endcol < tmp_col)
4466 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004468#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004469 // Match with the "Conceal" group results in hiding
4470 // the match.
4471 if (cur != NULL
4472 && shl != &search_hl
4473 && syn_name2id((char_u *)"Conceal")
4474 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004475 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004476 has_match_conc =
4477 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004478 match_conc = cur->conceal_char;
4479 }
4480 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004481 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004484 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
4486 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004487 next_search_hl(wp, shl, lnum, (colnr_T)v,
4488 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004489 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004490 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491
4492 /* Need to get the line again, a multi-line regexp
4493 * may have made it invalid. */
4494 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4495 ptr = line + v;
4496
4497 if (shl->lnum == lnum)
4498 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004499 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004501 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004503 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004505 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
4507 /* highlight empty match, try again after
4508 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004510 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004511 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004513 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 }
4515
4516 /* Loop to check if the match starts at the
4517 * current position */
4518 continue;
4519 }
4520 }
4521 break;
4522 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004523 if (shl != &search_hl && cur != NULL)
4524 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004526
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004527 /* Use attributes from match with highest priority among
4528 * 'search_hl' and the match list. */
4529 search_attr = search_hl.attr_cur;
4530 cur = wp->w_match_head;
4531 shl_flag = FALSE;
4532 while (cur != NULL || shl_flag == FALSE)
4533 {
4534 if (shl_flag == FALSE
4535 && ((cur != NULL
4536 && cur->priority > SEARCH_HL_PRIORITY)
4537 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004538 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004539 shl = &search_hl;
4540 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004541 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004542 else
4543 shl = &cur->hl;
4544 if (shl->attr_cur != 0)
4545 search_attr = shl->attr_cur;
4546 if (shl != &search_hl && cur != NULL)
4547 cur = cur->next;
4548 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004549 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004550 if (*ptr == NUL && (did_line_attr >= 1
4551 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004552 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 }
4554#endif
4555
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004557 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004559 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4560 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004562 if (diff_hlf == HLF_TXD && ptr - line > change_end
4563 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004565 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004566 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004567 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 }
4569#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004570
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004571#ifdef FEAT_TEXT_PROP
4572 if (text_props != NULL)
4573 {
4574 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004575 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004576
4577 // Check if any active property ends.
4578 for (pi = 0; pi < text_props_active; ++pi)
4579 {
4580 int tpi = text_prop_idxs[pi];
4581
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004582 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004583 + text_props[tpi].tp_len)
4584 {
4585 if (pi + 1 < text_props_active)
4586 mch_memmove(text_prop_idxs + pi,
4587 text_prop_idxs + pi + 1,
4588 sizeof(int)
4589 * (text_props_active - (pi + 1)));
4590 --text_props_active;
4591 --pi;
4592 }
4593 }
4594
4595 // Add any text property that starts in this column.
4596 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004597 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004598 text_prop_idxs[text_props_active++] = text_prop_next++;
4599
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004600 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004601 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004602 if (text_props_active > 0)
4603 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004604 // Sort the properties on priority and/or starting last.
4605 // Then combine the attributes, highest priority last.
4606 current_text_props = text_props;
4607 current_buf = wp->w_buffer;
4608 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4609 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004610
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004611 for (pi = 0; pi < text_props_active; ++pi)
4612 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004613 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004614 proptype_T *pt = text_prop_type_by_id(
4615 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004616
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004617 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004618 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004619 int pt_attr = syn_id2attr(pt->pt_hl_id);
4620
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004621 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004622 text_prop_attr =
4623 hl_combine_attr(text_prop_attr, pt_attr);
4624 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004625 }
4626 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004627 }
4628 }
4629#endif
4630
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004631 /* Decide which of the highlight attributes to use. */
4632 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004633#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004634 if (area_attr != 0)
4635 char_attr = hl_combine_attr(line_attr, area_attr);
4636 else if (search_attr != 0)
4637 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004638# ifdef FEAT_TEXT_PROP
4639 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004640 {
4641 char_attr = hl_combine_attr(
4642 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4643 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004644# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004645 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004646 || vcol < fromcol || vcol_prev < fromcol_prev
4647 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004648 // Use line_attr when not in the Visual or 'incsearch' area
4649 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004650 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004651#else
4652 if (area_attr != 0)
4653 char_attr = area_attr;
4654 else if (search_attr != 0)
4655 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004656#endif
4657 else
4658 {
4659 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004660#ifdef FEAT_TEXT_PROP
4661 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004662 {
4663 if (text_prop_combine)
4664 char_attr = hl_combine_attr(
4665 syntax_attr, text_prop_attr);
4666 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004667 char_attr = hl_combine_attr(
4668 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004669 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004670 else
4671#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004672#ifdef FEAT_SYN_HL
4673 if (has_syntax)
4674 char_attr = syntax_attr;
4675 else
4676#endif
4677 char_attr = 0;
4678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004680 if (char_attr == 0)
4681 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
4683 /*
4684 * Get the next character to put on the screen.
4685 */
4686 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004687 * The "p_extra" points to the extra stuff that is inserted to
4688 * represent special characters (non-printable stuff) and other
4689 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004690 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004691 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4692 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4694 */
4695 if (n_extra > 0)
4696 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004697 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004699 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004701 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 {
4703 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004704 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004705 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
4707 else
4708 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 }
4710 else
4711 {
4712 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 if (has_mbyte)
4714 {
4715 mb_c = c;
4716 if (enc_utf8)
4717 {
4718 /* If the UTF-8 character is more than one byte:
4719 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004720 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 mb_utf8 = FALSE;
4722 if (mb_l > n_extra)
4723 mb_l = 1;
4724 else if (mb_l > 1)
4725 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004726 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004728 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 }
4730 }
4731 else
4732 {
4733 /* if this is a DBCS character, put it in "mb_c" */
4734 mb_l = MB_BYTE2LEN(c);
4735 if (mb_l >= n_extra)
4736 mb_l = 1;
4737 else if (mb_l > 1)
4738 mb_c = (c << 8) + p_extra[1];
4739 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004740 if (mb_l == 0) /* at the NUL at end-of-line */
4741 mb_l = 1;
4742
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 /* If a double-width char doesn't fit display a '>' in the
4744 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004745 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746# ifdef FEAT_RIGHTLEFT
4747 wp->w_p_rl ? (col <= 0) :
4748# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004749 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 && (*mb_char2cells)(mb_c) == 2)
4751 {
4752 c = '>';
4753 mb_c = c;
4754 mb_l = 1;
4755 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004756 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 /* put the pointer back to output the double-width
4758 * character at the start of the next line. */
4759 ++n_extra;
4760 --p_extra;
4761 }
4762 else
4763 {
4764 n_extra -= mb_l - 1;
4765 p_extra += mb_l - 1;
4766 }
4767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 ++p_extra;
4769 }
4770 --n_extra;
4771 }
4772 else
4773 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004774#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004775 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004776#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004777
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004778 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004779 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 /*
4781 * Get a character from the line itself.
4782 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004783 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004784#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004785 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 if (has_mbyte)
4788 {
4789 mb_c = c;
4790 if (enc_utf8)
4791 {
4792 /* If the UTF-8 character is more than one byte: Decode it
4793 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004794 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 mb_utf8 = FALSE;
4796 if (mb_l > 1)
4797 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004798 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 /* Overlong encoded ASCII or ASCII with composing char
4800 * is displayed normally, except a NUL. */
4801 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004802 {
4803 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004804#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004805 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004806#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004809
4810 /* At start of the line we can have a composing char.
4811 * Draw it as a space with a composing char. */
4812 if (utf_iscomposing(mb_c))
4813 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004814 int i;
4815
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004816 for (i = Screen_mco - 1; i > 0; --i)
4817 u8cc[i] = u8cc[i - 1];
4818 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004819 mb_c = ' ';
4820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
4822
4823 if ((mb_l == 1 && c >= 0x80)
4824 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004825 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 {
4827 /*
4828 * Illegal UTF-8 byte: display as <xx>.
4829 * Non-BMP character : display as ? or fullwidth ?.
4830 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004831 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004832# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004833 if (wp->w_p_rl) /* reverse */
4834 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004835# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 p_extra = extra;
4837 c = *p_extra;
4838 mb_c = mb_ptr2char_adv(&p_extra);
4839 mb_utf8 = (c >= 0x80);
4840 n_extra = (int)STRLEN(p_extra);
4841 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004842 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 if (area_attr == 0 && search_attr == 0)
4844 {
4845 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004846 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 saved_attr2 = char_attr; /* save current attr */
4848 }
4849 }
4850 else if (mb_l == 0) /* at the NUL at end-of-line */
4851 mb_l = 1;
4852#ifdef FEAT_ARABIC
4853 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4854 {
4855 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004856 int pc, pc1, nc;
4857 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858
4859 /* The idea of what is the previous and next
4860 * character depends on 'rightleft'. */
4861 if (wp->w_p_rl)
4862 {
4863 pc = prev_c;
4864 pc1 = prev_c1;
4865 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004866 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
4868 else
4869 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004870 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004872 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
4874 prev_c = mb_c;
4875
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004876 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878 else
4879 prev_c = mb_c;
4880#endif
4881 }
4882 else /* enc_dbcs */
4883 {
4884 mb_l = MB_BYTE2LEN(c);
4885 if (mb_l == 0) /* at the NUL at end-of-line */
4886 mb_l = 1;
4887 else if (mb_l > 1)
4888 {
4889 /* We assume a second byte below 32 is illegal.
4890 * Hopefully this is OK for all double-byte encodings!
4891 */
4892 if (ptr[1] >= 32)
4893 mb_c = (c << 8) + ptr[1];
4894 else
4895 {
4896 if (ptr[1] == NUL)
4897 {
4898 /* head byte at end of line */
4899 mb_l = 1;
4900 transchar_nonprint(extra, c);
4901 }
4902 else
4903 {
4904 /* illegal tail byte */
4905 mb_l = 2;
4906 STRCPY(extra, "XX");
4907 }
4908 p_extra = extra;
4909 n_extra = (int)STRLEN(extra) - 1;
4910 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004911 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 c = *p_extra++;
4913 if (area_attr == 0 && search_attr == 0)
4914 {
4915 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004916 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 saved_attr2 = char_attr; /* save current attr */
4918 }
4919 mb_c = c;
4920 }
4921 }
4922 }
4923 /* If a double-width char doesn't fit display a '>' in the
4924 * last column; the character is displayed at the start of the
4925 * next line. */
4926 if ((
4927# ifdef FEAT_RIGHTLEFT
4928 wp->w_p_rl ? (col <= 0) :
4929# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004930 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 && (*mb_char2cells)(mb_c) == 2)
4932 {
4933 c = '>';
4934 mb_c = c;
4935 mb_utf8 = FALSE;
4936 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004937 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004938 // Put pointer back so that the character will be
4939 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004941#ifdef FEAT_CONCEAL
4942 did_decrement_ptr = TRUE;
4943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
4945 else if (*ptr != NUL)
4946 ptr += mb_l - 1;
4947
4948 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004949 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004950 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004951 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004954 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004955 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 c = ' ';
4957 if (area_attr == 0 && search_attr == 0)
4958 {
4959 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004960 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 saved_attr2 = char_attr; /* save current attr */
4962 }
4963 mb_c = c;
4964 mb_utf8 = FALSE;
4965 mb_l = 1;
4966 }
4967
4968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 ++ptr;
4970
4971 if (extra_check)
4972 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004973#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004974 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004975#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004976
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004977#ifdef FEAT_TERMINAL
4978 if (get_term_attr)
4979 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004980 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004981
4982 if (!attr_pri)
4983 char_attr = syntax_attr;
4984 else
4985 char_attr = hl_combine_attr(syntax_attr, char_attr);
4986 }
4987#endif
4988
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004989#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004990 // Get syntax attribute, unless still at the start of the line
4991 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004992 v = (long)(ptr - line);
4993 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {
4995 /* Get the syntax attribute for the character. If there
4996 * is an error, disable syntax highlighting. */
4997 save_did_emsg = did_emsg;
4998 did_emsg = FALSE;
4999
Bram Moolenaar217ad922005-03-20 22:37:15 +00005000 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02005001# ifdef FEAT_SPELL
5002 has_spell ? &can_spell :
5003# endif
5004 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
5006 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005007 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005008 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005009 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005010 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 else
5013 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005014
5015 // combine syntax attribute with 'wincolor'
5016 if (win_attr != 0)
5017 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
5018
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005019#ifdef SYN_TIME_LIMIT
5020 if (wp->w_s->b_syn_slow)
5021 has_syntax = FALSE;
5022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023
5024 /* Need to get the line again, a multi-line regexp may
5025 * have made it invalid. */
5026 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
5027 ptr = line + v;
5028
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005029# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02005030 // Text properties overrule syntax highlighting or combine.
5031 if (text_prop_attr == 0 || text_prop_combine)
5032# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005033 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02005034 int comb_attr = syntax_attr;
5035# ifdef FEAT_TEXT_PROP
5036 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
5037# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005038 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02005039 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005040 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02005041 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005042 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02005043# ifdef FEAT_CONCEAL
5044 /* no concealing past the end of the line, it interferes
5045 * with line highlighting */
5046 if (c == NUL)
5047 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005048 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005049 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02005050# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005052#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00005053
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005054#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005055 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00005056 * Only do this when there is no syntax highlighting, the
5057 * @Spell cluster is not used or the current syntax item
5058 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00005059 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00005060 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005061 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005062 if (c != 0 && (
5063# ifdef FEAT_SYN_HL
5064 !has_syntax ||
5065# endif
5066 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00005067 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00005068 char_u *prev_ptr, *p;
5069 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005070 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00005071 if (has_mbyte)
5072 {
5073 prev_ptr = ptr - mb_l;
5074 v -= mb_l - 1;
5075 }
5076 else
Bram Moolenaare7566042005-06-17 22:00:15 +00005077 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00005078
5079 /* Use nextline[] if possible, it has the start of the
5080 * next line concatenated. */
5081 if ((prev_ptr - line) - nextlinecol >= 0)
5082 p = nextline + (prev_ptr - line) - nextlinecol;
5083 else
5084 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005085 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005086 len = spell_check(wp, p, &spell_hlf, &cap_col,
5087 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005088 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005089
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005090 /* In Insert mode only highlight a word that
5091 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005092 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005093 && (State & INSERT) != 0
5094 && wp->w_cursor.lnum == lnum
5095 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00005096 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005097 && wp->w_cursor.col < (colnr_T)word_end)
5098 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005099 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005100 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005101 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00005102
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005103 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00005104 && (p - nextline) + len > nextline_idx)
5105 {
5106 /* Remember that the good word continues at the
5107 * start of the next line. */
5108 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005109 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005110 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005111
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005112 /* Turn index into actual attributes. */
5113 if (spell_hlf != HLF_COUNT)
5114 spell_attr = highlight_attr[spell_hlf];
5115
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005116 if (cap_col > 0)
5117 {
5118 if (p != prev_ptr
5119 && (p - nextline) + cap_col >= nextline_idx)
5120 {
5121 /* Remember that the word in the next line
5122 * must start with a capital. */
5123 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005124 cap_col = (int)((p - nextline) + cap_col
5125 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005126 }
5127 else
5128 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005129 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005130 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005131 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005132 }
5133 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005134 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005135 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005136 char_attr = hl_combine_attr(char_attr, spell_attr);
5137 else
5138 char_attr = hl_combine_attr(spell_attr, char_attr);
5139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140#endif
5141#ifdef FEAT_LINEBREAK
5142 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00005143 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01005145 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01005146 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01005148 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005149 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01005150
Bram Moolenaar597a4222014-06-25 14:39:50 +02005151 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005152 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02005153 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02005154 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005155# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02005156 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005157 wp->w_buffer->b_p_vts_array) - 1;
5158# else
Bram Moolenaara3650912014-11-19 13:21:57 +01005159 n_extra = (int)wp->w_buffer->b_p_ts
5160 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005161# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01005162
Bram Moolenaar4df70292015-03-21 14:20:16 +01005163 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01005164 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005165 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005166 {
5167#ifdef FEAT_CONCEAL
5168 if (c == TAB)
5169 /* See "Tab alignment" below. */
5170 FIX_FOR_BOGUSCOLS;
5171#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005172 if (!wp->w_p_list)
5173 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
5176#endif
5177
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005178 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
5179 // But not when the character is followed by a composing
5180 // character (use mb_l to check that).
5181 if (wp->w_p_list
5182 && ((((c == 160 && mb_l == 1)
5183 || (mb_utf8
5184 && ((mb_c == 160 && mb_l == 2)
5185 || (mb_c == 0x202f && mb_l == 3))))
5186 && lcs_nbsp)
5187 || (c == ' '
5188 && mb_l == 1
5189 && lcs_space
5190 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005191 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005192 c = (c == ' ') ? lcs_space : lcs_nbsp;
5193 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005194 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005195 n_attr = 1;
5196 extra_attr = HL_ATTR(HLF_8);
5197 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005198 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005199 mb_c = c;
5200 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005201 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005202 mb_utf8 = TRUE;
5203 u8cc[0] = 0;
5204 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005205 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005206 else
5207 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005208 }
5209
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
5211 {
5212 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005213 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 {
5215 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005216 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 saved_attr2 = char_attr; /* save current attr */
5218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005220 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
5222 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005223 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005224 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
5226 else
5227 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
5229 }
5230
5231 /*
5232 * Handling of non-printable characters.
5233 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005234 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 {
5236 /*
5237 * when getting a character from the file, we may have to
5238 * turn it into something else on the way to putting it
5239 * into "ScreenLines".
5240 */
5241 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5242 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005243 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005244 long vcol_adjusted = vcol; /* removed showbreak length */
5245#ifdef FEAT_LINEBREAK
5246 /* only adjust the tab_len, when at the first column
5247 * after the showbreak value was drawn */
5248 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5249 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005252#ifdef FEAT_VARTABS
5253 tab_len = tabstop_padding(vcol_adjusted,
5254 wp->w_buffer->b_p_ts,
5255 wp->w_buffer->b_p_vts_array) - 1;
5256#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005257 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005258 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5259#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005260
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005261#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005262 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005263#endif
5264 /* tab amount depends on current column */
5265 n_extra = tab_len;
5266#ifdef FEAT_LINEBREAK
5267 else
5268 {
5269 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005270 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005271 int i;
5272 int saved_nextra = n_extra;
5273
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005274#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005275 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005276 /* there are characters to conceal */
5277 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005278 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5279 */
5280 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5281 && n_extra > tab_len)
5282 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005283#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005284
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005285 /* if n_extra > 0, it gives the number of chars, to
5286 * use for a tab, else we need to calculate the width
5287 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005288 len = (tab_len * mb_char2len(lcs_tab2));
5289 if (n_extra > 0)
5290 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005291 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005292 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005293 vim_memset(p, ' ', len);
5294 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005295 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005296 p_extra_free = p;
5297 for (i = 0; i < tab_len; i++)
5298 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005299 if (*p == NUL)
5300 {
5301 tab_len = i;
5302 break;
5303 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005304 mb_char2bytes(lcs_tab2, p);
5305 p += mb_char2len(lcs_tab2);
5306 n_extra += mb_char2len(lcs_tab2)
5307 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005308 }
5309 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005310#ifdef FEAT_CONCEAL
5311 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5312 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005313 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005314 n_extra -= vcol_off;
5315#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005316 }
5317#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005318#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005319 {
5320 int vc_saved = vcol_off;
5321
5322 /* Tab alignment should be identical regardless of
5323 * 'conceallevel' value. So tab compensates of all
5324 * previous concealed characters, and thus resets
5325 * vcol_off and boguscols accumulated so far in the
5326 * line. Note that the tab can be longer than
5327 * 'tabstop' when there are concealed characters. */
5328 FIX_FOR_BOGUSCOLS;
5329
5330 /* Make sure, the highlighting for the tab char will be
5331 * correctly set further below (effectively reverts the
5332 * FIX_FOR_BOGSUCOLS macro */
5333 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005334 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005335 tab_len += vc_saved;
5336 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 if (wp->w_p_list)
5340 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005341 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005342#ifdef FEAT_LINEBREAK
5343 if (wp->w_p_lbr)
5344 c_extra = NUL; /* using p_extra from above */
5345 else
5346#endif
5347 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005348 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005349 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005350 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005353 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 {
5355 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005356 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005357 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 }
5360 else
5361 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005362 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 c_extra = ' ';
5364 c = ' ';
5365 }
5366 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005367 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005368 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005369 || ((fromcol >= 0 || fromcol_prev >= 0)
5370 && tocol > vcol
5371 && VIsual_mode != Ctrl_V
5372 && (
5373# ifdef FEAT_RIGHTLEFT
5374 wp->w_p_rl ? (col >= 0) :
5375# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005376 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005377 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005378 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005379 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005380 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005382 /* Display a '$' after the line or highlight an extra
5383 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5385 /* For a diff line the highlighting continues after the
5386 * "$". */
5387 if (
5388# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005389 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390# ifdef LINE_ATTR
5391 &&
5392# endif
5393# endif
5394# ifdef LINE_ATTR
5395 line_attr == 0
5396# endif
5397 )
5398#endif
5399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 /* In virtualedit, visual selections may extend
5401 * beyond end of line. */
5402 if (area_highlighting && virtual_active()
5403 && tocol != MAXCOL && vcol < tocol)
5404 n_extra = 0;
5405 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 {
5407 p_extra = at_end_str;
5408 n_extra = 1;
5409 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005410 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 }
5412 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005413 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005414 c = lcs_eol;
5415 else
5416 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 lcs_eol_one = -1;
5418 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005419 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005421 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 n_attr = 1;
5423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005425 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 {
5427 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005428 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005429 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
5431 else
5432 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 }
5434 else if (c != NUL)
5435 {
5436 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005437 if (n_extra == 0)
5438 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439#ifdef FEAT_RIGHTLEFT
5440 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5441 rl_mirror(p_extra); /* reverse "<12>" */
5442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005444 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005445#ifdef FEAT_LINEBREAK
5446 if (wp->w_p_lbr)
5447 {
5448 char_u *p;
5449
5450 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005451 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005452 vim_memset(p, ' ', n_extra);
5453 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5454 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005455 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005456 p_extra_free = p_extra = p;
5457 }
5458 else
5459#endif
5460 {
5461 n_extra = byte2cells(c) - 1;
5462 c = *p_extra++;
5463 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005464 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {
5466 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005467 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 saved_attr2 = char_attr; /* save current attr */
5469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 else if (VIsual_active
5473 && (VIsual_mode == Ctrl_V
5474 || VIsual_mode == 'v')
5475 && virtual_active()
5476 && tocol != MAXCOL
5477 && vcol < tocol
5478 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005479#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005481#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005482 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 {
5484 c = ' ';
5485 --ptr; /* put it back at the NUL */
5486 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005487#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 else if ((
5489# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005490 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005492# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005493 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005494# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 ) && (
5497# ifdef FEAT_RIGHTLEFT
5498 wp->w_p_rl ? (col >= 0) :
5499# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005500 (col
5501# ifdef FEAT_CONCEAL
5502 - boguscols
5503# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005504 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 {
5506 /* Highlight until the right side of the window */
5507 c = ' ';
5508 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005509
5510 /* Remember we do the char for line highlighting. */
5511 ++did_line_attr;
5512
5513 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005514 if (line_attr != 0 && char_attr == search_attr
5515 && (did_line_attr > 1
5516 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005517 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518# ifdef FEAT_DIFF
5519 if (diff_hlf == HLF_TXD)
5520 {
5521 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005522 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005523 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005524 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005525 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5526 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005527 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 }
5530# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005531# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005532 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005533 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005534 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005535 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5536 char_attr = hl_combine_attr(char_attr,
5537 HL_ATTR(HLF_CUL));
5538 }
5539# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 }
5541#endif
5542 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005543
5544#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005545 if ( wp->w_p_cole > 0
5546 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005547 conceal_cursor_line(wp))
5548 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005549 && !(lnum_in_visual_area
5550 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005551 {
5552 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005553 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005554 && (syn_get_sub_char() != NUL || match_conc
5555 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005556 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005557 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005558 /* First time at this concealed item: display one
5559 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005560 if (match_conc)
5561 c = match_conc;
5562 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005563 c = syn_get_sub_char();
5564 else if (lcs_conceal != NUL)
5565 c = lcs_conceal;
5566 else
5567 c = ' ';
5568
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005569 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005570
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005571 if (n_extra > 0)
5572 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005573 vcol += n_extra;
5574 if (wp->w_p_wrap && n_extra > 0)
5575 {
5576# ifdef FEAT_RIGHTLEFT
5577 if (wp->w_p_rl)
5578 {
5579 col -= n_extra;
5580 boguscols -= n_extra;
5581 }
5582 else
5583# endif
5584 {
5585 boguscols += n_extra;
5586 col += n_extra;
5587 }
5588 }
5589 n_extra = 0;
5590 n_attr = 0;
5591 }
5592 else if (n_skip == 0)
5593 {
5594 is_concealing = TRUE;
5595 n_skip = 1;
5596 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005597 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005598 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005599 {
5600 mb_utf8 = TRUE;
5601 u8cc[0] = 0;
5602 c = 0xc0;
5603 }
5604 else
5605 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005606 }
5607 else
5608 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005609 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005610 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005611 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005612
5613 if (n_skip > 0 && did_decrement_ptr)
5614 // not showing the '>', put pointer back to avoid getting stuck
5615 ++ptr;
5616
5617#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
5619
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005620#ifdef FEAT_CONCEAL
5621 /* In the cursor line and we may be concealing characters: correct
5622 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005623 if (!did_wcol && draw_state == WL_LINE
5624 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005625 && conceal_cursor_line(wp)
5626 && (int)wp->w_virtcol <= vcol + n_skip)
5627 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005628# ifdef FEAT_RIGHTLEFT
5629 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005630 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005631 else
5632# endif
5633 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005634 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005635 did_wcol = TRUE;
5636 }
5637#endif
5638
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 /* Don't override visual selection highlighting. */
5640 if (n_attr > 0
5641 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005642 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 char_attr = extra_attr;
5644
Bram Moolenaar81695252004-12-29 20:58:21 +00005645#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 /* XIM don't send preedit_start and preedit_end, but they send
5647 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5648 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005649 if (p_imst == IM_ON_THE_SPOT
5650 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005651 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 && (State & INSERT)
5653 && !p_imdisable
5654 && im_is_preediting()
5655 && draw_state == WL_LINE)
5656 {
5657 colnr_T tcol;
5658
5659 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005660 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 else
5662 tcol = preedit_end_col;
5663 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5664 {
5665 if (feedback_old_attr < 0)
5666 {
5667 feedback_col = 0;
5668 feedback_old_attr = char_attr;
5669 }
5670 char_attr = im_get_feedback_attr(feedback_col);
5671 if (char_attr < 0)
5672 char_attr = feedback_old_attr;
5673 feedback_col++;
5674 }
5675 else if (feedback_old_attr >= 0)
5676 {
5677 char_attr = feedback_old_attr;
5678 feedback_old_attr = -1;
5679 feedback_col = 0;
5680 }
5681 }
5682#endif
5683 /*
5684 * Handle the case where we are in column 0 but not on the first
5685 * character of the line and the user wants us to show us a
5686 * special character (via 'listchars' option "precedes:<char>".
5687 */
5688 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005689 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5691#ifdef FEAT_DIFF
5692 && filler_todo <= 0
5693#endif
5694 && draw_state > WL_NR
5695 && c != NUL)
5696 {
5697 c = lcs_prec;
5698 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005699 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5700 {
5701 /* Double-width character being overwritten by the "precedes"
5702 * character, need to fill up half the character. */
5703 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005704 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005705 n_extra = 1;
5706 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005707 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005710 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 {
5712 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005713 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005714 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 }
5716 else
5717 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005718 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 {
5720 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005721 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 n_attr3 = 1;
5723 }
5724 }
5725
5726 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005727 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005729 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005730#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005731 || did_line_attr == 1
5732#endif
5733 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005735#ifdef FEAT_SEARCH_EXTRA
5736 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005737
5738 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005739 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005740 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005741#endif
5742
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005743 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 * highlight match at end of line. If it's beyond the last
5745 * char on the screen, just overwrite that one (tricky!) Not
5746 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005747#ifdef FEAT_SEARCH_EXTRA
5748 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005749 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005750 prevcol_hl_flag = TRUE;
5751 else
5752 {
5753 cur = wp->w_match_head;
5754 while (cur != NULL)
5755 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005756 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005757 {
5758 prevcol_hl_flag = TRUE;
5759 break;
5760 }
5761 cur = cur->next;
5762 }
5763 }
5764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005766 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005767 && (VIsual_mode != Ctrl_V
5768 || lnum == VIsual.lnum
5769 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005770 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771#ifdef FEAT_SEARCH_EXTRA
5772 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005773 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005774# ifdef FEAT_SYN_HL
5775 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5776 && !(wp == curwin && VIsual_active))
5777# endif
5778# ifdef FEAT_DIFF
5779 && diff_hlf == (hlf_T)0
5780# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005781# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005782 && did_line_attr <= 1
5783# endif
5784 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785#endif
5786 ))
5787 {
5788 int n = 0;
5789
5790#ifdef FEAT_RIGHTLEFT
5791 if (wp->w_p_rl)
5792 {
5793 if (col < 0)
5794 n = 1;
5795 }
5796 else
5797#endif
5798 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005799 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 n = -1;
5801 }
5802 if (n != 0)
5803 {
5804 /* At the window boundary, highlight the last character
5805 * instead (better than nothing). */
5806 off += n;
5807 col += n;
5808 }
5809 else
5810 {
5811 /* Add a blank character to highlight. */
5812 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 if (enc_utf8)
5814 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 }
5816#ifdef FEAT_SEARCH_EXTRA
5817 if (area_attr == 0)
5818 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005819 /* Use attributes from match with highest priority among
5820 * 'search_hl' and the match list. */
5821 char_attr = search_hl.attr;
5822 cur = wp->w_match_head;
5823 shl_flag = FALSE;
5824 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005825 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005826 if (shl_flag == FALSE
5827 && ((cur != NULL
5828 && cur->priority > SEARCH_HL_PRIORITY)
5829 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005830 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005831 shl = &search_hl;
5832 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005833 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005834 else
5835 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005836 if ((ptr - line) - 1 == (long)shl->startcol
5837 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005838 char_attr = shl->attr;
5839 if (shl != &search_hl && cur != NULL)
5840 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 }
5843#endif
5844 ScreenAttrs[off] = char_attr;
5845#ifdef FEAT_RIGHTLEFT
5846 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005847 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005849 --off;
5850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 else
5852#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005853 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005855 ++off;
5856 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005857 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005858#ifdef FEAT_SYN_HL
5859 eol_hl_off = 1;
5860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863
Bram Moolenaar91170f82006-05-05 21:15:17 +00005864 /*
5865 * At end of the text line.
5866 */
5867 if (c == NUL)
5868 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005869#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005870 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005871 if (wp->w_p_wrap)
5872 v = wp->w_skipcol;
5873 else
5874 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005875
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005876 /* check if line ends before left margin */
5877 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005878 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005879#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005880 // Get rid of the boguscols now, we want to draw until the right
5881 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005882 col -= boguscols;
5883 boguscols = 0;
5884#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005885
5886 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005887 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005888
5889 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005890 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5891 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005892 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005893 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005894 || draw_color_col
5895 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005896# ifdef FEAT_RIGHTLEFT
5897 && !wp->w_p_rl
5898# endif
5899 )
5900 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005901 int rightmost_vcol = 0;
5902 int i;
5903
5904 if (wp->w_p_cuc)
5905 rightmost_vcol = wp->w_virtcol;
5906 if (draw_color_col)
5907 /* determine rightmost colorcolumn to possibly draw */
5908 for (i = 0; color_cols[i] >= 0; ++i)
5909 if (rightmost_vcol < color_cols[i])
5910 rightmost_vcol = color_cols[i];
5911
Bram Moolenaar02631462017-09-22 15:20:32 +02005912 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005913 {
5914 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005915 if (enc_utf8)
5916 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005917 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005918 if (draw_color_col)
5919 draw_color_col = advance_color_col(VCOL_HLC,
5920 &color_cols);
5921
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005922 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005923 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005924 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005925 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005926 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005927 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005928
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005929 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005930 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005931
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005932 ++vcol;
5933 }
5934 }
5935#endif
5936
Bram Moolenaar53f81742017-09-22 14:35:51 +02005937 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005938 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 row++;
5940
5941 /*
5942 * Update w_cline_height and w_cline_folded if the cursor line was
5943 * updated (saves a call to plines() later).
5944 */
5945 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5946 {
5947 curwin->w_cline_row = startrow;
5948 curwin->w_cline_height = row - startrow;
5949#ifdef FEAT_FOLDING
5950 curwin->w_cline_folded = FALSE;
5951#endif
5952 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5953 }
5954
5955 break;
5956 }
5957
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005958 // Show "extends" character from 'listchars' if beyond the line end and
5959 // 'list' is set.
5960 if (lcs_ext != NUL
5961 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 && !wp->w_p_wrap
5963#ifdef FEAT_DIFF
5964 && filler_todo <= 0
5965#endif
5966 && (
5967#ifdef FEAT_RIGHTLEFT
5968 wp->w_p_rl ? col == 0 :
5969#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005970 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005972 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5974 {
5975 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005976 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005978 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 {
5980 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005981 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005982 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 }
5984 else
5985 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 }
5987
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005988#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005989 /* advance to the next 'colorcolumn' */
5990 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005991 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005992
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005993 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005994 * highlight the cursor position itself.
5995 * Also highlight the 'colorcolumn' if it is different than
5996 * 'cursorcolumn' */
5997 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005998 if (draw_state == WL_LINE && !lnum_in_visual_area
5999 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006000 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02006001 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02006002 && lnum != wp->w_cursor.lnum)
6003 {
6004 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006005 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02006006 }
Bram Moolenaard160c342010-07-18 23:30:34 +02006007 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02006008 {
6009 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006010 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02006011 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006012 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006013#endif
6014
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 /*
6016 * Store character to be displayed.
6017 * Skip characters that are left of the screen for 'nowrap'.
6018 */
6019 vcol_prev = vcol;
6020 if (draw_state < WL_LINE || n_skip <= 0)
6021 {
6022 /*
6023 * Store the character.
6024 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01006025#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
6027 {
6028 /* A double-wide character is: put first halve in left cell. */
6029 --off;
6030 --col;
6031 }
6032#endif
6033 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01006035 {
6036 if ((mb_c & 0xff00) == 0x8e00)
6037 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01006039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 else if (enc_utf8)
6041 {
6042 if (mb_utf8)
6043 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006044 int i;
6045
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006047 if ((c & 0xff) == 0)
6048 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006049 for (i = 0; i < Screen_mco; ++i)
6050 {
6051 ScreenLinesC[i][off] = u8cc[i];
6052 if (u8cc[i] == 0)
6053 break;
6054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 }
6056 else
6057 ScreenLinesUC[off] = 0;
6058 }
6059 if (multi_attr)
6060 {
6061 ScreenAttrs[off] = multi_attr;
6062 multi_attr = 0;
6063 }
6064 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 ScreenAttrs[off] = char_attr;
6066
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
6068 {
6069 /* Need to fill two screen columns. */
6070 ++off;
6071 ++col;
6072 if (enc_utf8)
6073 /* UTF-8: Put a 0 in the second screen char. */
6074 ScreenLines[off] = 0;
6075 else
6076 /* DBCS: Put second byte in the second screen char. */
6077 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01006078 if (draw_state > WL_NR
6079#ifdef FEAT_DIFF
6080 && filler_todo <= 0
6081#endif
6082 )
6083 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 /* When "tocol" is halfway a character, set it to the end of
6085 * the character, otherwise highlighting won't stop. */
6086 if (tocol == vcol)
6087 ++tocol;
6088#ifdef FEAT_RIGHTLEFT
6089 if (wp->w_p_rl)
6090 {
6091 /* now it's time to backup one cell */
6092 --off;
6093 --col;
6094 }
6095#endif
6096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097#ifdef FEAT_RIGHTLEFT
6098 if (wp->w_p_rl)
6099 {
6100 --off;
6101 --col;
6102 }
6103 else
6104#endif
6105 {
6106 ++off;
6107 ++col;
6108 }
6109 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006110#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006111 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02006112 {
6113 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02006114 ++vcol_off;
6115 if (n_extra > 0)
6116 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02006117 if (wp->w_p_wrap)
6118 {
6119 /*
6120 * Special voodoo required if 'wrap' is on.
6121 *
6122 * Advance the column indicator to force the line
6123 * drawing to wrap early. This will make the line
6124 * take up the same screen space when parts are concealed,
6125 * so that cursor line computations aren't messed up.
6126 *
6127 * To avoid the fictitious advance of 'col' causing
6128 * trailing junk to be written out of the screen line
6129 * we are building, 'boguscols' keeps track of the number
6130 * of bad columns we have advanced.
6131 */
6132 if (n_extra > 0)
6133 {
6134 vcol += n_extra;
6135# ifdef FEAT_RIGHTLEFT
6136 if (wp->w_p_rl)
6137 {
6138 col -= n_extra;
6139 boguscols -= n_extra;
6140 }
6141 else
6142# endif
6143 {
6144 col += n_extra;
6145 boguscols += n_extra;
6146 }
6147 n_extra = 0;
6148 n_attr = 0;
6149 }
6150
6151
Bram Moolenaar860cae12010-06-05 23:22:07 +02006152 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
6153 {
6154 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01006155# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02006156 if (wp->w_p_rl)
6157 {
6158 --boguscols;
6159 --col;
6160 }
6161 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01006162# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02006163 {
6164 ++boguscols;
6165 ++col;
6166 }
6167 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006168
6169# ifdef FEAT_RIGHTLEFT
6170 if (wp->w_p_rl)
6171 {
6172 --boguscols;
6173 --col;
6174 }
6175 else
6176# endif
6177 {
6178 ++boguscols;
6179 ++col;
6180 }
6181 }
6182 else
6183 {
6184 if (n_extra > 0)
6185 {
6186 vcol += n_extra;
6187 n_extra = 0;
6188 n_attr = 0;
6189 }
6190 }
6191
6192 }
6193#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 else
6195 --n_skip;
6196
Bram Moolenaar64486672010-05-16 15:46:46 +02006197 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
6198 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00006199 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200#ifdef FEAT_DIFF
6201 && filler_todo <= 0
6202#endif
6203 )
6204 ++vcol;
6205
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006206#ifdef FEAT_SYN_HL
6207 if (vcol_save_attr >= 0)
6208 char_attr = vcol_save_attr;
6209#endif
6210
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 /* restore attributes after "predeces" in 'listchars' */
6212 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
6213 char_attr = saved_attr3;
6214
6215 /* restore attributes after last 'listchars' or 'number' char */
6216 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
6217 char_attr = saved_attr2;
6218
6219 /*
6220 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00006221 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 */
6223 if ((
6224#ifdef FEAT_RIGHTLEFT
6225 wp->w_p_rl ? (col < 0) :
6226#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006227 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 && (*ptr != NUL
6229#ifdef FEAT_DIFF
6230 || filler_todo > 0
6231#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006232 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6234 )
6235 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006236#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006237 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006238 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006239 boguscols = 0;
6240#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006241 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006242 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 ++row;
6245 ++screen_row;
6246
6247 /* When not wrapping and finished diff lines, or when displayed
6248 * '$' and highlighting until last column, break here. */
6249 if ((!wp->w_p_wrap
6250#ifdef FEAT_DIFF
6251 && filler_todo <= 0
6252#endif
6253 ) || lcs_eol_one == -1)
6254 break;
6255
6256 /* When the window is too narrow draw all "@" lines. */
6257 if (draw_state != WL_LINE
6258#ifdef FEAT_DIFF
6259 && filler_todo <= 0
6260#endif
6261 )
6262 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006263 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 row = endrow;
6266 }
6267
6268 /* When line got too long for screen break here. */
6269 if (row == endrow)
6270 {
6271 ++row;
6272 break;
6273 }
6274
6275 if (screen_cur_row == screen_row - 1
6276#ifdef FEAT_DIFF
6277 && filler_todo <= 0
6278#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006279 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 {
6281 /* Remember that the line wraps, used for modeless copy. */
6282 LineWraps[screen_row - 1] = TRUE;
6283
6284 /*
6285 * Special trick to make copy/paste of wrapped lines work with
6286 * xterm/screen: write an extra character beyond the end of
6287 * the line. This will work with all terminal types
6288 * (regardless of the xn,am settings).
6289 * Only do this on a fast tty.
6290 * Only do this if the cursor is on the current line
6291 * (something has been written in it).
6292 * Don't do this for the GUI.
6293 * Don't do this for double-width characters.
6294 * Don't do this for a window not at the right screen border.
6295 */
6296 if (p_tf
6297#ifdef FEAT_GUI
6298 && !gui.in_use
6299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006301 && ((*mb_off2cells)(LineOffset[screen_row],
6302 LineOffset[screen_row] + screen_Columns)
6303 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006305 + (int)Columns - 2,
6306 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006307 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 {
6309 /* First make sure we are at the end of the screen line,
6310 * then output the same character again to let the
6311 * terminal know about the wrap. If the terminal doesn't
6312 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006313 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 screen_char(LineOffset[screen_row - 1]
6315 + (unsigned)Columns - 1,
6316 screen_row - 1, (int)(Columns - 1));
6317
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 /* When there is a multi-byte character, just output a
6319 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006320 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6321 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 out_char(' ');
6323 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 out_char(ScreenLines[LineOffset[screen_row - 1]
6325 + (Columns - 1)]);
6326 /* force a redraw of the first char on the next line */
6327 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6328 screen_start(); /* don't know where cursor is now */
6329 }
6330 }
6331
6332 col = 0;
6333 off = (unsigned)(current_ScreenLine - ScreenLines);
6334#ifdef FEAT_RIGHTLEFT
6335 if (wp->w_p_rl)
6336 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006337 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 off += col;
6339 }
6340#endif
6341
6342 /* reset the drawing state for the start of a wrapped line */
6343 draw_state = WL_START;
6344 saved_n_extra = n_extra;
6345 saved_p_extra = p_extra;
6346 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006347 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 saved_char_attr = char_attr;
6349 n_extra = 0;
6350 lcs_prec_todo = lcs_prec;
6351#ifdef FEAT_LINEBREAK
6352# ifdef FEAT_DIFF
6353 if (filler_todo <= 0)
6354# endif
6355 need_showbreak = TRUE;
6356#endif
6357#ifdef FEAT_DIFF
6358 --filler_todo;
6359 /* When the filler lines are actually below the last line of the
6360 * file, don't draw the line itself, break here. */
6361 if (filler_todo == 0 && wp->w_botfill)
6362 break;
6363#endif
6364 }
6365
6366 } /* for every character in the line */
6367
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006368#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006369 /* After an empty line check first word for capital. */
6370 if (*skipwhite(line) == NUL)
6371 {
6372 capcol_lnum = lnum + 1;
6373 cap_col = 0;
6374 }
6375#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006376#ifdef FEAT_TEXT_PROP
6377 vim_free(text_props);
6378 vim_free(text_prop_idxs);
6379#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006380
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006381 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 return row;
6383}
6384
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006385/*
6386 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006387 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006388 */
6389 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006390comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006391{
6392 int i;
6393
6394 for (i = 0; i < Screen_mco; ++i)
6395 {
6396 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6397 return TRUE;
6398 if (ScreenLinesC[i][off_from] == 0)
6399 break;
6400 }
6401 return FALSE;
6402}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006403
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404/*
6405 * Check whether the given character needs redrawing:
6406 * - the (first byte of the) character is different
6407 * - the attributes are different
6408 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006409 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 */
6411 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006412char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413{
6414 if (cols > 0
6415 && ((ScreenLines[off_from] != ScreenLines[off_to]
6416 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 || (enc_dbcs != 0
6418 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6419 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6420 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6421 : (cols > 1 && ScreenLines[off_from + 1]
6422 != ScreenLines[off_to + 1])))
6423 || (enc_utf8
6424 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6425 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006426 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006427 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6428 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006429 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 return TRUE;
6431 return FALSE;
6432}
6433
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006434#if defined(FEAT_TERMINAL) || defined(PROTO)
6435/*
6436 * Return the index in ScreenLines[] for the current screen line.
6437 */
6438 int
6439screen_get_current_line_off()
6440{
6441 return (int)(current_ScreenLine - ScreenLines);
6442}
6443#endif
6444
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445/*
6446 * Move one "cooked" screen line to the screen, but only the characters that
6447 * have actually changed. Handle insert/delete character.
6448 * "coloff" gives the first column on the screen for this line.
6449 * "endcol" gives the columns where valid characters are.
6450 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6451 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006452 * "flags" can have bits:
6453 * SLF_POPUP popup window
6454 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6456 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6457 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006458 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006459screen_line(
6460 int row,
6461 int coloff,
6462 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006463 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006464 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465{
6466 unsigned off_from;
6467 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006468 unsigned max_off_from;
6469 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 int force = FALSE; /* force update rest of the line */
6473 int redraw_this /* bool: does character need redraw? */
6474#ifdef FEAT_GUI
6475 = TRUE /* For GUI when while-loop empty */
6476#endif
6477 ;
6478 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 int clear_next = FALSE;
6480 int char_cells; /* 1: normal char */
6481 /* 2: occupies two display cells */
6482# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006484 /* Check for illegal row and col, just in case. */
6485 if (row >= Rows)
6486 row = Rows - 1;
6487 if (endcol > Columns)
6488 endcol = Columns;
6489
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490# ifdef FEAT_CLIPBOARD
6491 clip_may_clear_selection(row, row);
6492# endif
6493
6494 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6495 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006496 max_off_from = off_from + screen_Columns;
6497 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498
6499#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006500 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 {
6502 /* Clear rest first, because it's left of the text. */
6503 if (clear_width > 0)
6504 {
6505 while (col <= endcol && ScreenLines[off_to] == ' '
6506 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006507 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 {
6509 ++off_to;
6510 ++col;
6511 }
6512 if (col <= endcol)
6513 screen_fill(row, row + 1, col + coloff,
6514 endcol + coloff + 1, ' ', ' ', 0);
6515 }
6516 col = endcol + 1;
6517 off_to = LineOffset[row] + col + coloff;
6518 off_from += col;
6519 endcol = (clear_width > 0 ? clear_width : -clear_width);
6520 }
6521#endif /* FEAT_RIGHTLEFT */
6522
6523 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6524
6525 while (col < endcol)
6526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006528 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 else
6530 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531
6532 redraw_this = redraw_next;
6533 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6534 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6535
6536#ifdef FEAT_GUI
6537 /* If the next character was bold, then redraw the current character to
6538 * remove any pixels that might have spilt over into us. This only
6539 * happens in the GUI.
6540 */
6541 if (redraw_next && gui.in_use)
6542 {
6543 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006544 if (hl > HL_ALL)
6545 hl = syn_attr2attr(hl);
6546 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 redraw_this = TRUE;
6548 }
6549#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006550#ifdef FEAT_TEXT_PROP
6551 // Skip if under a(nother) popup.
6552 if (popup_mask[row * screen_Columns + col + coloff] > screen_zindex)
6553 redraw_this = FALSE;
6554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555
6556 if (redraw_this)
6557 {
6558 /*
6559 * Special handling when 'xs' termcap flag set (hpterm):
6560 * Attributes for characters are stored at the position where the
6561 * cursor is when writing the highlighting code. The
6562 * start-highlighting code must be written with the cursor on the
6563 * first highlighted character. The stop-highlighting code must
6564 * be written with the cursor just after the last highlighted
6565 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006566 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 * to clear the rest of the line, and force redrawing it
6568 * completely.
6569 */
6570 if ( p_wiv
6571 && !force
6572#ifdef FEAT_GUI
6573 && !gui.in_use
6574#endif
6575 && ScreenAttrs[off_to] != 0
6576 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6577 {
6578 /*
6579 * Need to remove highlighting attributes here.
6580 */
6581 windgoto(row, col + coloff);
6582 out_str(T_CE); /* clear rest of this screen line */
6583 screen_start(); /* don't know where cursor is now */
6584 force = TRUE; /* force redraw of rest of the line */
6585 redraw_next = TRUE; /* or else next char would miss out */
6586
6587 /*
6588 * If the previous character was highlighted, need to stop
6589 * highlighting at this character.
6590 */
6591 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6592 {
6593 screen_attr = ScreenAttrs[off_to - 1];
6594 term_windgoto(row, col + coloff);
6595 screen_stop_highlight();
6596 }
6597 else
6598 screen_attr = 0; /* highlighting has stopped */
6599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 if (enc_dbcs != 0)
6601 {
6602 /* Check if overwriting a double-byte with a single-byte or
6603 * the other way around requires another character to be
6604 * redrawn. For UTF-8 this isn't needed, because comparing
6605 * ScreenLinesUC[] is sufficient. */
6606 if (char_cells == 1
6607 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006608 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 {
6610 /* Writing a single-cell character over a double-cell
6611 * character: need to redraw the next cell. */
6612 ScreenLines[off_to + 1] = 0;
6613 redraw_next = TRUE;
6614 }
6615 else if (char_cells == 2
6616 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006617 && (*mb_off2cells)(off_to, max_off_to) == 1
6618 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 {
6620 /* Writing the second half of a double-cell character over
6621 * a double-cell character: need to redraw the second
6622 * cell. */
6623 ScreenLines[off_to + 2] = 0;
6624 redraw_next = TRUE;
6625 }
6626
6627 if (enc_dbcs == DBCS_JPNU)
6628 ScreenLines2[off_to] = ScreenLines2[off_from];
6629 }
6630 /* When writing a single-width character over a double-width
6631 * character and at the end of the redrawn text, need to clear out
6632 * the right halve of the old character.
6633 * Also required when writing the right halve of a double-width
6634 * char over the left halve of an existing one. */
6635 if (has_mbyte && col + char_cells == endcol
6636 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006637 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006639 && (*mb_off2cells)(off_to, max_off_to) == 1
6640 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642
6643 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 if (enc_utf8)
6645 {
6646 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6647 if (ScreenLinesUC[off_from] != 0)
6648 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006649 int i;
6650
6651 for (i = 0; i < Screen_mco; ++i)
6652 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 }
6654 }
6655 if (char_cells == 2)
6656 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657
6658#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006659 /* The bold trick makes a single column of pixels appear in the
6660 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 * character should be redrawn too. This happens for our own GUI
6662 * and for some xterms. */
6663 if (
6664# ifdef FEAT_GUI
6665 gui.in_use
6666# endif
6667# if defined(FEAT_GUI) && defined(UNIX)
6668 ||
6669# endif
6670# ifdef UNIX
6671 term_is_xterm
6672# endif
6673 )
6674 {
6675 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006676 if (hl > HL_ALL)
6677 hl = syn_attr2attr(hl);
6678 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 redraw_next = TRUE;
6680 }
6681#endif
6682 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006683
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006684 /* For simplicity set the attributes of second half of a
6685 * double-wide character equal to the first half. */
6686 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006688
6689 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 screen_char(off_to, row, col + coloff);
6693 }
6694 else if ( p_wiv
6695#ifdef FEAT_GUI
6696 && !gui.in_use
6697#endif
6698 && col + coloff > 0)
6699 {
6700 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6701 {
6702 /*
6703 * Don't output stop-highlight when moving the cursor, it will
6704 * stop the highlighting when it should continue.
6705 */
6706 screen_attr = 0;
6707 }
6708 else if (screen_attr != 0)
6709 screen_stop_highlight();
6710 }
6711
6712 off_to += CHAR_CELLS;
6713 off_from += CHAR_CELLS;
6714 col += CHAR_CELLS;
6715 }
6716
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 if (clear_next)
6718 {
6719 /* Clear the second half of a double-wide character of which the left
6720 * half was overwritten with a single-wide character. */
6721 ScreenLines[off_to] = ' ';
6722 if (enc_utf8)
6723 ScreenLinesUC[off_to] = 0;
6724 screen_char(off_to, row, col + coloff);
6725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726
6727 if (clear_width > 0
6728#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006729 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730#endif
6731 )
6732 {
6733#ifdef FEAT_GUI
6734 int startCol = col;
6735#endif
6736
6737 /* blank out the rest of the line */
6738 while (col < clear_width && ScreenLines[off_to] == ' '
6739 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006740 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 {
6742 ++off_to;
6743 ++col;
6744 }
6745 if (col < clear_width)
6746 {
6747#ifdef FEAT_GUI
6748 /*
6749 * In the GUI, clearing the rest of the line may leave pixels
6750 * behind if the first character cleared was bold. Some bold
6751 * fonts spill over the left. In this case we redraw the previous
6752 * character too. If we didn't skip any blanks above, then we
6753 * only redraw if the character wasn't already redrawn anyway.
6754 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006755 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 {
6757 hl = ScreenAttrs[off_to];
6758 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006759 {
6760 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006761
Bram Moolenaar9c697322006-10-09 20:11:17 +00006762 if (enc_utf8)
6763 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6764 * that its width is 2. */
6765 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6766 else if (enc_dbcs != 0)
6767 {
6768 /* find previous character by counting from first
6769 * column and get its width. */
6770 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006771 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006772
6773 while (off < off_to)
6774 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006775 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006776 off += prev_cells;
6777 }
6778 }
6779
6780 if (enc_dbcs != 0 && prev_cells > 1)
6781 screen_char_2(off_to - prev_cells, row,
6782 col + coloff - prev_cells);
6783 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006784 screen_char(off_to - prev_cells, row,
6785 col + coloff - prev_cells);
6786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 }
6788#endif
6789 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6790 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 off_to += clear_width - col;
6792 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 }
6794 }
6795
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006796 if (clear_width > 0
6797#ifdef FEAT_TEXT_PROP
6798 && !(flags & SLF_POPUP) // no separator for popup window
6799#endif
6800 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006802 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006803 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006804 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006806#ifdef FEAT_TEXT_PROP
6807 if (popup_mask[row * screen_Columns + col + coloff]
6808 <= screen_zindex)
6809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006811 int c;
6812
6813 c = fillchar_vsep(&hl);
6814 if (ScreenLines[off_to] != (schar_T)c
6815 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6816 != (c >= 0x80 ? c : 0))
6817 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006819 ScreenLines[off_to] = c;
6820 ScreenAttrs[off_to] = hl;
6821 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006823 if (c >= 0x80)
6824 {
6825 ScreenLinesUC[off_to] = c;
6826 ScreenLinesC[0][off_to] = 0;
6827 }
6828 else
6829 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006831 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 }
6834 }
6835 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 LineWraps[row] = FALSE;
6837 }
6838}
6839
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006840#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006842 * Mirror text "str" for right-left displaying.
6843 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006845 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006846rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847{
6848 char_u *p1, *p2;
6849 int t;
6850
6851 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6852 {
6853 t = *p1;
6854 *p1 = *p2;
6855 *p2 = t;
6856 }
6857}
6858#endif
6859
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860/*
6861 * mark all status lines for redraw; used after first :cd
6862 */
6863 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006864status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865{
6866 win_T *wp;
6867
Bram Moolenaar29323592016-07-24 22:04:11 +02006868 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 if (wp->w_status_height)
6870 {
6871 wp->w_redr_status = TRUE;
6872 redraw_later(VALID);
6873 }
6874}
6875
6876/*
6877 * mark all status lines of the current buffer for redraw
6878 */
6879 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006880status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881{
6882 win_T *wp;
6883
Bram Moolenaar29323592016-07-24 22:04:11 +02006884 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6886 {
6887 wp->w_redr_status = TRUE;
6888 redraw_later(VALID);
6889 }
6890}
6891
6892/*
6893 * Redraw all status lines that need to be redrawn.
6894 */
6895 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006896redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897{
6898 win_T *wp;
6899
Bram Moolenaar29323592016-07-24 22:04:11 +02006900 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006902 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006903 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006904 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906
Bram Moolenaar4033c552017-09-16 20:54:51 +02006907#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908/*
6909 * Redraw all status lines at the bottom of frame "frp".
6910 */
6911 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006912win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913{
6914 if (frp->fr_layout == FR_LEAF)
6915 frp->fr_win->w_redr_status = TRUE;
6916 else if (frp->fr_layout == FR_ROW)
6917 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006918 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 win_redraw_last_status(frp);
6920 }
6921 else /* frp->fr_layout == FR_COL */
6922 {
6923 frp = frp->fr_child;
6924 while (frp->fr_next != NULL)
6925 frp = frp->fr_next;
6926 win_redraw_last_status(frp);
6927 }
6928}
6929#endif
6930
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931/*
6932 * Draw the verticap separator right of window "wp" starting with line "row".
6933 */
6934 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006935draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936{
6937 int hl;
6938 int c;
6939
6940 if (wp->w_vsep_width)
6941 {
6942 /* draw the vertical separator right of this window */
6943 c = fillchar_vsep(&hl);
6944 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6945 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6946 c, ' ', hl);
6947 }
6948}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949
6950#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006951static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952
6953/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006954 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 */
6956 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006957status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958{
6959 int len = 0;
6960
6961#ifdef FEAT_MENU
6962 int emenu = (xp->xp_context == EXPAND_MENUS
6963 || xp->xp_context == EXPAND_MENUNAMES);
6964
6965 /* Check for menu separators - replace with '|'. */
6966 if (emenu && menu_is_separator(s))
6967 return 1;
6968#endif
6969
6970 while (*s != NUL)
6971 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006972 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006973 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006974 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 }
6976
6977 return len;
6978}
6979
6980/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006981 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006982 * These are backslashes used for escaping. Do show backslashes in help tags.
6983 */
6984 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006985skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006986{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006987 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006988#ifdef FEAT_MENU
6989 || ((xp->xp_context == EXPAND_MENUS
6990 || xp->xp_context == EXPAND_MENUNAMES)
6991 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6992#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006993 )
6994 {
6995#ifndef BACKSLASH_IN_FILENAME
6996 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6997 return 2;
6998#endif
6999 return 1;
7000 }
7001 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007002}
7003
7004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 * Show wildchar matches in the status line.
7006 * Show at least the "match" item.
7007 * We start at item 'first_match' in the list and show all matches that fit.
7008 *
7009 * If inversion is possible we use it. Else '=' characters are used.
7010 */
7011 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007012win_redr_status_matches(
7013 expand_T *xp,
7014 int num_matches,
7015 char_u **matches, /* list of matches */
7016 int match,
7017 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018{
7019#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
7020 int row;
7021 char_u *buf;
7022 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007023 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 int fillchar;
7025 int attr;
7026 int i;
7027 int highlight = TRUE;
7028 char_u *selstart = NULL;
7029 int selstart_col = 0;
7030 char_u *selend = NULL;
7031 static int first_match = 0;
7032 int add_left = FALSE;
7033 char_u *s;
7034#ifdef FEAT_MENU
7035 int emenu;
7036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038
7039 if (matches == NULL) /* interrupted completion? */
7040 return;
7041
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007042 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02007043 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007044 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02007045 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 if (buf == NULL)
7047 return;
7048
7049 if (match == -1) /* don't show match but original text */
7050 {
7051 match = 0;
7052 highlight = FALSE;
7053 }
7054 /* count 1 for the ending ">" */
7055 clen = status_match_len(xp, L_MATCH(match)) + 3;
7056 if (match == 0)
7057 first_match = 0;
7058 else if (match < first_match)
7059 {
7060 /* jumping left, as far as we can go */
7061 first_match = match;
7062 add_left = TRUE;
7063 }
7064 else
7065 {
7066 /* check if match fits on the screen */
7067 for (i = first_match; i < match; ++i)
7068 clen += status_match_len(xp, L_MATCH(i)) + 2;
7069 if (first_match > 0)
7070 clen += 2;
7071 /* jumping right, put match at the left */
7072 if ((long)clen > Columns)
7073 {
7074 first_match = match;
7075 /* if showing the last match, we can add some on the left */
7076 clen = 2;
7077 for (i = match; i < num_matches; ++i)
7078 {
7079 clen += status_match_len(xp, L_MATCH(i)) + 2;
7080 if ((long)clen >= Columns)
7081 break;
7082 }
7083 if (i == num_matches)
7084 add_left = TRUE;
7085 }
7086 }
7087 if (add_left)
7088 while (first_match > 0)
7089 {
7090 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
7091 if ((long)clen >= Columns)
7092 break;
7093 --first_match;
7094 }
7095
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007096 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097
7098 if (first_match == 0)
7099 {
7100 *buf = NUL;
7101 len = 0;
7102 }
7103 else
7104 {
7105 STRCPY(buf, "< ");
7106 len = 2;
7107 }
7108 clen = len;
7109
7110 i = first_match;
7111 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
7112 {
7113 if (i == match)
7114 {
7115 selstart = buf + len;
7116 selstart_col = clen;
7117 }
7118
7119 s = L_MATCH(i);
7120 /* Check for menu separators - replace with '|' */
7121#ifdef FEAT_MENU
7122 emenu = (xp->xp_context == EXPAND_MENUS
7123 || xp->xp_context == EXPAND_MENUNAMES);
7124 if (emenu && menu_is_separator(s))
7125 {
7126 STRCPY(buf + len, transchar('|'));
7127 l = (int)STRLEN(buf + len);
7128 len += l;
7129 clen += l;
7130 }
7131 else
7132#endif
7133 for ( ; *s != NUL; ++s)
7134 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007135 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007137 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 {
7139 STRNCPY(buf + len, s, l);
7140 s += l - 1;
7141 len += l;
7142 }
7143 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 {
7145 STRCPY(buf + len, transchar_byte(*s));
7146 len += (int)STRLEN(buf + len);
7147 }
7148 }
7149 if (i == match)
7150 selend = buf + len;
7151
7152 *(buf + len++) = ' ';
7153 *(buf + len++) = ' ';
7154 clen += 2;
7155 if (++i == num_matches)
7156 break;
7157 }
7158
7159 if (i != num_matches)
7160 {
7161 *(buf + len++) = '>';
7162 ++clen;
7163 }
7164
7165 buf[len] = NUL;
7166
7167 row = cmdline_row - 1;
7168 if (row >= 0)
7169 {
7170 if (wild_menu_showing == 0)
7171 {
7172 if (msg_scrolled > 0)
7173 {
7174 /* Put the wildmenu just above the command line. If there is
7175 * no room, scroll the screen one line up. */
7176 if (cmdline_row == Rows - 1)
7177 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02007178 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 ++msg_scrolled;
7180 }
7181 else
7182 {
7183 ++cmdline_row;
7184 ++row;
7185 }
7186 wild_menu_showing = WM_SCROLLED;
7187 }
7188 else
7189 {
7190 /* Create status line if needed by setting 'laststatus' to 2.
7191 * Set 'winminheight' to zero to avoid that the window is
7192 * resized. */
7193 if (lastwin->w_status_height == 0)
7194 {
7195 save_p_ls = p_ls;
7196 save_p_wmh = p_wmh;
7197 p_ls = 2;
7198 p_wmh = 0;
7199 last_status(FALSE);
7200 }
7201 wild_menu_showing = WM_SHOWN;
7202 }
7203 }
7204
7205 screen_puts(buf, row, 0, attr);
7206 if (selstart != NULL && highlight)
7207 {
7208 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007209 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 }
7211
7212 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7213 }
7214
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 vim_free(buf);
7217}
7218#endif
7219
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220/*
7221 * Redraw the status line of window wp.
7222 *
7223 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007224 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7225 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007227 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007228win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
7230 int row;
7231 char_u *p;
7232 int len;
7233 int fillchar;
7234 int attr;
7235 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007236 static int busy = FALSE;
7237
7238 /* It's possible to get here recursively when 'statusline' (indirectly)
7239 * invokes ":redrawstatus". Simply ignore the call then. */
7240 if (busy)
7241 return;
7242 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243
7244 wp->w_redr_status = FALSE;
7245 if (wp->w_status_height == 0)
7246 {
7247 /* no status line, can only be last window */
7248 redraw_cmdline = TRUE;
7249 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007250 else if (!redrawing()
7251#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007252 // don't update status line when popup menu is visible and may be
7253 // drawn over it, unless it will be redrawn later
7254 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007255#endif
7256 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 {
7258 /* Don't redraw right now, do it later. */
7259 wp->w_redr_status = TRUE;
7260 }
7261#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007262 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 {
7264 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007265 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 }
7267#endif
7268 else
7269 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007270 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007272 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 p = NameBuff;
7274 len = (int)STRLEN(p);
7275
Bram Moolenaard85f2712017-07-28 21:51:57 +02007276 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277#ifdef FEAT_QUICKFIX
7278 || wp->w_p_pvw
7279#endif
7280 || bufIsChanged(wp->w_buffer)
7281 || wp->w_buffer->b_p_ro)
7282 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007283 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007285 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 len += (int)STRLEN(p + len);
7287 }
7288#ifdef FEAT_QUICKFIX
7289 if (wp->w_p_pvw)
7290 {
7291 STRCPY(p + len, _("[Preview]"));
7292 len += (int)STRLEN(p + len);
7293 }
7294#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007295 if (bufIsChanged(wp->w_buffer)
7296#ifdef FEAT_TERMINAL
7297 && !bt_terminal(wp->w_buffer)
7298#endif
7299 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 {
7301 STRCPY(p + len, "[+]");
7302 len += 3;
7303 }
7304 if (wp->w_buffer->b_p_ro)
7305 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007306 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007307 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 }
7309
Bram Moolenaar02631462017-09-22 15:20:32 +02007310 this_ru_col = ru_col - (Columns - wp->w_width);
7311 if (this_ru_col < (wp->w_width + 1) / 2)
7312 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 if (this_ru_col <= 1)
7314 {
7315 p = (char_u *)"<"; /* No room for file name! */
7316 len = 1;
7317 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007318 else if (has_mbyte)
7319 {
7320 int clen = 0, i;
7321
7322 /* Count total number of display cells. */
7323 clen = mb_string2cells(p, -1);
7324
7325 /* Find first character that will fit.
7326 * Going from start to end is much faster for DBCS. */
7327 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7328 i += (*mb_ptr2len)(p + i))
7329 clen -= (*mb_ptr2cells)(p + i);
7330 len = clen;
7331 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007333 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007335 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 }
7337
Bram Moolenaara12a1612019-01-24 16:39:02 +01007338 }
7339 else if (len > this_ru_col - 1)
7340 {
7341 p += len - (this_ru_col - 1);
7342 *p = '<';
7343 len = this_ru_col - 1;
7344 }
7345
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007347 screen_puts(p, row, wp->w_wincol, attr);
7348 screen_fill(row, row + 1, len + wp->w_wincol,
7349 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007351 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7353 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007354 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355
7356#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007357 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358#endif
7359 }
7360
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 /*
7362 * May need to draw the character below the vertical separator.
7363 */
7364 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7365 {
7366 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007367 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 else
7369 fillchar = fillchar_vsep(&attr);
7370 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7371 attr);
7372 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007373 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374}
7375
Bram Moolenaar238a5642006-02-21 22:12:05 +00007376#ifdef FEAT_STL_OPT
7377/*
7378 * Redraw the status line according to 'statusline' and take care of any
7379 * errors encountered.
7380 */
7381 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007382redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007383{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007384 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007385 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007386
7387 /* When called recursively return. This can happen when the statusline
7388 * contains an expression that triggers a redraw. */
7389 if (entered)
7390 return;
7391 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007392
Bram Moolenaara742e082016-04-05 21:10:38 +02007393 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007394 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007395 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007396 {
7397 /* When there is an error disable the statusline, otherwise the
7398 * display is messed up with errors and a redraw triggers the problem
7399 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007400 set_string_option_direct((char_u *)"statusline", -1,
7401 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007402 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007403 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007404 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007405 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007406}
7407#endif
7408
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409/*
7410 * Return TRUE if the status line of window "wp" is connected to the status
7411 * line of the window right of it. If not, then it's a vertical separator.
7412 * Only call if (wp->w_vsep_width != 0).
7413 */
7414 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007415stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416{
7417 frame_T *fr;
7418
7419 fr = wp->w_frame;
7420 while (fr->fr_parent != NULL)
7421 {
7422 if (fr->fr_parent->fr_layout == FR_COL)
7423 {
7424 if (fr->fr_next != NULL)
7425 break;
7426 }
7427 else
7428 {
7429 if (fr->fr_next != NULL)
7430 return TRUE;
7431 }
7432 fr = fr->fr_parent;
7433 }
7434 return FALSE;
7435}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438/*
7439 * Get the value to show for the language mappings, active 'keymap'.
7440 */
7441 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007442get_keymap_str(
7443 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007444 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007445 char_u *buf, /* buffer for the result */
7446 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447{
7448 char_u *p;
7449
7450 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7451 return FALSE;
7452
7453 {
7454#ifdef FEAT_EVAL
7455 buf_T *old_curbuf = curbuf;
7456 win_T *old_curwin = curwin;
7457 char_u *s;
7458
7459 curbuf = wp->w_buffer;
7460 curwin = wp;
7461 STRCPY(buf, "b:keymap_name"); /* must be writable */
7462 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007463 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 --emsg_skip;
7465 curbuf = old_curbuf;
7466 curwin = old_curwin;
7467 if (p == NULL || *p == NUL)
7468#endif
7469 {
7470#ifdef FEAT_KEYMAP
7471 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7472 p = wp->w_buffer->b_p_keymap;
7473 else
7474#endif
7475 p = (char_u *)"lang";
7476 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007477 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 buf[0] = NUL;
7479#ifdef FEAT_EVAL
7480 vim_free(s);
7481#endif
7482 }
7483 return buf[0] != NUL;
7484}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485
7486#if defined(FEAT_STL_OPT) || defined(PROTO)
7487/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007488 * Redraw the status line or ruler of window "wp".
7489 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 */
7491 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007492win_redr_custom(
7493 win_T *wp,
7494 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007496 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 int attr;
7498 int curattr;
7499 int row;
7500 int col = 0;
7501 int maxwidth;
7502 int width;
7503 int n;
7504 int len;
7505 int fillchar;
7506 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007507 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007509 struct stl_hlrec hltab[STL_MAX_ITEM];
7510 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007511 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007512 win_T *ewp;
7513 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514
Bram Moolenaar1d633412013-12-11 15:52:01 +01007515 /* There is a tiny chance that this gets called recursively: When
7516 * redrawing a status line triggers redrawing the ruler or tabline.
7517 * Avoid trouble by not allowing recursion. */
7518 if (entered)
7519 return;
7520 entered = TRUE;
7521
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007523 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007525 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007526 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007527 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007528 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007529 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007530 maxwidth = Columns;
7531# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007532 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007533# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007535 else
7536 {
7537 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007538 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007539 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007540
7541 if (draw_ruler)
7542 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007543 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007544 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007545 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007546 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007547 if (*++stl == '-')
7548 stl++;
7549 if (atoi((char *)stl))
7550 while (VIM_ISDIGIT(*stl))
7551 stl++;
7552 if (*stl++ != '(')
7553 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007554 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007555 col = ru_col - (Columns - wp->w_width);
7556 if (col < (wp->w_width + 1) / 2)
7557 col = (wp->w_width + 1) / 2;
7558 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007559 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007560 {
7561 row = Rows - 1;
7562 --maxwidth; /* writing in last column may cause scrolling */
7563 fillchar = ' ';
7564 attr = 0;
7565 }
7566
7567# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007568 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007569# endif
7570 }
7571 else
7572 {
7573 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007574 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007575 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007576 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007577# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007578 use_sandbox = was_set_insecurely((char_u *)"statusline",
7579 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007580# endif
7581 }
7582
Bram Moolenaar53f81742017-09-22 14:35:51 +02007583 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007584 }
7585
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007587 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588
Bram Moolenaar61452852011-02-01 18:01:11 +01007589 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7590 * the cursor away and back. */
7591 ewp = wp == NULL ? curwin : wp;
7592 p_crb_save = ewp->w_p_crb;
7593 ewp->w_p_crb = FALSE;
7594
Bram Moolenaar362f3562009-11-03 16:20:34 +00007595 /* Make a copy, because the statusline may include a function call that
7596 * might change the option value and free the memory. */
7597 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007598 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007599 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007600 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007601 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007602 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007604 /* Make all characters printable. */
7605 p = transstr(buf);
7606 if (p != NULL)
7607 {
7608 vim_strncpy(buf, p, sizeof(buf) - 1);
7609 vim_free(p);
7610 }
7611
7612 /* fill up with "fillchar" */
7613 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007614 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 ++width;
7618 }
7619 buf[len] = NUL;
7620
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007621 /*
7622 * Draw each snippet with the specified highlighting.
7623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 curattr = attr;
7625 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007626 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007628 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 screen_puts_len(p, len, row, col, curattr);
7630 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007631 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007633 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007635 else if (hltab[n].userhl < 0)
7636 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007637#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007638 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7639 && wp->w_status_height != 0)
7640 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007641 else if (wp != NULL && bt_terminal(wp->w_buffer)
7642 && wp->w_status_height != 0)
7643 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007644#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007645 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007646 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007648 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 }
7650 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007651
7652 if (wp == NULL)
7653 {
7654 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7655 col = 0;
7656 len = 0;
7657 p = buf;
7658 fillchar = 0;
7659 for (n = 0; tabtab[n].start != NULL; n++)
7660 {
7661 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7662 while (col < len)
7663 TabPageIdxs[col++] = fillchar;
7664 p = tabtab[n].start;
7665 fillchar = tabtab[n].userhl;
7666 }
7667 while (col < Columns)
7668 TabPageIdxs[col++] = fillchar;
7669 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007670
7671theend:
7672 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673}
7674
7675#endif /* FEAT_STL_OPT */
7676
7677/*
7678 * Output a single character directly to the screen and update ScreenLines.
7679 */
7680 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007681screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 char_u buf[MB_MAXBYTES + 1];
7684
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007685 if (has_mbyte)
7686 buf[(*mb_char2bytes)(c, buf)] = NUL;
7687 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007688 {
7689 buf[0] = c;
7690 buf[1] = NUL;
7691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 screen_puts(buf, row, col, attr);
7693}
7694
7695/*
7696 * Get a single character directly from ScreenLines into "bytes[]".
7697 * Also return its attribute in *attrp;
7698 */
7699 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007700screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701{
7702 unsigned off;
7703
7704 /* safety check */
7705 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7706 {
7707 off = LineOffset[row] + col;
7708 *attrp = ScreenAttrs[off];
7709 bytes[0] = ScreenLines[off];
7710 bytes[1] = NUL;
7711
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 if (enc_utf8 && ScreenLinesUC[off] != 0)
7713 bytes[utfc_char2bytes(off, bytes)] = NUL;
7714 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7715 {
7716 bytes[0] = ScreenLines[off];
7717 bytes[1] = ScreenLines2[off];
7718 bytes[2] = NUL;
7719 }
7720 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7721 {
7722 bytes[1] = ScreenLines[off + 1];
7723 bytes[2] = NUL;
7724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 }
7726}
7727
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007728/*
7729 * Return TRUE if composing characters for screen posn "off" differs from
7730 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007731 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007732 */
7733 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007734screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007735{
7736 int i;
7737
7738 for (i = 0; i < Screen_mco; ++i)
7739 {
7740 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7741 return TRUE;
7742 if (u8cc[i] == 0)
7743 break;
7744 }
7745 return FALSE;
7746}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007747
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748/*
7749 * Put string '*text' on the screen at position 'row' and 'col', with
7750 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7751 * Note: only outputs within one row, message is truncated at screen boundary!
7752 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7753 */
7754 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007755screen_puts(
7756 char_u *text,
7757 int row,
7758 int col,
7759 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760{
7761 screen_puts_len(text, -1, row, col, attr);
7762}
7763
7764/*
7765 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7766 * a NUL.
7767 */
7768 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007769screen_puts_len(
7770 char_u *text,
7771 int textlen,
7772 int row,
7773 int col,
7774 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775{
7776 unsigned off;
7777 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007778 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007780 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 int mbyte_blen = 1;
7782 int mbyte_cells = 1;
7783 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007784 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007786#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007788 int pc, nc, nc1;
7789 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007791 int force_redraw_this;
7792 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007793 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007795 // Safety check. The check for negative row and column is to fix issue
7796 // #4102. TODO: find out why row/col could be negative.
7797 if (ScreenLines == NULL
7798 || row >= screen_Rows || row < 0
7799 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007801 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802
Bram Moolenaarc236c162008-07-13 17:41:49 +00007803 /* When drawing over the right halve of a double-wide char clear out the
7804 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007805 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007806#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007807 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007808#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007809 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007810 {
7811 ScreenLines[off - 1] = ' ';
7812 ScreenAttrs[off - 1] = 0;
7813 if (enc_utf8)
7814 {
7815 ScreenLinesUC[off - 1] = 0;
7816 ScreenLinesC[0][off - 1] = 0;
7817 }
7818 /* redraw the previous cell, make it empty */
7819 screen_char(off - 1, row, col - 1);
7820 /* force the cell at "col" to be redrawn */
7821 force_redraw_next = TRUE;
7822 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007823
Bram Moolenaar367329b2007-08-30 11:53:22 +00007824 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007825 while (col < screen_Columns
7826 && (len < 0 || (int)(ptr - text) < len)
7827 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 {
7829 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 /* check if this is the first byte of a multibyte */
7831 if (has_mbyte)
7832 {
7833 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007834 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007836 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7838 mbyte_cells = 1;
7839 else if (enc_dbcs != 0)
7840 mbyte_cells = mbyte_blen;
7841 else /* enc_utf8 */
7842 {
7843 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007844 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 (int)((text + len) - ptr));
7846 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007847 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007849#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7851 {
7852 /* Do Arabic shaping. */
7853 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7854 {
7855 /* Past end of string to be displayed. */
7856 nc = NUL;
7857 nc1 = NUL;
7858 }
7859 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007860 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007861 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7862 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007863 nc1 = pcc[0];
7864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 pc = prev_c;
7866 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007867 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 }
7869 else
7870 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007871#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007872 if (col + mbyte_cells > screen_Columns)
7873 {
7874 /* Only 1 cell left, but character requires 2 cells:
7875 * display a '>' in the last column to avoid wrapping. */
7876 c = '>';
7877 mbyte_cells = 1;
7878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 }
7880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007882 force_redraw_this = force_redraw_next;
7883 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007884
7885 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 || (mbyte_cells == 2
7887 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7888 || (enc_dbcs == DBCS_JPNU
7889 && c == 0x8e
7890 && ScreenLines2[off] != ptr[1])
7891 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007892 && (ScreenLinesUC[off] !=
7893 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7894 || (ScreenLinesUC[off] != 0
7895 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007897 || exmode_active;
7898
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007899 if ((need_redraw || force_redraw_this)
7900#ifdef FEAT_TEXT_PROP
7901 && popup_mask[row * screen_Columns + col] <= screen_zindex
7902#endif
7903 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {
7905#if defined(FEAT_GUI) || defined(UNIX)
7906 /* The bold trick makes a single row of pixels appear in the next
7907 * character. When a bold character is removed, the next
7908 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007909 * and for some xterms. */
7910 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911# ifdef FEAT_GUI
7912 gui.in_use
7913# endif
7914# if defined(FEAT_GUI) && defined(UNIX)
7915 ||
7916# endif
7917# ifdef UNIX
7918 term_is_xterm
7919# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007920 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007922 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007924 if (n > HL_ALL)
7925 n = syn_attr2attr(n);
7926 if (n & HL_BOLD)
7927 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 }
7929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 /* When at the end of the text and overwriting a two-cell
7931 * character with a one-cell character, need to clear the next
7932 * cell. Also when overwriting the left halve of a two-cell char
7933 * with the right halve of a two-cell char. Do this only once
7934 * (mb_off2cells() may return 2 on the right halve). */
7935 if (clear_next_cell)
7936 clear_next_cell = FALSE;
7937 else if (has_mbyte
7938 && (len < 0 ? ptr[mbyte_blen] == NUL
7939 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007940 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007942 && (*mb_off2cells)(off, max_off) == 1
7943 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 clear_next_cell = TRUE;
7945
7946 /* Make sure we never leave a second byte of a double-byte behind,
7947 * it confuses mb_off2cells(). */
7948 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007949 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007951 && (*mb_off2cells)(off, max_off) == 1
7952 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 ScreenLines[off] = c;
7955 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 if (enc_utf8)
7957 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007958 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 ScreenLinesUC[off] = 0;
7960 else
7961 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007962 int i;
7963
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007965 for (i = 0; i < Screen_mco; ++i)
7966 {
7967 ScreenLinesC[i][off] = u8cc[i];
7968 if (u8cc[i] == 0)
7969 break;
7970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 }
7972 if (mbyte_cells == 2)
7973 {
7974 ScreenLines[off + 1] = 0;
7975 ScreenAttrs[off + 1] = attr;
7976 }
7977 screen_char(off, row, col);
7978 }
7979 else if (mbyte_cells == 2)
7980 {
7981 ScreenLines[off + 1] = ptr[1];
7982 ScreenAttrs[off + 1] = attr;
7983 screen_char_2(off, row, col);
7984 }
7985 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7986 {
7987 ScreenLines2[off] = ptr[1];
7988 screen_char(off, row, col);
7989 }
7990 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 screen_char(off, row, col);
7992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 if (has_mbyte)
7994 {
7995 off += mbyte_cells;
7996 col += mbyte_cells;
7997 ptr += mbyte_blen;
7998 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007999 {
8000 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02008002 len = -1;
8003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 }
8005 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {
8007 ++off;
8008 ++col;
8009 ++ptr;
8010 }
8011 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00008012
Bram Moolenaar2bea2912009-03-11 16:58:40 +00008013 /* If we detected the next character needs to be redrawn, but the text
8014 * doesn't extend up to there, update the character here. */
8015 if (force_redraw_next && col < screen_Columns)
8016 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00008017 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
8018 screen_char_2(off, row, col);
8019 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00008020 screen_char(off, row, col);
8021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022}
8023
8024#ifdef FEAT_SEARCH_EXTRA
8025/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008026 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 */
8028 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008029start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030{
8031 if (p_hls && !no_hlsearch)
8032 {
8033 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008034 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008035# ifdef FEAT_RELTIME
8036 /* Set the time limit to 'redrawtime'. */
8037 profile_setlimit(p_rdt, &search_hl.tm);
8038# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 }
8040}
8041
8042/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008043 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 */
8045 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008046end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047{
8048 if (search_hl.rm.regprog != NULL)
8049 {
Bram Moolenaar473de612013-06-08 18:19:48 +02008050 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 search_hl.rm.regprog = NULL;
8052 }
8053}
8054
8055/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02008056 * Init for calling prepare_search_hl().
8057 */
8058 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008059init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02008060{
8061 matchitem_T *cur;
8062
8063 /* Setup for match and 'hlsearch' highlighting. Disable any previous
8064 * match */
8065 cur = wp->w_match_head;
8066 while (cur != NULL)
8067 {
8068 cur->hl.rm = cur->match;
8069 if (cur->hlg_id == 0)
8070 cur->hl.attr = 0;
8071 else
8072 cur->hl.attr = syn_id2attr(cur->hlg_id);
8073 cur->hl.buf = wp->w_buffer;
8074 cur->hl.lnum = 0;
8075 cur->hl.first_lnum = 0;
8076# ifdef FEAT_RELTIME
8077 /* Set the time limit to 'redrawtime'. */
8078 profile_setlimit(p_rdt, &(cur->hl.tm));
8079# endif
8080 cur = cur->next;
8081 }
8082 search_hl.buf = wp->w_buffer;
8083 search_hl.lnum = 0;
8084 search_hl.first_lnum = 0;
8085 /* time limit is set at the toplevel, for all windows */
8086}
8087
8088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 * Advance to the match in window "wp" line "lnum" or past it.
8090 */
8091 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008092prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008094 matchitem_T *cur; /* points to the match list */
8095 match_T *shl; /* points to search_hl or a match */
8096 int shl_flag; /* flag to indicate whether search_hl
8097 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008098 int pos_inprogress; /* marks that position match search is
8099 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 int n;
8101
8102 /*
8103 * When using a multi-line pattern, start searching at the top
8104 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008105 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008107 cur = wp->w_match_head;
8108 shl_flag = FALSE;
8109 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008111 if (shl_flag == FALSE)
8112 {
8113 shl = &search_hl;
8114 shl_flag = TRUE;
8115 }
8116 else
8117 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 if (shl->rm.regprog != NULL
8119 && shl->lnum == 0
8120 && re_multiline(shl->rm.regprog))
8121 {
8122 if (shl->first_lnum == 0)
8123 {
8124# ifdef FEAT_FOLDING
8125 for (shl->first_lnum = lnum;
8126 shl->first_lnum > wp->w_topline; --shl->first_lnum)
8127 if (hasFoldingWin(wp, shl->first_lnum - 1,
8128 NULL, NULL, TRUE, NULL))
8129 break;
8130# else
8131 shl->first_lnum = wp->w_topline;
8132# endif
8133 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008134 if (cur != NULL)
8135 cur->pos.cur = 0;
8136 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008138 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
8139 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02008141 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
8142 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008143 pos_inprogress = cur == NULL || cur->pos.cur == 0
8144 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 if (shl->lnum != 0)
8146 {
8147 shl->first_lnum = shl->lnum
8148 + shl->rm.endpos[0].lnum
8149 - shl->rm.startpos[0].lnum;
8150 n = shl->rm.endpos[0].col;
8151 }
8152 else
8153 {
8154 ++shl->first_lnum;
8155 n = 0;
8156 }
8157 }
8158 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008159 if (shl != &search_hl && cur != NULL)
8160 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 }
8162}
8163
8164/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008165 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 * Uses shl->buf.
8167 * Sets shl->lnum and shl->rm contents.
8168 * Note: Assumes a previous match is always before "lnum", unless
8169 * shl->lnum is zero.
8170 * Careful: Any pointers for buffer lines will become invalid.
8171 */
8172 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008173next_search_hl(
8174 win_T *win,
8175 match_T *shl, /* points to search_hl or a match */
8176 linenr_T lnum,
8177 colnr_T mincol, /* minimal column for a match */
8178 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179{
8180 linenr_T l;
8181 colnr_T matchcol;
8182 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008183 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008185 // for :{range}s/pat only highlight inside the range
8186 if (lnum < search_first_line || lnum > search_last_line)
8187 {
8188 shl->lnum = 0;
8189 return;
8190 }
8191
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 if (shl->lnum != 0)
8193 {
8194 /* Check for three situations:
8195 * 1. If the "lnum" is below a previous match, start a new search.
8196 * 2. If the previous match includes "mincol", use it.
8197 * 3. Continue after the previous match.
8198 */
8199 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8200 if (lnum > l)
8201 shl->lnum = 0;
8202 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8203 return;
8204 }
8205
8206 /*
8207 * Repeat searching for a match until one is found that includes "mincol"
8208 * or none is found in this line.
8209 */
8210 called_emsg = FALSE;
8211 for (;;)
8212 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008213#ifdef FEAT_RELTIME
8214 /* Stop searching after passing the time limit. */
8215 if (profile_passed_limit(&(shl->tm)))
8216 {
8217 shl->lnum = 0; /* no match found in time */
8218 break;
8219 }
8220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 /* Three situations:
8222 * 1. No useful previous match: search from start of line.
8223 * 2. Not Vi compatible or empty match: continue at next character.
8224 * Break the loop if this is beyond the end of the line.
8225 * 3. Vi compatible searching: continue at end of previous match.
8226 */
8227 if (shl->lnum == 0)
8228 matchcol = 0;
8229 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8230 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008231 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008233 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008234
8235 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008236 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008237 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008239 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 shl->lnum = 0;
8241 break;
8242 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008243 if (has_mbyte)
8244 matchcol += mb_ptr2len(ml);
8245 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008246 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 }
8248 else
8249 matchcol = shl->rm.endpos[0].col;
8250
8251 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008252 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008254 /* Remember whether shl->rm is using a copy of the regprog in
8255 * cur->match. */
8256 int regprog_is_copy = (shl != &search_hl && cur != NULL
8257 && shl == &cur->hl
8258 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008259 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008260
Bram Moolenaarb3414592014-06-17 17:48:32 +02008261 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8262 matchcol,
8263#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008264 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008265#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008266 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008267#endif
8268 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008269 /* Copy the regprog, in case it got freed and recompiled. */
8270 if (regprog_is_copy)
8271 cur->match.regprog = cur->hl.rm.regprog;
8272
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008273 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008274 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008275 /* Error while handling regexp: stop using this regexp. */
8276 if (shl == &search_hl)
8277 {
8278 /* don't free regprog in the match list, it's a copy */
8279 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008280 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008281 }
8282 shl->rm.regprog = NULL;
8283 shl->lnum = 0;
8284 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8285 message */
8286 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008287 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008288 }
8289 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008290 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008291 else
8292 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 if (nmatched == 0)
8294 {
8295 shl->lnum = 0; /* no match found */
8296 break;
8297 }
8298 if (shl->rm.startpos[0].lnum > 0
8299 || shl->rm.startpos[0].col >= mincol
8300 || nmatched > 1
8301 || shl->rm.endpos[0].col > mincol)
8302 {
8303 shl->lnum += shl->rm.startpos[0].lnum;
8304 break; /* useful match found */
8305 }
8306 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008307
8308 // Restore called_emsg for assert_fails().
8309 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311
Bram Moolenaar85077472016-10-16 14:35:48 +02008312/*
8313 * If there is a match fill "shl" and return one.
8314 * Return zero otherwise.
8315 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008316 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008317next_search_hl_pos(
8318 match_T *shl, /* points to a match */
8319 linenr_T lnum,
8320 posmatch_T *posmatch, /* match positions */
8321 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008322{
8323 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008324 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008325
Bram Moolenaarb3414592014-06-17 17:48:32 +02008326 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8327 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008328 llpos_T *pos = &posmatch->pos[i];
8329
8330 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008331 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008332 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008333 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008334 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008335 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008336 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008337 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008338 /* if this match comes before the one at "found" then swap
8339 * them */
8340 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008341 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008342 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008343
Bram Moolenaar85077472016-10-16 14:35:48 +02008344 *pos = posmatch->pos[found];
8345 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008346 }
8347 }
8348 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008349 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008350 }
8351 }
8352 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008353 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008354 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008355 colnr_T start = posmatch->pos[found].col == 0
8356 ? 0 : posmatch->pos[found].col - 1;
8357 colnr_T end = posmatch->pos[found].col == 0
8358 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008359
Bram Moolenaar85077472016-10-16 14:35:48 +02008360 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008361 shl->rm.startpos[0].lnum = 0;
8362 shl->rm.startpos[0].col = start;
8363 shl->rm.endpos[0].lnum = 0;
8364 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008365 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008366 posmatch->cur = found + 1;
8367 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008368 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008369 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008370}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008371#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008372
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008374screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375{
8376 attrentry_T *aep = NULL;
8377
8378 screen_attr = attr;
8379 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008380#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 && termcap_active
8382#endif
8383 )
8384 {
8385#ifdef FEAT_GUI
8386 if (gui.in_use)
8387 {
8388 char buf[20];
8389
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008390 /* The GUI handles this internally. */
8391 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 OUT_STR(buf);
8393 }
8394 else
8395#endif
8396 {
8397 if (attr > HL_ALL) /* special HL attr. */
8398 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008399 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 aep = syn_cterm_attr2entry(attr);
8401 else
8402 aep = syn_term_attr2entry(attr);
8403 if (aep == NULL) /* did ":syntax clear" */
8404 attr = 0;
8405 else
8406 attr = aep->ae_attr;
8407 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008408 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008410 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008411#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008412 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8413 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8414 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008415#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008416 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008417 /* If the Normal FG color has BOLD attribute and the new HL
8418 * has a FG color defined, clear BOLD. */
8419 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008420 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008422 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008423 out_str(T_UCS);
8424 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008425 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8426 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008428 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008430 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008432 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008433 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434
8435 /*
8436 * Output the color or start string after bold etc., in case the
8437 * bold etc. override the color setting.
8438 */
8439 if (aep != NULL)
8440 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008441#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008442 /* When 'termguicolors' is set but fg or bg is unset,
8443 * fall back to the cterm colors. This helps for SpellBad,
8444 * where the GUI uses a red undercurl. */
8445 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008447 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008448 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008449 }
8450 else
8451#endif
8452 if (t_colors > 1)
8453 {
8454 if (aep->ae_u.cterm.fg_color)
8455 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8456 }
8457#ifdef FEAT_TERMGUICOLORS
8458 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8459 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008460 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008461 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 }
8463 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008464#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008465 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008467 if (aep->ae_u.cterm.bg_color)
8468 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8469 }
8470
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008471 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008472 {
8473 if (aep->ae_u.term.start != NULL)
8474 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 }
8476 }
8477 }
8478 }
8479}
8480
8481 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008482screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483{
8484 int do_ME = FALSE; /* output T_ME code */
8485
8486 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008487#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 && termcap_active
8489#endif
8490 )
8491 {
8492#ifdef FEAT_GUI
8493 if (gui.in_use)
8494 {
8495 char buf[20];
8496
8497 /* use internal GUI code */
8498 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8499 OUT_STR(buf);
8500 }
8501 else
8502#endif
8503 {
8504 if (screen_attr > HL_ALL) /* special HL attr. */
8505 {
8506 attrentry_T *aep;
8507
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008508 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 {
8510 /*
8511 * Assume that t_me restores the original colors!
8512 */
8513 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008514 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008515#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008516 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8517 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8518 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008519#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008520 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008521#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008522 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8523 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8524 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008525#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008526 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 do_ME = TRUE;
8528 }
8529 else
8530 {
8531 aep = syn_term_attr2entry(screen_attr);
8532 if (aep != NULL && aep->ae_u.term.stop != NULL)
8533 {
8534 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8535 do_ME = TRUE;
8536 else
8537 out_str(aep->ae_u.term.stop);
8538 }
8539 }
8540 if (aep == NULL) /* did ":syntax clear" */
8541 screen_attr = 0;
8542 else
8543 screen_attr = aep->ae_attr;
8544 }
8545
8546 /*
8547 * Often all ending-codes are equal to T_ME. Avoid outputting the
8548 * same sequence several times.
8549 */
8550 if (screen_attr & HL_STANDOUT)
8551 {
8552 if (STRCMP(T_SE, T_ME) == 0)
8553 do_ME = TRUE;
8554 else
8555 out_str(T_SE);
8556 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008557 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008558 {
8559 if (STRCMP(T_UCE, T_ME) == 0)
8560 do_ME = TRUE;
8561 else
8562 out_str(T_UCE);
8563 }
8564 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008565 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 {
8567 if (STRCMP(T_UE, T_ME) == 0)
8568 do_ME = TRUE;
8569 else
8570 out_str(T_UE);
8571 }
8572 if (screen_attr & HL_ITALIC)
8573 {
8574 if (STRCMP(T_CZR, T_ME) == 0)
8575 do_ME = TRUE;
8576 else
8577 out_str(T_CZR);
8578 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008579 if (screen_attr & HL_STRIKETHROUGH)
8580 {
8581 if (STRCMP(T_STE, T_ME) == 0)
8582 do_ME = TRUE;
8583 else
8584 out_str(T_STE);
8585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8587 out_str(T_ME);
8588
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008589#ifdef FEAT_TERMGUICOLORS
8590 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008592 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008593 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008594 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008595 term_bg_rgb_color(cterm_normal_bg_gui_color);
8596 }
8597 else
8598#endif
8599 {
8600 if (t_colors > 1)
8601 {
8602 /* set Normal cterm colors */
8603 if (cterm_normal_fg_color != 0)
8604 term_fg_color(cterm_normal_fg_color - 1);
8605 if (cterm_normal_bg_color != 0)
8606 term_bg_color(cterm_normal_bg_color - 1);
8607 if (cterm_normal_fg_bold)
8608 out_str(T_MD);
8609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 }
8611 }
8612 }
8613 screen_attr = 0;
8614}
8615
8616/*
8617 * Reset the colors for a cterm. Used when leaving Vim.
8618 * The machine specific code may override this again.
8619 */
8620 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008621reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008623 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 {
8625 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008626#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008627 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8628 || cterm_normal_bg_gui_color != INVALCOLOR)
8629 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008630#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 {
8634 out_str(T_OP);
8635 screen_attr = -1;
8636 }
8637 if (cterm_normal_fg_bold)
8638 {
8639 out_str(T_ME);
8640 screen_attr = -1;
8641 }
8642 }
8643}
8644
8645/*
8646 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8647 * using the attributes from ScreenAttrs["off"].
8648 */
8649 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008650screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651{
8652 int attr;
8653
8654 /* Check for illegal values, just in case (could happen just after
8655 * resizing). */
8656 if (row >= screen_Rows || col >= screen_Columns)
8657 return;
8658
Bram Moolenaarae654382019-01-17 21:09:05 +01008659#ifdef FEAT_INS_EXPAND
Bram Moolenaar33796b32019-06-08 16:01:13 +02008660 // Skip if under the popup menu.
8661 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
8662 if (pum_under_menu(row, col)
8663# ifdef FEAT_TEXT_PROP
8664 && screen_zindex <= POPUPMENU_ZINDEX
8665# endif
8666 )
Bram Moolenaarae654382019-01-17 21:09:05 +01008667 return;
8668#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008669#ifdef FEAT_TEXT_PROP
8670 // Skip if under a(nother) popup.
8671 if (popup_mask[row * screen_Columns + col] > screen_zindex)
8672 return;
8673#endif
8674
Bram Moolenaar494838a2015-02-10 19:20:37 +01008675 /* Outputting a character in the last cell on the screen may scroll the
8676 * screen up. Only do it when the "xn" termcap property is set, otherwise
8677 * mark the character invalid (update it when scrolled up). */
8678 if (*T_XN == NUL
8679 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680#ifdef FEAT_RIGHTLEFT
8681 /* account for first command-line character in rightleft mode */
8682 && !cmdmsg_rl
8683#endif
8684 )
8685 {
8686 ScreenAttrs[off] = (sattr_T)-1;
8687 return;
8688 }
8689
8690 /*
8691 * Stop highlighting first, so it's easier to move the cursor.
8692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 if (screen_char_attr != 0)
8694 attr = screen_char_attr;
8695 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 attr = ScreenAttrs[off];
8697 if (screen_attr != attr)
8698 screen_stop_highlight();
8699
8700 windgoto(row, col);
8701
8702 if (screen_attr != attr)
8703 screen_start_highlight(attr);
8704
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 if (enc_utf8 && ScreenLinesUC[off] != 0)
8706 {
8707 char_u buf[MB_MAXBYTES + 1];
8708
Bram Moolenaarcb070082016-04-02 22:14:51 +02008709 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008710 {
8711 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008712#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008713 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008714#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008715 )
8716 {
8717 /* Clear the two screen cells. If the character is actually
8718 * single width it won't change the second cell. */
8719 out_str((char_u *)" ");
8720 term_windgoto(row, col);
8721 }
8722 /* not sure where the cursor is after drawing the ambiguous width
8723 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008724 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008725 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008726 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008728
8729 /* Convert the UTF-8 character to bytes and write it. */
8730 buf[utfc_char2bytes(off, buf)] = NUL;
8731 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 }
8733 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 /* double-byte character in single-width cell */
8738 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8739 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 }
8741
8742 screen_cur_col++;
8743}
8744
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745/*
8746 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8747 * on the screen at position 'row' and 'col'.
8748 * The attributes of the first byte is used for all. This is required to
8749 * output the two bytes of a double-byte character with nothing in between.
8750 */
8751 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008752screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753{
8754 /* Check for illegal values (could be wrong when screen was resized). */
8755 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8756 return;
8757
8758 /* Outputting the last character on the screen may scrollup the screen.
8759 * Don't to it! Mark the character invalid (update it when scrolled up) */
8760 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8761 {
8762 ScreenAttrs[off] = (sattr_T)-1;
8763 return;
8764 }
8765
8766 /* Output the first byte normally (positions the cursor), then write the
8767 * second byte directly. */
8768 screen_char(off, row, col);
8769 out_char(ScreenLines[off + 1]);
8770 ++screen_cur_col;
8771}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773/*
8774 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8775 * This uses the contents of ScreenLines[] and doesn't change it.
8776 */
8777 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008778screen_draw_rectangle(
8779 int row,
8780 int col,
8781 int height,
8782 int width,
8783 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784{
8785 int r, c;
8786 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008787 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008789 /* Can't use ScreenLines unless initialized */
8790 if (ScreenLines == NULL)
8791 return;
8792
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 if (invert)
8794 screen_char_attr = HL_INVERSE;
8795 for (r = row; r < row + height; ++r)
8796 {
8797 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008798 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 for (c = col; c < col + width; ++c)
8800 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008801 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 {
8803 screen_char_2(off + c, r, c);
8804 ++c;
8805 }
8806 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 {
8808 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008809 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 }
8812 }
8813 }
8814 screen_char_attr = 0;
8815}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817/*
8818 * Redraw the characters for a vertically split window.
8819 */
8820 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008821redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822{
8823 int col;
8824 int width;
8825
8826# ifdef FEAT_CLIPBOARD
8827 clip_may_clear_selection(row, end - 1);
8828# endif
8829
8830 if (wp == NULL)
8831 {
8832 col = 0;
8833 width = Columns;
8834 }
8835 else
8836 {
8837 col = wp->w_wincol;
8838 width = wp->w_width;
8839 }
8840 screen_draw_rectangle(row, col, end - row, width, FALSE);
8841}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008843 static void
8844space_to_screenline(int off, int attr)
8845{
8846 ScreenLines[off] = ' ';
8847 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008848 if (enc_utf8)
8849 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008850}
8851
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852/*
8853 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8854 * with character 'c1' in first column followed by 'c2' in the other columns.
8855 * Use attributes 'attr'.
8856 */
8857 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008858screen_fill(
8859 int start_row,
8860 int end_row,
8861 int start_col,
8862 int end_col,
8863 int c1,
8864 int c2,
8865 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866{
8867 int row;
8868 int col;
8869 int off;
8870 int end_off;
8871 int did_delete;
8872 int c;
8873 int norm_term;
8874#if defined(FEAT_GUI) || defined(UNIX)
8875 int force_next = FALSE;
8876#endif
8877
8878 if (end_row > screen_Rows) /* safety check */
8879 end_row = screen_Rows;
8880 if (end_col > screen_Columns) /* safety check */
8881 end_col = screen_Columns;
8882 if (ScreenLines == NULL
8883 || start_row >= end_row
8884 || start_col >= end_col) /* nothing to do */
8885 return;
8886
8887 /* it's a "normal" terminal when not in a GUI or cterm */
8888 norm_term = (
8889#ifdef FEAT_GUI
8890 !gui.in_use &&
8891#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008892 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 for (row = start_row; row < end_row; ++row)
8894 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008895 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008896#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008897 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008898#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008899 )
8900 {
8901 /* When drawing over the right halve of a double-wide char clear
8902 * out the left halve. When drawing over the left halve of a
8903 * double wide-char clear out the right halve. Only needed in a
8904 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008905 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008906 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008907 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008908 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 /*
8911 * Try to use delete-line termcap code, when no attributes or in a
8912 * "normal" terminal, where a bold/italic space is just a
8913 * space.
8914 */
8915 did_delete = FALSE;
8916 if (c2 == ' '
8917 && end_col == Columns
8918 && can_clear(T_CE)
8919 && (attr == 0
8920 || (norm_term
8921 && attr <= HL_ALL
8922 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8923 {
8924 /*
8925 * check if we really need to clear something
8926 */
8927 col = start_col;
8928 if (c1 != ' ') /* don't clear first char */
8929 ++col;
8930
8931 off = LineOffset[row] + col;
8932 end_off = LineOffset[row] + end_col;
8933
8934 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 if (enc_utf8)
8936 while (off < end_off && ScreenLines[off] == ' '
8937 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8938 ++off;
8939 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 while (off < end_off && ScreenLines[off] == ' '
8941 && ScreenAttrs[off] == 0)
8942 ++off;
8943 if (off < end_off) /* something to be cleared */
8944 {
8945 col = off - LineOffset[row];
8946 screen_stop_highlight();
8947 term_windgoto(row, col);/* clear rest of this screen line */
8948 out_str(T_CE);
8949 screen_start(); /* don't know where cursor is now */
8950 col = end_col - col;
8951 while (col--) /* clear chars in ScreenLines */
8952 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008953 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 ++off;
8955 }
8956 }
8957 did_delete = TRUE; /* the chars are cleared now */
8958 }
8959
8960 off = LineOffset[row] + start_col;
8961 c = c1;
8962 for (col = start_col; col < end_col; ++col)
8963 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02008964 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008965 || (enc_utf8 && (int)ScreenLinesUC[off]
8966 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 || ScreenAttrs[off] != attr
8968#if defined(FEAT_GUI) || defined(UNIX)
8969 || force_next
8970#endif
8971 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02008972#ifdef FEAT_TEXT_PROP
8973 // Skip if under a(nother) popup.
8974 && popup_mask[row * screen_Columns + col] <= screen_zindex
8975#endif
8976 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 {
8978#if defined(FEAT_GUI) || defined(UNIX)
8979 /* The bold trick may make a single row of pixels appear in
8980 * the next character. When a bold character is removed, the
8981 * next character should be redrawn too. This happens for our
8982 * own GUI and for some xterms. */
8983 if (
8984# ifdef FEAT_GUI
8985 gui.in_use
8986# endif
8987# if defined(FEAT_GUI) && defined(UNIX)
8988 ||
8989# endif
8990# ifdef UNIX
8991 term_is_xterm
8992# endif
8993 )
8994 {
8995 if (ScreenLines[off] != ' '
8996 && (ScreenAttrs[off] > HL_ALL
8997 || ScreenAttrs[off] & HL_BOLD))
8998 force_next = TRUE;
8999 else
9000 force_next = FALSE;
9001 }
9002#endif
9003 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 if (enc_utf8)
9005 {
9006 if (c >= 0x80)
9007 {
9008 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009009 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 }
9011 else
9012 ScreenLinesUC[off] = 0;
9013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 ScreenAttrs[off] = attr;
9015 if (!did_delete || c != ' ')
9016 screen_char(off, row, col);
9017 }
9018 ++off;
9019 if (col == start_col)
9020 {
9021 if (did_delete)
9022 break;
9023 c = c2;
9024 }
9025 }
9026 if (end_col == Columns)
9027 LineWraps[row] = FALSE;
9028 if (row == Rows - 1) /* overwritten the command line */
9029 {
9030 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02009031 if (start_col == 0 && end_col == Columns
9032 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009034 if (start_col == 0)
9035 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 }
9037 }
9038}
9039
9040/*
9041 * Check if there should be a delay. Used before clearing or redrawing the
9042 * screen or the command line.
9043 */
9044 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009045check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046{
9047 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
9048 && !did_wait_return
9049 && emsg_silent == 0)
9050 {
9051 out_flush();
9052 ui_delay(1000L, TRUE);
9053 emsg_on_display = FALSE;
9054 if (check_msg_scroll)
9055 msg_scroll = FALSE;
9056 }
9057}
9058
9059/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009060 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
9061 */
9062 static void
9063clear_TabPageIdxs(void)
9064{
9065 int scol;
9066
9067 for (scol = 0; scol < Columns; ++scol)
9068 TabPageIdxs[scol] = 0;
9069}
9070
9071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009073 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 * Returns TRUE if there is a valid screen to write to.
9075 * Returns FALSE when starting up and screen not initialized yet.
9076 */
9077 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009078screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009080 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 return (ScreenLines != NULL);
9082}
9083
9084/*
9085 * Resize the shell to Rows and Columns.
9086 * Allocate ScreenLines[] and associated items.
9087 *
9088 * There may be some time between setting Rows and Columns and (re)allocating
9089 * ScreenLines[]. This happens when starting up and when (manually) changing
9090 * the shell size. Always use screen_Rows and screen_Columns to access items
9091 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
9092 * final size of the shell is needed.
9093 */
9094 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009095screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096{
9097 int new_row, old_row;
9098#ifdef FEAT_GUI
9099 int old_Rows;
9100#endif
9101 win_T *wp;
9102 int outofmem = FALSE;
9103 int len;
9104 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009106 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009108 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 sattr_T *new_ScreenAttrs;
9110 unsigned *new_LineOffset;
9111 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009112 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02009113#ifdef FEAT_TEXT_PROP
9114 short *new_popup_mask;
9115#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00009116 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009118 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009119 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009121retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 /*
9123 * Allocation of the screen buffers is done only when the size changes and
9124 * when Rows and Columns have been set and we have started doing full
9125 * screen stuff.
9126 */
9127 if ((ScreenLines != NULL
9128 && Rows == screen_Rows
9129 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 && enc_utf8 == (ScreenLinesUC != NULL)
9131 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01009132 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 || Rows == 0
9134 || Columns == 0
9135 || (!full_screen && ScreenLines == NULL))
9136 return;
9137
9138 /*
9139 * It's possible that we produce an out-of-memory message below, which
9140 * will cause this function to be called again. To break the loop, just
9141 * return here.
9142 */
9143 if (entered)
9144 return;
9145 entered = TRUE;
9146
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009147 /*
9148 * Note that the window sizes are updated before reallocating the arrays,
9149 * thus we must not redraw here!
9150 */
9151 ++RedrawingDisabled;
9152
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 win_new_shellsize(); /* fit the windows in the new sized shell */
9154
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 comp_col(); /* recompute columns for shown command and ruler */
9156
9157 /*
9158 * We're changing the size of the screen.
9159 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9160 * - Move lines from the old arrays into the new arrays, clear extra
9161 * lines (unless the screen is going to be cleared).
9162 * - Free the old arrays.
9163 *
9164 * If anything fails, make ScreenLines NULL, so we don't do anything!
9165 * Continuing with the old ScreenLines may result in a crash, because the
9166 * size is wrong.
9167 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009168 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009170 if (aucmd_win != NULL)
9171 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009172#ifdef FEAT_TEXT_PROP
9173 // global popup windows
9174 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9175 win_free_lsize(wp);
9176 // tab-local popup windows
9177 FOR_ALL_TABPAGES(tp)
9178 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9179 win_free_lsize(wp);
9180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009182 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01009183 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 if (enc_utf8)
9185 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009186 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009187 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009188 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
9189 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 }
9191 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009192 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
9193 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
9194 new_LineOffset = LALLOC_MULT(unsigned, Rows);
9195 new_LineWraps = LALLOC_MULT(char_u, Rows);
9196 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009197#ifdef FEAT_TEXT_PROP
9198 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
9199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009201 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 {
9203 if (win_alloc_lines(wp) == FAIL)
9204 {
9205 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009206 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 }
9208 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009209 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9210 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009211 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009212#ifdef FEAT_TEXT_PROP
9213 // global popup windows
9214 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9215 if (win_alloc_lines(wp) == FAIL)
9216 {
9217 outofmem = TRUE;
9218 goto give_up;
9219 }
9220 // tab-local popup windows
9221 FOR_ALL_TABPAGES(tp)
9222 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9223 if (win_alloc_lines(wp) == FAIL)
9224 {
9225 outofmem = TRUE;
9226 goto give_up;
9227 }
9228#endif
9229
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009230give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009232 for (i = 0; i < p_mco; ++i)
9233 if (new_ScreenLinesC[i] == NULL)
9234 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009236 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 || new_ScreenAttrs == NULL
9239 || new_LineOffset == NULL
9240 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009241 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02009242#ifdef FEAT_TEXT_PROP
9243 || new_popup_mask == NULL
9244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 || outofmem)
9246 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009247 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009248 {
9249 /* guess the size */
9250 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9251
9252 /* Remember we did this to avoid getting outofmem messages over
9253 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009254 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009255 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009256 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009257 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009258 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009259 VIM_CLEAR(new_ScreenLinesC[i]);
9260 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009261 VIM_CLEAR(new_ScreenAttrs);
9262 VIM_CLEAR(new_LineOffset);
9263 VIM_CLEAR(new_LineWraps);
9264 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009265#ifdef FEAT_TEXT_PROP
9266 VIM_CLEAR(new_popup_mask);
9267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 }
9269 else
9270 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009271 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009272
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 for (new_row = 0; new_row < Rows; ++new_row)
9274 {
9275 new_LineOffset[new_row] = new_row * Columns;
9276 new_LineWraps[new_row] = FALSE;
9277
9278 /*
9279 * If the screen is not going to be cleared, copy as much as
9280 * possible from the old screen to the new one and clear the rest
9281 * (used when resizing the window at the "--more--" prompt or when
9282 * executing an external command, for the GUI).
9283 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009284 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 {
9286 (void)vim_memset(new_ScreenLines + new_row * Columns,
9287 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 if (enc_utf8)
9289 {
9290 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9291 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009292 for (i = 0; i < p_mco; ++i)
9293 (void)vim_memset(new_ScreenLinesC[i]
9294 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 0, (size_t)Columns * sizeof(u8char_T));
9296 }
9297 if (enc_dbcs == DBCS_JPNU)
9298 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9299 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9301 0, (size_t)Columns * sizeof(sattr_T));
9302 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009303 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 {
9305 if (screen_Columns < Columns)
9306 len = screen_Columns;
9307 else
9308 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009309 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009310 * may be invalid now. Also when p_mco changes. */
9311 if (!(enc_utf8 && ScreenLinesUC == NULL)
9312 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009313 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9314 ScreenLines + LineOffset[old_row],
9315 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009316 if (enc_utf8 && ScreenLinesUC != NULL
9317 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 {
9319 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9320 ScreenLinesUC + LineOffset[old_row],
9321 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009322 for (i = 0; i < p_mco; ++i)
9323 mch_memmove(new_ScreenLinesC[i]
9324 + new_LineOffset[new_row],
9325 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 (size_t)len * sizeof(u8char_T));
9327 }
9328 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9329 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9330 ScreenLines2 + LineOffset[old_row],
9331 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9333 ScreenAttrs + LineOffset[old_row],
9334 (size_t)len * sizeof(sattr_T));
9335 }
9336 }
9337 }
9338 /* Use the last line of the screen for the current line. */
9339 current_ScreenLine = new_ScreenLines + Rows * Columns;
9340 }
9341
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009342 free_screenlines();
9343
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009346 for (i = 0; i < p_mco; ++i)
9347 ScreenLinesC[i] = new_ScreenLinesC[i];
9348 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 ScreenAttrs = new_ScreenAttrs;
9351 LineOffset = new_LineOffset;
9352 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009353 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02009354#ifdef FEAT_TEXT_PROP
9355 popup_mask = new_popup_mask;
Bram Moolenaar1748c7f2019-06-08 16:55:15 +02009356 vim_memset(popup_mask, 0, Rows * Columns * sizeof(short));
Bram Moolenaar33796b32019-06-08 16:01:13 +02009357 popup_mask_refresh = TRUE;
9358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359
9360 /* It's important that screen_Rows and screen_Columns reflect the actual
9361 * size of ScreenLines[]. Set them before calling anything. */
9362#ifdef FEAT_GUI
9363 old_Rows = screen_Rows;
9364#endif
9365 screen_Rows = Rows;
9366 screen_Columns = Columns;
9367
9368 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009369 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371#ifdef FEAT_GUI
9372 else if (gui.in_use
9373 && !gui.starting
9374 && ScreenLines != NULL
9375 && old_Rows != Rows)
9376 {
9377 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9378 /*
9379 * Adjust the position of the cursor, for when executing an external
9380 * command.
9381 */
9382 if (msg_row >= Rows) /* Rows got smaller */
9383 msg_row = Rows - 1; /* put cursor at last row */
9384 else if (Rows > old_Rows) /* Rows got bigger */
9385 msg_row += Rows - old_Rows; /* put cursor in same place */
9386 if (msg_col >= Columns) /* Columns got smaller */
9387 msg_col = Columns - 1; /* put cursor at last column */
9388 }
9389#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009390 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009393 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009394
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009395 /*
9396 * Do not apply autocommands more than 3 times to avoid an endless loop
9397 * in case applying autocommands always changes Rows or Columns.
9398 */
9399 if (starting == 0 && ++retry_count <= 3)
9400 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009401 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009402 /* In rare cases, autocommands may have altered Rows or Columns,
9403 * jump back to check if we need to allocate the screen again. */
9404 goto retry;
9405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406}
9407
9408 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009409free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009410{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009411 int i;
9412
Bram Moolenaar33796b32019-06-08 16:01:13 +02009413 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009414 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009415 VIM_CLEAR(ScreenLinesC[i]);
9416 VIM_CLEAR(ScreenLines2);
9417 VIM_CLEAR(ScreenLines);
9418 VIM_CLEAR(ScreenAttrs);
9419 VIM_CLEAR(LineOffset);
9420 VIM_CLEAR(LineWraps);
9421 VIM_CLEAR(TabPageIdxs);
9422#ifdef FEAT_TEXT_PROP
9423 VIM_CLEAR(popup_mask);
9424#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009425}
9426
9427 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009428screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429{
9430 check_for_delay(FALSE);
9431 screenalloc(FALSE); /* allocate screen buffers if size changed */
9432 screenclear2(); /* clear the screen */
9433}
9434
9435 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009436screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437{
9438 int i;
9439
9440 if (starting == NO_SCREEN || ScreenLines == NULL
9441#ifdef FEAT_GUI
9442 || (gui.in_use && gui.starting)
9443#endif
9444 )
9445 return;
9446
9447#ifdef FEAT_GUI
9448 if (!gui.in_use)
9449#endif
9450 screen_attr = -1; /* force setting the Normal colors */
9451 screen_stop_highlight(); /* don't want highlighting here */
9452
9453#ifdef FEAT_CLIPBOARD
9454 /* disable selection without redrawing it */
9455 clip_scroll_selection(9999);
9456#endif
9457
9458 /* blank out ScreenLines */
9459 for (i = 0; i < Rows; ++i)
9460 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009461 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 LineWraps[i] = FALSE;
9463 }
9464
9465 if (can_clear(T_CL))
9466 {
9467 out_str(T_CL); /* clear the display */
9468 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009469 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 }
9471 else
9472 {
9473 /* can't clear the screen, mark all chars with invalid attributes */
9474 for (i = 0; i < Rows; ++i)
9475 lineinvalid(LineOffset[i], (int)Columns);
9476 clear_cmdline = TRUE;
9477 }
9478
9479 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9480
9481 win_rest_invalid(firstwin);
9482 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009483 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 if (must_redraw == CLEAR) /* no need to clear again */
9485 must_redraw = NOT_VALID;
9486 compute_cmdrow();
9487 msg_row = cmdline_row; /* put cursor on last line for messages */
9488 msg_col = 0;
9489 screen_start(); /* don't know where cursor is now */
9490 msg_scrolled = 0; /* can't scroll back */
9491 msg_didany = FALSE;
9492 msg_didout = FALSE;
9493}
9494
9495/*
9496 * Clear one line in ScreenLines.
9497 */
9498 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009499lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500{
9501 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 if (enc_utf8)
9503 (void)vim_memset(ScreenLinesUC + off, 0,
9504 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009505 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506}
9507
9508/*
9509 * Mark one line in ScreenLines invalid by setting the attributes to an
9510 * invalid value.
9511 */
9512 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009513lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514{
9515 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9516}
9517
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518/*
9519 * Copy part of a Screenline for vertically split window "wp".
9520 */
9521 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009522linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523{
9524 unsigned off_to = LineOffset[to] + wp->w_wincol;
9525 unsigned off_from = LineOffset[from] + wp->w_wincol;
9526
9527 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9528 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 if (enc_utf8)
9530 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009531 int i;
9532
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9534 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009535 for (i = 0; i < p_mco; ++i)
9536 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9537 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 }
9539 if (enc_dbcs == DBCS_JPNU)
9540 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9541 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9543 wp->w_width * sizeof(sattr_T));
9544}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545
9546/*
9547 * Return TRUE if clearing with term string "p" would work.
9548 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02009549 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 */
9551 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009552can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553{
9554 return (*p != NUL && (t_colors <= 1
9555#ifdef FEAT_GUI
9556 || gui.in_use
9557#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009558#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009559 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009560 || (!p_tgc && cterm_normal_bg_color == 0)
9561#else
9562 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009563#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009564 || *T_UT != NUL)
9565#ifdef FEAT_TEXT_PROP
9566 && !(p == T_CE && popup_visible)
9567#endif
9568 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569}
9570
9571/*
9572 * Reset cursor position. Use whenever cursor was moved because of outputting
9573 * something directly to the screen (shell commands) or a terminal control
9574 * code.
9575 */
9576 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009577screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578{
9579 screen_cur_row = screen_cur_col = 9999;
9580}
9581
9582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 * Move the cursor to position "row","col" in the screen.
9584 * This tries to find the most efficient way to move, minimizing the number of
9585 * characters sent to the terminal.
9586 */
9587 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009588windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009590 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 int i;
9592 int plan;
9593 int cost;
9594 int wouldbe_col;
9595 int noinvcurs;
9596 char_u *bs;
9597 int goto_cost;
9598 int attr;
9599
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009600#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9602
9603#define PLAN_LE 1
9604#define PLAN_CR 2
9605#define PLAN_NL 3
9606#define PLAN_WRITE 4
9607 /* Can't use ScreenLines unless initialized */
9608 if (ScreenLines == NULL)
9609 return;
9610
9611 if (col != screen_cur_col || row != screen_cur_row)
9612 {
9613 /* Check for valid position. */
9614 if (row < 0) /* window without text lines? */
9615 row = 0;
9616 if (row >= screen_Rows)
9617 row = screen_Rows - 1;
9618 if (col >= screen_Columns)
9619 col = screen_Columns - 1;
9620
9621 /* check if no cursor movement is allowed in highlight mode */
9622 if (screen_attr && *T_MS == NUL)
9623 noinvcurs = HIGHL_COST;
9624 else
9625 noinvcurs = 0;
9626 goto_cost = GOTO_COST + noinvcurs;
9627
9628 /*
9629 * Plan how to do the positioning:
9630 * 1. Use CR to move it to column 0, same row.
9631 * 2. Use T_LE to move it a few columns to the left.
9632 * 3. Use NL to move a few lines down, column 0.
9633 * 4. Move a few columns to the right with T_ND or by writing chars.
9634 *
9635 * Don't do this if the cursor went beyond the last column, the cursor
9636 * position is unknown then (some terminals wrap, some don't )
9637 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009638 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 * characters to move the cursor to the right.
9640 */
9641 if (row >= screen_cur_row && screen_cur_col < Columns)
9642 {
9643 /*
9644 * If the cursor is in the same row, bigger col, we can use CR
9645 * or T_LE.
9646 */
9647 bs = NULL; /* init for GCC */
9648 attr = screen_attr;
9649 if (row == screen_cur_row && col < screen_cur_col)
9650 {
9651 /* "le" is preferred over "bc", because "bc" is obsolete */
9652 if (*T_LE)
9653 bs = T_LE; /* "cursor left" */
9654 else
9655 bs = T_BC; /* "backspace character (old) */
9656 if (*bs)
9657 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9658 else
9659 cost = 999;
9660 if (col + 1 < cost) /* using CR is less characters */
9661 {
9662 plan = PLAN_CR;
9663 wouldbe_col = 0;
9664 cost = 1; /* CR is just one character */
9665 }
9666 else
9667 {
9668 plan = PLAN_LE;
9669 wouldbe_col = col;
9670 }
9671 if (noinvcurs) /* will stop highlighting */
9672 {
9673 cost += noinvcurs;
9674 attr = 0;
9675 }
9676 }
9677
9678 /*
9679 * If the cursor is above where we want to be, we can use CR LF.
9680 */
9681 else if (row > screen_cur_row)
9682 {
9683 plan = PLAN_NL;
9684 wouldbe_col = 0;
9685 cost = (row - screen_cur_row) * 2; /* CR LF */
9686 if (noinvcurs) /* will stop highlighting */
9687 {
9688 cost += noinvcurs;
9689 attr = 0;
9690 }
9691 }
9692
9693 /*
9694 * If the cursor is in the same row, smaller col, just use write.
9695 */
9696 else
9697 {
9698 plan = PLAN_WRITE;
9699 wouldbe_col = screen_cur_col;
9700 cost = 0;
9701 }
9702
9703 /*
9704 * Check if any characters that need to be written have the
9705 * correct attributes. Also avoid UTF-8 characters.
9706 */
9707 i = col - wouldbe_col;
9708 if (i > 0)
9709 cost += i;
9710 if (cost < goto_cost && i > 0)
9711 {
9712 /*
9713 * Check if the attributes are correct without additionally
9714 * stopping highlighting.
9715 */
9716 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9717 while (i && *p++ == attr)
9718 --i;
9719 if (i != 0)
9720 {
9721 /*
9722 * Try if it works when highlighting is stopped here.
9723 */
9724 if (*--p == 0)
9725 {
9726 cost += noinvcurs;
9727 while (i && *p++ == 0)
9728 --i;
9729 }
9730 if (i != 0)
9731 cost = 999; /* different attributes, don't do it */
9732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 if (enc_utf8)
9734 {
9735 /* Don't use an UTF-8 char for positioning, it's slow. */
9736 for (i = wouldbe_col; i < col; ++i)
9737 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9738 {
9739 cost = 999;
9740 break;
9741 }
9742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 }
9744
9745 /*
9746 * We can do it without term_windgoto()!
9747 */
9748 if (cost < goto_cost)
9749 {
9750 if (plan == PLAN_LE)
9751 {
9752 if (noinvcurs)
9753 screen_stop_highlight();
9754 while (screen_cur_col > col)
9755 {
9756 out_str(bs);
9757 --screen_cur_col;
9758 }
9759 }
9760 else if (plan == PLAN_CR)
9761 {
9762 if (noinvcurs)
9763 screen_stop_highlight();
9764 out_char('\r');
9765 screen_cur_col = 0;
9766 }
9767 else if (plan == PLAN_NL)
9768 {
9769 if (noinvcurs)
9770 screen_stop_highlight();
9771 while (screen_cur_row < row)
9772 {
9773 out_char('\n');
9774 ++screen_cur_row;
9775 }
9776 screen_cur_col = 0;
9777 }
9778
9779 i = col - screen_cur_col;
9780 if (i > 0)
9781 {
9782 /*
9783 * Use cursor-right if it's one character only. Avoids
9784 * removing a line of pixels from the last bold char, when
9785 * using the bold trick in the GUI.
9786 */
9787 if (T_ND[0] != NUL && T_ND[1] == NUL)
9788 {
9789 while (i-- > 0)
9790 out_char(*T_ND);
9791 }
9792 else
9793 {
9794 int off;
9795
9796 off = LineOffset[row] + screen_cur_col;
9797 while (i-- > 0)
9798 {
9799 if (ScreenAttrs[off] != screen_attr)
9800 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 if (enc_dbcs == DBCS_JPNU
9804 && ScreenLines[off] == 0x8e)
9805 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 ++off;
9807 }
9808 }
9809 }
9810 }
9811 }
9812 else
9813 cost = 999;
9814
9815 if (cost >= goto_cost)
9816 {
9817 if (noinvcurs)
9818 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009819 if (row == screen_cur_row && (col > screen_cur_col)
9820 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 term_cursor_right(col - screen_cur_col);
9822 else
9823 term_windgoto(row, col);
9824 }
9825 screen_cur_row = row;
9826 screen_cur_col = col;
9827 }
9828}
9829
9830/*
9831 * Set cursor to its position in the current window.
9832 */
9833 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009834setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009836 setcursor_mayforce(FALSE);
9837}
9838
9839/*
9840 * Set cursor to its position in the current window.
9841 * When "force" is TRUE also when not redrawing.
9842 */
9843 void
9844setcursor_mayforce(int force)
9845{
9846 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 {
9848 validate_cursor();
9849 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009850 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009852 /* With 'rightleft' set and the cursor on a double-wide
9853 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009854 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9855 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009856 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009857 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858#endif
9859 curwin->w_wcol));
9860 }
9861}
9862
9863
9864/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009865 * Insert 'line_count' lines at 'row' in window 'wp'.
9866 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9867 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 * scrolling.
9869 * Returns FAIL if the lines are not inserted, OK for success.
9870 */
9871 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009872win_ins_lines(
9873 win_T *wp,
9874 int row,
9875 int line_count,
9876 int invalid,
9877 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878{
9879 int did_delete;
9880 int nextrow;
9881 int lastrow;
9882 int retval;
9883
9884 if (invalid)
9885 wp->w_lines_valid = 0;
9886
9887 if (wp->w_height < 5)
9888 return FAIL;
9889
9890 if (line_count > wp->w_height - row)
9891 line_count = wp->w_height - row;
9892
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009893 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 if (retval != MAYBE)
9895 return retval;
9896
9897 /*
9898 * If there is a next window or a status line, we first try to delete the
9899 * lines at the bottom to avoid messing what is after the window.
9900 * If this fails and there are following windows, don't do anything to avoid
9901 * messing up those windows, better just redraw.
9902 */
9903 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 if (wp->w_next != NULL || wp->w_status_height)
9905 {
9906 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009907 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 did_delete = TRUE;
9909 else if (wp->w_next)
9910 return FAIL;
9911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 /*
9913 * if no lines deleted, blank the lines that will end up below the window
9914 */
9915 if (!did_delete)
9916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009919 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 lastrow = nextrow + line_count;
9921 if (lastrow > Rows)
9922 lastrow = Rows;
9923 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009924 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 ' ', ' ', 0);
9926 }
9927
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009928 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 == FAIL)
9930 {
9931 /* deletion will have messed up other windows */
9932 if (did_delete)
9933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 win_rest_invalid(W_NEXT(wp));
9936 }
9937 return FAIL;
9938 }
9939
9940 return OK;
9941}
9942
9943/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009944 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9946 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9947 * scrolling
9948 * Return OK for success, FAIL if the lines are not deleted.
9949 */
9950 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009951win_del_lines(
9952 win_T *wp,
9953 int row,
9954 int line_count,
9955 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009956 int mayclear,
9957 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958{
9959 int retval;
9960
9961 if (invalid)
9962 wp->w_lines_valid = 0;
9963
9964 if (line_count > wp->w_height - row)
9965 line_count = wp->w_height - row;
9966
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009967 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 if (retval != MAYBE)
9969 return retval;
9970
9971 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009972 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 return FAIL;
9974
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 /*
9976 * If there are windows or status lines below, try to put them at the
9977 * correct place. If we can't do that, they have to be redrawn.
9978 */
9979 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9980 {
9981 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009982 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 {
9984 wp->w_redr_status = TRUE;
9985 win_rest_invalid(wp->w_next);
9986 }
9987 }
9988 /*
9989 * If this is the last window and there is no status line, redraw the
9990 * command line later.
9991 */
9992 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 redraw_cmdline = TRUE;
9994 return OK;
9995}
9996
9997/*
9998 * Common code for win_ins_lines() and win_del_lines().
9999 * Returns OK or FAIL when the work has been done.
10000 * Returns MAYBE when not finished yet.
10001 */
10002 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010003win_do_lines(
10004 win_T *wp,
10005 int row,
10006 int line_count,
10007 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010008 int del,
10009 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010{
10011 int retval;
10012
10013 if (!redrawing() || line_count <= 0)
10014 return FAIL;
10015
Bram Moolenaar33796b32019-06-08 16:01:13 +020010016 // When inserting lines would result in loss of command output, just redraw
10017 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010018 if (no_win_do_lines_ins && !del)
10019 return FAIL;
10020
Bram Moolenaar33796b32019-06-08 16:01:13 +020010021 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +020010022 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010024 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +020010025 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 return FAIL;
10027 }
10028
Bram Moolenaar33796b32019-06-08 16:01:13 +020010029#ifdef FEAT_TEXT_PROP
10030 // this doesn't work when tere are popups visible
10031 if (popup_visible)
10032 return FAIL;
10033#endif
10034
10035 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 if (row + line_count >= wp->w_height)
10037 {
10038 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +020010039 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 ' ', ' ', 0);
10041 return OK;
10042 }
10043
10044 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010045 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010047 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010049 if (!no_win_do_lines_ins)
10050 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051
10052 /*
10053 * If the terminal can set a scroll region, use that.
10054 * Always do this in a vertically split window. This will redraw from
10055 * ScreenLines[] when t_CV isn't defined. That's faster than using
10056 * win_line().
10057 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010058 * a character in the lower right corner of the scroll region may cause a
10059 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 */
Bram Moolenaar02631462017-09-22 15:20:32 +020010061 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 scroll_region_set(wp, row);
10065 if (del)
10066 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010067 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 else
10069 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010070 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 scroll_region_reset();
10073 return retval;
10074 }
10075
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
10077 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078
10079 return MAYBE;
10080}
10081
10082/*
10083 * window 'wp' and everything after it is messed up, mark it for redraw
10084 */
10085 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010086win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 {
10090 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 wp->w_redr_status = TRUE;
10092 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 }
10094 redraw_cmdline = TRUE;
10095}
10096
10097/*
10098 * The rest of the routines in this file perform screen manipulations. The
10099 * given operation is performed physically on the screen. The corresponding
10100 * change is also made to the internal screen image. In this way, the editor
10101 * anticipates the effect of editing changes on the appearance of the screen.
10102 * That way, when we call screenupdate a complete redraw isn't usually
10103 * necessary. Another advantage is that we can keep adding code to anticipate
10104 * screen changes, and in the meantime, everything still works.
10105 */
10106
10107/*
10108 * types for inserting or deleting lines
10109 */
10110#define USE_T_CAL 1
10111#define USE_T_CDL 2
10112#define USE_T_AL 3
10113#define USE_T_CE 4
10114#define USE_T_DL 5
10115#define USE_T_SR 6
10116#define USE_NL 7
10117#define USE_T_CD 8
10118#define USE_REDRAW 9
10119
10120/*
10121 * insert lines on the screen and update ScreenLines[]
10122 * 'end' is the line after the scrolled part. Normally it is Rows.
10123 * When scrolling region used 'off' is the offset from the top for the region.
10124 * 'row' and 'end' are relative to the start of the region.
10125 *
10126 * return FAIL for failure, OK for success.
10127 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000010128 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010129screen_ins_lines(
10130 int off,
10131 int row,
10132 int line_count,
10133 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010134 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +010010135 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136{
10137 int i;
10138 int j;
10139 unsigned temp;
10140 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010141 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 int type;
10143 int result_empty;
10144 int can_ce = can_clear(T_CE);
10145
10146 /*
10147 * FAIL if
10148 * - there is no valid screen
10149 * - the screen has to be redrawn completely
10150 * - the line count is less than one
10151 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010152 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +020010153 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 */
Bram Moolenaar33796b32019-06-08 16:01:13 +020010155 if (!screen_valid(TRUE)
10156 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010157#ifdef FEAT_CLIPBOARD
10158 || (clip_star.state != SELECT_CLEARED
10159 && redrawing_for_callback > 0)
10160#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +020010161#ifdef FEAT_TEXT_PROP
10162 || popup_visible
10163#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010164 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 return FAIL;
10166
10167 /*
10168 * There are seven ways to insert lines:
10169 * 0. When in a vertically split window and t_CV isn't set, redraw the
10170 * characters from ScreenLines[].
10171 * 1. Use T_CD (clear to end of display) if it exists and the result of
10172 * the insert is just empty lines
10173 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
10174 * present or line_count > 1. It looks better if we do all the inserts
10175 * at once.
10176 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
10177 * insert is just empty lines and T_CE is not present or line_count >
10178 * 1.
10179 * 4. Use T_AL (insert line) if it exists.
10180 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10181 * just empty lines.
10182 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10183 * just empty lines.
10184 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10185 * the 'da' flag is not set or we have clear line capability.
10186 * 8. redraw the characters from ScreenLines[].
10187 *
10188 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10189 * the scrollbar for the window. It does have insert line, use that if it
10190 * exists.
10191 */
10192 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10194 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010195 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 type = USE_T_CD;
10197 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10198 type = USE_T_CAL;
10199 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10200 type = USE_T_CDL;
10201 else if (*T_AL != NUL)
10202 type = USE_T_AL;
10203 else if (can_ce && result_empty)
10204 type = USE_T_CE;
10205 else if (*T_DL != NUL && result_empty)
10206 type = USE_T_DL;
10207 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10208 type = USE_T_SR;
10209 else
10210 return FAIL;
10211
10212 /*
10213 * For clearing the lines screen_del_lines() is used. This will also take
10214 * care of t_db if necessary.
10215 */
10216 if (type == USE_T_CD || type == USE_T_CDL ||
10217 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010218 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219
10220 /*
10221 * If text is retained below the screen, first clear or delete as many
10222 * lines at the bottom of the window as are about to be inserted so that
10223 * the deleted lines won't later surface during a screen_del_lines.
10224 */
10225 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010226 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227
10228#ifdef FEAT_CLIPBOARD
10229 /* Remove a modeless selection when inserting lines halfway the screen
10230 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010231 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010232 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 else
10234 clip_scroll_selection(-line_count);
10235#endif
10236
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237#ifdef FEAT_GUI
10238 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10239 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010240 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241#endif
10242
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010243 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10244 cursor_col = wp->w_wincol;
10245
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 if (*T_CCS != NUL) /* cursor relative to region */
10247 cursor_row = row;
10248 else
10249 cursor_row = row + off;
10250
10251 /*
10252 * Shift LineOffset[] line_count down to reflect the inserted lines.
10253 * Clear the inserted lines in ScreenLines[].
10254 */
10255 row += off;
10256 end += off;
10257 for (i = 0; i < line_count; ++i)
10258 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 if (wp != NULL && wp->w_width != Columns)
10260 {
10261 /* need to copy part of a line */
10262 j = end - 1 - i;
10263 while ((j -= line_count) >= row)
10264 linecopy(j + line_count, j, wp);
10265 j += line_count;
10266 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010267 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10268 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 else
10270 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10271 LineWraps[j] = FALSE;
10272 }
10273 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 {
10275 j = end - 1 - i;
10276 temp = LineOffset[j];
10277 while ((j -= line_count) >= row)
10278 {
10279 LineOffset[j + line_count] = LineOffset[j];
10280 LineWraps[j + line_count] = LineWraps[j];
10281 }
10282 LineOffset[j + line_count] = temp;
10283 LineWraps[j + line_count] = FALSE;
10284 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010285 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286 else
10287 lineinvalid(temp, (int)Columns);
10288 }
10289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290
10291 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010292 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010293 if (clear_attr != 0)
10294 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 /* redraw the characters */
10297 if (type == USE_REDRAW)
10298 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010299 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 {
10301 term_append_lines(line_count);
10302 screen_start(); /* don't know where cursor is now */
10303 }
10304 else
10305 {
10306 for (i = 0; i < line_count; i++)
10307 {
10308 if (type == USE_T_AL)
10309 {
10310 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010311 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 out_str(T_AL);
10313 }
10314 else /* type == USE_T_SR */
10315 out_str(T_SR);
10316 screen_start(); /* don't know where cursor is now */
10317 }
10318 }
10319
10320 /*
10321 * With scroll-reverse and 'da' flag set we need to clear the lines that
10322 * have been scrolled down into the region.
10323 */
10324 if (type == USE_T_SR && *T_DA)
10325 {
10326 for (i = 0; i < line_count; ++i)
10327 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010328 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 out_str(T_CE);
10330 screen_start(); /* don't know where cursor is now */
10331 }
10332 }
10333
10334#ifdef FEAT_GUI
10335 gui_can_update_cursor();
10336 if (gui.in_use)
10337 out_flush(); /* always flush after a scroll */
10338#endif
10339 return OK;
10340}
10341
10342/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010343 * Delete lines on the screen and update ScreenLines[].
10344 * "end" is the line after the scrolled part. Normally it is Rows.
10345 * When scrolling region used "off" is the offset from the top for the region.
10346 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 *
10348 * Return OK for success, FAIL if the lines are not deleted.
10349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010351screen_del_lines(
10352 int off,
10353 int row,
10354 int line_count,
10355 int end,
10356 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010357 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010358 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359{
10360 int j;
10361 int i;
10362 unsigned temp;
10363 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010364 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 int cursor_end;
10366 int result_empty; /* result is empty until end of region */
10367 int can_delete; /* deleting line codes can be used */
10368 int type;
10369
10370 /*
10371 * FAIL if
10372 * - there is no valid screen
10373 * - the screen has to be redrawn completely
10374 * - the line count is less than one
10375 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010376 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010378 if (!screen_valid(TRUE) || line_count <= 0
10379 || (!force && line_count > p_ttyscroll)
10380#ifdef FEAT_CLIPBOARD
10381 || (clip_star.state != SELECT_CLEARED
10382 && redrawing_for_callback > 0)
10383#endif
10384 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 return FAIL;
10386
10387 /*
10388 * Check if the rest of the current region will become empty.
10389 */
10390 result_empty = row + line_count >= end;
10391
10392 /*
10393 * We can delete lines only when 'db' flag not set or when 'ce' option
10394 * available.
10395 */
10396 can_delete = (*T_DB == NUL || can_clear(T_CE));
10397
10398 /*
10399 * There are six ways to delete lines:
10400 * 0. When in a vertically split window and t_CV isn't set, redraw the
10401 * characters from ScreenLines[].
10402 * 1. Use T_CD if it exists and the result is empty.
10403 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10404 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10405 * none of the other ways work.
10406 * 4. Use T_CE (erase line) if the result is empty.
10407 * 5. Use T_DL (delete line) if it exists.
10408 * 6. redraw the characters from ScreenLines[].
10409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10411 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010412 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 type = USE_T_CD;
10414#if defined(__BEOS__) && defined(BEOS_DR8)
10415 /*
10416 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10417 * its internal termcap... this works okay for tests which test *T_DB !=
10418 * NUL. It has the disadvantage that the user cannot use any :set t_*
10419 * command to get T_DB (back) to empty_option, only :set term=... will do
10420 * the trick...
10421 * Anyway, this hack will hopefully go away with the next OS release.
10422 * (Olaf Seibert)
10423 */
10424 else if (row == 0 && T_DB == empty_option
10425 && (line_count == 1 || *T_CDL == NUL))
10426#else
10427 else if (row == 0 && (
10428#ifndef AMIGA
10429 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10430 * up, so use delete-line command */
10431 line_count == 1 ||
10432#endif
10433 *T_CDL == NUL))
10434#endif
10435 type = USE_NL;
10436 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10437 type = USE_T_CDL;
10438 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010439 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 type = USE_T_CE;
10441 else if (*T_DL != NUL && can_delete)
10442 type = USE_T_DL;
10443 else if (*T_CDL != NUL && can_delete)
10444 type = USE_T_CDL;
10445 else
10446 return FAIL;
10447
10448#ifdef FEAT_CLIPBOARD
10449 /* Remove a modeless selection when deleting lines halfway the screen or
10450 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010451 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010452 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 else
10454 clip_scroll_selection(line_count);
10455#endif
10456
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457#ifdef FEAT_GUI
10458 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10459 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010460 gui_dont_update_cursor(gui.cursor_row >= row + off
10461 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462#endif
10463
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010464 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10465 cursor_col = wp->w_wincol;
10466
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 if (*T_CCS != NUL) /* cursor relative to region */
10468 {
10469 cursor_row = row;
10470 cursor_end = end;
10471 }
10472 else
10473 {
10474 cursor_row = row + off;
10475 cursor_end = end + off;
10476 }
10477
10478 /*
10479 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10480 * Clear the inserted lines in ScreenLines[].
10481 */
10482 row += off;
10483 end += off;
10484 for (i = 0; i < line_count; ++i)
10485 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 if (wp != NULL && wp->w_width != Columns)
10487 {
10488 /* need to copy part of a line */
10489 j = row + i;
10490 while ((j += line_count) <= end - 1)
10491 linecopy(j - line_count, j, wp);
10492 j -= line_count;
10493 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010494 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10495 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 else
10497 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10498 LineWraps[j] = FALSE;
10499 }
10500 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 {
10502 /* whole width, moving the line pointers is faster */
10503 j = row + i;
10504 temp = LineOffset[j];
10505 while ((j += line_count) <= end - 1)
10506 {
10507 LineOffset[j - line_count] = LineOffset[j];
10508 LineWraps[j - line_count] = LineWraps[j];
10509 }
10510 LineOffset[j - line_count] = temp;
10511 LineWraps[j - line_count] = FALSE;
10512 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010513 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 else
10515 lineinvalid(temp, (int)Columns);
10516 }
10517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010519 if (screen_attr != clear_attr)
10520 screen_stop_highlight();
10521 if (clear_attr != 0)
10522 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 /* redraw the characters */
10525 if (type == USE_REDRAW)
10526 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010527 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010529 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 out_str(T_CD);
10531 screen_start(); /* don't know where cursor is now */
10532 }
10533 else if (type == USE_T_CDL)
10534 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010535 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 term_delete_lines(line_count);
10537 screen_start(); /* don't know where cursor is now */
10538 }
10539 /*
10540 * Deleting lines at top of the screen or scroll region: Just scroll
10541 * the whole screen (scroll region) up by outputting newlines on the
10542 * last line.
10543 */
10544 else if (type == USE_NL)
10545 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010546 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 for (i = line_count; --i >= 0; )
10548 out_char('\n'); /* cursor will remain on same line */
10549 }
10550 else
10551 {
10552 for (i = line_count; --i >= 0; )
10553 {
10554 if (type == USE_T_DL)
10555 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010556 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 out_str(T_DL); /* delete a line */
10558 }
10559 else /* type == USE_T_CE */
10560 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010561 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 out_str(T_CE); /* erase a line */
10563 }
10564 screen_start(); /* don't know where cursor is now */
10565 }
10566 }
10567
10568 /*
10569 * If the 'db' flag is set, we need to clear the lines that have been
10570 * scrolled up at the bottom of the region.
10571 */
10572 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10573 {
10574 for (i = line_count; i > 0; --i)
10575 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010576 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 out_str(T_CE); /* erase a line */
10578 screen_start(); /* don't know where cursor is now */
10579 }
10580 }
10581
10582#ifdef FEAT_GUI
10583 gui_can_update_cursor();
10584 if (gui.in_use)
10585 out_flush(); /* always flush after a scroll */
10586#endif
10587
10588 return OK;
10589}
10590
10591/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010592 * Return TRUE when postponing displaying the mode message: when not redrawing
10593 * or inside a mapping.
10594 */
10595 int
10596skip_showmode()
10597{
10598 // Call char_avail() only when we are going to show something, because it
10599 // takes a bit of time. redrawing() may also call char_avail_avail().
10600 if (global_busy
10601 || msg_silent != 0
10602 || !redrawing()
10603 || (char_avail() && !KeyTyped))
10604 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010605 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010606 return TRUE;
10607 }
10608 return FALSE;
10609}
10610
10611/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010612 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 *
10614 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10615 * If clear_cmdline is FALSE there may be a message there that needs to be
10616 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010617 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 * Return the length of the message (0 if no message).
10619 */
10620 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010621showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622{
10623 int need_clear;
10624 int length = 0;
10625 int do_mode;
10626 int attr;
10627 int nwr_save;
10628#ifdef FEAT_INS_EXPAND
10629 int sub_attr;
10630#endif
10631
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010632 do_mode = ((p_smd && msg_silent == 0)
10633 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010634 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010635 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010636 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010638 if (skip_showmode())
10639 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640
10641 nwr_save = need_wait_return;
10642
10643 /* wait a bit before overwriting an important message */
10644 check_for_delay(FALSE);
10645
10646 /* if the cmdline is more than one line high, erase top lines */
10647 need_clear = clear_cmdline;
10648 if (clear_cmdline && cmdline_row < Rows - 1)
10649 msg_clr_cmdline(); /* will reset clear_cmdline */
10650
10651 /* Position on the last line in the window, column 0 */
10652 msg_pos_mode();
10653 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010654 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 if (do_mode)
10656 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010657 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010659 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010660# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010661 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010662# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010663 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010664# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010665 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010666# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010667 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010669 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670# endif
10671#endif
10672#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10673 if (gui.in_use)
10674 {
10675 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010676 {
10677 /* HANGUL */
10678 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010679 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010680 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010681 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 }
10684#endif
10685#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010686 /* CTRL-X in Insert mode */
10687 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 {
10689 /* These messages can get long, avoid a wrap in a narrow
10690 * window. Prefer showing edit_submode_extra. */
10691 length = (Rows - msg_row) * Columns - 3;
10692 if (edit_submode_extra != NULL)
10693 length -= vim_strsize(edit_submode_extra);
10694 if (length > 0)
10695 {
10696 if (edit_submode_pre != NULL)
10697 length -= vim_strsize(edit_submode_pre);
10698 if (length - vim_strsize(edit_submode) > 0)
10699 {
10700 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010701 msg_puts_attr((char *)edit_submode_pre, attr);
10702 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 }
10704 if (edit_submode_extra != NULL)
10705 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010706 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010708 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 else
10710 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010711 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712 }
10713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 }
10715 else
10716#endif
10717 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010719 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010720 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010721 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 else if (State & INSERT)
10723 {
10724#ifdef FEAT_RIGHTLEFT
10725 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010726 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010728 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010730 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010731 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010733 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010735 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736#ifdef FEAT_RIGHTLEFT
10737 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010738 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739#endif
10740#ifdef FEAT_KEYMAP
10741 if (State & LANGMAP)
10742 {
10743# ifdef FEAT_ARABIC
10744 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010745 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 else
10747# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010748 if (get_keymap_str(curwin, (char_u *)" (%s)",
10749 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010750 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 }
10752#endif
10753 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010754 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 if (VIsual_active)
10757 {
10758 char *p;
10759
10760 /* Don't concatenate separate words to avoid translation
10761 * problems. */
10762 switch ((VIsual_select ? 4 : 0)
10763 + (VIsual_mode == Ctrl_V) * 2
10764 + (VIsual_mode == 'V'))
10765 {
10766 case 0: p = N_(" VISUAL"); break;
10767 case 1: p = N_(" VISUAL LINE"); break;
10768 case 2: p = N_(" VISUAL BLOCK"); break;
10769 case 4: p = N_(" SELECT"); break;
10770 case 5: p = N_(" SELECT LINE"); break;
10771 default: p = N_(" SELECT BLOCK"); break;
10772 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010773 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010775 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010777
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 need_clear = TRUE;
10779 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010780 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781#ifdef FEAT_INS_EXPAND
10782 && edit_submode == NULL /* otherwise it gets too long */
10783#endif
10784 )
10785 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010786 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 need_clear = TRUE;
10788 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010789
10790 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010791 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 msg_clr_eos();
10793 msg_didout = FALSE; /* overwrite this message */
10794 length = msg_col;
10795 msg_col = 0;
10796 need_wait_return = nwr_save; /* never ask for hit-return for this */
10797 }
10798 else if (clear_cmdline && msg_silent == 0)
10799 /* Clear the whole command line. Will reset "clear_cmdline". */
10800 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010801 else if (redraw_mode)
10802 {
10803 msg_pos_mode();
10804 msg_clr_eos();
10805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806
10807#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 /* In Visual mode the size of the selected area must be redrawn. */
10809 if (VIsual_active)
10810 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811
10812 /* If the last window has no status line, the ruler is after the mode
10813 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010814 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010815 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816#endif
10817 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010818 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 clear_cmdline = FALSE;
10820
10821 return length;
10822}
10823
10824/*
10825 * Position for a mode message.
10826 */
10827 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010828msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829{
10830 msg_col = 0;
10831 msg_row = Rows - 1;
10832}
10833
10834/*
10835 * Delete mode message. Used when ESC is typed which is expected to end
10836 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010837 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 */
10839 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010840unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841{
10842 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010843 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 */
10845 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10846 redraw_cmdline = TRUE; /* delete mode later */
10847 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010848 clearmode();
10849}
10850
10851/*
10852 * Clear the mode message.
10853 */
10854 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010855clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010856{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010857 int save_msg_row = msg_row;
10858 int save_msg_col = msg_col;
10859
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010860 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010861 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010862 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010863 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010864
10865 msg_col = save_msg_col;
10866 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867}
10868
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010869 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010870recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010871{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010872 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010873 if (!shortmess(SHM_RECORDING))
10874 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010875 char s[4];
10876
10877 sprintf(s, " @%c", reg_recording);
10878 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010879 }
10880}
10881
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010882/*
10883 * Draw the tab pages line at the top of the Vim window.
10884 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010885 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010886draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010887{
10888 int tabcount = 0;
10889 tabpage_T *tp;
10890 int tabwidth;
10891 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010892 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010893 int attr;
10894 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010895 win_T *cwp;
10896 int wincount;
10897 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010898 int c;
10899 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010900 int attr_sel = HL_ATTR(HLF_TPS);
10901 int attr_nosel = HL_ATTR(HLF_TP);
10902 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010903 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010904 int room;
10905 int use_sep_chars = (t_colors < 8
10906#ifdef FEAT_GUI
10907 && !gui.in_use
10908#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010909#ifdef FEAT_TERMGUICOLORS
10910 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010911#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010912 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010913
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010914 if (ScreenLines == NULL)
10915 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010916 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010917
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010918#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010919 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010920 if (gui_use_tabline())
10921 {
10922 gui_update_tabline();
10923 return;
10924 }
10925#endif
10926
10927 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010928 return;
10929
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010930#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010931 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010932
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010933 /* Use the 'tabline' option if it's set. */
10934 if (*p_tal != NUL)
10935 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010936 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010937
10938 /* Check for an error. If there is one we would loop in redrawing the
10939 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010940 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010941 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010942 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010943 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010944 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010945 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010946 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010947 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010948#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010949 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010950 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010951 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010952
Bram Moolenaar238a5642006-02-21 22:12:05 +000010953 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10954 if (tabwidth < 6)
10955 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010956
Bram Moolenaar238a5642006-02-21 22:12:05 +000010957 attr = attr_nosel;
10958 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010959 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10960 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010961 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010962 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010963
Bram Moolenaar238a5642006-02-21 22:12:05 +000010964 if (tp->tp_topframe == topframe)
10965 attr = attr_sel;
10966 if (use_sep_chars && col > 0)
10967 screen_putchar('|', 0, col++, attr);
10968
10969 if (tp->tp_topframe != topframe)
10970 attr = attr_nosel;
10971
10972 screen_putchar(' ', 0, col++, attr);
10973
10974 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010975 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010976 cwp = curwin;
10977 wp = firstwin;
10978 }
10979 else
10980 {
10981 cwp = tp->tp_curwin;
10982 wp = tp->tp_firstwin;
10983 }
10984
10985 modified = FALSE;
10986 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10987 if (bufIsChanged(wp->w_buffer))
10988 modified = TRUE;
10989 if (modified || wincount > 1)
10990 {
10991 if (wincount > 1)
10992 {
10993 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010994 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010995 if (col + len >= Columns - 3)
10996 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010997 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010998#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010999 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000011000#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020011001 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000011002#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000011003 );
11004 col += len;
11005 }
11006 if (modified)
11007 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
11008 screen_putchar(' ', 0, col++, attr);
11009 }
11010
11011 room = scol - col + tabwidth - 1;
11012 if (room > 0)
11013 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000011014 /* Get buffer name in NameBuff[] */
11015 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011016 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011017 len = vim_strsize(NameBuff);
11018 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000011019 if (has_mbyte)
11020 while (len > room)
11021 {
11022 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011023 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011024 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011025 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011026 {
11027 p += len - room;
11028 len = room;
11029 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000011030 if (len > Columns - col - 1)
11031 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000011032
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011033 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000011034 col += len;
11035 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000011036 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011037
11038 /* Store the tab page number in TabPageIdxs[], so that
11039 * jump_to_mouse() knows where each one is. */
11040 ++tabcount;
11041 while (scol < col)
11042 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000011043 }
11044
Bram Moolenaar238a5642006-02-21 22:12:05 +000011045 if (use_sep_chars)
11046 c = '_';
11047 else
11048 c = ' ';
11049 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000011050
11051 /* Put an "X" for closing the current tab if there are several. */
11052 if (first_tabpage->tp_next != NULL)
11053 {
11054 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
11055 TabPageIdxs[Columns - 1] = -999;
11056 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000011057 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000011058
11059 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
11060 * set. */
11061 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000011062}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000011063
11064/*
11065 * Get buffer name for "buf" into NameBuff[].
11066 * Takes care of special buffer names and translates special characters.
11067 */
11068 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011069get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000011070{
11071 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020011072 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000011073 else
11074 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
11075 trans_characters(NameBuff, MAXPATHL);
11076}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000011077
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078/*
11079 * Get the character to use in a status line. Get its attributes in "*attr".
11080 */
11081 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011082fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083{
11084 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011085
11086#ifdef FEAT_TERMINAL
11087 if (bt_terminal(wp->w_buffer))
11088 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011089 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020011090 {
11091 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011092 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020011093 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011094 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020011095 {
11096 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011097 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020011098 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011099 }
11100 else
11101#endif
11102 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010011104 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 fill = fill_stl;
11106 }
11107 else
11108 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010011109 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 fill = fill_stlnc;
11111 }
11112 /* Use fill when there is highlighting, and highlighting of current
11113 * window differs, or the fillchars differ, or this is not the
11114 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010011115 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011116 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 || (fill_stl != fill_stlnc)))
11118 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011119 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 return '^';
11121 return '=';
11122}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124/*
11125 * Get the character to use in a separator between vertically split windows.
11126 * Get its attributes in "*attr".
11127 */
11128 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010011129fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130{
Bram Moolenaar8820b482017-03-16 17:23:31 +010011131 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 if (*attr == 0 && fill_vert == ' ')
11133 return '|';
11134 else
11135 return fill_vert;
11136}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137
11138/*
11139 * Return TRUE if redrawing should currently be done.
11140 */
11141 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011142redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010011144#ifdef FEAT_EVAL
11145 if (disable_redraw_for_testing)
11146 return 0;
11147 else
11148#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020011149 return ((!RedrawingDisabled
11150#ifdef FEAT_EVAL
11151 || ignore_redraw_flag_for_testing
11152#endif
11153 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154}
11155
11156/*
11157 * Return TRUE if printing messages should currently be done.
11158 */
11159 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011160messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161{
11162 return (!(p_lz && char_avail() && !KeyTyped));
11163}
11164
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011165#ifdef FEAT_MENU
11166/*
11167 * Draw the window toolbar.
11168 */
11169 static void
11170redraw_win_toolbar(win_T *wp)
11171{
11172 vimmenu_T *menu;
11173 int item_idx = 0;
11174 int item_count = 0;
11175 int col = 0;
11176 int next_col;
11177 int off = (int)(current_ScreenLine - ScreenLines);
11178 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
11179 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
11180
11181 vim_free(wp->w_winbar_items);
11182 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
11183 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011184 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011185
11186 /* TODO: use fewer spaces if there is not enough room */
11187 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020011188 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011189 {
11190 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011191 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011192 break;
11193 if (col > 1)
11194 {
11195 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011196 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011197 break;
11198 }
11199
11200 wp->w_winbar_items[item_idx].wb_startcol = col;
11201 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011202 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011203 break;
11204
11205 next_col = text_to_screenline(wp, menu->name, col);
11206 while (col < next_col)
11207 {
11208 ScreenAttrs[off + col] = button_attr;
11209 ++col;
11210 }
11211 wp->w_winbar_items[item_idx].wb_endcol = col;
11212 wp->w_winbar_items[item_idx].wb_menu = menu;
11213 ++item_idx;
11214
Bram Moolenaar02631462017-09-22 15:20:32 +020011215 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011216 break;
11217 space_to_screenline(off + col, button_attr);
11218 ++col;
11219 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011220 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011221 {
11222 space_to_screenline(off + col, fill_attr);
11223 ++col;
11224 }
11225 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11226
Bram Moolenaar02631462017-09-22 15:20:32 +020011227 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020011228 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011229}
11230#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011231
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232/*
11233 * Show current status info in ruler and various other places
11234 * If always is FALSE, only show ruler if position has changed.
11235 */
11236 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011237showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238{
11239 if (!always && !redrawing())
11240 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011241#ifdef FEAT_INS_EXPAND
11242 if (pum_visible())
11243 {
11244 /* Don't redraw right now, do it later. */
11245 curwin->w_redr_status = TRUE;
11246 return;
11247 }
11248#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011249#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011250 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000011251 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 else
11253#endif
11254#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011255 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256#endif
11257
11258#ifdef FEAT_TITLE
11259 if (need_maketitle
11260# ifdef FEAT_STL_OPT
11261 || (p_icon && (stl_syntax & STL_IN_ICON))
11262 || (p_title && (stl_syntax & STL_IN_TITLE))
11263# endif
11264 )
11265 maketitle();
11266#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011267 /* Redraw the tab pages line if needed. */
11268 if (redraw_tabline)
11269 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270}
11271
11272#ifdef FEAT_CMDL_INFO
11273 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011274win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011276#define RULER_BUF_LEN 70
11277 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278 int row;
11279 int fillchar;
11280 int attr;
11281 int empty_line = FALSE;
11282 colnr_T virtcol;
11283 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011284 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 int this_ru_col;
11287 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010011288 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289
11290 /* If 'ruler' off or redrawing disabled, don't do anything */
11291 if (!p_ru)
11292 return;
11293
11294 /*
11295 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11296 * after deleting lines, before cursor.lnum is corrected.
11297 */
11298 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11299 return;
11300
11301#ifdef FEAT_INS_EXPAND
11302 /* Don't draw the ruler while doing insert-completion, it might overwrite
11303 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 if (edit_submode != NULL)
11306 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011307 // Don't draw the ruler when the popup menu is visible, it may overlap.
11308 // Except when the popup menu will be redrawn anyway.
11309 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011310 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311#endif
11312
11313#ifdef FEAT_STL_OPT
11314 if (*p_ruf)
11315 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011316 int save_called_emsg = called_emsg;
11317
11318 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011320 if (called_emsg)
11321 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011322 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011323 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324 return;
11325 }
11326#endif
11327
11328 /*
11329 * Check if not in Insert mode and the line is empty (will show "0-1").
11330 */
11331 if (!(State & INSERT)
11332 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11333 empty_line = TRUE;
11334
11335 /*
11336 * Only draw the ruler when something changed.
11337 */
11338 validate_virtcol_win(wp);
11339 if ( redraw_cmdline
11340 || always
11341 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11342 || wp->w_cursor.col != wp->w_ru_cursor.col
11343 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 || wp->w_topline != wp->w_ru_topline
11346 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11347#ifdef FEAT_DIFF
11348 || wp->w_topfill != wp->w_ru_topfill
11349#endif
11350 || empty_line != wp->w_ru_empty)
11351 {
11352 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 if (wp->w_status_height)
11354 {
11355 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011356 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011357 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011358 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359 }
11360 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 {
11362 row = Rows - 1;
11363 fillchar = ' ';
11364 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 width = Columns;
11366 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 }
11368
11369 /* In list mode virtcol needs to be recomputed */
11370 virtcol = wp->w_virtcol;
11371 if (wp->w_p_list && lcs_tab1 == NUL)
11372 {
11373 wp->w_p_list = FALSE;
11374 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11375 wp->w_p_list = TRUE;
11376 }
11377
11378 /*
11379 * Some sprintfs return the length, some return a pointer.
11380 * To avoid portability problems we use strlen() here.
11381 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011382 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11384 ? 0L
11385 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011386 len = STRLEN(buffer);
11387 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11389 (int)virtcol + 1);
11390
11391 /*
11392 * Add a "50%" if there is room for it.
11393 * On the last line, don't print in the last column (scrolls the
11394 * screen up on some terminals).
11395 */
11396 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011397 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401 this_ru_col = ru_col - (Columns - width);
11402 if (this_ru_col < 0)
11403 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404 /* Never use more than half the window/screen width, leave the other
11405 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011406 if (this_ru_col < (width + 1) / 2)
11407 this_ru_col = (width + 1) / 2;
11408 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011410 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011411 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 if (has_mbyte)
11414 i += (*mb_char2bytes)(fillchar, buffer + i);
11415 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416 buffer[i++] = fillchar;
11417 ++o;
11418 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011419 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420 }
11421 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 if (has_mbyte)
11423 {
11424 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011425 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426 {
11427 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011428 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 {
11430 buffer[i] = NUL;
11431 break;
11432 }
11433 }
11434 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011435 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011436 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437
Bram Moolenaar4033c552017-09-16 20:54:51 +020011438 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 i = redraw_cmdline;
11440 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011441 this_ru_col + off + (int)STRLEN(buffer),
11442 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 fillchar, fillchar, attr);
11444 /* don't redraw the cmdline because of showing the ruler */
11445 redraw_cmdline = i;
11446 wp->w_ru_cursor = wp->w_cursor;
11447 wp->w_ru_virtcol = wp->w_virtcol;
11448 wp->w_ru_empty = empty_line;
11449 wp->w_ru_topline = wp->w_topline;
11450 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11451#ifdef FEAT_DIFF
11452 wp->w_ru_topfill = wp->w_topfill;
11453#endif
11454 }
11455}
11456#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011457
11458#if defined(FEAT_LINEBREAK) || defined(PROTO)
11459/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011460 * Return the width of the 'number' and 'relativenumber' column.
11461 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011462 * Otherwise it depends on 'numberwidth' and the line count.
11463 */
11464 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011465number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011466{
11467 int n;
11468 linenr_T lnum;
11469
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011470 if (wp->w_p_rnu && !wp->w_p_nu)
11471 /* cursor line shows "0" */
11472 lnum = wp->w_height;
11473 else
11474 /* cursor line shows absolute line number */
11475 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011476
Bram Moolenaar6b314672015-03-20 15:42:10 +010011477 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011478 return wp->w_nrwidth_width;
11479 wp->w_nrwidth_line_count = lnum;
11480
11481 n = 0;
11482 do
11483 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011484 lnum /= 10;
11485 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011486 } while (lnum > 0);
11487
11488 /* 'numberwidth' gives the minimal width plus one */
11489 if (n < wp->w_p_nuw - 1)
11490 n = wp->w_p_nuw - 1;
11491
11492 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011493 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011494 return n;
11495}
11496#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011497
Bram Moolenaar113e1072019-01-20 15:30:40 +010011498#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011499/*
11500 * Return the current cursor column. This is the actual position on the
11501 * screen. First column is 0.
11502 */
11503 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011504screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011505{
11506 return screen_cur_col;
11507}
11508
11509/*
11510 * Return the current cursor row. This is the actual position on the screen.
11511 * First row is 0.
11512 */
11513 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011514screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011515{
11516 return screen_cur_row;
11517}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011518#endif