blob: bd76f7ba5355f12b7a7142c984278b9a1c9c8e25 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200125static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100126static void win_draw_end(win_T *wp, int c1, int c2, int draw_margin, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
129static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
130static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200132static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000134#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200138# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void start_search_hl(void);
140static void end_search_hl(void);
141static void init_search_hl(win_T *wp);
142static void prepare_search_hl(win_T *wp, linenr_T lnum);
143static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
144static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static void screen_char(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200149static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200151static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void win_rest_invalid(win_T *wp);
153static void msg_pos_mode(void);
154static void recording_mode(int attr);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200155static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200157#ifdef FEAT_MENU
158static void redraw_win_toolbar(win_T *wp);
159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100161static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
163#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200164static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167/* Ugly global: overrule attribute used by screen_char() */
168static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200170#ifdef FEAT_RIGHTLEFT
171# define HAS_RIGHTLEFT(x) x
172#else
173# define HAS_RIGHTLEFT(x) FALSE
174#endif
175
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200176// flags for screen_line()
177#define SLF_RIGHTLEFT 1
178#define SLF_POPUP 2
179
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180/*
181 * Redraw the current window later, with update_screen(type).
182 * Set must_redraw only if not already set to a higher value.
Bram Moolenaarae654382019-01-17 21:09:05 +0100183 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 */
185 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100186redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187{
188 redraw_win_later(curwin, type);
189}
190
191 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100192redraw_win_later(
193 win_T *wp,
194 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200196 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 {
198 wp->w_redr_type = type;
199 if (type >= NOT_VALID)
200 wp->w_lines_valid = 0;
201 if (must_redraw < type) /* must_redraw is the maximum of all windows */
202 must_redraw = type;
203 }
204}
205
206/*
207 * Force a complete redraw later. Also resets the highlighting. To be used
208 * after executing a shell command that messes up the screen.
209 */
210 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100211redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212{
213 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000214#ifdef FEAT_GUI
215 if (gui.in_use)
216 /* Use a code that will reset gui.highlight_mask in
217 * gui_stop_highlight(). */
218 screen_attr = HL_ALL + 1;
219 else
220#endif
221 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200222 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223}
224
225/*
226 * Mark all windows to be redrawn later.
227 */
228 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100229redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230{
231 win_T *wp;
232
233 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 redraw_win_later(wp, type);
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100235 // This may be needed when switching tabs.
236 if (must_redraw < type)
237 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238}
239
240/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000241 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 */
243 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100244redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245{
246 redraw_buf_later(curbuf, type);
247}
248
249 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100250redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251{
252 win_T *wp;
253
254 FOR_ALL_WINDOWS(wp)
255 {
256 if (wp->w_buffer == buf)
257 redraw_win_later(wp, type);
258 }
259}
260
Bram Moolenaar113e1072019-01-20 15:30:40 +0100261#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200262 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100263redraw_buf_line_later(buf_T *buf, linenr_T lnum)
264{
265 win_T *wp;
266
267 FOR_ALL_WINDOWS(wp)
268 if (wp->w_buffer == buf && lnum >= wp->w_topline
269 && lnum < wp->w_botline)
270 redrawWinline(wp, lnum);
271}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100272#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100273
Bram Moolenaar113e1072019-01-20 15:30:40 +0100274#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100275 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200276redraw_buf_and_status_later(buf_T *buf, int type)
277{
278 win_T *wp;
279
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200280#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200281 if (wild_menu_showing != 0)
282 /* Don't redraw while the command line completion is displayed, it
283 * would disappear. */
284 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200285#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200286 FOR_ALL_WINDOWS(wp)
287 {
288 if (wp->w_buffer == buf)
289 {
290 redraw_win_later(wp, type);
291 wp->w_redr_status = TRUE;
292 }
293 }
294}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100295#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200296
Bram Moolenaar113e1072019-01-20 15:30:40 +0100297#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299 * Redraw as soon as possible. When the command line is not scrolled redraw
300 * right away and restore what was on the command line.
301 * Return a code indicating what happened.
302 */
303 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100304redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200305{
306 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200307 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308 int r;
309 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200310 schar_T *screenline; /* copy from ScreenLines[] */
311 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200313 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200315 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316
317 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200318 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200319 return ret;
320
321 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200322 rows = screen_Rows - cmdline_row;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200323 screenline = LALLOC_MULT(schar_T, rows * cols);
324 screenattr = LALLOC_MULT(sattr_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200325 if (screenline == NULL || screenattr == NULL)
326 ret = 2;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200327 if (enc_utf8)
328 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200329 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200330 if (screenlineUC == NULL)
331 ret = 2;
332 for (i = 0; i < p_mco; ++i)
333 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200334 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200335 if (screenlineC[i] == NULL)
336 ret = 2;
337 }
338 }
339 if (enc_dbcs == DBCS_JPNU)
340 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200341 screenline2 = LALLOC_MULT(schar_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200342 if (screenline2 == NULL)
343 ret = 2;
344 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200345
346 if (ret != 2)
347 {
348 /* Save the text displayed in the command line area. */
349 for (r = 0; r < rows; ++r)
350 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200351 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200352 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200353 (size_t)cols * sizeof(schar_T));
354 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200355 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 if (enc_utf8)
358 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200359 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200360 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200362 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 mch_memmove(screenlineC[i] + r * cols,
364 ScreenLinesC[i] + LineOffset[cmdline_row + r],
365 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366 }
367 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200368 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200369 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200370 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200371 }
372
373 update_screen(0);
374 ret = 3;
375
376 if (must_redraw == 0)
377 {
378 int off = (int)(current_ScreenLine - ScreenLines);
379
380 /* Restore the text displayed in the command line area. */
381 for (r = 0; r < rows; ++r)
382 {
383 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200384 screenline + r * cols,
385 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200386 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200387 screenattr + r * cols,
388 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200389 if (enc_utf8)
390 {
391 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200392 screenlineUC + r * cols,
393 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200394 for (i = 0; i < p_mco; ++i)
395 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200396 screenlineC[i] + r * cols,
397 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200398 }
399 if (enc_dbcs == DBCS_JPNU)
400 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200401 screenline2 + r * cols,
402 (size_t)cols * sizeof(schar_T));
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200403 screen_line(cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200404 }
405 ret = 4;
406 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200407 }
408
409 vim_free(screenline);
410 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200411 if (enc_utf8)
412 {
413 vim_free(screenlineUC);
414 for (i = 0; i < p_mco; ++i)
415 vim_free(screenlineC[i]);
416 }
417 if (enc_dbcs == DBCS_JPNU)
418 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200419
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200420 /* Show the intro message when appropriate. */
421 maybe_intro_message();
422
423 setcursor();
424
Bram Moolenaar2951b772013-07-03 12:45:31 +0200425 return ret;
426}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100427#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200428
429/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100430 * Invoked after an asynchronous callback is called.
431 * If an echo command was used the cursor needs to be put back where
432 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200433 * If "call_update_screen" is FALSE don't call update_screen() when at the
434 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100435 */
436 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200437redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100438{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200439 ++redrawing_for_callback;
440
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100441 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200442 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100443 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200444 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200445 // Don't redraw when in prompt_for_number().
446 if (cmdline_row > 0)
447 {
448 // Redrawing only works when the screen didn't scroll. Don't clear
449 // wildmenu entries.
450 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200451#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200452 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200453#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200454 && call_update_screen)
455 update_screen(0);
456
457 // Redraw in the same position, so that the user can continue
458 // editing the command.
459 redrawcmdline_ex(FALSE);
460 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200461 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200462 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100463 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200464 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200465 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100466 setcursor();
467 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100468 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100469#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100470 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200471 // Don't update the cursor when it is blinking and off to avoid
472 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100473 out_flush_cursor(FALSE, FALSE);
474 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100475#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100476 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200477
478 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100479}
480
481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 * Changed something in the current window, at buffer line "lnum", that
483 * requires that line and possibly other lines to be redrawn.
484 * Used when entering/leaving Insert mode with the cursor on a folded line.
485 * Used to remove the "$" from a change command.
486 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
487 * may become invalid and the whole window will have to be redrawn.
488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100490redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200491 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100492 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200494 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
495 wp->w_redraw_top = lnum;
496 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
497 wp->w_redraw_bot = lnum;
498 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499}
500
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200501/*
502 * To be called when "updating_screen" was set before and now the postponed
503 * side effects may take place.
504 */
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200505 void
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200506after_updating_screen(int may_resize_shell UNUSED)
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200507{
508 updating_screen = FALSE;
509#ifdef FEAT_GUI
510 if (may_resize_shell)
511 gui_may_resize_shell();
512#endif
513#ifdef FEAT_TERMINAL
514 term_check_channel_closed_recently();
515#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200516
517#ifdef HAVE_DROP_FILE
518 // If handle_drop() was called while updating_screen was TRUE need to
519 // handle the drop now.
520 handle_any_postponed_drop();
521#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200522}
523
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200525 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 */
527 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100528update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529{
530 redraw_curbuf_later(type);
531 update_screen(type);
532}
533
534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 * Based on the current value of curwin->w_topline, transfer a screenfull
536 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200537 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200539 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200540update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200542 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 win_T *wp;
544 static int did_intro = FALSE;
545#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
546 int did_one;
547#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200548#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200549 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200550 int gui_cursor_col = 0;
551 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200552#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200553 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000555 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200557 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200559 if (type == VALID_NO_UPDATE)
560 {
561 no_update = TRUE;
562 type = 0;
563 }
564
Bram Moolenaara3347722019-05-11 21:14:24 +0200565#ifdef FEAT_EVAL
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200566 {
567 buf_T *buf;
568
569 // Before updating the screen, notify any listeners of changed text.
570 FOR_ALL_BUFFERS(buf)
571 invoke_listeners(buf);
572 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200573#endif
574
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (must_redraw)
576 {
577 if (type < must_redraw) /* use maximal type */
578 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000579
580 /* must_redraw is reset here, so that when we run into some weird
581 * reason to redraw while busy redrawing (e.g., asynchronous
582 * scrolling), or update_topline() in win_update() will cause a
583 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 must_redraw = 0;
585 }
586
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200587 /* May need to update w_lines[]. */
588 if (curwin->w_lines_valid == 0 && type < NOT_VALID
589#ifdef FEAT_TERMINAL
590 && !term_do_update_window(curwin)
591#endif
592 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 type = NOT_VALID;
594
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000595 /* Postpone the redrawing when it's not needed and when being called
596 * recursively. */
597 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 {
599 redraw_later(type); /* remember type for next time */
600 must_redraw = type;
601 if (type > INVERTED_ALL)
602 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200603 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +0200605 updating_screen = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200606
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200607#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200608 // Update popup_mask if needed. This may set w_redraw_top and w_redraw_bot
609 // in some windows.
610 may_update_popup_mask(type);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613#ifdef FEAT_SYN_HL
614 ++display_tick; /* let syntax code know we're in a next round of
615 * display updating */
616#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200617 if (no_update)
618 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619
620 /*
621 * if the screen was scrolled up when displaying a message, scroll it down
622 */
623 if (msg_scrolled)
624 {
625 clear_cmdline = TRUE;
626 if (msg_scrolled > Rows - 5) /* clearing is faster */
627 type = CLEAR;
628 else if (type != CLEAR)
629 {
630 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200631 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
632 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 type = CLEAR;
634 FOR_ALL_WINDOWS(wp)
635 {
Bram Moolenaar98fb65c2019-06-02 20:33:32 +0200636 if (wp->w_winrow < msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 {
638 if (W_WINROW(wp) + wp->w_height > msg_scrolled
639 && wp->w_redr_type < REDRAW_TOP
640 && wp->w_lines_valid > 0
641 && wp->w_topline == wp->w_lines[0].wl_lnum)
642 {
643 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
644 wp->w_redr_type = REDRAW_TOP;
645 }
646 else
647 {
648 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200649 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
650 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 }
653 }
654 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200655 if (!no_update)
656 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000657 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 }
659 msg_scrolled = 0;
660 need_wait_return = FALSE;
661 }
662
663 /* reset cmdline_row now (may have been changed temporarily) */
664 compute_cmdrow();
665
666 /* Check for changed highlighting */
667 if (need_highlight_changed)
668 highlight_changed();
669
670 if (type == CLEAR) /* first clear screen */
671 {
672 screenclear(); /* will reset clear_cmdline */
673 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200674 /* must_redraw may be set indirectly, avoid another redraw later */
675 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 }
677
678 if (clear_cmdline) /* going to clear cmdline (done below) */
679 check_for_delay(FALSE);
680
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000681#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200682 /* Force redraw when width of 'number' or 'relativenumber' column
683 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000684 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200685 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
686 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000687 curwin->w_redr_type = NOT_VALID;
688#endif
689
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 /*
691 * Only start redrawing if there is really something to do.
692 */
693 if (type == INVERTED)
694 update_curswant();
695 if (curwin->w_redr_type < type
696 && !((type == VALID
697 && curwin->w_lines[0].wl_valid
698#ifdef FEAT_DIFF
699 && curwin->w_topfill == curwin->w_old_topfill
700 && curwin->w_botfill == curwin->w_old_botfill
701#endif
702 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000704 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
706 && curwin->w_old_visual_mode == VIsual_mode
707 && (curwin->w_valid & VALID_VIRTCOL)
708 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 ))
710 curwin->w_redr_type = type;
711
Bram Moolenaar5a305422006-04-28 22:38:25 +0000712 /* Redraw the tab pages line if needed. */
713 if (redraw_tabline || type >= NOT_VALID)
714 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000715
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716#ifdef FEAT_SYN_HL
717 /*
718 * Correct stored syntax highlighting info for changes in each displayed
719 * buffer. Each buffer must only be done once.
720 */
721 FOR_ALL_WINDOWS(wp)
722 {
723 if (wp->w_buffer->b_mod_set)
724 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 win_T *wwp;
726
727 /* Check if we already did this buffer. */
728 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
729 if (wwp->w_buffer == wp->w_buffer)
730 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200731 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 syn_stack_apply_changes(wp->w_buffer);
733 }
734 }
735#endif
736
737 /*
738 * Go from top to bottom through the windows, redrawing the ones that need
739 * it.
740 */
741#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
742 did_one = FALSE;
743#endif
744#ifdef FEAT_SEARCH_EXTRA
745 search_hl.rm.regprog = NULL;
746#endif
747 FOR_ALL_WINDOWS(wp)
748 {
749 if (wp->w_redr_type != 0)
750 {
751 cursor_off();
752#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
753 if (!did_one)
754 {
755 did_one = TRUE;
756# ifdef FEAT_SEARCH_EXTRA
757 start_search_hl();
758# endif
759# ifdef FEAT_CLIPBOARD
760 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200761 if (clip_star.available && clip_isautosel_star())
762 clip_update_selection(&clip_star);
763 if (clip_plus.available && clip_isautosel_plus())
764 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765# endif
766#ifdef FEAT_GUI
767 /* Remove the cursor before starting to do anything, because
768 * scrolling may make it difficult to redraw the text under
769 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200770 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200771 {
772 gui_cursor_col = gui.cursor_col;
773 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200775 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#endif
778 }
779#endif
780 win_update(wp);
781 }
782
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 /* redraw status line after the window to minimize cursor movement */
784 if (wp->w_redr_status)
785 {
786 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200787 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 }
790#if defined(FEAT_SEARCH_EXTRA)
791 end_search_hl();
792#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100793#ifdef FEAT_INS_EXPAND
794 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200795 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 /* Reset b_mod_set flags. Going through all windows is probably faster
799 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200800 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200803 after_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
805 /* Clear or redraw the command line. Done last, because scrolling may
806 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200807 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 showmode();
809
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200810 if (no_update)
811 --no_win_do_lines_ins;
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200814 if (!did_intro)
815 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 did_intro = TRUE;
817
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200818#ifdef FEAT_TEXT_PROP
Bram Moolenaar988c4332019-06-02 14:12:11 +0200819 // Display popup windows on top of the windows.
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200820 update_popups(win_update);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200821#endif
822
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823#ifdef FEAT_GUI
824 /* Redraw the cursor and update the scrollbars when all screen updating is
825 * done. */
826 if (gui.in_use)
827 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200828 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200829 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100830 mch_disable_flush();
831 out_flush(); /* required before updating the cursor */
832 mch_enable_flush();
833
Bram Moolenaar144445d2016-07-08 21:41:54 +0200834 /* Put the GUI position where the cursor was, gui_update_cursor()
835 * uses that. */
836 gui.col = gui_cursor_col;
837 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200838 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100840 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200841 screen_cur_col = gui.col;
842 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200843 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100844 else
845 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 gui_update_scrollbars(FALSE);
847 }
848#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200849 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850}
851
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100852#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100853/*
854 * Prepare for updating one or more windows.
855 * Caller must check for "updating_screen" already set to avoid recursiveness.
856 */
857 static void
858update_prepare(void)
859{
860 cursor_off();
861 updating_screen = TRUE;
862#ifdef FEAT_GUI
863 /* Remove the cursor before starting to do anything, because scrolling may
864 * make it difficult to redraw the text under it. */
865 if (gui.in_use)
866 gui_undraw_cursor();
867#endif
868#ifdef FEAT_SEARCH_EXTRA
869 start_search_hl();
870#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +0200871#ifdef FEAT_TEXT_PROP
872 // Update popup_mask if needed.
Bram Moolenaar68acb412019-06-26 03:40:36 +0200873 may_update_popup_mask(must_redraw);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200874#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100875}
876
877/*
878 * Finish updating one or more windows.
879 */
880 static void
881update_finish(void)
882{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200883 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100884 showmode();
885
886# ifdef FEAT_SEARCH_EXTRA
887 end_search_hl();
888# endif
889
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200890 after_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100891
892# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100893 /* Redraw the cursor and update the scrollbars when all screen updating is
894 * done. */
895 if (gui.in_use)
896 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100897 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100898 gui_update_scrollbars(FALSE);
899 }
900# endif
901}
902#endif
903
Bram Moolenaar860cae12010-06-05 23:22:07 +0200904#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200905/*
906 * Return TRUE if the cursor line in window "wp" may be concealed, according
907 * to the 'concealcursor' option.
908 */
909 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100910conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200911{
912 int c;
913
914 if (*wp->w_p_cocu == NUL)
915 return FALSE;
916 if (get_real_state() & VISUAL)
917 c = 'v';
918 else if (State & INSERT)
919 c = 'i';
920 else if (State & NORMAL)
921 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200922 else if (State & CMDLINE)
923 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200924 else
925 return FALSE;
926 return vim_strchr(wp->w_p_cocu, c) != NULL;
927}
928
929/*
930 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
931 */
932 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200933conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200934{
935 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
936 {
937 need_cursor_line_redraw = TRUE;
938 /* Need to recompute cursor column, e.g., when starting Visual mode
939 * without concealing. */
940 curs_columns(TRUE);
941 }
942}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943#endif
944
Bram Moolenaar113e1072019-01-20 15:30:40 +0100945#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100947update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948{
949 win_T *wp;
950 int doit = FALSE;
951
952# ifdef FEAT_FOLDING
953 win_foldinfo.fi_level = 0;
954# endif
955
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100956 // update/delete a specific sign
957 redraw_buf_line_later(buf, lnum);
958
959 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 if (wp->w_redr_type != 0)
962 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100964 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200965 * happening (recursive call), messages on the screen or still starting up.
966 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100967 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200968 || State == ASKMORE || State == HITRETURN
969 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100970#ifdef FEAT_GUI
971 || gui.starting
972#endif
973 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 return;
975
976 /* update all windows that need updating */
977 update_prepare();
978
Bram Moolenaar29323592016-07-24 22:04:11 +0200979 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 {
981 if (wp->w_redr_type != 0)
982 win_update(wp);
983 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200984 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
987 update_finish();
988}
989#endif
990
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200991/*
992 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
993 * window then get the "Pmenu" highlight attribute.
994 */
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200995 int
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200996get_wcr_attr(win_T *wp)
997{
998 int wcr_attr = 0;
999
1000 if (*wp->w_p_wcr != NUL)
1001 wcr_attr = syn_name2attr(wp->w_p_wcr);
1002#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001003 if (WIN_IS_POPUP(wp) && wcr_attr == 0)
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001004 wcr_attr = HL_ATTR(HLF_PNI);
1005#endif
1006 return wcr_attr;
1007}
1008
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009#if defined(FEAT_GUI) || defined(PROTO)
1010/*
1011 * Update a single window, its status line and maybe the command line msg.
1012 * Used for the GUI scrollbar.
1013 */
1014 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001015updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001017 /* return if already busy updating */
1018 if (updating_screen)
1019 return;
1020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 update_prepare();
1022
1023#ifdef FEAT_CLIPBOARD
1024 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001025 if (clip_star.available && clip_isautosel_star())
1026 clip_update_selection(&clip_star);
1027 if (clip_plus.available && clip_isautosel_plus())
1028 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001030
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001032
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001033 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001034 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001035 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001036
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 if (wp->w_redr_status
1038# ifdef FEAT_CMDL_INFO
1039 || p_ru
1040# endif
1041# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001042 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043# endif
1044 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001045 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046
Bram Moolenaar988c4332019-06-02 14:12:11 +02001047#ifdef FEAT_TEXT_PROP
1048 // Display popup windows on top of everything.
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001049 update_popups(win_update);
Bram Moolenaar988c4332019-06-02 14:12:11 +02001050#endif
1051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 update_finish();
1053}
1054#endif
1055
1056/*
1057 * Update a single window.
1058 *
1059 * This may cause the windows below it also to be redrawn (when clearing the
1060 * screen or scrolling lines).
1061 *
1062 * How the window is redrawn depends on wp->w_redr_type. Each type also
1063 * implies the one below it.
1064 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001065 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1067 * INVERTED redraw the changed part of the Visual area
1068 * INVERTED_ALL redraw the whole Visual area
1069 * VALID 1. scroll up/down to adjust for a changed w_topline
1070 * 2. update lines at the top when scrolled down
1071 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001072 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 * b_mod_top and b_mod_bot.
1074 * - if wp->w_redraw_top non-zero, redraw lines between
1075 * wp->w_redraw_top and wp->w_redr_bot.
1076 * - continue redrawing when syntax status is invalid.
1077 * 4. if scrolled up, update lines at the bottom.
1078 * This results in three areas that may need updating:
1079 * top: from first row to top_end (when scrolled down)
1080 * mid: from mid_start to mid_end (update inversion or changed text)
1081 * bot: from bot_start to last row (when scrolled up)
1082 */
1083 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001084win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085{
1086 buf_T *buf = wp->w_buffer;
1087 int type;
1088 int top_end = 0; /* Below last row of the top area that needs
1089 updating. 0 when no top area updating. */
1090 int mid_start = 999;/* first row of the mid area that needs
1091 updating. 999 when no mid area updating. */
1092 int mid_end = 0; /* Below last row of the mid area that needs
1093 updating. 0 when no mid area updating. */
1094 int bot_start = 999;/* first row of the bot area that needs
1095 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 int scrolled_down = FALSE; /* TRUE when scrolled down when
1097 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001099 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 int top_to_mod = FALSE; /* redraw above mod_top */
1101#endif
1102
1103 int row; /* current window row to display */
1104 linenr_T lnum; /* current buffer lnum to display */
1105 int idx; /* current index in w_lines[] */
1106 int srow; /* starting row of the current line */
1107
1108 int eof = FALSE; /* if TRUE, we hit the end of the file */
1109 int didline = FALSE; /* if TRUE, we finished the last line */
1110 int i;
1111 long j;
1112 static int recursive = FALSE; /* being called recursively */
1113 int old_botline = wp->w_botline;
1114#ifdef FEAT_FOLDING
1115 long fold_count;
1116#endif
1117#ifdef FEAT_SYN_HL
1118 /* remember what happened to the previous line, to know if
1119 * check_visual_highlight() can be used */
1120#define DID_NONE 1 /* didn't update a line */
1121#define DID_LINE 2 /* updated a normal line */
1122#define DID_FOLD 3 /* updated a folded line */
1123 int did_update = DID_NONE;
1124 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1125#endif
1126 linenr_T mod_top = 0;
1127 linenr_T mod_bot = 0;
1128#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1129 int save_got_int;
1130#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001131#ifdef SYN_TIME_LIMIT
1132 proftime_T syntax_tm;
1133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134
1135 type = wp->w_redr_type;
1136
1137 if (type == NOT_VALID)
1138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 wp->w_lines_valid = 0;
1141 }
1142
1143 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001144 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
1146 wp->w_redr_type = 0;
1147 return;
1148 }
1149
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 /* Window is zero-width: Only need to draw the separator. */
1151 if (wp->w_width == 0)
1152 {
1153 /* draw the vertical separator right of this window */
1154 draw_vsep_win(wp, 0);
1155 wp->w_redr_type = 0;
1156 return;
1157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001159#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001160 // If this window contains a terminal, redraw works completely differently.
1161 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001162 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001163 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001164# ifdef FEAT_MENU
1165 /* Draw the window toolbar, if there is one. */
1166 if (winbar_height(wp) > 0)
1167 redraw_win_toolbar(wp);
1168# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001169 wp->w_redr_type = 0;
1170 return;
1171 }
1172#endif
1173
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001175 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176#endif
1177
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001178#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001179 /* Force redraw when width of 'number' or 'relativenumber' column
1180 * changes. */
1181 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001182 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001183 {
1184 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001185 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001186 }
1187 else
1188#endif
1189
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1191 {
1192 /*
1193 * When there are both inserted/deleted lines and specific lines to be
1194 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1195 * everything (only happens when redrawing is off for while).
1196 */
1197 type = NOT_VALID;
1198 }
1199 else
1200 {
1201 /*
1202 * Set mod_top to the first line that needs displaying because of
1203 * changes. Set mod_bot to the first line after the changes.
1204 */
1205 mod_top = wp->w_redraw_top;
1206 if (wp->w_redraw_bot != 0)
1207 mod_bot = wp->w_redraw_bot + 1;
1208 else
1209 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 if (buf->b_mod_set)
1211 {
1212 if (mod_top == 0 || mod_top > buf->b_mod_top)
1213 {
1214 mod_top = buf->b_mod_top;
1215#ifdef FEAT_SYN_HL
1216 /* Need to redraw lines above the change that may be included
1217 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001218 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001220 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (mod_top < 1)
1222 mod_top = 1;
1223 }
1224#endif
1225 }
1226 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1227 mod_bot = buf->b_mod_bot;
1228
1229#ifdef FEAT_SEARCH_EXTRA
1230 /* When 'hlsearch' is on and using a multi-line search pattern, a
1231 * change in one line may make the Search highlighting in a
1232 * previous line invalid. Simple solution: redraw all visible
1233 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001234 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001236 if (search_hl.rm.regprog != NULL
1237 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001239 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001240 {
1241 cur = wp->w_match_head;
1242 while (cur != NULL)
1243 {
1244 if (cur->match.regprog != NULL
1245 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001246 {
1247 top_to_mod = TRUE;
1248 break;
1249 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001250 cur = cur->next;
1251 }
1252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253#endif
1254 }
1255#ifdef FEAT_FOLDING
1256 if (mod_top != 0 && hasAnyFolding(wp))
1257 {
1258 linenr_T lnumt, lnumb;
1259
1260 /*
1261 * A change in a line can cause lines above it to become folded or
1262 * unfolded. Find the top most buffer line that may be affected.
1263 * If the line was previously folded and displayed, get the first
1264 * line of that fold. If the line is folded now, get the first
1265 * folded line. Use the minimum of these two.
1266 */
1267
1268 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1269 * the line below it. If there is no valid entry, use w_topline.
1270 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1271 * to this line. If there is no valid entry, use MAXLNUM. */
1272 lnumt = wp->w_topline;
1273 lnumb = MAXLNUM;
1274 for (i = 0; i < wp->w_lines_valid; ++i)
1275 if (wp->w_lines[i].wl_valid)
1276 {
1277 if (wp->w_lines[i].wl_lastlnum < mod_top)
1278 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1279 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1280 {
1281 lnumb = wp->w_lines[i].wl_lnum;
1282 /* When there is a fold column it might need updating
1283 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001284 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 ++lnumb;
1286 }
1287 }
1288
1289 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1290 if (mod_top > lnumt)
1291 mod_top = lnumt;
1292
1293 /* Now do the same for the bottom line (one above mod_bot). */
1294 --mod_bot;
1295 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1296 ++mod_bot;
1297 if (mod_bot < lnumb)
1298 mod_bot = lnumb;
1299 }
1300#endif
1301
1302 /* When a change starts above w_topline and the end is below
1303 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001304 * If the end of the change is above w_topline: do like no change was
1305 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 if (mod_top != 0 && mod_top < wp->w_topline)
1307 {
1308 if (mod_bot > wp->w_topline)
1309 mod_top = wp->w_topline;
1310#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001311 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 top_end = 1;
1313#endif
1314 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001315
1316 /* When line numbers are displayed need to redraw all lines below
1317 * inserted/deleted lines. */
1318 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1319 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001321 wp->w_redraw_top = 0; // reset for next time
1322 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
1324 /*
1325 * When only displaying the lines at the top, set top_end. Used when
1326 * window has scrolled down for msg_scrolled.
1327 */
1328 if (type == REDRAW_TOP)
1329 {
1330 j = 0;
1331 for (i = 0; i < wp->w_lines_valid; ++i)
1332 {
1333 j += wp->w_lines[i].wl_size;
1334 if (j >= wp->w_upd_rows)
1335 {
1336 top_end = j;
1337 break;
1338 }
1339 }
1340 if (top_end == 0)
1341 /* not found (cannot happen?): redraw everything */
1342 type = NOT_VALID;
1343 else
1344 /* top area defined, the rest is VALID */
1345 type = VALID;
1346 }
1347
Bram Moolenaar367329b2007-08-30 11:53:22 +00001348 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001349 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1350 * non-zero and thus not FALSE) will indicate that screenclear() was not
1351 * called. */
1352 if (screen_cleared)
1353 screen_cleared = MAYBE;
1354
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 /*
1356 * If there are no changes on the screen that require a complete redraw,
1357 * handle three cases:
1358 * 1: we are off the top of the screen by a few lines: scroll down
1359 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1360 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1361 * w_lines[] that needs updating.
1362 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001363 if ((type == VALID || type == SOME_VALID
1364 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365#ifdef FEAT_DIFF
1366 && !wp->w_botfill && !wp->w_old_botfill
1367#endif
1368 )
1369 {
1370 if (mod_top != 0 && wp->w_topline == mod_top)
1371 {
1372 /*
1373 * w_topline is the first changed line, the scrolling will be done
1374 * further down.
1375 */
1376 }
1377 else if (wp->w_lines[0].wl_valid
1378 && (wp->w_topline < wp->w_lines[0].wl_lnum
1379#ifdef FEAT_DIFF
1380 || (wp->w_topline == wp->w_lines[0].wl_lnum
1381 && wp->w_topfill > wp->w_old_topfill)
1382#endif
1383 ))
1384 {
1385 /*
1386 * New topline is above old topline: May scroll down.
1387 */
1388#ifdef FEAT_FOLDING
1389 if (hasAnyFolding(wp))
1390 {
1391 linenr_T ln;
1392
1393 /* count the number of lines we are off, counting a sequence
1394 * of folded lines as one */
1395 j = 0;
1396 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1397 {
1398 ++j;
1399 if (j >= wp->w_height - 2)
1400 break;
1401 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1402 }
1403 }
1404 else
1405#endif
1406 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1407 if (j < wp->w_height - 2) /* not too far off */
1408 {
1409 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1410#ifdef FEAT_DIFF
1411 /* insert extra lines for previously invisible filler lines */
1412 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1413 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1414 - wp->w_old_topfill;
1415#endif
1416 if (i < wp->w_height - 2) /* less than a screen off */
1417 {
1418 /*
1419 * Try to insert the correct number of lines.
1420 * If not the last window, delete the lines at the bottom.
1421 * win_ins_lines may fail when the terminal can't do it.
1422 */
1423 if (i > 0)
1424 check_for_delay(FALSE);
1425 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1426 {
1427 if (wp->w_lines_valid != 0)
1428 {
1429 /* Need to update rows that are new, stop at the
1430 * first one that scrolled down. */
1431 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433
1434 /* Move the entries that were scrolled, disable
1435 * the entries for the lines to be redrawn. */
1436 if ((wp->w_lines_valid += j) > wp->w_height)
1437 wp->w_lines_valid = wp->w_height;
1438 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1439 wp->w_lines[idx] = wp->w_lines[idx - j];
1440 while (idx >= 0)
1441 wp->w_lines[idx--].wl_valid = FALSE;
1442 }
1443 }
1444 else
1445 mid_start = 0; /* redraw all lines */
1446 }
1447 else
1448 mid_start = 0; /* redraw all lines */
1449 }
1450 else
1451 mid_start = 0; /* redraw all lines */
1452 }
1453 else
1454 {
1455 /*
1456 * New topline is at or below old topline: May scroll up.
1457 * When topline didn't change, find first entry in w_lines[] that
1458 * needs updating.
1459 */
1460
1461 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1462 j = -1;
1463 row = 0;
1464 for (i = 0; i < wp->w_lines_valid; i++)
1465 {
1466 if (wp->w_lines[i].wl_valid
1467 && wp->w_lines[i].wl_lnum == wp->w_topline)
1468 {
1469 j = i;
1470 break;
1471 }
1472 row += wp->w_lines[i].wl_size;
1473 }
1474 if (j == -1)
1475 {
1476 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1477 * lines */
1478 mid_start = 0;
1479 }
1480 else
1481 {
1482 /*
1483 * Try to delete the correct number of lines.
1484 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1485 */
1486#ifdef FEAT_DIFF
1487 /* If the topline didn't change, delete old filler lines,
1488 * otherwise delete filler lines of the new topline... */
1489 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1490 row += wp->w_old_topfill;
1491 else
1492 row += diff_check_fill(wp, wp->w_topline);
1493 /* ... but don't delete new filler lines. */
1494 row -= wp->w_topfill;
1495#endif
1496 if (row > 0)
1497 {
1498 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001499 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1500 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 bot_start = wp->w_height - row;
1502 else
1503 mid_start = 0; /* redraw all lines */
1504 }
1505 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1506 {
1507 /*
1508 * Skip the lines (below the deleted lines) that are still
1509 * valid and don't need redrawing. Copy their info
1510 * upwards, to compensate for the deleted lines. Set
1511 * bot_start to the first row that needs redrawing.
1512 */
1513 bot_start = 0;
1514 idx = 0;
1515 for (;;)
1516 {
1517 wp->w_lines[idx] = wp->w_lines[j];
1518 /* stop at line that didn't fit, unless it is still
1519 * valid (no lines deleted) */
1520 if (row > 0 && bot_start + row
1521 + (int)wp->w_lines[j].wl_size > wp->w_height)
1522 {
1523 wp->w_lines_valid = idx + 1;
1524 break;
1525 }
1526 bot_start += wp->w_lines[idx++].wl_size;
1527
1528 /* stop at the last valid entry in w_lines[].wl_size */
1529 if (++j >= wp->w_lines_valid)
1530 {
1531 wp->w_lines_valid = idx;
1532 break;
1533 }
1534 }
1535#ifdef FEAT_DIFF
1536 /* Correct the first entry for filler lines at the top
1537 * when it won't get updated below. */
1538 if (wp->w_p_diff && bot_start > 0)
1539 wp->w_lines[0].wl_size =
1540 plines_win_nofill(wp, wp->w_topline, TRUE)
1541 + wp->w_topfill;
1542#endif
1543 }
1544 }
1545 }
1546
1547 /* When starting redraw in the first line, redraw all lines. When
1548 * there is only one window it's probably faster to clear the screen
1549 * first. */
1550 if (mid_start == 0)
1551 {
1552 mid_end = wp->w_height;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001553 if (ONE_WINDOW && !WIN_IS_POPUP(wp))
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001554 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001555 /* Clear the screen when it was not done by win_del_lines() or
1556 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1557 * then. */
1558 if (screen_cleared != TRUE)
1559 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001560 /* The screen was cleared, redraw the tab pages line. */
1561 if (redraw_tabline)
1562 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001565
1566 /* When win_del_lines() or win_ins_lines() caused the screen to be
1567 * cleared (only happens for the first window) or when screenclear()
1568 * was called directly above, "must_redraw" will have been set to
1569 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1570 if (screen_cleared == TRUE)
1571 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 }
1573 else
1574 {
1575 /* Not VALID or INVERTED: redraw all lines. */
1576 mid_start = 0;
1577 mid_end = wp->w_height;
1578 }
1579
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001580 if (type == SOME_VALID)
1581 {
1582 /* SOME_VALID: redraw all lines. */
1583 mid_start = 0;
1584 mid_end = wp->w_height;
1585 type = NOT_VALID;
1586 }
1587
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 /* check if we are updating or removing the inverted part */
1589 if ((VIsual_active && buf == curwin->w_buffer)
1590 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1591 {
1592 linenr_T from, to;
1593
1594 if (VIsual_active)
1595 {
1596 if (VIsual_active
1597 && (VIsual_mode != wp->w_old_visual_mode
1598 || type == INVERTED_ALL))
1599 {
1600 /*
1601 * If the type of Visual selection changed, redraw the whole
1602 * selection. Also when the ownership of the X selection is
1603 * gained or lost.
1604 */
1605 if (curwin->w_cursor.lnum < VIsual.lnum)
1606 {
1607 from = curwin->w_cursor.lnum;
1608 to = VIsual.lnum;
1609 }
1610 else
1611 {
1612 from = VIsual.lnum;
1613 to = curwin->w_cursor.lnum;
1614 }
1615 /* redraw more when the cursor moved as well */
1616 if (wp->w_old_cursor_lnum < from)
1617 from = wp->w_old_cursor_lnum;
1618 if (wp->w_old_cursor_lnum > to)
1619 to = wp->w_old_cursor_lnum;
1620 if (wp->w_old_visual_lnum < from)
1621 from = wp->w_old_visual_lnum;
1622 if (wp->w_old_visual_lnum > to)
1623 to = wp->w_old_visual_lnum;
1624 }
1625 else
1626 {
1627 /*
1628 * Find the line numbers that need to be updated: The lines
1629 * between the old cursor position and the current cursor
1630 * position. Also check if the Visual position changed.
1631 */
1632 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1633 {
1634 from = curwin->w_cursor.lnum;
1635 to = wp->w_old_cursor_lnum;
1636 }
1637 else
1638 {
1639 from = wp->w_old_cursor_lnum;
1640 to = curwin->w_cursor.lnum;
1641 if (from == 0) /* Visual mode just started */
1642 from = to;
1643 }
1644
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001645 if (VIsual.lnum != wp->w_old_visual_lnum
1646 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 {
1648 if (wp->w_old_visual_lnum < from
1649 && wp->w_old_visual_lnum != 0)
1650 from = wp->w_old_visual_lnum;
1651 if (wp->w_old_visual_lnum > to)
1652 to = wp->w_old_visual_lnum;
1653 if (VIsual.lnum < from)
1654 from = VIsual.lnum;
1655 if (VIsual.lnum > to)
1656 to = VIsual.lnum;
1657 }
1658 }
1659
1660 /*
1661 * If in block mode and changed column or curwin->w_curswant:
1662 * update all lines.
1663 * First compute the actual start and end column.
1664 */
1665 if (VIsual_mode == Ctrl_V)
1666 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001667 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001668#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001669 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
Bram Moolenaar404406a2014-10-09 13:24:43 +02001671 if (curwin->w_p_lbr)
1672 ve_flags = VE_ALL;
1673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001675#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001676 ve_flags = save_ve_flags;
1677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 ++toc;
1679 if (curwin->w_curswant == MAXCOL)
1680 toc = MAXCOL;
1681
1682 if (fromc != wp->w_old_cursor_fcol
1683 || toc != wp->w_old_cursor_lcol)
1684 {
1685 if (from > VIsual.lnum)
1686 from = VIsual.lnum;
1687 if (to < VIsual.lnum)
1688 to = VIsual.lnum;
1689 }
1690 wp->w_old_cursor_fcol = fromc;
1691 wp->w_old_cursor_lcol = toc;
1692 }
1693 }
1694 else
1695 {
1696 /* Use the line numbers of the old Visual area. */
1697 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1698 {
1699 from = wp->w_old_cursor_lnum;
1700 to = wp->w_old_visual_lnum;
1701 }
1702 else
1703 {
1704 from = wp->w_old_visual_lnum;
1705 to = wp->w_old_cursor_lnum;
1706 }
1707 }
1708
1709 /*
1710 * There is no need to update lines above the top of the window.
1711 */
1712 if (from < wp->w_topline)
1713 from = wp->w_topline;
1714
1715 /*
1716 * If we know the value of w_botline, use it to restrict the update to
1717 * the lines that are visible in the window.
1718 */
1719 if (wp->w_valid & VALID_BOTLINE)
1720 {
1721 if (from >= wp->w_botline)
1722 from = wp->w_botline - 1;
1723 if (to >= wp->w_botline)
1724 to = wp->w_botline - 1;
1725 }
1726
1727 /*
1728 * Find the minimal part to be updated.
1729 * Watch out for scrolling that made entries in w_lines[] invalid.
1730 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1731 * top_end; need to redraw from top_end to the "to" line.
1732 * A middle mouse click with a Visual selection may change the text
1733 * above the Visual area and reset wl_valid, do count these for
1734 * mid_end (in srow).
1735 */
1736 if (mid_start > 0)
1737 {
1738 lnum = wp->w_topline;
1739 idx = 0;
1740 srow = 0;
1741 if (scrolled_down)
1742 mid_start = top_end;
1743 else
1744 mid_start = 0;
1745 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1746 {
1747 if (wp->w_lines[idx].wl_valid)
1748 mid_start += wp->w_lines[idx].wl_size;
1749 else if (!scrolled_down)
1750 srow += wp->w_lines[idx].wl_size;
1751 ++idx;
1752# ifdef FEAT_FOLDING
1753 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1754 lnum = wp->w_lines[idx].wl_lnum;
1755 else
1756# endif
1757 ++lnum;
1758 }
1759 srow += mid_start;
1760 mid_end = wp->w_height;
1761 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1762 {
1763 if (wp->w_lines[idx].wl_valid
1764 && wp->w_lines[idx].wl_lnum >= to + 1)
1765 {
1766 /* Only update until first row of this line */
1767 mid_end = srow;
1768 break;
1769 }
1770 srow += wp->w_lines[idx].wl_size;
1771 }
1772 }
1773 }
1774
1775 if (VIsual_active && buf == curwin->w_buffer)
1776 {
1777 wp->w_old_visual_mode = VIsual_mode;
1778 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1779 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001780 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 wp->w_old_curswant = curwin->w_curswant;
1782 }
1783 else
1784 {
1785 wp->w_old_visual_mode = 0;
1786 wp->w_old_cursor_lnum = 0;
1787 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001788 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790
1791#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1792 /* reset got_int, otherwise regexp won't work */
1793 save_got_int = got_int;
1794 got_int = 0;
1795#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001796#ifdef SYN_TIME_LIMIT
1797 /* Set the time limit to 'redrawtime'. */
1798 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001799 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801#ifdef FEAT_FOLDING
1802 win_foldinfo.fi_level = 0;
1803#endif
1804
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001805#ifdef FEAT_MENU
1806 /*
1807 * Draw the window toolbar, if there is one.
1808 * TODO: only when needed.
1809 */
1810 if (winbar_height(wp) > 0)
1811 redraw_win_toolbar(wp);
1812#endif
1813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 /*
1815 * Update all the window rows.
1816 */
1817 idx = 0; /* first entry in w_lines[].wl_size */
1818 row = 0;
1819 srow = 0;
1820 lnum = wp->w_topline; /* first line shown in window */
1821 for (;;)
1822 {
1823 /* stop updating when reached the end of the window (check for _past_
1824 * the end of the window is at the end of the loop) */
1825 if (row == wp->w_height)
1826 {
1827 didline = TRUE;
1828 break;
1829 }
1830
1831 /* stop updating when hit the end of the file */
1832 if (lnum > buf->b_ml.ml_line_count)
1833 {
1834 eof = TRUE;
1835 break;
1836 }
1837
1838 /* Remember the starting row of the line that is going to be dealt
1839 * with. It is used further down when the line doesn't fit. */
1840 srow = row;
1841
1842 /*
1843 * Update a line when it is in an area that needs updating, when it
1844 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001845 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 * win_del_lines(), check if the current line includes it.
1847 * When syntax folding is being used, the saved syntax states will
1848 * already have been updated, we can't see where the syntax state is
1849 * the same again, just update until the end of the window.
1850 */
1851 if (row < top_end
1852 || (row >= mid_start && row < mid_end)
1853#ifdef FEAT_SEARCH_EXTRA
1854 || top_to_mod
1855#endif
1856 || idx >= wp->w_lines_valid
1857 || (row + wp->w_lines[idx].wl_size > bot_start)
1858 || (mod_top != 0
1859 && (lnum == mod_top
1860 || (lnum >= mod_top
1861 && (lnum < mod_bot
1862#ifdef FEAT_SYN_HL
1863 || did_update == DID_FOLD
1864 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001865 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 && (
1867# ifdef FEAT_FOLDING
1868 (foldmethodIsSyntax(wp)
1869 && hasAnyFolding(wp)) ||
1870# endif
1871 syntax_check_changed(lnum)))
1872#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001873#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001874 /* match in fixed position might need redraw
1875 * if lines were inserted or deleted */
1876 || (wp->w_match_head != NULL
1877 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 )))))
1880 {
1881#ifdef FEAT_SEARCH_EXTRA
1882 if (lnum == mod_top)
1883 top_to_mod = FALSE;
1884#endif
1885
1886 /*
1887 * When at start of changed lines: May scroll following lines
1888 * up or down to minimize redrawing.
1889 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001890 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 */
1892 if (lnum == mod_top
1893 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001894 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 {
1896 int old_rows = 0;
1897 int new_rows = 0;
1898 int xtra_rows;
1899 linenr_T l;
1900
1901 /* Count the old number of window rows, using w_lines[], which
1902 * should still contain the sizes for the lines as they are
1903 * currently displayed. */
1904 for (i = idx; i < wp->w_lines_valid; ++i)
1905 {
1906 /* Only valid lines have a meaningful wl_lnum. Invalid
1907 * lines are part of the changed area. */
1908 if (wp->w_lines[i].wl_valid
1909 && wp->w_lines[i].wl_lnum == mod_bot)
1910 break;
1911 old_rows += wp->w_lines[i].wl_size;
1912#ifdef FEAT_FOLDING
1913 if (wp->w_lines[i].wl_valid
1914 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1915 {
1916 /* Must have found the last valid entry above mod_bot.
1917 * Add following invalid entries. */
1918 ++i;
1919 while (i < wp->w_lines_valid
1920 && !wp->w_lines[i].wl_valid)
1921 old_rows += wp->w_lines[i++].wl_size;
1922 break;
1923 }
1924#endif
1925 }
1926
1927 if (i >= wp->w_lines_valid)
1928 {
1929 /* We can't find a valid line below the changed lines,
1930 * need to redraw until the end of the window.
1931 * Inserting/deleting lines has no use. */
1932 bot_start = 0;
1933 }
1934 else
1935 {
1936 /* Able to count old number of rows: Count new window
1937 * rows, and may insert/delete lines */
1938 j = idx;
1939 for (l = lnum; l < mod_bot; ++l)
1940 {
1941#ifdef FEAT_FOLDING
1942 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1943 ++new_rows;
1944 else
1945#endif
1946#ifdef FEAT_DIFF
1947 if (l == wp->w_topline)
1948 new_rows += plines_win_nofill(wp, l, TRUE)
1949 + wp->w_topfill;
1950 else
1951#endif
1952 new_rows += plines_win(wp, l, TRUE);
1953 ++j;
1954 if (new_rows > wp->w_height - row - 2)
1955 {
1956 /* it's getting too much, must redraw the rest */
1957 new_rows = 9999;
1958 break;
1959 }
1960 }
1961 xtra_rows = new_rows - old_rows;
1962 if (xtra_rows < 0)
1963 {
1964 /* May scroll text up. If there is not enough
1965 * remaining text or scrolling fails, must redraw the
1966 * rest. If scrolling works, must redraw the text
1967 * below the scrolled text. */
1968 if (row - xtra_rows >= wp->w_height - 2)
1969 mod_bot = MAXLNUM;
1970 else
1971 {
1972 check_for_delay(FALSE);
1973 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001974 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 mod_bot = MAXLNUM;
1976 else
1977 bot_start = wp->w_height + xtra_rows;
1978 }
1979 }
1980 else if (xtra_rows > 0)
1981 {
1982 /* May scroll text down. If there is not enough
1983 * remaining text of scrolling fails, must redraw the
1984 * rest. */
1985 if (row + xtra_rows >= wp->w_height - 2)
1986 mod_bot = MAXLNUM;
1987 else
1988 {
1989 check_for_delay(FALSE);
1990 if (win_ins_lines(wp, row + old_rows,
1991 xtra_rows, FALSE, FALSE) == FAIL)
1992 mod_bot = MAXLNUM;
1993 else if (top_end > row + old_rows)
1994 /* Scrolled the part at the top that requires
1995 * updating down. */
1996 top_end += xtra_rows;
1997 }
1998 }
1999
2000 /* When not updating the rest, may need to move w_lines[]
2001 * entries. */
2002 if (mod_bot != MAXLNUM && i != j)
2003 {
2004 if (j < i)
2005 {
2006 int x = row + new_rows;
2007
2008 /* move entries in w_lines[] upwards */
2009 for (;;)
2010 {
2011 /* stop at last valid entry in w_lines[] */
2012 if (i >= wp->w_lines_valid)
2013 {
2014 wp->w_lines_valid = j;
2015 break;
2016 }
2017 wp->w_lines[j] = wp->w_lines[i];
2018 /* stop at a line that won't fit */
2019 if (x + (int)wp->w_lines[j].wl_size
2020 > wp->w_height)
2021 {
2022 wp->w_lines_valid = j + 1;
2023 break;
2024 }
2025 x += wp->w_lines[j++].wl_size;
2026 ++i;
2027 }
2028 if (bot_start > x)
2029 bot_start = x;
2030 }
2031 else /* j > i */
2032 {
2033 /* move entries in w_lines[] downwards */
2034 j -= i;
2035 wp->w_lines_valid += j;
2036 if (wp->w_lines_valid > wp->w_height)
2037 wp->w_lines_valid = wp->w_height;
2038 for (i = wp->w_lines_valid; i - j >= idx; --i)
2039 wp->w_lines[i] = wp->w_lines[i - j];
2040
2041 /* The w_lines[] entries for inserted lines are
2042 * now invalid, but wl_size may be used above.
2043 * Reset to zero. */
2044 while (i >= idx)
2045 {
2046 wp->w_lines[i].wl_size = 0;
2047 wp->w_lines[i--].wl_valid = FALSE;
2048 }
2049 }
2050 }
2051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
2053
2054#ifdef FEAT_FOLDING
2055 /*
2056 * When lines are folded, display one line for all of them.
2057 * Otherwise, display normally (can be several display lines when
2058 * 'wrap' is on).
2059 */
2060 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2061 if (fold_count != 0)
2062 {
2063 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2064 ++row;
2065 --fold_count;
2066 wp->w_lines[idx].wl_folded = TRUE;
2067 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2068# ifdef FEAT_SYN_HL
2069 did_update = DID_FOLD;
2070# endif
2071 }
2072 else
2073#endif
2074 if (idx < wp->w_lines_valid
2075 && wp->w_lines[idx].wl_valid
2076 && wp->w_lines[idx].wl_lnum == lnum
2077 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002078 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002079 && !WIN_IS_POPUP(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 && srow + wp->w_lines[idx].wl_size > wp->w_height
2081#ifdef FEAT_DIFF
2082 && diff_check_fill(wp, lnum) == 0
2083#endif
2084 )
2085 {
2086 /* This line is not going to fit. Don't draw anything here,
2087 * will draw "@ " lines below. */
2088 row = wp->w_height + 1;
2089 }
2090 else
2091 {
2092#ifdef FEAT_SEARCH_EXTRA
2093 prepare_search_hl(wp, lnum);
2094#endif
2095#ifdef FEAT_SYN_HL
2096 /* Let the syntax stuff know we skipped a few lines. */
2097 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002098 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 syntax_end_parsing(syntax_last_parsed + 1);
2100#endif
2101
2102 /*
2103 * Display one line.
2104 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002105 row = win_line(wp, lnum, srow, wp->w_height,
2106 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
2108#ifdef FEAT_FOLDING
2109 wp->w_lines[idx].wl_folded = FALSE;
2110 wp->w_lines[idx].wl_lastlnum = lnum;
2111#endif
2112#ifdef FEAT_SYN_HL
2113 did_update = DID_LINE;
2114 syntax_last_parsed = lnum;
2115#endif
2116 }
2117
2118 wp->w_lines[idx].wl_lnum = lnum;
2119 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002120
2121 /* Past end of the window or end of the screen. Note that after
2122 * resizing wp->w_height may be end up too big. That's a problem
2123 * elsewhere, but prevent a crash here. */
2124 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
2126 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002127 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2129 ++idx;
2130 break;
2131 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002132 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 wp->w_lines[idx].wl_size = row - srow;
2134 ++idx;
2135#ifdef FEAT_FOLDING
2136 lnum += fold_count + 1;
2137#else
2138 ++lnum;
2139#endif
2140 }
2141 else
2142 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002143 if (wp->w_p_rnu)
2144 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002145#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002146 // 'relativenumber' set: The text doesn't need to be drawn, but
2147 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002148 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2149 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002150 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002151 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002152#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002153 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002154 }
2155
2156 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 row += wp->w_lines[idx++].wl_size;
2158 if (row > wp->w_height) /* past end of screen */
2159 break;
2160#ifdef FEAT_FOLDING
2161 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2162#else
2163 ++lnum;
2164#endif
2165#ifdef FEAT_SYN_HL
2166 did_update = DID_NONE;
2167#endif
2168 }
2169
2170 if (lnum > buf->b_ml.ml_line_count)
2171 {
2172 eof = TRUE;
2173 break;
2174 }
2175 }
2176 /*
2177 * End of loop over all window lines.
2178 */
2179
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002180#ifdef FEAT_VTP
2181 /* Rewrite the character at the end of the screen line. */
2182 if (use_vtp())
2183 {
2184 int i;
2185
2186 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002187 if (enc_utf8)
2188 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2189 LineOffset[i] + screen_Columns) > 1)
2190 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2191 else
2192 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2193 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002194 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2195 }
2196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
2198 if (idx > wp->w_lines_valid)
2199 wp->w_lines_valid = idx;
2200
2201#ifdef FEAT_SYN_HL
2202 /*
2203 * Let the syntax stuff know we stop parsing here.
2204 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002205 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 syntax_end_parsing(syntax_last_parsed + 1);
2207#endif
2208
2209 /*
2210 * If we didn't hit the end of the file, and we didn't finish the last
2211 * line we were working on, then the line didn't fit.
2212 */
2213 wp->w_empty_rows = 0;
2214#ifdef FEAT_DIFF
2215 wp->w_filler_rows = 0;
2216#endif
2217 if (!eof && !didline)
2218 {
2219 if (lnum == wp->w_topline)
2220 {
2221 /*
2222 * Single line that does not fit!
2223 * Don't overwrite it, it can be edited.
2224 */
2225 wp->w_botline = lnum + 1;
2226 }
2227#ifdef FEAT_DIFF
2228 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2229 {
2230 /* Window ends in filler lines. */
2231 wp->w_botline = lnum;
2232 wp->w_filler_rows = wp->w_height - srow;
2233 }
2234#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002235#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002236 else if (WIN_IS_POPUP(wp))
Bram Moolenaar17146962019-05-30 00:12:11 +02002237 {
2238 // popup line that doesn't fit is left as-is
2239 wp->w_botline = lnum;
2240 }
2241#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002242 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2243 {
2244 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2245
2246 /*
2247 * Last line isn't finished: Display "@@@" in the last screen line.
2248 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002249 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002250 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002251 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002252 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002253 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002254 set_empty_rows(wp, srow);
2255 wp->w_botline = lnum;
2256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2258 {
2259 /*
2260 * Last line isn't finished: Display "@@@" at the end.
2261 */
2262 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2263 W_WINROW(wp) + wp->w_height,
2264 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002265 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 set_empty_rows(wp, srow);
2267 wp->w_botline = lnum;
2268 }
2269 else
2270 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002271 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 wp->w_botline = lnum;
2273 }
2274 }
2275 else
2276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 if (eof) /* we hit the end of the file */
2279 {
2280 wp->w_botline = buf->b_ml.ml_line_count + 1;
2281#ifdef FEAT_DIFF
2282 j = diff_check_fill(wp, wp->w_botline);
2283 if (j > 0 && !wp->w_botfill)
2284 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002285 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 if (char2cells(fill_diff) > 1)
2287 i = '-';
2288 else
2289 i = fill_diff;
2290 if (row + j > wp->w_height)
2291 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002292 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 row += j;
2294 }
2295#endif
2296 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002297 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 wp->w_botline = lnum;
2299
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002300 // Make sure the rest of the screen is blank
2301 // put '~'s on rows that aren't part of the file.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002302 win_draw_end(wp, WIN_IS_POPUP(wp) ? ' ' : '~',
2303 ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 }
2305
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002306#ifdef SYN_TIME_LIMIT
2307 syn_set_timeout(NULL);
2308#endif
2309
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 /* Reset the type of redrawing required, the window has been updated. */
2311 wp->w_redr_type = 0;
2312#ifdef FEAT_DIFF
2313 wp->w_old_topfill = wp->w_topfill;
2314 wp->w_old_botfill = wp->w_botfill;
2315#endif
2316
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002317 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
2319 /*
2320 * There is a trick with w_botline. If we invalidate it on each
2321 * change that might modify it, this will cause a lot of expensive
2322 * calls to plines() in update_topline() each time. Therefore the
2323 * value of w_botline is often approximated, and this value is used to
2324 * compute the value of w_topline. If the value of w_botline was
2325 * wrong, check that the value of w_topline is correct (cursor is on
2326 * the visible part of the text). If it's not, we need to redraw
2327 * again. Mostly this just means scrolling up a few lines, so it
2328 * doesn't look too bad. Only do this for the current window (where
2329 * changes are relevant).
2330 */
2331 wp->w_valid |= VALID_BOTLINE;
2332 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2333 {
2334 recursive = TRUE;
2335 curwin->w_valid &= ~VALID_TOPLINE;
2336 update_topline(); /* may invalidate w_botline again */
2337 if (must_redraw != 0)
2338 {
2339 /* Don't update for changes in buffer again. */
2340 i = curbuf->b_mod_set;
2341 curbuf->b_mod_set = FALSE;
2342 win_update(curwin);
2343 must_redraw = 0;
2344 curbuf->b_mod_set = i;
2345 }
2346 recursive = FALSE;
2347 }
2348 }
2349
2350#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2351 /* restore got_int, unless CTRL-C was hit while redrawing */
2352 if (!got_int)
2353 got_int = save_got_int;
2354#endif
2355}
2356
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002358 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2359 * Return the new offset.
2360 */
2361 static int
2362screen_fill_end(
2363 win_T *wp,
2364 int c1,
2365 int c2,
2366 int off,
2367 int width,
2368 int row,
2369 int endrow,
2370 int attr)
2371{
2372 int nn = off + width;
2373
2374 if (nn > wp->w_width)
2375 nn = wp->w_width;
2376#ifdef FEAT_RIGHTLEFT
2377 if (wp->w_p_rl)
2378 {
2379 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2380 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2381 c1, c2, attr);
2382 }
2383 else
2384#endif
2385 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2386 wp->w_wincol + off, (int)wp->w_wincol + nn,
2387 c1, c2, attr);
2388 return nn;
2389}
2390
2391/*
2392 * Clear lines near the end the window and mark the unused lines with "c1".
2393 * use "c2" as the filler character.
2394 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 */
2396 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002397win_draw_end(
2398 win_T *wp,
2399 int c1,
2400 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002401 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002402 int row,
2403 int endrow,
2404 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002407 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002408 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002409
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002410 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002411
2412 if (draw_margin)
2413 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002414#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002415 int fdc = compute_foldcolumn(wp, 0);
2416
2417 if (fdc > 0)
2418 // draw the fold column
2419 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002420 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002421#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002422#ifdef FEAT_SIGNS
2423 if (signcolumn_on(wp))
2424 // draw the sign column
2425 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002426 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002427#endif
2428 if ((wp->w_p_nu || wp->w_p_rnu)
2429 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2430 // draw the number column
2431 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002432 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435#ifdef FEAT_RIGHTLEFT
2436 if (wp->w_p_rl)
2437 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002439 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002440 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002442 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002443 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 }
2445 else
2446#endif
2447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002449 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002450 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002452
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 set_empty_rows(wp, row);
2454}
2455
Bram Moolenaar1a384422010-07-14 19:53:30 +02002456#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002457/*
2458 * Advance **color_cols and return TRUE when there are columns to draw.
2459 */
2460 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002461advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002462{
2463 while (**color_cols >= 0 && vcol > **color_cols)
2464 ++*color_cols;
2465 return (**color_cols >= 0);
2466}
2467#endif
2468
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002469#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2470/*
2471 * Copy "text" to ScreenLines using "attr".
2472 * Returns the next screen column.
2473 */
2474 static int
2475text_to_screenline(win_T *wp, char_u *text, int col)
2476{
2477 int off = (int)(current_ScreenLine - ScreenLines);
2478
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002479 if (has_mbyte)
2480 {
2481 int cells;
2482 int u8c, u8cc[MAX_MCO];
2483 int i;
2484 int idx;
2485 int c_len;
2486 char_u *p;
2487# ifdef FEAT_ARABIC
2488 int prev_c = 0; /* previous Arabic character */
2489 int prev_c1 = 0; /* first composing char for prev_c */
2490# endif
2491
2492# ifdef FEAT_RIGHTLEFT
2493 if (wp->w_p_rl)
2494 idx = off;
2495 else
2496# endif
2497 idx = off + col;
2498
2499 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2500 for (p = text; *p != NUL; )
2501 {
2502 cells = (*mb_ptr2cells)(p);
2503 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002504 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002505# ifdef FEAT_RIGHTLEFT
2506 - (wp->w_p_rl ? col : 0)
2507# endif
2508 )
2509 break;
2510 ScreenLines[idx] = *p;
2511 if (enc_utf8)
2512 {
2513 u8c = utfc_ptr2char(p, u8cc);
2514 if (*p < 0x80 && u8cc[0] == 0)
2515 {
2516 ScreenLinesUC[idx] = 0;
2517#ifdef FEAT_ARABIC
2518 prev_c = u8c;
2519#endif
2520 }
2521 else
2522 {
2523#ifdef FEAT_ARABIC
2524 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2525 {
2526 /* Do Arabic shaping. */
2527 int pc, pc1, nc;
2528 int pcc[MAX_MCO];
2529 int firstbyte = *p;
2530
2531 /* The idea of what is the previous and next
2532 * character depends on 'rightleft'. */
2533 if (wp->w_p_rl)
2534 {
2535 pc = prev_c;
2536 pc1 = prev_c1;
2537 nc = utf_ptr2char(p + c_len);
2538 prev_c1 = u8cc[0];
2539 }
2540 else
2541 {
2542 pc = utfc_ptr2char(p + c_len, pcc);
2543 nc = prev_c;
2544 pc1 = pcc[0];
2545 }
2546 prev_c = u8c;
2547
2548 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2549 pc, pc1, nc);
2550 ScreenLines[idx] = firstbyte;
2551 }
2552 else
2553 prev_c = u8c;
2554#endif
2555 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002556 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002557 for (i = 0; i < Screen_mco; ++i)
2558 {
2559 ScreenLinesC[i][idx] = u8cc[i];
2560 if (u8cc[i] == 0)
2561 break;
2562 }
2563 }
2564 if (cells > 1)
2565 ScreenLines[idx + 1] = 0;
2566 }
2567 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2568 /* double-byte single width character */
2569 ScreenLines2[idx] = p[1];
2570 else if (cells > 1)
2571 /* double-width character */
2572 ScreenLines[idx + 1] = p[1];
2573 col += cells;
2574 idx += cells;
2575 p += c_len;
2576 }
2577 }
2578 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002579 {
2580 int len = (int)STRLEN(text);
2581
Bram Moolenaar02631462017-09-22 15:20:32 +02002582 if (len > wp->w_width - col)
2583 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002584 if (len > 0)
2585 {
2586#ifdef FEAT_RIGHTLEFT
2587 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002588 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002589 else
2590#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002591 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002592 col += len;
2593 }
2594 }
2595 return col;
2596}
2597#endif
2598
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599#ifdef FEAT_FOLDING
2600/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002601 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2602 * space is available for window "wp", minus "col".
2603 */
2604 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002605compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002606{
2607 int fdc = wp->w_p_fdc;
2608 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002609 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002610
2611 if (fdc > wwidth - (col + wmw))
2612 fdc = wwidth - (col + wmw);
2613 return fdc;
2614}
2615
2616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 * Display one folded line.
2618 */
2619 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002620fold_line(
2621 win_T *wp,
2622 long fold_count,
2623 foldinfo_T *foldinfo,
2624 linenr_T lnum,
2625 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002627 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 pos_T *top, *bot;
2629 linenr_T lnume = lnum + fold_count - 1;
2630 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002631 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 int col;
2634 int txtcol;
2635 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002636 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
2638 /* Build the fold line:
2639 * 1. Add the cmdwin_type for the command-line window
2640 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002641 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 * 4. Compose the text
2643 * 5. Add the text
2644 * 6. set highlighting for the Visual area an other text
2645 */
2646 col = 0;
2647
2648 /*
2649 * 1. Add the cmdwin_type for the command-line window
2650 * Ignores 'rightleft', this window is never right-left.
2651 */
2652#ifdef FEAT_CMDWIN
2653 if (cmdwin_type != 0 && wp == curwin)
2654 {
2655 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002656 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 if (enc_utf8)
2658 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 ++col;
2660 }
2661#endif
2662
2663 /*
2664 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002665 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002667 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (fdc > 0)
2669 {
2670 fill_foldcolumn(buf, wp, TRUE, lnum);
2671#ifdef FEAT_RIGHTLEFT
2672 if (wp->w_p_rl)
2673 {
2674 int i;
2675
Bram Moolenaar02631462017-09-22 15:20:32 +02002676 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002677 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 /* reverse the fold column */
2679 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002680 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
2682 else
2683#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002684 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 col += fdc;
2686 }
2687
2688#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002689# define RL_MEMSET(p, v, l) \
2690 do { \
2691 if (wp->w_p_rl) \
2692 for (ri = 0; ri < l; ++ri) \
2693 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2694 else \
2695 for (ri = 0; ri < l; ++ri) \
2696 ScreenAttrs[off + (p) + ri] = v; \
2697 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002699# define RL_MEMSET(p, v, l) \
2700 do { \
2701 for (ri = 0; ri < l; ++ri) \
2702 ScreenAttrs[off + (p) + ri] = v; \
2703 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704#endif
2705
Bram Moolenaar64486672010-05-16 15:46:46 +02002706 /* Set all attributes of the 'number' or 'relativenumber' column and the
2707 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002708 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
2710#ifdef FEAT_SIGNS
2711 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002712 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002714 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 if (len > 0)
2716 {
2717 if (len > 2)
2718 len = 2;
2719# ifdef FEAT_RIGHTLEFT
2720 if (wp->w_p_rl)
2721 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002722 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002723 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 else
2725# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002726 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 col += len;
2728 }
2729 }
2730#endif
2731
2732 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002733 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002735 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002737 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 if (len > 0)
2739 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002740 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002741 long num;
2742 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002743
2744 if (len > w + 1)
2745 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002746
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002747 if (wp->w_p_nu && !wp->w_p_rnu)
2748 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002749 num = (long)lnum;
2750 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002751 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002752 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002753 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002754 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002755 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002756 /* 'number' + 'relativenumber': cursor line shows absolute
2757 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002758 num = lnum;
2759 fmt = "%-*ld ";
2760 }
2761 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002762
Bram Moolenaar700e7342013-01-30 12:31:36 +01002763 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764#ifdef FEAT_RIGHTLEFT
2765 if (wp->w_p_rl)
2766 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002767 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002768 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 else
2770#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002771 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 col += len;
2773 }
2774 }
2775
2776 /*
2777 * 4. Compose the folded-line string with 'foldtext', if set.
2778 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002779 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780
2781 txtcol = col; /* remember where text starts */
2782
2783 /*
2784 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2785 * Right-left text is put in columns 0 - number-col, normal text is put
2786 * in columns number-col - window-width.
2787 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002788 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789
2790 /* Fill the rest of the line with the fold filler */
2791#ifdef FEAT_RIGHTLEFT
2792 if (wp->w_p_rl)
2793 col -= txtcol;
2794#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002795 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796#ifdef FEAT_RIGHTLEFT
2797 - (wp->w_p_rl ? txtcol : 0)
2798#endif
2799 )
2800 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 if (enc_utf8)
2802 {
2803 if (fill_fold >= 0x80)
2804 {
2805 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002806 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002807 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 }
2809 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002812 ScreenLines[off + col] = fill_fold;
2813 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002814 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002816 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002817 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
2819
2820 if (text != buf)
2821 vim_free(text);
2822
2823 /*
2824 * 6. set highlighting for the Visual area an other text.
2825 * If all folded lines are in the Visual area, highlight the line.
2826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2828 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002829 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 {
2831 /* Visual is after curwin->w_cursor */
2832 top = &curwin->w_cursor;
2833 bot = &VIsual;
2834 }
2835 else
2836 {
2837 /* Visual is before curwin->w_cursor */
2838 top = &VIsual;
2839 bot = &curwin->w_cursor;
2840 }
2841 if (lnum >= top->lnum
2842 && lnume <= bot->lnum
2843 && (VIsual_mode != 'v'
2844 || ((lnum > top->lnum
2845 || (lnum == top->lnum
2846 && top->col == 0))
2847 && (lnume < bot->lnum
2848 || (lnume == bot->lnum
2849 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002850 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
2852 if (VIsual_mode == Ctrl_V)
2853 {
2854 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002855 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002857 if (wp->w_old_cursor_lcol != MAXCOL
2858 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002859 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 len = wp->w_old_cursor_lcol;
2861 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002862 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002863 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002864 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 }
2866 }
2867 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002868 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002870 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 }
2873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002875#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002876 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2877 if (wp->w_p_cc_cols)
2878 {
2879 int i = 0;
2880 int j = wp->w_p_cc_cols[i];
2881 int old_txtcol = txtcol;
2882
2883 while (j > -1)
2884 {
2885 txtcol += j;
2886 if (wp->w_p_wrap)
2887 txtcol -= wp->w_skipcol;
2888 else
2889 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002890 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002891 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002892 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002893 txtcol = old_txtcol;
2894 j = wp->w_p_cc_cols[++i];
2895 }
2896 }
2897
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002898 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002899 if (wp->w_p_cuc)
2900 {
2901 txtcol += wp->w_virtcol;
2902 if (wp->w_p_wrap)
2903 txtcol -= wp->w_skipcol;
2904 else
2905 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002906 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002907 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002908 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002909 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911
Bram Moolenaar02631462017-09-22 15:20:32 +02002912 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002913 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914
2915 /*
2916 * Update w_cline_height and w_cline_folded if the cursor line was
2917 * updated (saves a call to plines() later).
2918 */
2919 if (wp == curwin
2920 && lnum <= curwin->w_cursor.lnum
2921 && lnume >= curwin->w_cursor.lnum)
2922 {
2923 curwin->w_cline_row = row;
2924 curwin->w_cline_height = 1;
2925 curwin->w_cline_folded = TRUE;
2926 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2927 }
2928}
2929
2930/*
2931 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2932 */
2933 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002934copy_text_attr(
2935 int off,
2936 char_u *buf,
2937 int len,
2938 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002940 int i;
2941
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 if (enc_utf8)
2944 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002945 for (i = 0; i < len; ++i)
2946 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947}
2948
2949/*
2950 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002951 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 */
2953 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002954fill_foldcolumn(
2955 char_u *p,
2956 win_T *wp,
2957 int closed, /* TRUE of FALSE */
2958 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
2960 int i = 0;
2961 int level;
2962 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002963 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002964 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
2966 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002967 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
2969 level = win_foldinfo.fi_level;
2970 if (level > 0)
2971 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002972 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002973 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002974
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 /* If the column is too narrow, we start at the lowest level that
2976 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002977 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 if (first_level < 1)
2979 first_level = 1;
2980
Bram Moolenaar1c934292015-01-27 16:39:29 +01002981 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 {
2983 if (win_foldinfo.fi_lnum == lnum
2984 && first_level + i >= win_foldinfo.fi_low_level)
2985 p[i] = '-';
2986 else if (first_level == 1)
2987 p[i] = '|';
2988 else if (first_level + i <= 9)
2989 p[i] = '0' + first_level + i;
2990 else
2991 p[i] = '>';
2992 if (first_level + i == level)
2993 break;
2994 }
2995 }
2996 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002997 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998}
2999#endif /* FEAT_FOLDING */
3000
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003001#ifdef FEAT_TEXT_PROP
3002static textprop_T *current_text_props = NULL;
3003static buf_T *current_buf = NULL;
3004
3005 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003006text_prop_compare(const void *s1, const void *s2)
3007{
3008 int idx1, idx2;
3009 proptype_T *pt1, *pt2;
3010 colnr_T col1, col2;
3011
3012 idx1 = *(int *)s1;
3013 idx2 = *(int *)s2;
3014 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3015 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3016 if (pt1 == pt2)
3017 return 0;
3018 if (pt1 == NULL)
3019 return -1;
3020 if (pt2 == NULL)
3021 return 1;
3022 if (pt1->pt_priority != pt2->pt_priority)
3023 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3024 col1 = current_text_props[idx1].tp_col;
3025 col2 = current_text_props[idx2].tp_col;
3026 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3027}
3028#endif
3029
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003030#ifdef FEAT_SIGNS
3031/*
3032 * Get information needed to display the sign in line 'lnum' in window 'wp'.
3033 * If 'nrcol' is TRUE, the sign is going to be displayed in the number column.
3034 * Otherwise the sign is going to be displayed in the sign column.
3035 */
3036 static void
3037get_sign_display_info(
3038 int nrcol,
3039 win_T *wp,
Bram Moolenaar4e038572019-07-04 18:28:35 +02003040 linenr_T lnum UNUSED,
3041 sign_attrs_T *sattr,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003042 int wcr_attr,
3043 int row,
3044 int startrow,
Bram Moolenaarbf8c3ad2019-06-19 14:28:43 +02003045 int filler_lines UNUSED,
3046 int filler_todo UNUSED,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003047 int *c_extrap,
3048 int *c_finalp,
3049 char_u *extra,
3050 char_u **pp_extra,
3051 int *n_extrap,
3052 int *char_attrp)
3053{
3054 int text_sign;
3055# ifdef FEAT_SIGN_ICONS
3056 int icon_sign;
3057# endif
3058
3059 // Draw two cells with the sign value or blank.
3060 *c_extrap = ' ';
3061 *c_finalp = NUL;
3062 if (nrcol)
3063 *n_extrap = number_width(wp) + 1;
3064 else
3065 {
3066 *char_attrp = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
3067 *n_extrap = 2;
3068 }
3069
3070 if (row == startrow
3071#ifdef FEAT_DIFF
3072 + filler_lines && filler_todo <= 0
3073#endif
3074 )
3075 {
Bram Moolenaar4e038572019-07-04 18:28:35 +02003076 text_sign = (sattr->text != NULL) ? sattr->typenr : 0;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003077# ifdef FEAT_SIGN_ICONS
Bram Moolenaar4e038572019-07-04 18:28:35 +02003078 icon_sign = (sattr->icon != NULL) ? sattr->typenr : 0;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003079 if (gui.in_use && icon_sign != 0)
3080 {
3081 // Use the image in this position.
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003082 if (nrcol)
3083 {
3084 *c_extrap = NUL;
3085 sprintf((char *)extra, "%-*c ", number_width(wp), SIGN_BYTE);
3086 *pp_extra = extra;
3087 *n_extrap = (int)STRLEN(*pp_extra);
3088 }
3089 else
3090 *c_extrap = SIGN_BYTE;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003091# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4e038572019-07-04 18:28:35 +02003092 if (netbeans_active() && (buf_signcount(wp->w_buffer, lnum) > 1))
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003093 {
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003094 if (nrcol)
3095 {
3096 *c_extrap = NUL;
3097 sprintf((char *)extra, "%-*c ", number_width(wp),
3098 MULTISIGN_BYTE);
3099 *pp_extra = extra;
3100 *n_extrap = (int)STRLEN(*pp_extra);
3101 }
3102 else
3103 *c_extrap = MULTISIGN_BYTE;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003104 }
3105# endif
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003106 *c_finalp = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003107 *char_attrp = icon_sign;
3108 }
3109 else
3110# endif
3111 if (text_sign != 0)
3112 {
Bram Moolenaar4e038572019-07-04 18:28:35 +02003113 *pp_extra = sattr->text;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003114 if (*pp_extra != NULL)
3115 {
3116 if (nrcol)
3117 {
Bram Moolenaard6bcff42019-07-18 12:48:16 +02003118 int n, width = number_width(wp) - 2;
3119
3120 for (n = 0; n < width; n++)
3121 extra[n] = ' ';
3122 extra[n] = 0;
3123 STRCAT(extra, *pp_extra);
3124 STRCAT(extra, " ");
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003125 *pp_extra = extra;
3126 }
3127 *c_extrap = NUL;
3128 *c_finalp = NUL;
3129 *n_extrap = (int)STRLEN(*pp_extra);
3130 }
Bram Moolenaar4e038572019-07-04 18:28:35 +02003131 *char_attrp = sattr->texthl;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003132 }
3133 }
3134}
3135#endif
3136
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137/*
3138 * Display line "lnum" of window 'wp' on the screen.
3139 * Start at row "startrow", stop when "endrow" is reached.
3140 * wp->w_virtcol needs to be valid.
3141 *
3142 * Return the number of last row the line occupies.
3143 */
3144 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003145win_line(
3146 win_T *wp,
3147 linenr_T lnum,
3148 int startrow,
3149 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003150 int nochange UNUSED, // not updating for changed text
3151 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003153 int col = 0; // visual column on screen
3154 unsigned off; // offset in ScreenLines/ScreenAttrs
3155 int c = 0; // init for GCC
3156 long vcol = 0; // virtual column (for tabs)
Bram Moolenaard574ea22015-01-14 19:35:14 +01003157#ifdef FEAT_LINEBREAK
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003158 long vcol_sbr = -1; // virtual column after showbreak
Bram Moolenaard574ea22015-01-14 19:35:14 +01003159#endif
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003160 long vcol_prev = -1; // "vcol" of previous character
3161 char_u *line; // current line
3162 char_u *ptr; // current position in "line"
3163 int row; // row in the window, excl w_winrow
3164 int screen_row; // row on the screen, incl w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003166 char_u extra[21]; // "%ld " and 'fdc' must fit in here
3167 int n_extra = 0; // number of extra chars
3168 char_u *p_extra = NULL; // string of extra chars, plus NUL
3169 char_u *p_extra_free = NULL; // p_extra needs to be freed
3170 int c_extra = NUL; // extra chars, all the same
3171 int c_final = NUL; // final char, mandatory if set
3172 int extra_attr = 0; // attributes when n_extra != 0
3173 static char_u *at_end_str = (char_u *)""; // used for p_extra when
3174 // displaying lcs_eol at end-of-line
3175 int lcs_eol_one = lcs_eol; // lcs_eol until it's been used
3176 int lcs_prec_todo = lcs_prec; // lcs_prec until it's been used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003178 // saved "extra" items for when draw_state becomes WL_LINE (again)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 int saved_n_extra = 0;
3180 char_u *saved_p_extra = NULL;
3181 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003182 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 int saved_char_attr = 0;
3184
3185 int n_attr = 0; /* chars with special attr */
3186 int saved_attr2 = 0; /* char_attr saved for n_attr */
3187 int n_attr3 = 0; /* chars with overruling special attr */
3188 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3189
3190 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3191
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003192 int fromcol = -10; // start of inverting
3193 int tocol = MAXCOL; // end of inverting
3194 int fromcol_prev = -2; // start of inverting after cursor
3195 int noinvcur = FALSE; // don't invert the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003197 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 pos_T pos;
3199 long v;
3200
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003201 int char_attr = 0; // attributes for next character
3202 int attr_pri = FALSE; // char_attr has priority
3203 int area_highlighting = FALSE; // Visual or incsearch highlighting
3204 // in this line
3205 int vi_attr = 0; // attributes for Visual and incsearch
3206 // highlighting
3207 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003208 int win_attr = 0; // background for whole window, except
3209 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003210 int area_attr = 0; // attributes desired by highlighting
3211 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003213 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 int syntax_attr = 0; /* attributes desired by syntax */
3215 int has_syntax = FALSE; /* this buffer has syntax highl. */
3216 int save_did_emsg;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003217 int draw_color_col = FALSE; /* highlight colorcolumn */
3218 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003219#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02003220 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003221#ifdef FEAT_TEXT_PROP
3222 int text_prop_count;
3223 int text_prop_next = 0; // next text property to use
3224 textprop_T *text_props = NULL;
3225 int *text_prop_idxs = NULL;
3226 int text_props_active = 0;
3227 proptype_T *text_prop_type = NULL;
3228 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003229 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003230#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003231#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003232 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003233# define SPWORDLEN 150
3234 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003235 int nextlinecol = 0; /* column where nextline[] starts */
3236 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003237 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003238 int spell_attr = 0; /* attributes desired by spelling */
3239 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003240 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3241 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003242 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003243 static int cap_col = -1; /* column to check for Cap word */
3244 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003245 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003247 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 int multi_attr = 0; /* attributes desired by multibyte */
3249 int mb_l = 1; /* multi-byte byte length */
3250 int mb_c = 0; /* decoded multi-byte character */
3251 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003252 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaare9726e32019-06-19 18:01:21 +02003253#if defined(FEAT_DIFF) || defined(FEAT_SIGNS)
Bram Moolenaarbf8c3ad2019-06-19 14:28:43 +02003254 int filler_lines = 0; /* nr of filler lines to be drawn */
3255 int filler_todo = 0; /* nr of filler lines still to do + 1 */
Bram Moolenaare9726e32019-06-19 18:01:21 +02003256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257#ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003258 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 int change_start = MAXCOL; /* first col of changed area */
3260 int change_end = -1; /* last col of changed area */
3261#endif
3262 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3263#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003264 int need_showbreak = FALSE; /* overlong line, skipping first x
3265 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003267#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003268 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003270 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271#endif
Bram Moolenaarb4d9b892019-07-04 22:59:06 +02003272#ifdef FEAT_SIGNS
3273 int sign_present = FALSE;
3274 sign_attrs_T sattr;
3275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003277 matchitem_T *cur; /* points to the match list */
3278 match_T *shl; /* points to search_hl or a match */
3279 int shl_flag; /* flag to indicate whether search_hl
3280 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003281 int pos_inprogress; /* marks that position match search is
3282 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003283 int prevcol_hl_flag; /* flag to indicate whether prevcol
3284 equals startcol of search_hl or one
3285 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286#endif
3287#ifdef FEAT_ARABIC
3288 int prev_c = 0; /* previous Arabic character */
3289 int prev_c1 = 0; /* first composing char for prev_c */
3290#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003291#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003292 int did_line_attr = 0;
3293#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003294#ifdef FEAT_TERMINAL
3295 int get_term_attr = FALSE;
3296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297
3298 /* draw_state: items that are drawn in sequence: */
3299#define WL_START 0 /* nothing done yet */
3300#ifdef FEAT_CMDWIN
3301# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3302#else
3303# define WL_CMDLINE WL_START
3304#endif
3305#ifdef FEAT_FOLDING
3306# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3307#else
3308# define WL_FOLD WL_CMDLINE
3309#endif
3310#ifdef FEAT_SIGNS
3311# define WL_SIGN WL_FOLD + 1 /* column for signs */
3312#else
3313# define WL_SIGN WL_FOLD /* column for signs */
3314#endif
3315#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003316#ifdef FEAT_LINEBREAK
3317# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003319# define WL_BRI WL_NR
3320#endif
3321#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3322# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3323#else
3324# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325#endif
3326#define WL_LINE WL_SBR + 1 /* text in the line */
3327 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003328#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 int feedback_col = 0;
3330 int feedback_old_attr = -1;
3331#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003332 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
Bram Moolenaar860cae12010-06-05 23:22:07 +02003334#ifdef FEAT_CONCEAL
3335 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003336 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003337 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003338 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003339 int is_concealing = FALSE;
3340 int boguscols = 0; /* nonexistent columns added to force
3341 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003342 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003343 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003344 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003345 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003346# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003347# define FIX_FOR_BOGUSCOLS \
3348 { \
3349 n_extra += vcol_off; \
3350 vcol -= vcol_off; \
3351 vcol_off = 0; \
3352 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003353 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003354 boguscols = 0; \
3355 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003356#else
3357# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
3360 if (startrow > endrow) /* past the end already! */
3361 return startrow;
3362
3363 row = startrow;
3364 screen_row = row + W_WINROW(wp);
3365
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003366 if (!number_only)
3367 {
3368 /*
3369 * To speed up the loop below, set extra_check when there is linebreak,
3370 * trailing white space and/or syntax processing to be done.
3371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003373 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374#endif
3375#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003376 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003377# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003378 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003379# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003380 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003382 /* Prepare for syntax highlighting in this line. When there is an
3383 * error, stop syntax highlighting. */
3384 save_did_emsg = did_emsg;
3385 did_emsg = FALSE;
3386 syntax_start(wp, lnum);
3387 if (did_emsg)
3388 wp->w_s->b_syn_error = TRUE;
3389 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003390 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003391 did_emsg = save_did_emsg;
3392#ifdef SYN_TIME_LIMIT
3393 if (!wp->w_s->b_syn_slow)
3394#endif
3395 {
3396 has_syntax = TRUE;
3397 extra_check = TRUE;
3398 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003401
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003402 /* Check for columns to display for 'colorcolumn'. */
3403 color_cols = wp->w_p_cc_cols;
3404 if (color_cols != NULL)
3405 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003406#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003407
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003408#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003409 if (term_show_buffer(wp->w_buffer))
3410 {
3411 extra_check = TRUE;
3412 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003413 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003414 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003415#endif
3416
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003417#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003418 if (wp->w_p_spell
3419 && *wp->w_s->b_p_spl != NUL
3420 && wp->w_s->b_langp.ga_len > 0
3421 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003422 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003423 /* Prepare for spell checking. */
3424 has_spell = TRUE;
3425 extra_check = TRUE;
3426
Bram Moolenaar7701f302018-10-02 21:20:32 +02003427 /* Get the start of the next line, so that words that wrap to the
3428 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003429 * Trick: skip a few chars for C/shell/Vim comments */
3430 nextline[SPWORDLEN] = NUL;
3431 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3432 {
3433 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3434 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3435 }
3436
Bram Moolenaar7701f302018-10-02 21:20:32 +02003437 /* When a word wrapped from the previous line the start of the
3438 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003439 if (lnum == checked_lnum)
3440 cur_checked_col = checked_col;
3441 checked_lnum = 0;
3442
3443 /* When there was a sentence end in the previous line may require a
3444 * word starting with capital in this line. In line 1 always check
3445 * the first word. */
3446 if (lnum != capcol_lnum)
3447 cap_col = -1;
3448 if (lnum == 1)
3449 cap_col = 0;
3450 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452#endif
3453
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003454 /*
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003455 * handle Visual active in this window
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003456 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003457 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003459 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003461 // Visual is after curwin->w_cursor
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003462 top = &curwin->w_cursor;
3463 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003465 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003467 // Visual is before curwin->w_cursor
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003468 top = &VIsual;
3469 bot = &curwin->w_cursor;
3470 }
3471 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003472 if (VIsual_mode == Ctrl_V)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003473 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003474 // block mode
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003475 if (lnum_in_visual_area)
3476 {
3477 fromcol = wp->w_old_cursor_fcol;
3478 tocol = wp->w_old_cursor_lcol;
3479 }
3480 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003481 else
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003482 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003483 // non-block mode
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003484 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003486 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003488 if (VIsual_mode == 'V') // linewise
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003489 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 else
3491 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003492 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3493 if (gchar_pos(top) == NUL)
3494 tocol = fromcol + 1;
3495 }
3496 }
3497 if (VIsual_mode != 'V' && lnum == bot->lnum)
3498 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003499 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003500 {
3501 fromcol = -10;
3502 tocol = MAXCOL;
3503 }
3504 else if (bot->col == MAXCOL)
3505 tocol = MAXCOL;
3506 else
3507 {
3508 pos = *bot;
3509 if (*p_sel == 'e')
3510 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3511 else
3512 {
3513 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3514 ++tocol;
3515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
3517 }
3518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003520 /* Check if the character under the cursor should not be inverted */
3521 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003522#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003523 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003524#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003525 )
3526 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003528 /* if inverting in this line set area_highlighting */
3529 if (fromcol >= 0)
3530 {
3531 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003532 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003534 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003535 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003536 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003537 && clip_isautosel_plus()))
3538 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003543 /*
3544 * handle 'incsearch' and ":s///c" highlighting
3545 */
3546 else if (highlight_match
3547 && wp == curwin
3548 && lnum >= curwin->w_cursor.lnum
3549 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003551 if (lnum == curwin->w_cursor.lnum)
3552 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003553 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003554 else
3555 fromcol = 0;
3556 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3557 {
3558 pos.lnum = lnum;
3559 pos.col = search_match_endcol;
3560 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3561 }
3562 else
3563 tocol = MAXCOL;
3564 /* do at least one character; happens when past end of line */
3565 if (fromcol == tocol)
3566 tocol = fromcol + 1;
3567 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003568 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
3571
3572#ifdef FEAT_DIFF
3573 filler_lines = diff_check(wp, lnum);
3574 if (filler_lines < 0)
3575 {
3576 if (filler_lines == -1)
3577 {
3578 if (diff_find_change(wp, lnum, &change_start, &change_end))
3579 diff_hlf = HLF_ADD; /* added line */
3580 else if (change_start == 0)
3581 diff_hlf = HLF_TXD; /* changed text */
3582 else
3583 diff_hlf = HLF_CHD; /* changed line */
3584 }
3585 else
3586 diff_hlf = HLF_ADD; /* added line */
3587 filler_lines = 0;
3588 area_highlighting = TRUE;
3589 }
3590 if (lnum == wp->w_topline)
3591 filler_lines = wp->w_topfill;
3592 filler_todo = filler_lines;
3593#endif
3594
Bram Moolenaar4e038572019-07-04 18:28:35 +02003595#ifdef FEAT_SIGNS
3596 sign_present = buf_get_signattrs(wp->w_buffer, lnum, &sattr);
3597#endif
3598
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599#ifdef LINE_ATTR
3600# ifdef FEAT_SIGNS
3601 /* If this line has a sign with line highlighting set line_attr. */
Bram Moolenaar4e038572019-07-04 18:28:35 +02003602 if (sign_present)
3603 line_attr = sattr.linehl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003605# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003607 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003608 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609# endif
3610 if (line_attr != 0)
3611 area_highlighting = TRUE;
3612#endif
3613
3614 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3615 ptr = line;
3616
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003617#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003618 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003619 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003620 /* For checking first word with a capital skip white space. */
3621 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003622 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003623
Bram Moolenaar30abd282005-06-22 22:35:10 +00003624 /* To be able to spell-check over line boundaries copy the end of the
3625 * current line into nextline[]. Above the start of the next line was
3626 * copied to nextline[SPWORDLEN]. */
3627 if (nextline[SPWORDLEN] == NUL)
3628 {
3629 /* No next line or it is empty. */
3630 nextlinecol = MAXCOL;
3631 nextline_idx = 0;
3632 }
3633 else
3634 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003635 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003636 if (v < SPWORDLEN)
3637 {
3638 /* Short line, use it completely and append the start of the
3639 * next line. */
3640 nextlinecol = 0;
3641 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003642 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003643 nextline_idx = v + 1;
3644 }
3645 else
3646 {
3647 /* Long line, use only the last SPWORDLEN bytes. */
3648 nextlinecol = v - SPWORDLEN;
3649 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3650 nextline_idx = SPWORDLEN + 1;
3651 }
3652 }
3653 }
3654#endif
3655
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003656 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003658 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003659 extra_check = TRUE;
3660 /* find start of trailing whitespace */
3661 if (lcs_trail)
3662 {
3663 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003664 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003665 --trailcol;
3666 trailcol += (colnr_T) (ptr - line);
3667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
3669
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003670 wcr_attr = get_wcr_attr(wp);
3671 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003672 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003673 win_attr = wcr_attr;
3674 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003675 }
3676#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02003677 if (WIN_IS_POPUP(wp))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003678 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003679#endif
3680
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 /*
3682 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3683 * first character to be displayed.
3684 */
3685 if (wp->w_p_wrap)
3686 v = wp->w_skipcol;
3687 else
3688 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003689 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 while (vcol < v && *ptr != NUL)
3694 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003695 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003698 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
3700
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003701 /* When:
3702 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003703 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003704 * - 'virtualedit' is set, or
3705 * - the visual mode is active,
3706 * the end of the line may be before the start of the displayed part.
3707 */
3708 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003709#ifdef FEAT_SYN_HL
3710 wp->w_p_cuc || draw_color_col ||
3711#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003712 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003713 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715
3716 /* Handle a character that's not completely on the screen: Put ptr at
3717 * that character but skip the first few screen characters. */
3718 if (vcol > v)
3719 {
3720 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003722 /* If the character fits on the screen, don't need to skip it.
3723 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003724 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003725 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
3727
3728 /*
3729 * Adjust for when the inverted text is before the screen,
3730 * and when the start of the inverted text is before the screen.
3731 */
3732 if (tocol <= vcol)
3733 fromcol = 0;
3734 else if (fromcol >= 0 && fromcol < vcol)
3735 fromcol = vcol;
3736
3737#ifdef FEAT_LINEBREAK
3738 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3739 if (wp->w_p_wrap)
3740 need_showbreak = TRUE;
3741#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003742#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003743 /* When spell checking a word we need to figure out the start of the
3744 * word and if it's badly spelled or not. */
3745 if (has_spell)
3746 {
3747 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003748 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003749 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003750
3751 pos = wp->w_cursor;
3752 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003753 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003754 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003755
3756 /* spell_move_to() may call ml_get() and make "line" invalid */
3757 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3758 ptr = line + linecol;
3759
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003760 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003761 {
3762 /* no bad word found at line start, don't check until end of a
3763 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003764 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003765 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003766 }
3767 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003768 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003769 /* bad word found, use attributes until end of word */
3770 word_end = wp->w_cursor.col + len + 1;
3771
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003772 /* Turn index into actual attributes. */
3773 if (spell_hlf != HLF_COUNT)
3774 spell_attr = highlight_attr[spell_hlf];
3775 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003776 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003777
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003778# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003779 /* Need to restart syntax highlighting for this line. */
3780 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003781 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003782# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003783 }
3784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
3786
3787 /*
3788 * Correct highlighting for cursor that can't be disabled.
3789 * Avoids having to check this for each character.
3790 */
3791 if (fromcol >= 0)
3792 {
3793 if (noinvcur)
3794 {
3795 if ((colnr_T)fromcol == wp->w_virtcol)
3796 {
3797 /* highlighting starts at cursor, let it start just after the
3798 * cursor */
3799 fromcol_prev = fromcol;
3800 fromcol = -1;
3801 }
3802 else if ((colnr_T)fromcol < wp->w_virtcol)
3803 /* restart highlighting after the cursor */
3804 fromcol_prev = wp->w_virtcol;
3805 }
3806 if (fromcol >= tocol)
3807 fromcol = -1;
3808 }
3809
3810#ifdef FEAT_SEARCH_EXTRA
3811 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003812 * Handle highlighting the last used search pattern and matches.
3813 * Do this for both search_hl and the match list.
Bram Moolenaarac2450a2019-06-09 18:04:28 +02003814 * Do not use search_hl in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003816 cur = wp->w_match_head;
Bram Moolenaarac2450a2019-06-09 18:04:28 +02003817 shl_flag = (screen_line_flags & SLF_POPUP);
3818 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003820 if (shl_flag == FALSE)
3821 {
3822 shl = &search_hl;
3823 shl_flag = TRUE;
3824 }
3825 else
3826 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003827 shl->startcol = MAXCOL;
3828 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003830 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003831 v = (long)(ptr - line);
3832 if (cur != NULL)
3833 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003834 next_search_hl(wp, shl, lnum, (colnr_T)v,
3835 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003836
3837 /* Need to get the line again, a multi-line regexp may have made it
3838 * invalid. */
3839 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3840 ptr = line + v;
3841
3842 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003844 if (shl->lnum == lnum)
3845 shl->startcol = shl->rm.startpos[0].col;
3846 else
3847 shl->startcol = 0;
3848 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3849 - shl->rm.startpos[0].lnum)
3850 shl->endcol = shl->rm.endpos[0].col;
3851 else
3852 shl->endcol = MAXCOL;
3853 /* Highlight one character for an empty match. */
3854 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003856 if (has_mbyte && line[shl->endcol] != NUL)
3857 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3858 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003859 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003861 if ((long)shl->startcol < v) /* match at leftcol */
3862 {
3863 shl->attr_cur = shl->attr;
3864 search_attr = shl->attr;
3865 }
3866 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003868 if (shl != &search_hl && cur != NULL)
3869 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 }
3871#endif
3872
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003873#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003874 // Cursor line highlighting for 'cursorline' in the current window.
3875 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003876 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003877 // Do not show the cursor line when Visual mode is active, because it's
3878 // not clear what is selected then. Do update w_last_cursorline.
3879 if (!(wp == curwin && VIsual_active))
3880 {
3881 line_attr = HL_ATTR(HLF_CUL);
3882 area_highlighting = TRUE;
3883 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003884 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003885 }
3886#endif
3887
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003888#ifdef FEAT_TEXT_PROP
3889 {
3890 char_u *prop_start;
3891
3892 text_prop_count = get_text_props(wp->w_buffer, lnum,
3893 &prop_start, FALSE);
3894 if (text_prop_count > 0)
3895 {
3896 // Make a copy of the properties, so that they are properly
3897 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003898 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003899 if (text_props != NULL)
3900 mch_memmove(text_props, prop_start,
3901 text_prop_count * sizeof(textprop_T));
3902
3903 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003904 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003905 area_highlighting = TRUE;
3906 extra_check = TRUE;
3907 }
3908 }
3909#endif
3910
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003911 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003913
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914#ifdef FEAT_RIGHTLEFT
3915 if (wp->w_p_rl)
3916 {
3917 /* Rightleft window: process the text in the normal direction, but put
3918 * it in current_ScreenLine[] from right to left. Start at the
3919 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003920 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003922 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 }
3924#endif
3925
3926 /*
3927 * Repeat for the whole displayed line.
3928 */
3929 for (;;)
3930 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003931#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003932 int has_match_conc = 0; // match wants to conceal
3933 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 /* Skip this quickly when working on the text. */
3936 if (draw_state != WL_LINE)
3937 {
3938#ifdef FEAT_CMDWIN
3939 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3940 {
3941 draw_state = WL_CMDLINE;
3942 if (cmdwin_type != 0 && wp == curwin)
3943 {
3944 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003946 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003947 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003948 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
3950 }
3951#endif
3952
3953#ifdef FEAT_FOLDING
3954 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3955 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003956 int fdc = compute_foldcolumn(wp, 0);
3957
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003959 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003961 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003962 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003963 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003964 p_extra_free = alloc(12 + 1);
3965
3966 if (p_extra_free != NULL)
3967 {
3968 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3969 n_extra = fdc;
3970 p_extra_free[n_extra] = NUL;
3971 p_extra = p_extra_free;
3972 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003973 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003974 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01003975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 }
3977 }
3978#endif
3979
3980#ifdef FEAT_SIGNS
3981 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3982 {
3983 draw_state = WL_SIGN;
3984 /* Show the sign column when there are any signs in this
3985 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003986 if (signcolumn_on(wp))
Bram Moolenaar4e038572019-07-04 18:28:35 +02003987 get_sign_display_info(FALSE, wp, lnum, &sattr, wcr_attr,
3988 row, startrow, filler_lines, filler_todo, &c_extra,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003989 &c_final, extra, &p_extra, &n_extra, &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 }
3991#endif
3992
3993 if (draw_state == WL_NR - 1 && n_extra == 0)
3994 {
3995 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003996 /* Display the absolute or relative line number. After the
3997 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3998 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 && (row == startrow
4000#ifdef FEAT_DIFF
4001 + filler_lines
4002#endif
4003 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
4004 {
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004005#ifdef FEAT_SIGNS
4006 // If 'signcolumn' is set to 'number' and a sign is present
4007 // in 'lnum', then display the sign instead of the line
4008 // number.
4009 if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
Bram Moolenaar4e038572019-07-04 18:28:35 +02004010 && sign_present)
4011 get_sign_display_info(TRUE, wp, lnum, &sattr, wcr_attr,
4012 row, startrow, filler_lines, filler_todo,
4013 &c_extra, &c_final, extra, &p_extra, &n_extra,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004014 &char_attr);
4015 else
4016#endif
4017 {
4018 /* Draw the line number (empty space after wrapping). */
4019 if (row == startrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020#ifdef FEAT_DIFF
4021 + filler_lines
4022#endif
4023 )
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004024 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004025 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01004026 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02004027
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004028 if (wp->w_p_nu && !wp->w_p_rnu)
4029 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02004030 num = (long)lnum;
4031 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01004032 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004033 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01004034 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004035 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004036 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004037 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004038 num = lnum;
4039 fmt = "%-*ld ";
4040 }
4041 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004042
Bram Moolenaar700e7342013-01-30 12:31:36 +01004043 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004044 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 if (wp->w_skipcol > 0)
4046 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4047 *p_extra = '-';
4048#ifdef FEAT_RIGHTLEFT
4049 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004050 {
4051 char_u *p1, *p2;
4052 int t;
4053
4054 // like rl_mirror(), but keep the space at the end
4055 p2 = skiptowhite(extra) - 1;
4056 for (p1 = extra; p1 < p2; ++p1, --p2)
4057 {
4058 t = *p1;
4059 *p1 = *p2;
4060 *p2 = t;
4061 }
4062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063#endif
4064 p_extra = extra;
4065 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004066 c_final = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004067 }
4068 else
4069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004071 c_final = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004072 }
4073 n_extra = number_width(wp) + 1;
4074 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004075#ifdef FEAT_SYN_HL
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004076 /* When 'cursorline' is set highlight the line number of
4077 * the current line differently.
4078 * TODO: Can we use CursorLine instead of CursorLineNr
4079 * when CursorLineNr isn't set? */
4080 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004081 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004082 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004083#endif
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 }
4086 }
4087
Bram Moolenaar597a4222014-06-25 14:39:50 +02004088#ifdef FEAT_LINEBREAK
4089 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4090 && n_extra == 0 && *p_sbr != NUL)
4091 /* draw indent after showbreak value */
4092 draw_state = WL_BRI;
4093 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4094 /* After the showbreak, draw the breakindent */
4095 draw_state = WL_BRI - 1;
4096
4097 /* draw 'breakindent': indent wrapped text accordingly */
4098 if (draw_state == WL_BRI - 1 && n_extra == 0)
4099 {
4100 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004101 /* if need_showbreak is set, breakindent also applies */
4102 if (wp->w_p_bri && n_extra == 0
4103 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004104# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004105 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004106# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004107 )
4108 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004109 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004110# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004111 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004112 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004113 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004114# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004115 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4116 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004117 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004118# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004119 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004120# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004121 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004122 c_extra = ' ';
4123 n_extra = get_breakindent_win(wp,
4124 ml_get_buf(wp->w_buffer, lnum, FALSE));
4125 /* Correct end of highlighted area for 'breakindent',
4126 * required when 'linebreak' is also set. */
4127 if (tocol == vcol)
4128 tocol += n_extra;
4129 }
4130 }
4131#endif
4132
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4134 if (draw_state == WL_SBR - 1 && n_extra == 0)
4135 {
4136 draw_state = WL_SBR;
4137# ifdef FEAT_DIFF
4138 if (filler_todo > 0)
4139 {
4140 /* Draw "deleted" diff line(s). */
4141 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004144 c_final = NUL;
4145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004147 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004149 c_final = NUL;
4150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151# ifdef FEAT_RIGHTLEFT
4152 if (wp->w_p_rl)
4153 n_extra = col + 1;
4154 else
4155# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004156 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004157 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 }
4159# endif
4160# ifdef FEAT_LINEBREAK
4161 if (*p_sbr != NUL && need_showbreak)
4162 {
4163 /* Draw 'showbreak' at the start of each broken line. */
4164 p_extra = p_sbr;
4165 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004166 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004168 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004170 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 /* Correct end of highlighted area for 'showbreak',
4172 * required when 'linebreak' is also set. */
4173 if (tocol == vcol)
4174 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004175#ifdef FEAT_SYN_HL
4176 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004177 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004178 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004179 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
4182# endif
4183 }
4184#endif
4185
4186 if (draw_state == WL_LINE - 1 && n_extra == 0)
4187 {
4188 draw_state = WL_LINE;
4189 if (saved_n_extra)
4190 {
4191 /* Continue item from end of wrapped line. */
4192 n_extra = saved_n_extra;
4193 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004194 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 p_extra = saved_p_extra;
4196 char_attr = saved_char_attr;
4197 }
4198 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004199 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 }
4202
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004203 // When still displaying '$' of change command, stop at cursor.
4204 // When only displaying the (relative) line number and that's done,
4205 // stop here.
4206 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004207 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208#ifdef FEAT_DIFF
4209 && filler_todo <= 0
4210#endif
4211 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004212 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004214 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004215 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004216 /* Pretend we have finished updating the window. Except when
4217 * 'cursorcolumn' is set. */
4218#ifdef FEAT_SYN_HL
4219 if (wp->w_p_cuc)
4220 row = wp->w_cline_row + wp->w_cline_height;
4221 else
4222#endif
4223 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 break;
4225 }
4226
Bram Moolenaar637532b2019-01-03 21:44:40 +01004227 if (draw_state == WL_LINE && (area_highlighting
4228#ifdef FEAT_SPELL
4229 || has_spell
4230#endif
4231 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 {
4233 /* handle Visual or match highlighting in this line */
4234 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4236 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004238 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004240 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 else if (area_attr != 0
4242 && (vcol == tocol
4243 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
4246#ifdef FEAT_SEARCH_EXTRA
4247 if (!n_extra)
4248 {
4249 /*
4250 * Check for start/end of search pattern match.
4251 * After end, check for start/end of next match.
4252 * When another match, have to check for start again.
4253 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004254 * Do this for 'search_hl' and the match list (ordered by
4255 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004257 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004258 cur = wp->w_match_head;
Bram Moolenaara730e552019-06-16 19:05:31 +02004259 shl_flag = FALSE;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004260 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004262 if (shl_flag == FALSE
4263 && ((cur != NULL
4264 && cur->priority > SEARCH_HL_PRIORITY)
4265 || cur == NULL))
4266 {
4267 shl = &search_hl;
4268 shl_flag = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02004269 if (screen_line_flags & SLF_POPUP)
4270 continue; // do not use search_hl
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004271 }
4272 else
4273 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004274 if (cur != NULL)
4275 cur->pos.cur = 0;
4276 pos_inprogress = TRUE;
4277 while (shl->rm.regprog != NULL
4278 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004280 if (shl->startcol != MAXCOL
4281 && v >= (long)shl->startcol
4282 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004284 int tmp_col = v + MB_PTR2LEN(ptr);
4285
4286 if (shl->endcol < tmp_col)
4287 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004289#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004290 // Match with the "Conceal" group results in hiding
4291 // the match.
4292 if (cur != NULL
4293 && shl != &search_hl
4294 && syn_name2id((char_u *)"Conceal")
4295 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004296 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004297 has_match_conc =
4298 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004299 match_conc = cur->conceal_char;
4300 }
4301 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004302 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004305 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 {
4307 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004308 next_search_hl(wp, shl, lnum, (colnr_T)v,
4309 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004310 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004311 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
4313 /* Need to get the line again, a multi-line regexp
4314 * may have made it invalid. */
4315 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4316 ptr = line + v;
4317
4318 if (shl->lnum == lnum)
4319 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004320 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004322 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004324 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004326 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 {
4328 /* highlight empty match, try again after
4329 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004331 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004332 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004334 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 }
4336
4337 /* Loop to check if the match starts at the
4338 * current position */
4339 continue;
4340 }
4341 }
4342 break;
4343 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004344 if (shl != &search_hl && cur != NULL)
4345 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004347
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004348 /* Use attributes from match with highest priority among
4349 * 'search_hl' and the match list. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004350 cur = wp->w_match_head;
4351 shl_flag = FALSE;
Bram Moolenaara730e552019-06-16 19:05:31 +02004352 search_attr = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004353 while (cur != NULL || shl_flag == FALSE)
4354 {
4355 if (shl_flag == FALSE
4356 && ((cur != NULL
4357 && cur->priority > SEARCH_HL_PRIORITY)
4358 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004359 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004360 shl = &search_hl;
4361 shl_flag = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02004362 if (screen_line_flags & SLF_POPUP)
4363 continue; // do not use search_hl
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004364 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004365 else
4366 shl = &cur->hl;
4367 if (shl->attr_cur != 0)
4368 search_attr = shl->attr_cur;
4369 if (shl != &search_hl && cur != NULL)
4370 cur = cur->next;
4371 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004372 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004373 if (*ptr == NUL && (did_line_attr >= 1
4374 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004375 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 }
4377#endif
4378
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004380 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004382 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4383 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004385 if (diff_hlf == HLF_TXD && ptr - line > change_end
4386 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004388 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004389 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004390 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 }
4392#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004393
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004394#ifdef FEAT_TEXT_PROP
4395 if (text_props != NULL)
4396 {
4397 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004398 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004399
Bram Moolenaara956bf62019-06-19 17:34:24 +02004400 if (n_extra > 0)
4401 --bcol; // still working on the previous char, e.g. Tab
4402
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004403 // Check if any active property ends.
4404 for (pi = 0; pi < text_props_active; ++pi)
4405 {
4406 int tpi = text_prop_idxs[pi];
4407
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004408 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004409 + text_props[tpi].tp_len)
4410 {
4411 if (pi + 1 < text_props_active)
4412 mch_memmove(text_prop_idxs + pi,
4413 text_prop_idxs + pi + 1,
4414 sizeof(int)
4415 * (text_props_active - (pi + 1)));
4416 --text_props_active;
4417 --pi;
4418 }
4419 }
4420
4421 // Add any text property that starts in this column.
4422 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004423 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004424 text_prop_idxs[text_props_active++] = text_prop_next++;
4425
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004426 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004427 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004428 if (text_props_active > 0)
4429 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004430 // Sort the properties on priority and/or starting last.
4431 // Then combine the attributes, highest priority last.
4432 current_text_props = text_props;
4433 current_buf = wp->w_buffer;
4434 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4435 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004436
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004437 for (pi = 0; pi < text_props_active; ++pi)
4438 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004439 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004440 proptype_T *pt = text_prop_type_by_id(
4441 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004442
Bram Moolenaard74af422019-06-28 21:38:00 +02004443 if (pt != NULL && pt->pt_hl_id > 0)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004444 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004445 int pt_attr = syn_id2attr(pt->pt_hl_id);
4446
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004447 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004448 text_prop_attr =
4449 hl_combine_attr(text_prop_attr, pt_attr);
4450 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004451 }
4452 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004453 }
4454 }
4455#endif
4456
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004457 /* Decide which of the highlight attributes to use. */
4458 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004459#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004460 if (area_attr != 0)
4461 char_attr = hl_combine_attr(line_attr, area_attr);
4462 else if (search_attr != 0)
4463 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004464# ifdef FEAT_TEXT_PROP
4465 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004466 {
4467 char_attr = hl_combine_attr(
4468 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4469 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004470# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004471 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004472 || vcol < fromcol || vcol_prev < fromcol_prev
4473 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004474 // Use line_attr when not in the Visual or 'incsearch' area
4475 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004476 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004477#else
4478 if (area_attr != 0)
4479 char_attr = area_attr;
4480 else if (search_attr != 0)
4481 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004482#endif
4483 else
4484 {
4485 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004486#ifdef FEAT_TEXT_PROP
4487 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004488 {
4489 if (text_prop_combine)
4490 char_attr = hl_combine_attr(
4491 syntax_attr, text_prop_attr);
4492 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004493 char_attr = hl_combine_attr(
4494 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004495 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004496 else
4497#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004498#ifdef FEAT_SYN_HL
4499 if (has_syntax)
4500 char_attr = syntax_attr;
4501 else
4502#endif
4503 char_attr = 0;
4504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004506 if (char_attr == 0)
4507 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508
4509 /*
4510 * Get the next character to put on the screen.
4511 */
4512 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004513 * The "p_extra" points to the extra stuff that is inserted to
4514 * represent special characters (non-printable stuff) and other
4515 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004516 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004517 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4518 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4520 */
4521 if (n_extra > 0)
4522 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004523 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004525 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004527 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 {
4529 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004530 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004531 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 }
4533 else
4534 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 }
4536 else
4537 {
4538 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 if (has_mbyte)
4540 {
4541 mb_c = c;
4542 if (enc_utf8)
4543 {
4544 /* If the UTF-8 character is more than one byte:
4545 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004546 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 mb_utf8 = FALSE;
4548 if (mb_l > n_extra)
4549 mb_l = 1;
4550 else if (mb_l > 1)
4551 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004552 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004554 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 }
4556 }
4557 else
4558 {
4559 /* if this is a DBCS character, put it in "mb_c" */
4560 mb_l = MB_BYTE2LEN(c);
4561 if (mb_l >= n_extra)
4562 mb_l = 1;
4563 else if (mb_l > 1)
4564 mb_c = (c << 8) + p_extra[1];
4565 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004566 if (mb_l == 0) /* at the NUL at end-of-line */
4567 mb_l = 1;
4568
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 /* If a double-width char doesn't fit display a '>' in the
4570 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004571 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572# ifdef FEAT_RIGHTLEFT
4573 wp->w_p_rl ? (col <= 0) :
4574# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004575 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 && (*mb_char2cells)(mb_c) == 2)
4577 {
4578 c = '>';
4579 mb_c = c;
4580 mb_l = 1;
4581 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004582 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 /* put the pointer back to output the double-width
4584 * character at the start of the next line. */
4585 ++n_extra;
4586 --p_extra;
4587 }
4588 else
4589 {
4590 n_extra -= mb_l - 1;
4591 p_extra += mb_l - 1;
4592 }
4593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 ++p_extra;
4595 }
4596 --n_extra;
4597 }
4598 else
4599 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004600#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004601 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004602#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004603
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004604 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004605 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 /*
4607 * Get a character from the line itself.
4608 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004609 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004610#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004611 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 if (has_mbyte)
4614 {
4615 mb_c = c;
4616 if (enc_utf8)
4617 {
4618 /* If the UTF-8 character is more than one byte: Decode it
4619 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004620 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 mb_utf8 = FALSE;
4622 if (mb_l > 1)
4623 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004624 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 /* Overlong encoded ASCII or ASCII with composing char
4626 * is displayed normally, except a NUL. */
4627 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004628 {
4629 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004630#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004631 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004632#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004635
4636 /* At start of the line we can have a composing char.
4637 * Draw it as a space with a composing char. */
4638 if (utf_iscomposing(mb_c))
4639 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004640 int i;
4641
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004642 for (i = Screen_mco - 1; i > 0; --i)
4643 u8cc[i] = u8cc[i - 1];
4644 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004645 mb_c = ' ';
4646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 }
4648
4649 if ((mb_l == 1 && c >= 0x80)
4650 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004651 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 {
4653 /*
4654 * Illegal UTF-8 byte: display as <xx>.
4655 * Non-BMP character : display as ? or fullwidth ?.
4656 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004657 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004658# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004659 if (wp->w_p_rl) /* reverse */
4660 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004661# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 p_extra = extra;
4663 c = *p_extra;
4664 mb_c = mb_ptr2char_adv(&p_extra);
4665 mb_utf8 = (c >= 0x80);
4666 n_extra = (int)STRLEN(p_extra);
4667 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004668 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 if (area_attr == 0 && search_attr == 0)
4670 {
4671 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004672 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 saved_attr2 = char_attr; /* save current attr */
4674 }
4675 }
4676 else if (mb_l == 0) /* at the NUL at end-of-line */
4677 mb_l = 1;
4678#ifdef FEAT_ARABIC
4679 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4680 {
4681 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004682 int pc, pc1, nc;
4683 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684
4685 /* The idea of what is the previous and next
4686 * character depends on 'rightleft'. */
4687 if (wp->w_p_rl)
4688 {
4689 pc = prev_c;
4690 pc1 = prev_c1;
4691 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004692 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
4694 else
4695 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004696 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004698 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
4700 prev_c = mb_c;
4701
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004702 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704 else
4705 prev_c = mb_c;
4706#endif
4707 }
4708 else /* enc_dbcs */
4709 {
4710 mb_l = MB_BYTE2LEN(c);
4711 if (mb_l == 0) /* at the NUL at end-of-line */
4712 mb_l = 1;
4713 else if (mb_l > 1)
4714 {
4715 /* We assume a second byte below 32 is illegal.
4716 * Hopefully this is OK for all double-byte encodings!
4717 */
4718 if (ptr[1] >= 32)
4719 mb_c = (c << 8) + ptr[1];
4720 else
4721 {
4722 if (ptr[1] == NUL)
4723 {
4724 /* head byte at end of line */
4725 mb_l = 1;
4726 transchar_nonprint(extra, c);
4727 }
4728 else
4729 {
4730 /* illegal tail byte */
4731 mb_l = 2;
4732 STRCPY(extra, "XX");
4733 }
4734 p_extra = extra;
4735 n_extra = (int)STRLEN(extra) - 1;
4736 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004737 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 c = *p_extra++;
4739 if (area_attr == 0 && search_attr == 0)
4740 {
4741 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004742 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 saved_attr2 = char_attr; /* save current attr */
4744 }
4745 mb_c = c;
4746 }
4747 }
4748 }
4749 /* If a double-width char doesn't fit display a '>' in the
4750 * last column; the character is displayed at the start of the
4751 * next line. */
4752 if ((
4753# ifdef FEAT_RIGHTLEFT
4754 wp->w_p_rl ? (col <= 0) :
4755# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004756 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 && (*mb_char2cells)(mb_c) == 2)
4758 {
4759 c = '>';
4760 mb_c = c;
4761 mb_utf8 = FALSE;
4762 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004763 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004764 // Put pointer back so that the character will be
4765 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004767#ifdef FEAT_CONCEAL
4768 did_decrement_ptr = TRUE;
4769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 }
4771 else if (*ptr != NUL)
4772 ptr += mb_l - 1;
4773
4774 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004775 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004776 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004777 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004780 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004781 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 c = ' ';
4783 if (area_attr == 0 && search_attr == 0)
4784 {
4785 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004786 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 saved_attr2 = char_attr; /* save current attr */
4788 }
4789 mb_c = c;
4790 mb_utf8 = FALSE;
4791 mb_l = 1;
4792 }
4793
4794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 ++ptr;
4796
4797 if (extra_check)
4798 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004799#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004800 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004801#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004802
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004803#ifdef FEAT_TERMINAL
4804 if (get_term_attr)
4805 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004806 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004807
4808 if (!attr_pri)
4809 char_attr = syntax_attr;
4810 else
4811 char_attr = hl_combine_attr(syntax_attr, char_attr);
4812 }
4813#endif
4814
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004815#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004816 // Get syntax attribute, unless still at the start of the line
4817 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004818 v = (long)(ptr - line);
4819 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
4821 /* Get the syntax attribute for the character. If there
4822 * is an error, disable syntax highlighting. */
4823 save_did_emsg = did_emsg;
4824 did_emsg = FALSE;
4825
Bram Moolenaar217ad922005-03-20 22:37:15 +00004826 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004827# ifdef FEAT_SPELL
4828 has_spell ? &can_spell :
4829# endif
4830 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831
4832 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004833 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004834 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004835 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004836 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 else
4839 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004840
4841 // combine syntax attribute with 'wincolor'
4842 if (win_attr != 0)
4843 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4844
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004845#ifdef SYN_TIME_LIMIT
4846 if (wp->w_s->b_syn_slow)
4847 has_syntax = FALSE;
4848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
4850 /* Need to get the line again, a multi-line regexp may
4851 * have made it invalid. */
4852 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4853 ptr = line + v;
4854
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004855# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004856 // Text properties overrule syntax highlighting or combine.
4857 if (text_prop_attr == 0 || text_prop_combine)
4858# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004859 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004860 int comb_attr = syntax_attr;
4861# ifdef FEAT_TEXT_PROP
4862 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4863# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004864 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004865 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004866 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004867 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004868 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004869# ifdef FEAT_CONCEAL
4870 /* no concealing past the end of the line, it interferes
4871 * with line highlighting */
4872 if (c == NUL)
4873 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004874 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004875 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004876# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004878#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004879
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004880#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004881 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004882 * Only do this when there is no syntax highlighting, the
4883 * @Spell cluster is not used or the current syntax item
4884 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004885 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004886 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004887 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004888 if (c != 0 && (
4889# ifdef FEAT_SYN_HL
4890 !has_syntax ||
4891# endif
4892 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004893 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004894 char_u *prev_ptr, *p;
4895 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004896 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004897 if (has_mbyte)
4898 {
4899 prev_ptr = ptr - mb_l;
4900 v -= mb_l - 1;
4901 }
4902 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004903 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004904
4905 /* Use nextline[] if possible, it has the start of the
4906 * next line concatenated. */
4907 if ((prev_ptr - line) - nextlinecol >= 0)
4908 p = nextline + (prev_ptr - line) - nextlinecol;
4909 else
4910 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004911 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004912 len = spell_check(wp, p, &spell_hlf, &cap_col,
4913 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004914 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004915
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004916 /* In Insert mode only highlight a word that
4917 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004918 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004919 && (State & INSERT) != 0
4920 && wp->w_cursor.lnum == lnum
4921 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004922 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004923 && wp->w_cursor.col < (colnr_T)word_end)
4924 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004925 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004926 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004927 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004928
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004929 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004930 && (p - nextline) + len > nextline_idx)
4931 {
4932 /* Remember that the good word continues at the
4933 * start of the next line. */
4934 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004935 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004936 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004937
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004938 /* Turn index into actual attributes. */
4939 if (spell_hlf != HLF_COUNT)
4940 spell_attr = highlight_attr[spell_hlf];
4941
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004942 if (cap_col > 0)
4943 {
4944 if (p != prev_ptr
4945 && (p - nextline) + cap_col >= nextline_idx)
4946 {
4947 /* Remember that the word in the next line
4948 * must start with a capital. */
4949 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004950 cap_col = (int)((p - nextline) + cap_col
4951 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004952 }
4953 else
4954 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004955 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004956 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004957 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004958 }
4959 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004960 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004961 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004962 char_attr = hl_combine_attr(char_attr, spell_attr);
4963 else
4964 char_attr = hl_combine_attr(spell_attr, char_attr);
4965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966#endif
4967#ifdef FEAT_LINEBREAK
4968 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004969 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004971 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004972 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004974 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004975 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004976
Bram Moolenaar597a4222014-06-25 14:39:50 +02004977 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004978 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004979 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004980 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004981# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004982 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004983 wp->w_buffer->b_p_vts_array) - 1;
4984# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004985 n_extra = (int)wp->w_buffer->b_p_ts
4986 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004987# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004988
Bram Moolenaar4df70292015-03-21 14:20:16 +01004989 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004990 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004991 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004992 {
4993#ifdef FEAT_CONCEAL
4994 if (c == TAB)
4995 /* See "Tab alignment" below. */
4996 FIX_FOR_BOGUSCOLS;
4997#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004998 if (!wp->w_p_list)
4999 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
5002#endif
5003
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005004 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
5005 // But not when the character is followed by a composing
5006 // character (use mb_l to check that).
5007 if (wp->w_p_list
5008 && ((((c == 160 && mb_l == 1)
5009 || (mb_utf8
5010 && ((mb_c == 160 && mb_l == 2)
5011 || (mb_c == 0x202f && mb_l == 3))))
5012 && lcs_nbsp)
5013 || (c == ' '
5014 && mb_l == 1
5015 && lcs_space
5016 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005017 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005018 c = (c == ' ') ? lcs_space : lcs_nbsp;
5019 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005020 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005021 n_attr = 1;
5022 extra_attr = HL_ATTR(HLF_8);
5023 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005024 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005025 mb_c = c;
5026 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005027 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005028 mb_utf8 = TRUE;
5029 u8cc[0] = 0;
5030 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005031 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005032 else
5033 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005034 }
5035
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
5037 {
5038 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005039 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
5041 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005042 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 saved_attr2 = char_attr; /* save current attr */
5044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005046 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 {
5048 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005049 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005050 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
5052 else
5053 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
5055 }
5056
5057 /*
5058 * Handling of non-printable characters.
5059 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005060 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
5062 /*
5063 * when getting a character from the file, we may have to
5064 * turn it into something else on the way to putting it
5065 * into "ScreenLines".
5066 */
5067 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5068 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005069 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005070 long vcol_adjusted = vcol; /* removed showbreak length */
5071#ifdef FEAT_LINEBREAK
5072 /* only adjust the tab_len, when at the first column
5073 * after the showbreak value was drawn */
5074 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5075 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005078#ifdef FEAT_VARTABS
5079 tab_len = tabstop_padding(vcol_adjusted,
5080 wp->w_buffer->b_p_ts,
5081 wp->w_buffer->b_p_vts_array) - 1;
5082#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005083 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005084 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5085#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005086
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005087#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005088 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005089#endif
5090 /* tab amount depends on current column */
5091 n_extra = tab_len;
5092#ifdef FEAT_LINEBREAK
5093 else
5094 {
5095 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005096 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005097 int i;
5098 int saved_nextra = n_extra;
5099
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005100#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005101 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005102 /* there are characters to conceal */
5103 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005104 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5105 */
5106 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5107 && n_extra > tab_len)
5108 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005109#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005110
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005111 /* if n_extra > 0, it gives the number of chars, to
5112 * use for a tab, else we need to calculate the width
5113 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005114 len = (tab_len * mb_char2len(lcs_tab2));
5115 if (n_extra > 0)
5116 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005117 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005118 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005119 vim_memset(p, ' ', len);
5120 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005121 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005122 p_extra_free = p;
5123 for (i = 0; i < tab_len; i++)
5124 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005125 if (*p == NUL)
5126 {
5127 tab_len = i;
5128 break;
5129 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005130 mb_char2bytes(lcs_tab2, p);
5131 p += mb_char2len(lcs_tab2);
5132 n_extra += mb_char2len(lcs_tab2)
5133 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005134 }
5135 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005136#ifdef FEAT_CONCEAL
5137 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5138 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005139 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005140 n_extra -= vcol_off;
5141#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005142 }
5143#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005144#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005145 {
5146 int vc_saved = vcol_off;
5147
5148 /* Tab alignment should be identical regardless of
5149 * 'conceallevel' value. So tab compensates of all
5150 * previous concealed characters, and thus resets
5151 * vcol_off and boguscols accumulated so far in the
5152 * line. Note that the tab can be longer than
5153 * 'tabstop' when there are concealed characters. */
5154 FIX_FOR_BOGUSCOLS;
5155
5156 /* Make sure, the highlighting for the tab char will be
5157 * correctly set further below (effectively reverts the
5158 * FIX_FOR_BOGSUCOLS macro */
5159 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005160 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005161 tab_len += vc_saved;
5162 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 if (wp->w_p_list)
5166 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005167 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005168#ifdef FEAT_LINEBREAK
5169 if (wp->w_p_lbr)
5170 c_extra = NUL; /* using p_extra from above */
5171 else
5172#endif
5173 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005174 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005175 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005176 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005179 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 {
5181 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005182 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005183 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186 else
5187 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005188 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 c_extra = ' ';
5190 c = ' ';
5191 }
5192 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005193 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005194 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005195 || ((fromcol >= 0 || fromcol_prev >= 0)
5196 && tocol > vcol
5197 && VIsual_mode != Ctrl_V
5198 && (
5199# ifdef FEAT_RIGHTLEFT
5200 wp->w_p_rl ? (col >= 0) :
5201# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005202 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005203 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005204 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005205 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005206 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005208 /* Display a '$' after the line or highlight an extra
5209 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5211 /* For a diff line the highlighting continues after the
5212 * "$". */
5213 if (
5214# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005215 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216# ifdef LINE_ATTR
5217 &&
5218# endif
5219# endif
5220# ifdef LINE_ATTR
5221 line_attr == 0
5222# endif
5223 )
5224#endif
5225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 /* In virtualedit, visual selections may extend
5227 * beyond end of line. */
5228 if (area_highlighting && virtual_active()
5229 && tocol != MAXCOL && vcol < tocol)
5230 n_extra = 0;
5231 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
5233 p_extra = at_end_str;
5234 n_extra = 1;
5235 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005236 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
5238 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005239 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005240 c = lcs_eol;
5241 else
5242 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 lcs_eol_one = -1;
5244 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005245 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005247 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 n_attr = 1;
5249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005251 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 {
5253 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005254 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005255 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 }
5257 else
5258 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 }
5260 else if (c != NUL)
5261 {
5262 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005263 if (n_extra == 0)
5264 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265#ifdef FEAT_RIGHTLEFT
5266 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5267 rl_mirror(p_extra); /* reverse "<12>" */
5268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005270 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005271#ifdef FEAT_LINEBREAK
5272 if (wp->w_p_lbr)
5273 {
5274 char_u *p;
5275
5276 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005277 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005278 vim_memset(p, ' ', n_extra);
5279 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5280 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005281 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005282 p_extra_free = p_extra = p;
5283 }
5284 else
5285#endif
5286 {
5287 n_extra = byte2cells(c) - 1;
5288 c = *p_extra++;
5289 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005290 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 {
5292 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005293 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 saved_attr2 = char_attr; /* save current attr */
5295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 else if (VIsual_active
5299 && (VIsual_mode == Ctrl_V
5300 || VIsual_mode == 'v')
5301 && virtual_active()
5302 && tocol != MAXCOL
5303 && vcol < tocol
5304 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005305#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005307#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005308 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
5310 c = ' ';
5311 --ptr; /* put it back at the NUL */
5312 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005313#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 else if ((
5315# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005316 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005318# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005319 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005320# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 ) && (
5323# ifdef FEAT_RIGHTLEFT
5324 wp->w_p_rl ? (col >= 0) :
5325# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005326 (col
5327# ifdef FEAT_CONCEAL
5328 - boguscols
5329# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005330 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {
5332 /* Highlight until the right side of the window */
5333 c = ' ';
5334 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005335
5336 /* Remember we do the char for line highlighting. */
5337 ++did_line_attr;
5338
5339 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005340 if (line_attr != 0 && char_attr == search_attr
5341 && (did_line_attr > 1
5342 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005343 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344# ifdef FEAT_DIFF
5345 if (diff_hlf == HLF_TXD)
5346 {
5347 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005348 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005349 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005350 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005351 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5352 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005353 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 }
5356# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005357# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005358 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005359 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005360 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005361 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5362 char_attr = hl_combine_attr(char_attr,
5363 HL_ATTR(HLF_CUL));
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02005364 else if (line_attr)
5365 char_attr = hl_combine_attr(char_attr, line_attr);
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005366 }
5367# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 }
5369#endif
5370 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005371
5372#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005373 if ( wp->w_p_cole > 0
5374 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005375 conceal_cursor_line(wp))
5376 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005377 && !(lnum_in_visual_area
5378 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005379 {
5380 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005381 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005382 && (syn_get_sub_char() != NUL || match_conc
5383 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005384 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005385 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005386 /* First time at this concealed item: display one
5387 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005388 if (match_conc)
5389 c = match_conc;
5390 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005391 c = syn_get_sub_char();
5392 else if (lcs_conceal != NUL)
5393 c = lcs_conceal;
5394 else
5395 c = ' ';
5396
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005397 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005398
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005399 if (n_extra > 0)
5400 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005401 vcol += n_extra;
5402 if (wp->w_p_wrap && n_extra > 0)
5403 {
5404# ifdef FEAT_RIGHTLEFT
5405 if (wp->w_p_rl)
5406 {
5407 col -= n_extra;
5408 boguscols -= n_extra;
5409 }
5410 else
5411# endif
5412 {
5413 boguscols += n_extra;
5414 col += n_extra;
5415 }
5416 }
5417 n_extra = 0;
5418 n_attr = 0;
5419 }
5420 else if (n_skip == 0)
5421 {
5422 is_concealing = TRUE;
5423 n_skip = 1;
5424 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005425 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005426 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005427 {
5428 mb_utf8 = TRUE;
5429 u8cc[0] = 0;
5430 c = 0xc0;
5431 }
5432 else
5433 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005434 }
5435 else
5436 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005437 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005438 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005439 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005440
5441 if (n_skip > 0 && did_decrement_ptr)
5442 // not showing the '>', put pointer back to avoid getting stuck
5443 ++ptr;
5444
5445#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 }
5447
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005448#ifdef FEAT_CONCEAL
5449 /* In the cursor line and we may be concealing characters: correct
5450 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005451 if (!did_wcol && draw_state == WL_LINE
5452 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005453 && conceal_cursor_line(wp)
5454 && (int)wp->w_virtcol <= vcol + n_skip)
5455 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005456# ifdef FEAT_RIGHTLEFT
5457 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005458 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005459 else
5460# endif
5461 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005462 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005463 did_wcol = TRUE;
5464 }
5465#endif
5466
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 /* Don't override visual selection highlighting. */
5468 if (n_attr > 0
5469 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005470 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 char_attr = extra_attr;
5472
Bram Moolenaar81695252004-12-29 20:58:21 +00005473#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 /* XIM don't send preedit_start and preedit_end, but they send
5475 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5476 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005477 if (p_imst == IM_ON_THE_SPOT
5478 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005479 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 && (State & INSERT)
5481 && !p_imdisable
5482 && im_is_preediting()
5483 && draw_state == WL_LINE)
5484 {
5485 colnr_T tcol;
5486
5487 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005488 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 else
5490 tcol = preedit_end_col;
5491 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5492 {
5493 if (feedback_old_attr < 0)
5494 {
5495 feedback_col = 0;
5496 feedback_old_attr = char_attr;
5497 }
5498 char_attr = im_get_feedback_attr(feedback_col);
5499 if (char_attr < 0)
5500 char_attr = feedback_old_attr;
5501 feedback_col++;
5502 }
5503 else if (feedback_old_attr >= 0)
5504 {
5505 char_attr = feedback_old_attr;
5506 feedback_old_attr = -1;
5507 feedback_col = 0;
5508 }
5509 }
5510#endif
5511 /*
5512 * Handle the case where we are in column 0 but not on the first
5513 * character of the line and the user wants us to show us a
5514 * special character (via 'listchars' option "precedes:<char>".
5515 */
5516 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005517 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5519#ifdef FEAT_DIFF
5520 && filler_todo <= 0
5521#endif
5522 && draw_state > WL_NR
5523 && c != NUL)
5524 {
5525 c = lcs_prec;
5526 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005527 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5528 {
5529 /* Double-width character being overwritten by the "precedes"
5530 * character, need to fill up half the character. */
5531 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005532 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005533 n_extra = 1;
5534 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005535 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005538 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {
5540 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005541 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005542 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 }
5544 else
5545 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005546 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 {
5548 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005549 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 n_attr3 = 1;
5551 }
5552 }
5553
5554 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005555 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 */
Bram Moolenaarf914a332019-07-20 15:09:56 +02005557 if ((c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005558#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005559 || did_line_attr == 1
5560#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02005561 ) && eol_hl_off == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005563#ifdef FEAT_SEARCH_EXTRA
5564 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005565
5566 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005567 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005568 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005569#endif
5570
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005571 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 * highlight match at end of line. If it's beyond the last
5573 * char on the screen, just overwrite that one (tricky!) Not
5574 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005575#ifdef FEAT_SEARCH_EXTRA
5576 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005577 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005578 prevcol_hl_flag = TRUE;
5579 else
5580 {
5581 cur = wp->w_match_head;
5582 while (cur != NULL)
5583 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005584 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005585 {
5586 prevcol_hl_flag = TRUE;
5587 break;
5588 }
5589 cur = cur->next;
5590 }
5591 }
5592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005594 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005595 && (VIsual_mode != Ctrl_V
5596 || lnum == VIsual.lnum
5597 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005598 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599#ifdef FEAT_SEARCH_EXTRA
5600 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005601 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005602# ifdef FEAT_SYN_HL
5603 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5604 && !(wp == curwin && VIsual_active))
5605# endif
5606# ifdef FEAT_DIFF
5607 && diff_hlf == (hlf_T)0
5608# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005609# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005610 && did_line_attr <= 1
5611# endif
5612 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613#endif
5614 ))
5615 {
5616 int n = 0;
5617
5618#ifdef FEAT_RIGHTLEFT
5619 if (wp->w_p_rl)
5620 {
5621 if (col < 0)
5622 n = 1;
5623 }
5624 else
5625#endif
5626 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005627 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 n = -1;
5629 }
5630 if (n != 0)
5631 {
5632 /* At the window boundary, highlight the last character
5633 * instead (better than nothing). */
5634 off += n;
5635 col += n;
5636 }
5637 else
5638 {
5639 /* Add a blank character to highlight. */
5640 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 if (enc_utf8)
5642 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 }
5644#ifdef FEAT_SEARCH_EXTRA
5645 if (area_attr == 0)
5646 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005647 /* Use attributes from match with highest priority among
5648 * 'search_hl' and the match list. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005649 cur = wp->w_match_head;
5650 shl_flag = FALSE;
5651 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005652 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005653 if (shl_flag == FALSE
5654 && ((cur != NULL
5655 && cur->priority > SEARCH_HL_PRIORITY)
5656 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005657 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005658 shl = &search_hl;
5659 shl_flag = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02005660 if (screen_line_flags & SLF_POPUP)
5661 continue; // do not use search_hl
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005662 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005663 else
5664 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005665 if ((ptr - line) - 1 == (long)shl->startcol
5666 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005667 char_attr = shl->attr;
5668 if (shl != &search_hl && cur != NULL)
5669 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 }
5672#endif
5673 ScreenAttrs[off] = char_attr;
5674#ifdef FEAT_RIGHTLEFT
5675 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005676 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005678 --off;
5679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 else
5681#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005682 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005684 ++off;
5685 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005686 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005687 eol_hl_off = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690
Bram Moolenaar91170f82006-05-05 21:15:17 +00005691 /*
5692 * At end of the text line.
5693 */
5694 if (c == NUL)
5695 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005696#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005697 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005698 if (wp->w_p_wrap)
5699 v = wp->w_skipcol;
5700 else
5701 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005702
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005703 /* check if line ends before left margin */
5704 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005705 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005706#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005707 // Get rid of the boguscols now, we want to draw until the right
5708 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005709 col -= boguscols;
5710 boguscols = 0;
5711#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005712
5713 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005714 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005715
5716 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005717 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5718 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005719 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005720 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005721 || draw_color_col
5722 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005723# ifdef FEAT_RIGHTLEFT
5724 && !wp->w_p_rl
5725# endif
5726 )
5727 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005728 int rightmost_vcol = 0;
5729 int i;
5730
5731 if (wp->w_p_cuc)
5732 rightmost_vcol = wp->w_virtcol;
5733 if (draw_color_col)
5734 /* determine rightmost colorcolumn to possibly draw */
5735 for (i = 0; color_cols[i] >= 0; ++i)
5736 if (rightmost_vcol < color_cols[i])
5737 rightmost_vcol = color_cols[i];
5738
Bram Moolenaar02631462017-09-22 15:20:32 +02005739 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005740 {
5741 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005742 if (enc_utf8)
5743 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005744 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005745 if (draw_color_col)
5746 draw_color_col = advance_color_col(VCOL_HLC,
5747 &color_cols);
5748
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005749 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005750 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005751 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005752 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005753 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005754 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005755
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005756 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005757 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005758
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005759 ++vcol;
5760 }
5761 }
5762#endif
5763
Bram Moolenaar53f81742017-09-22 14:35:51 +02005764 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005765 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 row++;
5767
5768 /*
5769 * Update w_cline_height and w_cline_folded if the cursor line was
5770 * updated (saves a call to plines() later).
5771 */
5772 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5773 {
5774 curwin->w_cline_row = startrow;
5775 curwin->w_cline_height = row - startrow;
5776#ifdef FEAT_FOLDING
5777 curwin->w_cline_folded = FALSE;
5778#endif
5779 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5780 }
5781
5782 break;
5783 }
5784
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005785 // Show "extends" character from 'listchars' if beyond the line end and
5786 // 'list' is set.
5787 if (lcs_ext != NUL
5788 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 && !wp->w_p_wrap
5790#ifdef FEAT_DIFF
5791 && filler_todo <= 0
5792#endif
5793 && (
5794#ifdef FEAT_RIGHTLEFT
5795 wp->w_p_rl ? col == 0 :
5796#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005797 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005799 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5801 {
5802 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005803 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005805 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 {
5807 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005808 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005809 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 }
5811 else
5812 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 }
5814
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005815#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005816 /* advance to the next 'colorcolumn' */
5817 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005818 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005819
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005820 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005821 * highlight the cursor position itself.
5822 * Also highlight the 'colorcolumn' if it is different than
5823 * 'cursorcolumn' */
5824 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005825 if (draw_state == WL_LINE && !lnum_in_visual_area
5826 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005827 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005828 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005829 && lnum != wp->w_cursor.lnum)
5830 {
5831 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005832 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005833 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005834 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005835 {
5836 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005837 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005838 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005839 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005840#endif
5841
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 /*
5843 * Store character to be displayed.
5844 * Skip characters that are left of the screen for 'nowrap'.
5845 */
5846 vcol_prev = vcol;
5847 if (draw_state < WL_LINE || n_skip <= 0)
5848 {
5849 /*
5850 * Store the character.
5851 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005852#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5854 {
5855 /* A double-wide character is: put first halve in left cell. */
5856 --off;
5857 --col;
5858 }
5859#endif
5860 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005862 {
5863 if ((mb_c & 0xff00) == 0x8e00)
5864 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 else if (enc_utf8)
5868 {
5869 if (mb_utf8)
5870 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005871 int i;
5872
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005874 if ((c & 0xff) == 0)
5875 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005876 for (i = 0; i < Screen_mco; ++i)
5877 {
5878 ScreenLinesC[i][off] = u8cc[i];
5879 if (u8cc[i] == 0)
5880 break;
5881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 }
5883 else
5884 ScreenLinesUC[off] = 0;
5885 }
5886 if (multi_attr)
5887 {
5888 ScreenAttrs[off] = multi_attr;
5889 multi_attr = 0;
5890 }
5891 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 ScreenAttrs[off] = char_attr;
5893
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5895 {
5896 /* Need to fill two screen columns. */
5897 ++off;
5898 ++col;
5899 if (enc_utf8)
5900 /* UTF-8: Put a 0 in the second screen char. */
5901 ScreenLines[off] = 0;
5902 else
5903 /* DBCS: Put second byte in the second screen char. */
5904 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005905 if (draw_state > WL_NR
5906#ifdef FEAT_DIFF
5907 && filler_todo <= 0
5908#endif
5909 )
5910 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 /* When "tocol" is halfway a character, set it to the end of
5912 * the character, otherwise highlighting won't stop. */
5913 if (tocol == vcol)
5914 ++tocol;
5915#ifdef FEAT_RIGHTLEFT
5916 if (wp->w_p_rl)
5917 {
5918 /* now it's time to backup one cell */
5919 --off;
5920 --col;
5921 }
5922#endif
5923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924#ifdef FEAT_RIGHTLEFT
5925 if (wp->w_p_rl)
5926 {
5927 --off;
5928 --col;
5929 }
5930 else
5931#endif
5932 {
5933 ++off;
5934 ++col;
5935 }
5936 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005937#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005938 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005939 {
5940 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005941 ++vcol_off;
5942 if (n_extra > 0)
5943 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005944 if (wp->w_p_wrap)
5945 {
5946 /*
5947 * Special voodoo required if 'wrap' is on.
5948 *
5949 * Advance the column indicator to force the line
5950 * drawing to wrap early. This will make the line
5951 * take up the same screen space when parts are concealed,
5952 * so that cursor line computations aren't messed up.
5953 *
5954 * To avoid the fictitious advance of 'col' causing
5955 * trailing junk to be written out of the screen line
5956 * we are building, 'boguscols' keeps track of the number
5957 * of bad columns we have advanced.
5958 */
5959 if (n_extra > 0)
5960 {
5961 vcol += n_extra;
5962# ifdef FEAT_RIGHTLEFT
5963 if (wp->w_p_rl)
5964 {
5965 col -= n_extra;
5966 boguscols -= n_extra;
5967 }
5968 else
5969# endif
5970 {
5971 col += n_extra;
5972 boguscols += n_extra;
5973 }
5974 n_extra = 0;
5975 n_attr = 0;
5976 }
5977
5978
Bram Moolenaar860cae12010-06-05 23:22:07 +02005979 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5980 {
5981 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005982# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005983 if (wp->w_p_rl)
5984 {
5985 --boguscols;
5986 --col;
5987 }
5988 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005989# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005990 {
5991 ++boguscols;
5992 ++col;
5993 }
5994 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005995
5996# ifdef FEAT_RIGHTLEFT
5997 if (wp->w_p_rl)
5998 {
5999 --boguscols;
6000 --col;
6001 }
6002 else
6003# endif
6004 {
6005 ++boguscols;
6006 ++col;
6007 }
6008 }
6009 else
6010 {
6011 if (n_extra > 0)
6012 {
6013 vcol += n_extra;
6014 n_extra = 0;
6015 n_attr = 0;
6016 }
6017 }
6018
6019 }
6020#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 else
6022 --n_skip;
6023
Bram Moolenaar64486672010-05-16 15:46:46 +02006024 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
6025 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00006026 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027#ifdef FEAT_DIFF
6028 && filler_todo <= 0
6029#endif
6030 )
6031 ++vcol;
6032
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006033#ifdef FEAT_SYN_HL
6034 if (vcol_save_attr >= 0)
6035 char_attr = vcol_save_attr;
6036#endif
6037
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 /* restore attributes after "predeces" in 'listchars' */
6039 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
6040 char_attr = saved_attr3;
6041
6042 /* restore attributes after last 'listchars' or 'number' char */
6043 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
6044 char_attr = saved_attr2;
6045
6046 /*
6047 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00006048 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 */
6050 if ((
6051#ifdef FEAT_RIGHTLEFT
6052 wp->w_p_rl ? (col < 0) :
6053#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006054 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 && (*ptr != NUL
6056#ifdef FEAT_DIFF
6057 || filler_todo > 0
6058#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006059 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6061 )
6062 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006063#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006064 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006065 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006066 boguscols = 0;
6067#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006068 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006069 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 ++row;
6072 ++screen_row;
6073
6074 /* When not wrapping and finished diff lines, or when displayed
6075 * '$' and highlighting until last column, break here. */
6076 if ((!wp->w_p_wrap
6077#ifdef FEAT_DIFF
6078 && filler_todo <= 0
6079#endif
6080 ) || lcs_eol_one == -1)
6081 break;
6082
6083 /* When the window is too narrow draw all "@" lines. */
6084 if (draw_state != WL_LINE
6085#ifdef FEAT_DIFF
6086 && filler_todo <= 0
6087#endif
6088 )
6089 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006090 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 row = endrow;
6093 }
6094
6095 /* When line got too long for screen break here. */
6096 if (row == endrow)
6097 {
6098 ++row;
6099 break;
6100 }
6101
6102 if (screen_cur_row == screen_row - 1
6103#ifdef FEAT_DIFF
6104 && filler_todo <= 0
6105#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006106 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 {
6108 /* Remember that the line wraps, used for modeless copy. */
6109 LineWraps[screen_row - 1] = TRUE;
6110
6111 /*
6112 * Special trick to make copy/paste of wrapped lines work with
6113 * xterm/screen: write an extra character beyond the end of
6114 * the line. This will work with all terminal types
6115 * (regardless of the xn,am settings).
6116 * Only do this on a fast tty.
6117 * Only do this if the cursor is on the current line
6118 * (something has been written in it).
6119 * Don't do this for the GUI.
6120 * Don't do this for double-width characters.
6121 * Don't do this for a window not at the right screen border.
6122 */
6123 if (p_tf
6124#ifdef FEAT_GUI
6125 && !gui.in_use
6126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006128 && ((*mb_off2cells)(LineOffset[screen_row],
6129 LineOffset[screen_row] + screen_Columns)
6130 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006132 + (int)Columns - 2,
6133 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006134 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 {
6136 /* First make sure we are at the end of the screen line,
6137 * then output the same character again to let the
6138 * terminal know about the wrap. If the terminal doesn't
6139 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006140 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 screen_char(LineOffset[screen_row - 1]
6142 + (unsigned)Columns - 1,
6143 screen_row - 1, (int)(Columns - 1));
6144
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 /* When there is a multi-byte character, just output a
6146 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006147 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6148 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 out_char(' ');
6150 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 out_char(ScreenLines[LineOffset[screen_row - 1]
6152 + (Columns - 1)]);
6153 /* force a redraw of the first char on the next line */
6154 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6155 screen_start(); /* don't know where cursor is now */
6156 }
6157 }
6158
6159 col = 0;
6160 off = (unsigned)(current_ScreenLine - ScreenLines);
6161#ifdef FEAT_RIGHTLEFT
6162 if (wp->w_p_rl)
6163 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006164 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 off += col;
6166 }
6167#endif
6168
6169 /* reset the drawing state for the start of a wrapped line */
6170 draw_state = WL_START;
6171 saved_n_extra = n_extra;
6172 saved_p_extra = p_extra;
6173 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006174 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 saved_char_attr = char_attr;
6176 n_extra = 0;
6177 lcs_prec_todo = lcs_prec;
6178#ifdef FEAT_LINEBREAK
6179# ifdef FEAT_DIFF
6180 if (filler_todo <= 0)
6181# endif
6182 need_showbreak = TRUE;
6183#endif
6184#ifdef FEAT_DIFF
6185 --filler_todo;
6186 /* When the filler lines are actually below the last line of the
6187 * file, don't draw the line itself, break here. */
6188 if (filler_todo == 0 && wp->w_botfill)
6189 break;
6190#endif
6191 }
6192
6193 } /* for every character in the line */
6194
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006195#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006196 /* After an empty line check first word for capital. */
6197 if (*skipwhite(line) == NUL)
6198 {
6199 capcol_lnum = lnum + 1;
6200 cap_col = 0;
6201 }
6202#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006203#ifdef FEAT_TEXT_PROP
6204 vim_free(text_props);
6205 vim_free(text_prop_idxs);
6206#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006207
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006208 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 return row;
6210}
6211
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006212/*
6213 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006214 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006215 */
6216 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006217comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006218{
6219 int i;
6220
6221 for (i = 0; i < Screen_mco; ++i)
6222 {
6223 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6224 return TRUE;
6225 if (ScreenLinesC[i][off_from] == 0)
6226 break;
6227 }
6228 return FALSE;
6229}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006230
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231/*
6232 * Check whether the given character needs redrawing:
6233 * - the (first byte of the) character is different
6234 * - the attributes are different
6235 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006236 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 */
6238 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006239char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240{
6241 if (cols > 0
6242 && ((ScreenLines[off_from] != ScreenLines[off_to]
6243 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 || (enc_dbcs != 0
6245 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6246 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6247 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6248 : (cols > 1 && ScreenLines[off_from + 1]
6249 != ScreenLines[off_to + 1])))
6250 || (enc_utf8
6251 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6252 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006253 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006254 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6255 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006256 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 return TRUE;
6258 return FALSE;
6259}
6260
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006261#if defined(FEAT_TERMINAL) || defined(PROTO)
6262/*
6263 * Return the index in ScreenLines[] for the current screen line.
6264 */
6265 int
6266screen_get_current_line_off()
6267{
6268 return (int)(current_ScreenLine - ScreenLines);
6269}
6270#endif
6271
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006272#ifdef FEAT_TEXT_PROP
6273/*
6274 * Return TRUE if this position has a higher level popup or this cell is
6275 * transparent in the current popup.
6276 */
6277 static int
6278blocked_by_popup(int row, int col)
6279{
6280 int off;
6281
6282 if (!popup_visible)
6283 return FALSE;
6284 off = row * screen_Columns + col;
6285 return popup_mask[off] > screen_zindex || popup_transparent[off];
6286}
6287#endif
6288
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289/*
6290 * Move one "cooked" screen line to the screen, but only the characters that
6291 * have actually changed. Handle insert/delete character.
6292 * "coloff" gives the first column on the screen for this line.
6293 * "endcol" gives the columns where valid characters are.
6294 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6295 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006296 * "flags" can have bits:
6297 * SLF_POPUP popup window
6298 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6300 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6301 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006302 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006303screen_line(
6304 int row,
6305 int coloff,
6306 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006307 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006308 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309{
6310 unsigned off_from;
6311 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006312 unsigned max_off_from;
6313 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 int force = FALSE; /* force update rest of the line */
6317 int redraw_this /* bool: does character need redraw? */
6318#ifdef FEAT_GUI
6319 = TRUE /* For GUI when while-loop empty */
6320#endif
6321 ;
6322 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 int clear_next = FALSE;
6324 int char_cells; /* 1: normal char */
6325 /* 2: occupies two display cells */
6326# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006328 /* Check for illegal row and col, just in case. */
6329 if (row >= Rows)
6330 row = Rows - 1;
6331 if (endcol > Columns)
6332 endcol = Columns;
6333
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334# ifdef FEAT_CLIPBOARD
6335 clip_may_clear_selection(row, row);
6336# endif
6337
6338 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6339 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006340 max_off_from = off_from + screen_Columns;
6341 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342
6343#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006344 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 {
6346 /* Clear rest first, because it's left of the text. */
6347 if (clear_width > 0)
6348 {
6349 while (col <= endcol && ScreenLines[off_to] == ' '
6350 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006351 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 {
6353 ++off_to;
6354 ++col;
6355 }
6356 if (col <= endcol)
6357 screen_fill(row, row + 1, col + coloff,
6358 endcol + coloff + 1, ' ', ' ', 0);
6359 }
6360 col = endcol + 1;
6361 off_to = LineOffset[row] + col + coloff;
6362 off_from += col;
6363 endcol = (clear_width > 0 ? clear_width : -clear_width);
6364 }
6365#endif /* FEAT_RIGHTLEFT */
6366
6367 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6368
6369 while (col < endcol)
6370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006372 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 else
6374 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
6376 redraw_this = redraw_next;
6377 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6378 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6379
6380#ifdef FEAT_GUI
6381 /* If the next character was bold, then redraw the current character to
6382 * remove any pixels that might have spilt over into us. This only
6383 * happens in the GUI.
6384 */
6385 if (redraw_next && gui.in_use)
6386 {
6387 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006388 if (hl > HL_ALL)
6389 hl = syn_attr2attr(hl);
6390 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 redraw_this = TRUE;
6392 }
6393#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006394#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006395 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +02006396 redraw_this = FALSE;
6397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 if (redraw_this)
6399 {
6400 /*
6401 * Special handling when 'xs' termcap flag set (hpterm):
6402 * Attributes for characters are stored at the position where the
6403 * cursor is when writing the highlighting code. The
6404 * start-highlighting code must be written with the cursor on the
6405 * first highlighted character. The stop-highlighting code must
6406 * be written with the cursor just after the last highlighted
6407 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006408 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 * to clear the rest of the line, and force redrawing it
6410 * completely.
6411 */
6412 if ( p_wiv
6413 && !force
6414#ifdef FEAT_GUI
6415 && !gui.in_use
6416#endif
6417 && ScreenAttrs[off_to] != 0
6418 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6419 {
6420 /*
6421 * Need to remove highlighting attributes here.
6422 */
6423 windgoto(row, col + coloff);
6424 out_str(T_CE); /* clear rest of this screen line */
6425 screen_start(); /* don't know where cursor is now */
6426 force = TRUE; /* force redraw of rest of the line */
6427 redraw_next = TRUE; /* or else next char would miss out */
6428
6429 /*
6430 * If the previous character was highlighted, need to stop
6431 * highlighting at this character.
6432 */
6433 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6434 {
6435 screen_attr = ScreenAttrs[off_to - 1];
6436 term_windgoto(row, col + coloff);
6437 screen_stop_highlight();
6438 }
6439 else
6440 screen_attr = 0; /* highlighting has stopped */
6441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 if (enc_dbcs != 0)
6443 {
6444 /* Check if overwriting a double-byte with a single-byte or
6445 * the other way around requires another character to be
6446 * redrawn. For UTF-8 this isn't needed, because comparing
6447 * ScreenLinesUC[] is sufficient. */
6448 if (char_cells == 1
6449 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006450 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 {
6452 /* Writing a single-cell character over a double-cell
6453 * character: need to redraw the next cell. */
6454 ScreenLines[off_to + 1] = 0;
6455 redraw_next = TRUE;
6456 }
6457 else if (char_cells == 2
6458 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006459 && (*mb_off2cells)(off_to, max_off_to) == 1
6460 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 {
6462 /* Writing the second half of a double-cell character over
6463 * a double-cell character: need to redraw the second
6464 * cell. */
6465 ScreenLines[off_to + 2] = 0;
6466 redraw_next = TRUE;
6467 }
6468
6469 if (enc_dbcs == DBCS_JPNU)
6470 ScreenLines2[off_to] = ScreenLines2[off_from];
6471 }
6472 /* When writing a single-width character over a double-width
6473 * character and at the end of the redrawn text, need to clear out
6474 * the right halve of the old character.
6475 * Also required when writing the right halve of a double-width
6476 * char over the left halve of an existing one. */
6477 if (has_mbyte && col + char_cells == endcol
6478 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006479 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006481 && (*mb_off2cells)(off_to, max_off_to) == 1
6482 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484
6485 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 if (enc_utf8)
6487 {
6488 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6489 if (ScreenLinesUC[off_from] != 0)
6490 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006491 int i;
6492
6493 for (i = 0; i < Screen_mco; ++i)
6494 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 }
6496 }
6497 if (char_cells == 2)
6498 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499
6500#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006501 /* The bold trick makes a single column of pixels appear in the
6502 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 * character should be redrawn too. This happens for our own GUI
6504 * and for some xterms. */
6505 if (
6506# ifdef FEAT_GUI
6507 gui.in_use
6508# endif
6509# if defined(FEAT_GUI) && defined(UNIX)
6510 ||
6511# endif
6512# ifdef UNIX
6513 term_is_xterm
6514# endif
6515 )
6516 {
6517 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006518 if (hl > HL_ALL)
6519 hl = syn_attr2attr(hl);
6520 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 redraw_next = TRUE;
6522 }
6523#endif
6524 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006525
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006526 /* For simplicity set the attributes of second half of a
6527 * double-wide character equal to the first half. */
6528 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006530
6531 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 screen_char(off_to, row, col + coloff);
6535 }
6536 else if ( p_wiv
6537#ifdef FEAT_GUI
6538 && !gui.in_use
6539#endif
6540 && col + coloff > 0)
6541 {
6542 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6543 {
6544 /*
6545 * Don't output stop-highlight when moving the cursor, it will
6546 * stop the highlighting when it should continue.
6547 */
6548 screen_attr = 0;
6549 }
6550 else if (screen_attr != 0)
6551 screen_stop_highlight();
6552 }
6553
6554 off_to += CHAR_CELLS;
6555 off_from += CHAR_CELLS;
6556 col += CHAR_CELLS;
6557 }
6558
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 if (clear_next)
6560 {
6561 /* Clear the second half of a double-wide character of which the left
6562 * half was overwritten with a single-wide character. */
6563 ScreenLines[off_to] = ' ';
6564 if (enc_utf8)
6565 ScreenLinesUC[off_to] = 0;
6566 screen_char(off_to, row, col + coloff);
6567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568
6569 if (clear_width > 0
6570#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006571 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572#endif
6573 )
6574 {
6575#ifdef FEAT_GUI
6576 int startCol = col;
6577#endif
6578
6579 /* blank out the rest of the line */
6580 while (col < clear_width && ScreenLines[off_to] == ' '
6581 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006582 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 {
6584 ++off_to;
6585 ++col;
6586 }
6587 if (col < clear_width)
6588 {
6589#ifdef FEAT_GUI
6590 /*
6591 * In the GUI, clearing the rest of the line may leave pixels
6592 * behind if the first character cleared was bold. Some bold
6593 * fonts spill over the left. In this case we redraw the previous
6594 * character too. If we didn't skip any blanks above, then we
6595 * only redraw if the character wasn't already redrawn anyway.
6596 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006597 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 {
6599 hl = ScreenAttrs[off_to];
6600 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006601 {
6602 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006603
Bram Moolenaar9c697322006-10-09 20:11:17 +00006604 if (enc_utf8)
6605 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6606 * that its width is 2. */
6607 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6608 else if (enc_dbcs != 0)
6609 {
6610 /* find previous character by counting from first
6611 * column and get its width. */
6612 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006613 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006614
6615 while (off < off_to)
6616 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006617 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006618 off += prev_cells;
6619 }
6620 }
6621
6622 if (enc_dbcs != 0 && prev_cells > 1)
6623 screen_char_2(off_to - prev_cells, row,
6624 col + coloff - prev_cells);
6625 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006626 screen_char(off_to - prev_cells, row,
6627 col + coloff - prev_cells);
6628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 }
6630#endif
6631 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6632 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 off_to += clear_width - col;
6634 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 }
6636 }
6637
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006638 if (clear_width > 0
6639#ifdef FEAT_TEXT_PROP
6640 && !(flags & SLF_POPUP) // no separator for popup window
6641#endif
6642 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006644 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006645 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006646 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006648#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006649 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006652 int c;
6653
6654 c = fillchar_vsep(&hl);
6655 if (ScreenLines[off_to] != (schar_T)c
6656 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6657 != (c >= 0x80 ? c : 0))
6658 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006660 ScreenLines[off_to] = c;
6661 ScreenAttrs[off_to] = hl;
6662 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006664 if (c >= 0x80)
6665 {
6666 ScreenLinesUC[off_to] = c;
6667 ScreenLinesC[0][off_to] = 0;
6668 }
6669 else
6670 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006672 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 }
6675 }
6676 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 LineWraps[row] = FALSE;
6678 }
6679}
6680
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006681#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006683 * Mirror text "str" for right-left displaying.
6684 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006686 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006687rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688{
6689 char_u *p1, *p2;
6690 int t;
6691
6692 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6693 {
6694 t = *p1;
6695 *p1 = *p2;
6696 *p2 = t;
6697 }
6698}
6699#endif
6700
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701/*
6702 * mark all status lines for redraw; used after first :cd
6703 */
6704 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006705status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706{
6707 win_T *wp;
6708
Bram Moolenaar29323592016-07-24 22:04:11 +02006709 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 if (wp->w_status_height)
6711 {
6712 wp->w_redr_status = TRUE;
6713 redraw_later(VALID);
6714 }
6715}
6716
6717/*
6718 * mark all status lines of the current buffer for redraw
6719 */
6720 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006721status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
6723 win_T *wp;
6724
Bram Moolenaar29323592016-07-24 22:04:11 +02006725 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6727 {
6728 wp->w_redr_status = TRUE;
6729 redraw_later(VALID);
6730 }
6731}
6732
6733/*
6734 * Redraw all status lines that need to be redrawn.
6735 */
6736 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006737redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
6739 win_T *wp;
6740
Bram Moolenaar29323592016-07-24 22:04:11 +02006741 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006743 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006744 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006745 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747
Bram Moolenaar4033c552017-09-16 20:54:51 +02006748#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749/*
6750 * Redraw all status lines at the bottom of frame "frp".
6751 */
6752 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006753win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754{
6755 if (frp->fr_layout == FR_LEAF)
6756 frp->fr_win->w_redr_status = TRUE;
6757 else if (frp->fr_layout == FR_ROW)
6758 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006759 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 win_redraw_last_status(frp);
6761 }
6762 else /* frp->fr_layout == FR_COL */
6763 {
6764 frp = frp->fr_child;
6765 while (frp->fr_next != NULL)
6766 frp = frp->fr_next;
6767 win_redraw_last_status(frp);
6768 }
6769}
6770#endif
6771
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772/*
6773 * Draw the verticap separator right of window "wp" starting with line "row".
6774 */
6775 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006776draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
6778 int hl;
6779 int c;
6780
6781 if (wp->w_vsep_width)
6782 {
6783 /* draw the vertical separator right of this window */
6784 c = fillchar_vsep(&hl);
6785 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6786 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6787 c, ' ', hl);
6788 }
6789}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790
6791#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006792static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793
6794/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006795 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 */
6797 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006798status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799{
6800 int len = 0;
6801
6802#ifdef FEAT_MENU
6803 int emenu = (xp->xp_context == EXPAND_MENUS
6804 || xp->xp_context == EXPAND_MENUNAMES);
6805
6806 /* Check for menu separators - replace with '|'. */
6807 if (emenu && menu_is_separator(s))
6808 return 1;
6809#endif
6810
6811 while (*s != NUL)
6812 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006813 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006814 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006815 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 }
6817
6818 return len;
6819}
6820
6821/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006822 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006823 * These are backslashes used for escaping. Do show backslashes in help tags.
6824 */
6825 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006826skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006827{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006828 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006829#ifdef FEAT_MENU
6830 || ((xp->xp_context == EXPAND_MENUS
6831 || xp->xp_context == EXPAND_MENUNAMES)
6832 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6833#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006834 )
6835 {
6836#ifndef BACKSLASH_IN_FILENAME
6837 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6838 return 2;
6839#endif
6840 return 1;
6841 }
6842 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006843}
6844
6845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 * Show wildchar matches in the status line.
6847 * Show at least the "match" item.
6848 * We start at item 'first_match' in the list and show all matches that fit.
6849 *
6850 * If inversion is possible we use it. Else '=' characters are used.
6851 */
6852 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006853win_redr_status_matches(
6854 expand_T *xp,
6855 int num_matches,
6856 char_u **matches, /* list of matches */
6857 int match,
6858 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859{
6860#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6861 int row;
6862 char_u *buf;
6863 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006864 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 int fillchar;
6866 int attr;
6867 int i;
6868 int highlight = TRUE;
6869 char_u *selstart = NULL;
6870 int selstart_col = 0;
6871 char_u *selend = NULL;
6872 static int first_match = 0;
6873 int add_left = FALSE;
6874 char_u *s;
6875#ifdef FEAT_MENU
6876 int emenu;
6877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879
6880 if (matches == NULL) /* interrupted completion? */
6881 return;
6882
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006883 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006884 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006885 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006886 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 if (buf == NULL)
6888 return;
6889
6890 if (match == -1) /* don't show match but original text */
6891 {
6892 match = 0;
6893 highlight = FALSE;
6894 }
6895 /* count 1 for the ending ">" */
6896 clen = status_match_len(xp, L_MATCH(match)) + 3;
6897 if (match == 0)
6898 first_match = 0;
6899 else if (match < first_match)
6900 {
6901 /* jumping left, as far as we can go */
6902 first_match = match;
6903 add_left = TRUE;
6904 }
6905 else
6906 {
6907 /* check if match fits on the screen */
6908 for (i = first_match; i < match; ++i)
6909 clen += status_match_len(xp, L_MATCH(i)) + 2;
6910 if (first_match > 0)
6911 clen += 2;
6912 /* jumping right, put match at the left */
6913 if ((long)clen > Columns)
6914 {
6915 first_match = match;
6916 /* if showing the last match, we can add some on the left */
6917 clen = 2;
6918 for (i = match; i < num_matches; ++i)
6919 {
6920 clen += status_match_len(xp, L_MATCH(i)) + 2;
6921 if ((long)clen >= Columns)
6922 break;
6923 }
6924 if (i == num_matches)
6925 add_left = TRUE;
6926 }
6927 }
6928 if (add_left)
6929 while (first_match > 0)
6930 {
6931 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6932 if ((long)clen >= Columns)
6933 break;
6934 --first_match;
6935 }
6936
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006937 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938
6939 if (first_match == 0)
6940 {
6941 *buf = NUL;
6942 len = 0;
6943 }
6944 else
6945 {
6946 STRCPY(buf, "< ");
6947 len = 2;
6948 }
6949 clen = len;
6950
6951 i = first_match;
6952 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6953 {
6954 if (i == match)
6955 {
6956 selstart = buf + len;
6957 selstart_col = clen;
6958 }
6959
6960 s = L_MATCH(i);
6961 /* Check for menu separators - replace with '|' */
6962#ifdef FEAT_MENU
6963 emenu = (xp->xp_context == EXPAND_MENUS
6964 || xp->xp_context == EXPAND_MENUNAMES);
6965 if (emenu && menu_is_separator(s))
6966 {
6967 STRCPY(buf + len, transchar('|'));
6968 l = (int)STRLEN(buf + len);
6969 len += l;
6970 clen += l;
6971 }
6972 else
6973#endif
6974 for ( ; *s != NUL; ++s)
6975 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006976 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006978 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 {
6980 STRNCPY(buf + len, s, l);
6981 s += l - 1;
6982 len += l;
6983 }
6984 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 {
6986 STRCPY(buf + len, transchar_byte(*s));
6987 len += (int)STRLEN(buf + len);
6988 }
6989 }
6990 if (i == match)
6991 selend = buf + len;
6992
6993 *(buf + len++) = ' ';
6994 *(buf + len++) = ' ';
6995 clen += 2;
6996 if (++i == num_matches)
6997 break;
6998 }
6999
7000 if (i != num_matches)
7001 {
7002 *(buf + len++) = '>';
7003 ++clen;
7004 }
7005
7006 buf[len] = NUL;
7007
7008 row = cmdline_row - 1;
7009 if (row >= 0)
7010 {
7011 if (wild_menu_showing == 0)
7012 {
7013 if (msg_scrolled > 0)
7014 {
7015 /* Put the wildmenu just above the command line. If there is
7016 * no room, scroll the screen one line up. */
7017 if (cmdline_row == Rows - 1)
7018 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02007019 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 ++msg_scrolled;
7021 }
7022 else
7023 {
7024 ++cmdline_row;
7025 ++row;
7026 }
7027 wild_menu_showing = WM_SCROLLED;
7028 }
7029 else
7030 {
7031 /* Create status line if needed by setting 'laststatus' to 2.
7032 * Set 'winminheight' to zero to avoid that the window is
7033 * resized. */
7034 if (lastwin->w_status_height == 0)
7035 {
7036 save_p_ls = p_ls;
7037 save_p_wmh = p_wmh;
7038 p_ls = 2;
7039 p_wmh = 0;
7040 last_status(FALSE);
7041 }
7042 wild_menu_showing = WM_SHOWN;
7043 }
7044 }
7045
7046 screen_puts(buf, row, 0, attr);
7047 if (selstart != NULL && highlight)
7048 {
7049 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007050 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 }
7052
7053 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7054 }
7055
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 vim_free(buf);
7058}
7059#endif
7060
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061/*
7062 * Redraw the status line of window wp.
7063 *
7064 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007065 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7066 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007068 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007069win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
7071 int row;
7072 char_u *p;
7073 int len;
7074 int fillchar;
7075 int attr;
7076 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007077 static int busy = FALSE;
7078
7079 /* It's possible to get here recursively when 'statusline' (indirectly)
7080 * invokes ":redrawstatus". Simply ignore the call then. */
7081 if (busy)
7082 return;
7083 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084
7085 wp->w_redr_status = FALSE;
7086 if (wp->w_status_height == 0)
7087 {
7088 /* no status line, can only be last window */
7089 redraw_cmdline = TRUE;
7090 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007091 else if (!redrawing()
7092#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007093 // don't update status line when popup menu is visible and may be
7094 // drawn over it, unless it will be redrawn later
7095 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007096#endif
7097 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 {
7099 /* Don't redraw right now, do it later. */
7100 wp->w_redr_status = TRUE;
7101 }
7102#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007103 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 {
7105 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007106 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 }
7108#endif
7109 else
7110 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007111 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007113 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 p = NameBuff;
7115 len = (int)STRLEN(p);
7116
Bram Moolenaard85f2712017-07-28 21:51:57 +02007117 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118#ifdef FEAT_QUICKFIX
7119 || wp->w_p_pvw
7120#endif
7121 || bufIsChanged(wp->w_buffer)
7122 || wp->w_buffer->b_p_ro)
7123 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007124 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007126 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 len += (int)STRLEN(p + len);
7128 }
7129#ifdef FEAT_QUICKFIX
7130 if (wp->w_p_pvw)
7131 {
7132 STRCPY(p + len, _("[Preview]"));
7133 len += (int)STRLEN(p + len);
7134 }
7135#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007136 if (bufIsChanged(wp->w_buffer)
7137#ifdef FEAT_TERMINAL
7138 && !bt_terminal(wp->w_buffer)
7139#endif
7140 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 {
7142 STRCPY(p + len, "[+]");
7143 len += 3;
7144 }
7145 if (wp->w_buffer->b_p_ro)
7146 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007147 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007148 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 }
7150
Bram Moolenaar02631462017-09-22 15:20:32 +02007151 this_ru_col = ru_col - (Columns - wp->w_width);
7152 if (this_ru_col < (wp->w_width + 1) / 2)
7153 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 if (this_ru_col <= 1)
7155 {
7156 p = (char_u *)"<"; /* No room for file name! */
7157 len = 1;
7158 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007159 else if (has_mbyte)
7160 {
7161 int clen = 0, i;
7162
7163 /* Count total number of display cells. */
7164 clen = mb_string2cells(p, -1);
7165
7166 /* Find first character that will fit.
7167 * Going from start to end is much faster for DBCS. */
7168 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7169 i += (*mb_ptr2len)(p + i))
7170 clen -= (*mb_ptr2cells)(p + i);
7171 len = clen;
7172 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007174 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007176 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 }
7178
Bram Moolenaara12a1612019-01-24 16:39:02 +01007179 }
7180 else if (len > this_ru_col - 1)
7181 {
7182 p += len - (this_ru_col - 1);
7183 *p = '<';
7184 len = this_ru_col - 1;
7185 }
7186
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007188 screen_puts(p, row, wp->w_wincol, attr);
7189 screen_fill(row, row + 1, len + wp->w_wincol,
7190 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007192 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7194 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007195 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196
7197#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007198 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199#endif
7200 }
7201
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 /*
7203 * May need to draw the character below the vertical separator.
7204 */
7205 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7206 {
7207 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007208 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 else
7210 fillchar = fillchar_vsep(&attr);
7211 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7212 attr);
7213 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007214 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215}
7216
Bram Moolenaar238a5642006-02-21 22:12:05 +00007217#ifdef FEAT_STL_OPT
7218/*
7219 * Redraw the status line according to 'statusline' and take care of any
7220 * errors encountered.
7221 */
7222 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007223redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007224{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007225 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007226 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007227
7228 /* When called recursively return. This can happen when the statusline
7229 * contains an expression that triggers a redraw. */
7230 if (entered)
7231 return;
7232 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007233
Bram Moolenaara742e082016-04-05 21:10:38 +02007234 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007235 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007236 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007237 {
7238 /* When there is an error disable the statusline, otherwise the
7239 * display is messed up with errors and a redraw triggers the problem
7240 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007241 set_string_option_direct((char_u *)"statusline", -1,
7242 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007243 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007244 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007245 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007246 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007247}
7248#endif
7249
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250/*
7251 * Return TRUE if the status line of window "wp" is connected to the status
7252 * line of the window right of it. If not, then it's a vertical separator.
7253 * Only call if (wp->w_vsep_width != 0).
7254 */
7255 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007256stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257{
7258 frame_T *fr;
7259
7260 fr = wp->w_frame;
7261 while (fr->fr_parent != NULL)
7262 {
7263 if (fr->fr_parent->fr_layout == FR_COL)
7264 {
7265 if (fr->fr_next != NULL)
7266 break;
7267 }
7268 else
7269 {
7270 if (fr->fr_next != NULL)
7271 return TRUE;
7272 }
7273 fr = fr->fr_parent;
7274 }
7275 return FALSE;
7276}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279/*
7280 * Get the value to show for the language mappings, active 'keymap'.
7281 */
7282 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007283get_keymap_str(
7284 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007285 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007286 char_u *buf, /* buffer for the result */
7287 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288{
7289 char_u *p;
7290
7291 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7292 return FALSE;
7293
7294 {
7295#ifdef FEAT_EVAL
7296 buf_T *old_curbuf = curbuf;
7297 win_T *old_curwin = curwin;
7298 char_u *s;
7299
7300 curbuf = wp->w_buffer;
7301 curwin = wp;
7302 STRCPY(buf, "b:keymap_name"); /* must be writable */
7303 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007304 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 --emsg_skip;
7306 curbuf = old_curbuf;
7307 curwin = old_curwin;
7308 if (p == NULL || *p == NUL)
7309#endif
7310 {
7311#ifdef FEAT_KEYMAP
7312 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7313 p = wp->w_buffer->b_p_keymap;
7314 else
7315#endif
7316 p = (char_u *)"lang";
7317 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007318 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 buf[0] = NUL;
7320#ifdef FEAT_EVAL
7321 vim_free(s);
7322#endif
7323 }
7324 return buf[0] != NUL;
7325}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326
7327#if defined(FEAT_STL_OPT) || defined(PROTO)
7328/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007329 * Redraw the status line or ruler of window "wp".
7330 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 */
7332 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007333win_redr_custom(
7334 win_T *wp,
7335 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007337 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 int attr;
7339 int curattr;
7340 int row;
7341 int col = 0;
7342 int maxwidth;
7343 int width;
7344 int n;
7345 int len;
7346 int fillchar;
7347 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007348 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007350 struct stl_hlrec hltab[STL_MAX_ITEM];
7351 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007352 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007353 win_T *ewp;
7354 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355
Bram Moolenaar1d633412013-12-11 15:52:01 +01007356 /* There is a tiny chance that this gets called recursively: When
7357 * redrawing a status line triggers redrawing the ruler or tabline.
7358 * Avoid trouble by not allowing recursion. */
7359 if (entered)
7360 return;
7361 entered = TRUE;
7362
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007364 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007366 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007367 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007368 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007369 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007370 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007371 maxwidth = Columns;
7372# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007373 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007376 else
7377 {
7378 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007379 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007380 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007381
7382 if (draw_ruler)
7383 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007384 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007385 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007386 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007387 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007388 if (*++stl == '-')
7389 stl++;
7390 if (atoi((char *)stl))
7391 while (VIM_ISDIGIT(*stl))
7392 stl++;
7393 if (*stl++ != '(')
7394 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007395 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007396 col = ru_col - (Columns - wp->w_width);
7397 if (col < (wp->w_width + 1) / 2)
7398 col = (wp->w_width + 1) / 2;
7399 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007400 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007401 {
7402 row = Rows - 1;
7403 --maxwidth; /* writing in last column may cause scrolling */
7404 fillchar = ' ';
7405 attr = 0;
7406 }
7407
7408# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007409 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007410# endif
7411 }
7412 else
7413 {
7414 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007415 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007416 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007417 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007418# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007419 use_sandbox = was_set_insecurely((char_u *)"statusline",
7420 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007421# endif
7422 }
7423
Bram Moolenaar53f81742017-09-22 14:35:51 +02007424 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007425 }
7426
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007428 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429
Bram Moolenaar61452852011-02-01 18:01:11 +01007430 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7431 * the cursor away and back. */
7432 ewp = wp == NULL ? curwin : wp;
7433 p_crb_save = ewp->w_p_crb;
7434 ewp->w_p_crb = FALSE;
7435
Bram Moolenaar362f3562009-11-03 16:20:34 +00007436 /* Make a copy, because the statusline may include a function call that
7437 * might change the option value and free the memory. */
7438 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007439 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007440 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007441 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007442 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007443 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007445 /* Make all characters printable. */
7446 p = transstr(buf);
7447 if (p != NULL)
7448 {
7449 vim_strncpy(buf, p, sizeof(buf) - 1);
7450 vim_free(p);
7451 }
7452
7453 /* fill up with "fillchar" */
7454 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007455 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 ++width;
7459 }
7460 buf[len] = NUL;
7461
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007462 /*
7463 * Draw each snippet with the specified highlighting.
7464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 curattr = attr;
7466 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007467 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007469 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 screen_puts_len(p, len, row, col, curattr);
7471 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007472 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007474 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007476 else if (hltab[n].userhl < 0)
7477 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007478#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007479 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7480 && wp->w_status_height != 0)
7481 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007482 else if (wp != NULL && bt_terminal(wp->w_buffer)
7483 && wp->w_status_height != 0)
7484 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007485#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007486 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007487 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007489 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 }
7491 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007492
7493 if (wp == NULL)
7494 {
7495 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7496 col = 0;
7497 len = 0;
7498 p = buf;
7499 fillchar = 0;
7500 for (n = 0; tabtab[n].start != NULL; n++)
7501 {
7502 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7503 while (col < len)
7504 TabPageIdxs[col++] = fillchar;
7505 p = tabtab[n].start;
7506 fillchar = tabtab[n].userhl;
7507 }
7508 while (col < Columns)
7509 TabPageIdxs[col++] = fillchar;
7510 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007511
7512theend:
7513 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514}
7515
7516#endif /* FEAT_STL_OPT */
7517
7518/*
7519 * Output a single character directly to the screen and update ScreenLines.
7520 */
7521 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007522screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 char_u buf[MB_MAXBYTES + 1];
7525
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007526 if (has_mbyte)
7527 buf[(*mb_char2bytes)(c, buf)] = NUL;
7528 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007529 {
7530 buf[0] = c;
7531 buf[1] = NUL;
7532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 screen_puts(buf, row, col, attr);
7534}
7535
7536/*
7537 * Get a single character directly from ScreenLines into "bytes[]".
7538 * Also return its attribute in *attrp;
7539 */
7540 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007541screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542{
7543 unsigned off;
7544
7545 /* safety check */
7546 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7547 {
7548 off = LineOffset[row] + col;
7549 *attrp = ScreenAttrs[off];
7550 bytes[0] = ScreenLines[off];
7551 bytes[1] = NUL;
7552
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 if (enc_utf8 && ScreenLinesUC[off] != 0)
7554 bytes[utfc_char2bytes(off, bytes)] = NUL;
7555 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7556 {
7557 bytes[0] = ScreenLines[off];
7558 bytes[1] = ScreenLines2[off];
7559 bytes[2] = NUL;
7560 }
7561 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7562 {
7563 bytes[1] = ScreenLines[off + 1];
7564 bytes[2] = NUL;
7565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 }
7567}
7568
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007569/*
7570 * Return TRUE if composing characters for screen posn "off" differs from
7571 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007572 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007573 */
7574 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007575screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007576{
7577 int i;
7578
7579 for (i = 0; i < Screen_mco; ++i)
7580 {
7581 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7582 return TRUE;
7583 if (u8cc[i] == 0)
7584 break;
7585 }
7586 return FALSE;
7587}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007588
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589/*
7590 * Put string '*text' on the screen at position 'row' and 'col', with
7591 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7592 * Note: only outputs within one row, message is truncated at screen boundary!
7593 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7594 */
7595 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007596screen_puts(
7597 char_u *text,
7598 int row,
7599 int col,
7600 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601{
7602 screen_puts_len(text, -1, row, col, attr);
7603}
7604
7605/*
7606 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7607 * a NUL.
7608 */
7609 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007610screen_puts_len(
7611 char_u *text,
7612 int textlen,
7613 int row,
7614 int col,
7615 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616{
7617 unsigned off;
7618 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007619 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007621 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 int mbyte_blen = 1;
7623 int mbyte_cells = 1;
7624 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007625 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007627#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007629 int pc, nc, nc1;
7630 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007632 int force_redraw_this;
7633 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007634 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007636 // Safety check. The check for negative row and column is to fix issue
7637 // #4102. TODO: find out why row/col could be negative.
7638 if (ScreenLines == NULL
7639 || row >= screen_Rows || row < 0
7640 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007642 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643
Bram Moolenaarc236c162008-07-13 17:41:49 +00007644 /* When drawing over the right halve of a double-wide char clear out the
7645 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007646 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007647#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007648 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007649#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007650 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007651 {
7652 ScreenLines[off - 1] = ' ';
7653 ScreenAttrs[off - 1] = 0;
7654 if (enc_utf8)
7655 {
7656 ScreenLinesUC[off - 1] = 0;
7657 ScreenLinesC[0][off - 1] = 0;
7658 }
7659 /* redraw the previous cell, make it empty */
7660 screen_char(off - 1, row, col - 1);
7661 /* force the cell at "col" to be redrawn */
7662 force_redraw_next = TRUE;
7663 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007664
Bram Moolenaar367329b2007-08-30 11:53:22 +00007665 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007666 while (col < screen_Columns
7667 && (len < 0 || (int)(ptr - text) < len)
7668 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 {
7670 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 /* check if this is the first byte of a multibyte */
7672 if (has_mbyte)
7673 {
7674 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007675 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007677 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7679 mbyte_cells = 1;
7680 else if (enc_dbcs != 0)
7681 mbyte_cells = mbyte_blen;
7682 else /* enc_utf8 */
7683 {
7684 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007685 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 (int)((text + len) - ptr));
7687 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007688 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007690#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7692 {
7693 /* Do Arabic shaping. */
7694 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7695 {
7696 /* Past end of string to be displayed. */
7697 nc = NUL;
7698 nc1 = NUL;
7699 }
7700 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007701 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007702 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7703 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007704 nc1 = pcc[0];
7705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 pc = prev_c;
7707 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007708 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 }
7710 else
7711 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007712#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007713 if (col + mbyte_cells > screen_Columns)
7714 {
7715 /* Only 1 cell left, but character requires 2 cells:
7716 * display a '>' in the last column to avoid wrapping. */
7717 c = '>';
7718 mbyte_cells = 1;
7719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 }
7721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007723 force_redraw_this = force_redraw_next;
7724 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007725
7726 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 || (mbyte_cells == 2
7728 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7729 || (enc_dbcs == DBCS_JPNU
7730 && c == 0x8e
7731 && ScreenLines2[off] != ptr[1])
7732 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007733 && (ScreenLinesUC[off] !=
7734 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7735 || (ScreenLinesUC[off] != 0
7736 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007738 || exmode_active;
7739
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007740 if ((need_redraw || force_redraw_this)
7741#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007742 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007743#endif
7744 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 {
7746#if defined(FEAT_GUI) || defined(UNIX)
7747 /* The bold trick makes a single row of pixels appear in the next
7748 * character. When a bold character is removed, the next
7749 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007750 * and for some xterms. */
7751 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752# ifdef FEAT_GUI
7753 gui.in_use
7754# endif
7755# if defined(FEAT_GUI) && defined(UNIX)
7756 ||
7757# endif
7758# ifdef UNIX
7759 term_is_xterm
7760# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007761 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007763 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007765 if (n > HL_ALL)
7766 n = syn_attr2attr(n);
7767 if (n & HL_BOLD)
7768 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 }
7770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 /* When at the end of the text and overwriting a two-cell
7772 * character with a one-cell character, need to clear the next
7773 * cell. Also when overwriting the left halve of a two-cell char
7774 * with the right halve of a two-cell char. Do this only once
7775 * (mb_off2cells() may return 2 on the right halve). */
7776 if (clear_next_cell)
7777 clear_next_cell = FALSE;
7778 else if (has_mbyte
7779 && (len < 0 ? ptr[mbyte_blen] == NUL
7780 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007781 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007783 && (*mb_off2cells)(off, max_off) == 1
7784 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 clear_next_cell = TRUE;
7786
7787 /* Make sure we never leave a second byte of a double-byte behind,
7788 * it confuses mb_off2cells(). */
7789 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007790 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007792 && (*mb_off2cells)(off, max_off) == 1
7793 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 ScreenLines[off] = c;
7796 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 if (enc_utf8)
7798 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007799 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 ScreenLinesUC[off] = 0;
7801 else
7802 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007803 int i;
7804
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007806 for (i = 0; i < Screen_mco; ++i)
7807 {
7808 ScreenLinesC[i][off] = u8cc[i];
7809 if (u8cc[i] == 0)
7810 break;
7811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 }
7813 if (mbyte_cells == 2)
7814 {
7815 ScreenLines[off + 1] = 0;
7816 ScreenAttrs[off + 1] = attr;
7817 }
7818 screen_char(off, row, col);
7819 }
7820 else if (mbyte_cells == 2)
7821 {
7822 ScreenLines[off + 1] = ptr[1];
7823 ScreenAttrs[off + 1] = attr;
7824 screen_char_2(off, row, col);
7825 }
7826 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7827 {
7828 ScreenLines2[off] = ptr[1];
7829 screen_char(off, row, col);
7830 }
7831 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 screen_char(off, row, col);
7833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 if (has_mbyte)
7835 {
7836 off += mbyte_cells;
7837 col += mbyte_cells;
7838 ptr += mbyte_blen;
7839 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007840 {
7841 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007843 len = -1;
7844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 }
7846 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {
7848 ++off;
7849 ++col;
7850 ++ptr;
7851 }
7852 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007853
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007854 /* If we detected the next character needs to be redrawn, but the text
7855 * doesn't extend up to there, update the character here. */
7856 if (force_redraw_next && col < screen_Columns)
7857 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007858 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7859 screen_char_2(off, row, col);
7860 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007861 screen_char(off, row, col);
7862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863}
7864
7865#ifdef FEAT_SEARCH_EXTRA
7866/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007867 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 */
7869 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007870start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871{
7872 if (p_hls && !no_hlsearch)
7873 {
7874 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007875 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007876# ifdef FEAT_RELTIME
7877 /* Set the time limit to 'redrawtime'. */
7878 profile_setlimit(p_rdt, &search_hl.tm);
7879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 }
7881}
7882
7883/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007884 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 */
7886 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007887end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888{
7889 if (search_hl.rm.regprog != NULL)
7890 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007891 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 search_hl.rm.regprog = NULL;
7893 }
7894}
7895
7896/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007897 * Init for calling prepare_search_hl().
7898 */
7899 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007900init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007901{
7902 matchitem_T *cur;
7903
7904 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7905 * match */
7906 cur = wp->w_match_head;
7907 while (cur != NULL)
7908 {
7909 cur->hl.rm = cur->match;
7910 if (cur->hlg_id == 0)
7911 cur->hl.attr = 0;
7912 else
7913 cur->hl.attr = syn_id2attr(cur->hlg_id);
7914 cur->hl.buf = wp->w_buffer;
7915 cur->hl.lnum = 0;
7916 cur->hl.first_lnum = 0;
7917# ifdef FEAT_RELTIME
7918 /* Set the time limit to 'redrawtime'. */
7919 profile_setlimit(p_rdt, &(cur->hl.tm));
7920# endif
7921 cur = cur->next;
7922 }
7923 search_hl.buf = wp->w_buffer;
7924 search_hl.lnum = 0;
7925 search_hl.first_lnum = 0;
7926 /* time limit is set at the toplevel, for all windows */
7927}
7928
7929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 * Advance to the match in window "wp" line "lnum" or past it.
7931 */
7932 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007933prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007935 matchitem_T *cur; /* points to the match list */
7936 match_T *shl; /* points to search_hl or a match */
7937 int shl_flag; /* flag to indicate whether search_hl
7938 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007939 int pos_inprogress; /* marks that position match search is
7940 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 int n;
7942
7943 /*
7944 * When using a multi-line pattern, start searching at the top
7945 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007946 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007948 cur = wp->w_match_head;
7949 shl_flag = FALSE;
7950 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007952 if (shl_flag == FALSE)
7953 {
7954 shl = &search_hl;
7955 shl_flag = TRUE;
7956 }
7957 else
7958 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 if (shl->rm.regprog != NULL
7960 && shl->lnum == 0
7961 && re_multiline(shl->rm.regprog))
7962 {
7963 if (shl->first_lnum == 0)
7964 {
7965# ifdef FEAT_FOLDING
7966 for (shl->first_lnum = lnum;
7967 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7968 if (hasFoldingWin(wp, shl->first_lnum - 1,
7969 NULL, NULL, TRUE, NULL))
7970 break;
7971# else
7972 shl->first_lnum = wp->w_topline;
7973# endif
7974 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007975 if (cur != NULL)
7976 cur->pos.cur = 0;
7977 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007979 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7980 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007982 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7983 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007984 pos_inprogress = cur == NULL || cur->pos.cur == 0
7985 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 if (shl->lnum != 0)
7987 {
7988 shl->first_lnum = shl->lnum
7989 + shl->rm.endpos[0].lnum
7990 - shl->rm.startpos[0].lnum;
7991 n = shl->rm.endpos[0].col;
7992 }
7993 else
7994 {
7995 ++shl->first_lnum;
7996 n = 0;
7997 }
7998 }
7999 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008000 if (shl != &search_hl && cur != NULL)
8001 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 }
8003}
8004
8005/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008006 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 * Uses shl->buf.
8008 * Sets shl->lnum and shl->rm contents.
8009 * Note: Assumes a previous match is always before "lnum", unless
8010 * shl->lnum is zero.
8011 * Careful: Any pointers for buffer lines will become invalid.
8012 */
8013 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008014next_search_hl(
8015 win_T *win,
8016 match_T *shl, /* points to search_hl or a match */
8017 linenr_T lnum,
8018 colnr_T mincol, /* minimal column for a match */
8019 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020{
8021 linenr_T l;
8022 colnr_T matchcol;
8023 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008024 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008026 // for :{range}s/pat only highlight inside the range
8027 if (lnum < search_first_line || lnum > search_last_line)
8028 {
8029 shl->lnum = 0;
8030 return;
8031 }
8032
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 if (shl->lnum != 0)
8034 {
8035 /* Check for three situations:
8036 * 1. If the "lnum" is below a previous match, start a new search.
8037 * 2. If the previous match includes "mincol", use it.
8038 * 3. Continue after the previous match.
8039 */
8040 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8041 if (lnum > l)
8042 shl->lnum = 0;
8043 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8044 return;
8045 }
8046
8047 /*
8048 * Repeat searching for a match until one is found that includes "mincol"
8049 * or none is found in this line.
8050 */
8051 called_emsg = FALSE;
8052 for (;;)
8053 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008054#ifdef FEAT_RELTIME
8055 /* Stop searching after passing the time limit. */
8056 if (profile_passed_limit(&(shl->tm)))
8057 {
8058 shl->lnum = 0; /* no match found in time */
8059 break;
8060 }
8061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 /* Three situations:
8063 * 1. No useful previous match: search from start of line.
8064 * 2. Not Vi compatible or empty match: continue at next character.
8065 * Break the loop if this is beyond the end of the line.
8066 * 3. Vi compatible searching: continue at end of previous match.
8067 */
8068 if (shl->lnum == 0)
8069 matchcol = 0;
8070 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8071 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008072 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008074 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008075
8076 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008077 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008078 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008080 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 shl->lnum = 0;
8082 break;
8083 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008084 if (has_mbyte)
8085 matchcol += mb_ptr2len(ml);
8086 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008087 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 }
8089 else
8090 matchcol = shl->rm.endpos[0].col;
8091
8092 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008093 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008095 /* Remember whether shl->rm is using a copy of the regprog in
8096 * cur->match. */
8097 int regprog_is_copy = (shl != &search_hl && cur != NULL
8098 && shl == &cur->hl
8099 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008100 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008101
Bram Moolenaarb3414592014-06-17 17:48:32 +02008102 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8103 matchcol,
8104#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008105 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008106#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008107 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008108#endif
8109 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008110 /* Copy the regprog, in case it got freed and recompiled. */
8111 if (regprog_is_copy)
8112 cur->match.regprog = cur->hl.rm.regprog;
8113
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008114 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008115 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008116 /* Error while handling regexp: stop using this regexp. */
8117 if (shl == &search_hl)
8118 {
8119 /* don't free regprog in the match list, it's a copy */
8120 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008121 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008122 }
8123 shl->rm.regprog = NULL;
8124 shl->lnum = 0;
8125 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8126 message */
8127 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008128 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008129 }
8130 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008131 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008132 else
8133 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 if (nmatched == 0)
8135 {
8136 shl->lnum = 0; /* no match found */
8137 break;
8138 }
8139 if (shl->rm.startpos[0].lnum > 0
8140 || shl->rm.startpos[0].col >= mincol
8141 || nmatched > 1
8142 || shl->rm.endpos[0].col > mincol)
8143 {
8144 shl->lnum += shl->rm.startpos[0].lnum;
8145 break; /* useful match found */
8146 }
8147 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008148
8149 // Restore called_emsg for assert_fails().
8150 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152
Bram Moolenaar85077472016-10-16 14:35:48 +02008153/*
8154 * If there is a match fill "shl" and return one.
8155 * Return zero otherwise.
8156 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008157 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008158next_search_hl_pos(
8159 match_T *shl, /* points to a match */
8160 linenr_T lnum,
8161 posmatch_T *posmatch, /* match positions */
8162 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008163{
8164 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008165 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008166
Bram Moolenaarb3414592014-06-17 17:48:32 +02008167 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8168 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008169 llpos_T *pos = &posmatch->pos[i];
8170
8171 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008172 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008173 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008174 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008175 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008176 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008177 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008178 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008179 /* if this match comes before the one at "found" then swap
8180 * them */
8181 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008182 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008183 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008184
Bram Moolenaar85077472016-10-16 14:35:48 +02008185 *pos = posmatch->pos[found];
8186 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008187 }
8188 }
8189 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008190 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008191 }
8192 }
8193 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008194 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008195 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008196 colnr_T start = posmatch->pos[found].col == 0
8197 ? 0 : posmatch->pos[found].col - 1;
8198 colnr_T end = posmatch->pos[found].col == 0
8199 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008200
Bram Moolenaar85077472016-10-16 14:35:48 +02008201 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008202 shl->rm.startpos[0].lnum = 0;
8203 shl->rm.startpos[0].col = start;
8204 shl->rm.endpos[0].lnum = 0;
8205 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008206 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008207 posmatch->cur = found + 1;
8208 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008209 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008210 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008211}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008212#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008213
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008215screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216{
8217 attrentry_T *aep = NULL;
8218
8219 screen_attr = attr;
8220 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008221#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 && termcap_active
8223#endif
8224 )
8225 {
8226#ifdef FEAT_GUI
8227 if (gui.in_use)
8228 {
8229 char buf[20];
8230
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008231 /* The GUI handles this internally. */
8232 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 OUT_STR(buf);
8234 }
8235 else
8236#endif
8237 {
8238 if (attr > HL_ALL) /* special HL attr. */
8239 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008240 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 aep = syn_cterm_attr2entry(attr);
8242 else
8243 aep = syn_term_attr2entry(attr);
8244 if (aep == NULL) /* did ":syntax clear" */
8245 attr = 0;
8246 else
8247 attr = aep->ae_attr;
8248 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008249 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008251 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008252#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008253 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8254 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8255 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008256#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008257 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008258 /* If the Normal FG color has BOLD attribute and the new HL
8259 * has a FG color defined, clear BOLD. */
8260 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008261 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008263 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008264 out_str(T_UCS);
8265 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008266 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8267 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008269 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008271 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008273 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008274 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275
8276 /*
8277 * Output the color or start string after bold etc., in case the
8278 * bold etc. override the color setting.
8279 */
8280 if (aep != NULL)
8281 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008282#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008283 /* When 'termguicolors' is set but fg or bg is unset,
8284 * fall back to the cterm colors. This helps for SpellBad,
8285 * where the GUI uses a red undercurl. */
8286 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008288 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008289 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008290 }
8291 else
8292#endif
8293 if (t_colors > 1)
8294 {
8295 if (aep->ae_u.cterm.fg_color)
8296 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8297 }
8298#ifdef FEAT_TERMGUICOLORS
8299 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8300 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008301 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008302 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 }
8304 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008305#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008306 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008308 if (aep->ae_u.cterm.bg_color)
8309 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8310 }
8311
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008312 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008313 {
8314 if (aep->ae_u.term.start != NULL)
8315 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 }
8317 }
8318 }
8319 }
8320}
8321
8322 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008323screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324{
8325 int do_ME = FALSE; /* output T_ME code */
8326
8327 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008328#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 && termcap_active
8330#endif
8331 )
8332 {
8333#ifdef FEAT_GUI
8334 if (gui.in_use)
8335 {
8336 char buf[20];
8337
8338 /* use internal GUI code */
8339 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8340 OUT_STR(buf);
8341 }
8342 else
8343#endif
8344 {
8345 if (screen_attr > HL_ALL) /* special HL attr. */
8346 {
8347 attrentry_T *aep;
8348
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008349 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {
8351 /*
8352 * Assume that t_me restores the original colors!
8353 */
8354 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008355 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008356#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008357 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8358 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8359 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008360#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008361 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008362#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008363 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8364 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8365 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008366#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008367 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 do_ME = TRUE;
8369 }
8370 else
8371 {
8372 aep = syn_term_attr2entry(screen_attr);
8373 if (aep != NULL && aep->ae_u.term.stop != NULL)
8374 {
8375 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8376 do_ME = TRUE;
8377 else
8378 out_str(aep->ae_u.term.stop);
8379 }
8380 }
8381 if (aep == NULL) /* did ":syntax clear" */
8382 screen_attr = 0;
8383 else
8384 screen_attr = aep->ae_attr;
8385 }
8386
8387 /*
8388 * Often all ending-codes are equal to T_ME. Avoid outputting the
8389 * same sequence several times.
8390 */
8391 if (screen_attr & HL_STANDOUT)
8392 {
8393 if (STRCMP(T_SE, T_ME) == 0)
8394 do_ME = TRUE;
8395 else
8396 out_str(T_SE);
8397 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008398 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008399 {
8400 if (STRCMP(T_UCE, T_ME) == 0)
8401 do_ME = TRUE;
8402 else
8403 out_str(T_UCE);
8404 }
8405 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008406 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 {
8408 if (STRCMP(T_UE, T_ME) == 0)
8409 do_ME = TRUE;
8410 else
8411 out_str(T_UE);
8412 }
8413 if (screen_attr & HL_ITALIC)
8414 {
8415 if (STRCMP(T_CZR, T_ME) == 0)
8416 do_ME = TRUE;
8417 else
8418 out_str(T_CZR);
8419 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008420 if (screen_attr & HL_STRIKETHROUGH)
8421 {
8422 if (STRCMP(T_STE, T_ME) == 0)
8423 do_ME = TRUE;
8424 else
8425 out_str(T_STE);
8426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8428 out_str(T_ME);
8429
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008430#ifdef FEAT_TERMGUICOLORS
8431 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008433 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008434 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008435 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008436 term_bg_rgb_color(cterm_normal_bg_gui_color);
8437 }
8438 else
8439#endif
8440 {
8441 if (t_colors > 1)
8442 {
8443 /* set Normal cterm colors */
8444 if (cterm_normal_fg_color != 0)
8445 term_fg_color(cterm_normal_fg_color - 1);
8446 if (cterm_normal_bg_color != 0)
8447 term_bg_color(cterm_normal_bg_color - 1);
8448 if (cterm_normal_fg_bold)
8449 out_str(T_MD);
8450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 }
8452 }
8453 }
8454 screen_attr = 0;
8455}
8456
8457/*
8458 * Reset the colors for a cterm. Used when leaving Vim.
8459 * The machine specific code may override this again.
8460 */
8461 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008462reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008464 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 {
8466 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008467#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008468 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8469 || cterm_normal_bg_gui_color != INVALCOLOR)
8470 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008471#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 {
8475 out_str(T_OP);
8476 screen_attr = -1;
8477 }
8478 if (cterm_normal_fg_bold)
8479 {
8480 out_str(T_ME);
8481 screen_attr = -1;
8482 }
8483 }
8484}
8485
8486/*
8487 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8488 * using the attributes from ScreenAttrs["off"].
8489 */
8490 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008491screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492{
8493 int attr;
8494
8495 /* Check for illegal values, just in case (could happen just after
8496 * resizing). */
8497 if (row >= screen_Rows || col >= screen_Columns)
8498 return;
8499
Bram Moolenaarae654382019-01-17 21:09:05 +01008500#ifdef FEAT_INS_EXPAND
Bram Moolenaar33796b32019-06-08 16:01:13 +02008501 // Skip if under the popup menu.
8502 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
8503 if (pum_under_menu(row, col)
8504# ifdef FEAT_TEXT_PROP
8505 && screen_zindex <= POPUPMENU_ZINDEX
8506# endif
8507 )
Bram Moolenaarae654382019-01-17 21:09:05 +01008508 return;
8509#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008510#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008511 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02008512 return;
8513#endif
8514
Bram Moolenaar494838a2015-02-10 19:20:37 +01008515 /* Outputting a character in the last cell on the screen may scroll the
8516 * screen up. Only do it when the "xn" termcap property is set, otherwise
8517 * mark the character invalid (update it when scrolled up). */
8518 if (*T_XN == NUL
8519 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520#ifdef FEAT_RIGHTLEFT
8521 /* account for first command-line character in rightleft mode */
8522 && !cmdmsg_rl
8523#endif
8524 )
8525 {
8526 ScreenAttrs[off] = (sattr_T)-1;
8527 return;
8528 }
8529
8530 /*
8531 * Stop highlighting first, so it's easier to move the cursor.
8532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 if (screen_char_attr != 0)
8534 attr = screen_char_attr;
8535 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 attr = ScreenAttrs[off];
8537 if (screen_attr != attr)
8538 screen_stop_highlight();
8539
8540 windgoto(row, col);
8541
8542 if (screen_attr != attr)
8543 screen_start_highlight(attr);
8544
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 if (enc_utf8 && ScreenLinesUC[off] != 0)
8546 {
8547 char_u buf[MB_MAXBYTES + 1];
8548
Bram Moolenaarcb070082016-04-02 22:14:51 +02008549 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008550 {
8551 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008552#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008553 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008554#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008555 )
8556 {
8557 /* Clear the two screen cells. If the character is actually
8558 * single width it won't change the second cell. */
8559 out_str((char_u *)" ");
8560 term_windgoto(row, col);
8561 }
8562 /* not sure where the cursor is after drawing the ambiguous width
8563 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008564 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008565 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008566 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008568
8569 /* Convert the UTF-8 character to bytes and write it. */
8570 buf[utfc_char2bytes(off, buf)] = NUL;
8571 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 }
8573 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 /* double-byte character in single-width cell */
8578 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8579 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 }
8581
8582 screen_cur_col++;
8583}
8584
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585/*
8586 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8587 * on the screen at position 'row' and 'col'.
8588 * The attributes of the first byte is used for all. This is required to
8589 * output the two bytes of a double-byte character with nothing in between.
8590 */
8591 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008592screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593{
8594 /* Check for illegal values (could be wrong when screen was resized). */
8595 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8596 return;
8597
8598 /* Outputting the last character on the screen may scrollup the screen.
8599 * Don't to it! Mark the character invalid (update it when scrolled up) */
8600 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8601 {
8602 ScreenAttrs[off] = (sattr_T)-1;
8603 return;
8604 }
8605
8606 /* Output the first byte normally (positions the cursor), then write the
8607 * second byte directly. */
8608 screen_char(off, row, col);
8609 out_char(ScreenLines[off + 1]);
8610 ++screen_cur_col;
8611}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613/*
8614 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8615 * This uses the contents of ScreenLines[] and doesn't change it.
8616 */
8617 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008618screen_draw_rectangle(
8619 int row,
8620 int col,
8621 int height,
8622 int width,
8623 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624{
8625 int r, c;
8626 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008627 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008629 /* Can't use ScreenLines unless initialized */
8630 if (ScreenLines == NULL)
8631 return;
8632
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 if (invert)
8634 screen_char_attr = HL_INVERSE;
8635 for (r = row; r < row + height; ++r)
8636 {
8637 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008638 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 for (c = col; c < col + width; ++c)
8640 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008641 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 {
8643 screen_char_2(off + c, r, c);
8644 ++c;
8645 }
8646 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 {
8648 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008649 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 }
8652 }
8653 }
8654 screen_char_attr = 0;
8655}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657/*
8658 * Redraw the characters for a vertically split window.
8659 */
8660 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008661redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662{
8663 int col;
8664 int width;
8665
8666# ifdef FEAT_CLIPBOARD
8667 clip_may_clear_selection(row, end - 1);
8668# endif
8669
8670 if (wp == NULL)
8671 {
8672 col = 0;
8673 width = Columns;
8674 }
8675 else
8676 {
8677 col = wp->w_wincol;
8678 width = wp->w_width;
8679 }
8680 screen_draw_rectangle(row, col, end - row, width, FALSE);
8681}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008683 static void
8684space_to_screenline(int off, int attr)
8685{
8686 ScreenLines[off] = ' ';
8687 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008688 if (enc_utf8)
8689 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008690}
8691
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692/*
8693 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8694 * with character 'c1' in first column followed by 'c2' in the other columns.
8695 * Use attributes 'attr'.
8696 */
8697 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008698screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008699 int start_row,
8700 int end_row,
8701 int start_col,
8702 int end_col,
8703 int c1,
8704 int c2,
8705 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008707 int row;
8708 int col;
8709 int off;
8710 int end_off;
8711 int did_delete;
8712 int c;
8713 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008715 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716#endif
8717
8718 if (end_row > screen_Rows) /* safety check */
8719 end_row = screen_Rows;
8720 if (end_col > screen_Columns) /* safety check */
8721 end_col = screen_Columns;
8722 if (ScreenLines == NULL
8723 || start_row >= end_row
8724 || start_col >= end_col) /* nothing to do */
8725 return;
8726
8727 /* it's a "normal" terminal when not in a GUI or cterm */
8728 norm_term = (
8729#ifdef FEAT_GUI
8730 !gui.in_use &&
8731#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008732 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 for (row = start_row; row < end_row; ++row)
8734 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008735 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008736#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008737 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008738#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008739 )
8740 {
8741 /* When drawing over the right halve of a double-wide char clear
8742 * out the left halve. When drawing over the left halve of a
8743 * double wide-char clear out the right halve. Only needed in a
8744 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008745 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008746 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008747 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008748 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 /*
8751 * Try to use delete-line termcap code, when no attributes or in a
8752 * "normal" terminal, where a bold/italic space is just a
8753 * space.
8754 */
8755 did_delete = FALSE;
8756 if (c2 == ' '
8757 && end_col == Columns
8758 && can_clear(T_CE)
8759 && (attr == 0
8760 || (norm_term
8761 && attr <= HL_ALL
8762 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8763 {
8764 /*
8765 * check if we really need to clear something
8766 */
8767 col = start_col;
8768 if (c1 != ' ') /* don't clear first char */
8769 ++col;
8770
8771 off = LineOffset[row] + col;
8772 end_off = LineOffset[row] + end_col;
8773
8774 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 if (enc_utf8)
8776 while (off < end_off && ScreenLines[off] == ' '
8777 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8778 ++off;
8779 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 while (off < end_off && ScreenLines[off] == ' '
8781 && ScreenAttrs[off] == 0)
8782 ++off;
8783 if (off < end_off) /* something to be cleared */
8784 {
8785 col = off - LineOffset[row];
8786 screen_stop_highlight();
8787 term_windgoto(row, col);/* clear rest of this screen line */
8788 out_str(T_CE);
8789 screen_start(); /* don't know where cursor is now */
8790 col = end_col - col;
8791 while (col--) /* clear chars in ScreenLines */
8792 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008793 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 ++off;
8795 }
8796 }
8797 did_delete = TRUE; /* the chars are cleared now */
8798 }
8799
8800 off = LineOffset[row] + start_col;
8801 c = c1;
8802 for (col = start_col; col < end_col; ++col)
8803 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02008804 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008805 || (enc_utf8 && (int)ScreenLinesUC[off]
8806 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 || ScreenAttrs[off] != attr
8808#if defined(FEAT_GUI) || defined(UNIX)
8809 || force_next
8810#endif
8811 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02008812#ifdef FEAT_TEXT_PROP
8813 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008814 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008815#endif
8816 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 {
8818#if defined(FEAT_GUI) || defined(UNIX)
8819 /* The bold trick may make a single row of pixels appear in
8820 * the next character. When a bold character is removed, the
8821 * next character should be redrawn too. This happens for our
8822 * own GUI and for some xterms. */
8823 if (
8824# ifdef FEAT_GUI
8825 gui.in_use
8826# endif
8827# if defined(FEAT_GUI) && defined(UNIX)
8828 ||
8829# endif
8830# ifdef UNIX
8831 term_is_xterm
8832# endif
8833 )
8834 {
8835 if (ScreenLines[off] != ' '
8836 && (ScreenAttrs[off] > HL_ALL
8837 || ScreenAttrs[off] & HL_BOLD))
8838 force_next = TRUE;
8839 else
8840 force_next = FALSE;
8841 }
8842#endif
8843 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 if (enc_utf8)
8845 {
8846 if (c >= 0x80)
8847 {
8848 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008849 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 }
8851 else
8852 ScreenLinesUC[off] = 0;
8853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 ScreenAttrs[off] = attr;
8855 if (!did_delete || c != ' ')
8856 screen_char(off, row, col);
8857 }
8858 ++off;
8859 if (col == start_col)
8860 {
8861 if (did_delete)
8862 break;
8863 c = c2;
8864 }
8865 }
8866 if (end_col == Columns)
8867 LineWraps[row] = FALSE;
8868 if (row == Rows - 1) /* overwritten the command line */
8869 {
8870 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008871 if (start_col == 0 && end_col == Columns
8872 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008874 if (start_col == 0)
8875 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 }
8877 }
8878}
8879
8880/*
8881 * Check if there should be a delay. Used before clearing or redrawing the
8882 * screen or the command line.
8883 */
8884 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008885check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886{
8887 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8888 && !did_wait_return
8889 && emsg_silent == 0)
8890 {
8891 out_flush();
8892 ui_delay(1000L, TRUE);
8893 emsg_on_display = FALSE;
8894 if (check_msg_scroll)
8895 msg_scroll = FALSE;
8896 }
8897}
8898
8899/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008900 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8901 */
8902 static void
8903clear_TabPageIdxs(void)
8904{
8905 int scol;
8906
8907 for (scol = 0; scol < Columns; ++scol)
8908 TabPageIdxs[scol] = 0;
8909}
8910
8911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008913 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 * Returns TRUE if there is a valid screen to write to.
8915 * Returns FALSE when starting up and screen not initialized yet.
8916 */
8917 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008918screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008920 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 return (ScreenLines != NULL);
8922}
8923
8924/*
8925 * Resize the shell to Rows and Columns.
8926 * Allocate ScreenLines[] and associated items.
8927 *
8928 * There may be some time between setting Rows and Columns and (re)allocating
8929 * ScreenLines[]. This happens when starting up and when (manually) changing
8930 * the shell size. Always use screen_Rows and screen_Columns to access items
8931 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8932 * final size of the shell is needed.
8933 */
8934 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008935screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936{
8937 int new_row, old_row;
8938#ifdef FEAT_GUI
8939 int old_Rows;
8940#endif
8941 win_T *wp;
8942 int outofmem = FALSE;
8943 int len;
8944 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008946 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008948 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 sattr_T *new_ScreenAttrs;
8950 unsigned *new_LineOffset;
8951 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008952 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008953#ifdef FEAT_TEXT_PROP
8954 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008955 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008956 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008957#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00008958 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008960 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008961 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008963retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 /*
8965 * Allocation of the screen buffers is done only when the size changes and
8966 * when Rows and Columns have been set and we have started doing full
8967 * screen stuff.
8968 */
8969 if ((ScreenLines != NULL
8970 && Rows == screen_Rows
8971 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 && enc_utf8 == (ScreenLinesUC != NULL)
8973 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008974 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 || Rows == 0
8976 || Columns == 0
8977 || (!full_screen && ScreenLines == NULL))
8978 return;
8979
8980 /*
8981 * It's possible that we produce an out-of-memory message below, which
8982 * will cause this function to be called again. To break the loop, just
8983 * return here.
8984 */
8985 if (entered)
8986 return;
8987 entered = TRUE;
8988
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008989 /*
8990 * Note that the window sizes are updated before reallocating the arrays,
8991 * thus we must not redraw here!
8992 */
8993 ++RedrawingDisabled;
8994
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 win_new_shellsize(); /* fit the windows in the new sized shell */
8996
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 comp_col(); /* recompute columns for shown command and ruler */
8998
8999 /*
9000 * We're changing the size of the screen.
9001 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9002 * - Move lines from the old arrays into the new arrays, clear extra
9003 * lines (unless the screen is going to be cleared).
9004 * - Free the old arrays.
9005 *
9006 * If anything fails, make ScreenLines NULL, so we don't do anything!
9007 * Continuing with the old ScreenLines may result in a crash, because the
9008 * size is wrong.
9009 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009010 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009012 if (aucmd_win != NULL)
9013 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009014#ifdef FEAT_TEXT_PROP
9015 // global popup windows
9016 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9017 win_free_lsize(wp);
9018 // tab-local popup windows
9019 FOR_ALL_TABPAGES(tp)
9020 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9021 win_free_lsize(wp);
9022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009024 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01009025 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 if (enc_utf8)
9027 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009028 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009029 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009030 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
9031 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 }
9033 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009034 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
9035 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
9036 new_LineOffset = LALLOC_MULT(unsigned, Rows);
9037 new_LineWraps = LALLOC_MULT(char_u, Rows);
9038 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009039#ifdef FEAT_TEXT_PROP
9040 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009041 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02009042 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009045 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 {
9047 if (win_alloc_lines(wp) == FAIL)
9048 {
9049 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009050 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 }
9052 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009053 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9054 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009055 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009056#ifdef FEAT_TEXT_PROP
9057 // global popup windows
9058 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9059 if (win_alloc_lines(wp) == FAIL)
9060 {
9061 outofmem = TRUE;
9062 goto give_up;
9063 }
9064 // tab-local popup windows
9065 FOR_ALL_TABPAGES(tp)
9066 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9067 if (win_alloc_lines(wp) == FAIL)
9068 {
9069 outofmem = TRUE;
9070 goto give_up;
9071 }
9072#endif
9073
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009074give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009076 for (i = 0; i < p_mco; ++i)
9077 if (new_ScreenLinesC[i] == NULL)
9078 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009080 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 || new_ScreenAttrs == NULL
9083 || new_LineOffset == NULL
9084 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009085 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02009086#ifdef FEAT_TEXT_PROP
9087 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009088 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02009089 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02009090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 || outofmem)
9092 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009093 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009094 {
9095 /* guess the size */
9096 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9097
9098 /* Remember we did this to avoid getting outofmem messages over
9099 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009100 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009101 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009102 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009103 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009104 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009105 VIM_CLEAR(new_ScreenLinesC[i]);
9106 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009107 VIM_CLEAR(new_ScreenAttrs);
9108 VIM_CLEAR(new_LineOffset);
9109 VIM_CLEAR(new_LineWraps);
9110 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009111#ifdef FEAT_TEXT_PROP
9112 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009113 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02009114 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 }
9117 else
9118 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009119 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009120
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 for (new_row = 0; new_row < Rows; ++new_row)
9122 {
9123 new_LineOffset[new_row] = new_row * Columns;
9124 new_LineWraps[new_row] = FALSE;
9125
9126 /*
9127 * If the screen is not going to be cleared, copy as much as
9128 * possible from the old screen to the new one and clear the rest
9129 * (used when resizing the window at the "--more--" prompt or when
9130 * executing an external command, for the GUI).
9131 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009132 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 {
9134 (void)vim_memset(new_ScreenLines + new_row * Columns,
9135 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 if (enc_utf8)
9137 {
9138 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9139 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009140 for (i = 0; i < p_mco; ++i)
9141 (void)vim_memset(new_ScreenLinesC[i]
9142 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 0, (size_t)Columns * sizeof(u8char_T));
9144 }
9145 if (enc_dbcs == DBCS_JPNU)
9146 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9147 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9149 0, (size_t)Columns * sizeof(sattr_T));
9150 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009151 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 {
9153 if (screen_Columns < Columns)
9154 len = screen_Columns;
9155 else
9156 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009157 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009158 * may be invalid now. Also when p_mco changes. */
9159 if (!(enc_utf8 && ScreenLinesUC == NULL)
9160 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009161 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9162 ScreenLines + LineOffset[old_row],
9163 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009164 if (enc_utf8 && ScreenLinesUC != NULL
9165 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 {
9167 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9168 ScreenLinesUC + LineOffset[old_row],
9169 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009170 for (i = 0; i < p_mco; ++i)
9171 mch_memmove(new_ScreenLinesC[i]
9172 + new_LineOffset[new_row],
9173 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 (size_t)len * sizeof(u8char_T));
9175 }
9176 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9177 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9178 ScreenLines2 + LineOffset[old_row],
9179 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9181 ScreenAttrs + LineOffset[old_row],
9182 (size_t)len * sizeof(sattr_T));
9183 }
9184 }
9185 }
9186 /* Use the last line of the screen for the current line. */
9187 current_ScreenLine = new_ScreenLines + Rows * Columns;
9188 }
9189
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009190 free_screenlines();
9191
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009194 for (i = 0; i < p_mco; ++i)
9195 ScreenLinesC[i] = new_ScreenLinesC[i];
9196 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 ScreenAttrs = new_ScreenAttrs;
9199 LineOffset = new_LineOffset;
9200 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009201 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02009202#ifdef FEAT_TEXT_PROP
9203 popup_mask = new_popup_mask;
Bram Moolenaar1748c7f2019-06-08 16:55:15 +02009204 vim_memset(popup_mask, 0, Rows * Columns * sizeof(short));
Bram Moolenaarc662ec92019-06-23 00:15:57 +02009205 popup_mask_next = new_popup_mask_next;
9206 popup_transparent = new_popup_transparent;
9207 vim_memset(popup_transparent, 0, Rows * Columns * sizeof(char));
Bram Moolenaar33796b32019-06-08 16:01:13 +02009208 popup_mask_refresh = TRUE;
9209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210
9211 /* It's important that screen_Rows and screen_Columns reflect the actual
9212 * size of ScreenLines[]. Set them before calling anything. */
9213#ifdef FEAT_GUI
9214 old_Rows = screen_Rows;
9215#endif
9216 screen_Rows = Rows;
9217 screen_Columns = Columns;
9218
9219 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009220 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222#ifdef FEAT_GUI
9223 else if (gui.in_use
9224 && !gui.starting
9225 && ScreenLines != NULL
9226 && old_Rows != Rows)
9227 {
9228 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9229 /*
9230 * Adjust the position of the cursor, for when executing an external
9231 * command.
9232 */
9233 if (msg_row >= Rows) /* Rows got smaller */
9234 msg_row = Rows - 1; /* put cursor at last row */
9235 else if (Rows > old_Rows) /* Rows got bigger */
9236 msg_row += Rows - old_Rows; /* put cursor in same place */
9237 if (msg_col >= Columns) /* Columns got smaller */
9238 msg_col = Columns - 1; /* put cursor at last column */
9239 }
9240#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009241 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009244 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009245
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009246 /*
9247 * Do not apply autocommands more than 3 times to avoid an endless loop
9248 * in case applying autocommands always changes Rows or Columns.
9249 */
9250 if (starting == 0 && ++retry_count <= 3)
9251 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009252 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009253 /* In rare cases, autocommands may have altered Rows or Columns,
9254 * jump back to check if we need to allocate the screen again. */
9255 goto retry;
9256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257}
9258
9259 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009260free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009261{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009262 int i;
9263
Bram Moolenaar33796b32019-06-08 16:01:13 +02009264 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009265 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009266 VIM_CLEAR(ScreenLinesC[i]);
9267 VIM_CLEAR(ScreenLines2);
9268 VIM_CLEAR(ScreenLines);
9269 VIM_CLEAR(ScreenAttrs);
9270 VIM_CLEAR(LineOffset);
9271 VIM_CLEAR(LineWraps);
9272 VIM_CLEAR(TabPageIdxs);
9273#ifdef FEAT_TEXT_PROP
9274 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009275 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02009276 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009277#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009278}
9279
9280 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009281screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282{
9283 check_for_delay(FALSE);
9284 screenalloc(FALSE); /* allocate screen buffers if size changed */
9285 screenclear2(); /* clear the screen */
9286}
9287
9288 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009289screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290{
9291 int i;
9292
9293 if (starting == NO_SCREEN || ScreenLines == NULL
9294#ifdef FEAT_GUI
9295 || (gui.in_use && gui.starting)
9296#endif
9297 )
9298 return;
9299
9300#ifdef FEAT_GUI
9301 if (!gui.in_use)
9302#endif
9303 screen_attr = -1; /* force setting the Normal colors */
9304 screen_stop_highlight(); /* don't want highlighting here */
9305
9306#ifdef FEAT_CLIPBOARD
9307 /* disable selection without redrawing it */
9308 clip_scroll_selection(9999);
9309#endif
9310
9311 /* blank out ScreenLines */
9312 for (i = 0; i < Rows; ++i)
9313 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009314 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 LineWraps[i] = FALSE;
9316 }
9317
9318 if (can_clear(T_CL))
9319 {
9320 out_str(T_CL); /* clear the display */
9321 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009322 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 }
9324 else
9325 {
9326 /* can't clear the screen, mark all chars with invalid attributes */
9327 for (i = 0; i < Rows; ++i)
9328 lineinvalid(LineOffset[i], (int)Columns);
9329 clear_cmdline = TRUE;
9330 }
9331
9332 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9333
9334 win_rest_invalid(firstwin);
9335 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009336 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 if (must_redraw == CLEAR) /* no need to clear again */
9338 must_redraw = NOT_VALID;
9339 compute_cmdrow();
9340 msg_row = cmdline_row; /* put cursor on last line for messages */
9341 msg_col = 0;
9342 screen_start(); /* don't know where cursor is now */
9343 msg_scrolled = 0; /* can't scroll back */
9344 msg_didany = FALSE;
9345 msg_didout = FALSE;
9346}
9347
9348/*
9349 * Clear one line in ScreenLines.
9350 */
9351 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009352lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353{
9354 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 if (enc_utf8)
9356 (void)vim_memset(ScreenLinesUC + off, 0,
9357 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009358 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359}
9360
9361/*
9362 * Mark one line in ScreenLines invalid by setting the attributes to an
9363 * invalid value.
9364 */
9365 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009366lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367{
9368 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9369}
9370
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371/*
9372 * Copy part of a Screenline for vertically split window "wp".
9373 */
9374 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009375linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376{
9377 unsigned off_to = LineOffset[to] + wp->w_wincol;
9378 unsigned off_from = LineOffset[from] + wp->w_wincol;
9379
9380 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9381 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 if (enc_utf8)
9383 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009384 int i;
9385
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9387 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009388 for (i = 0; i < p_mco; ++i)
9389 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9390 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 }
9392 if (enc_dbcs == DBCS_JPNU)
9393 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9394 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9396 wp->w_width * sizeof(sattr_T));
9397}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398
9399/*
9400 * Return TRUE if clearing with term string "p" would work.
9401 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02009402 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 */
9404 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009405can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406{
9407 return (*p != NUL && (t_colors <= 1
9408#ifdef FEAT_GUI
9409 || gui.in_use
9410#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009411#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009412 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009413 || (!p_tgc && cterm_normal_bg_color == 0)
9414#else
9415 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009416#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009417 || *T_UT != NUL)
9418#ifdef FEAT_TEXT_PROP
9419 && !(p == T_CE && popup_visible)
9420#endif
9421 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422}
9423
9424/*
9425 * Reset cursor position. Use whenever cursor was moved because of outputting
9426 * something directly to the screen (shell commands) or a terminal control
9427 * code.
9428 */
9429 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009430screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431{
9432 screen_cur_row = screen_cur_col = 9999;
9433}
9434
9435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 * Move the cursor to position "row","col" in the screen.
9437 * This tries to find the most efficient way to move, minimizing the number of
9438 * characters sent to the terminal.
9439 */
9440 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009441windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009443 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 int i;
9445 int plan;
9446 int cost;
9447 int wouldbe_col;
9448 int noinvcurs;
9449 char_u *bs;
9450 int goto_cost;
9451 int attr;
9452
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009453#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9455
9456#define PLAN_LE 1
9457#define PLAN_CR 2
9458#define PLAN_NL 3
9459#define PLAN_WRITE 4
9460 /* Can't use ScreenLines unless initialized */
9461 if (ScreenLines == NULL)
9462 return;
9463
9464 if (col != screen_cur_col || row != screen_cur_row)
9465 {
9466 /* Check for valid position. */
9467 if (row < 0) /* window without text lines? */
9468 row = 0;
9469 if (row >= screen_Rows)
9470 row = screen_Rows - 1;
9471 if (col >= screen_Columns)
9472 col = screen_Columns - 1;
9473
9474 /* check if no cursor movement is allowed in highlight mode */
9475 if (screen_attr && *T_MS == NUL)
9476 noinvcurs = HIGHL_COST;
9477 else
9478 noinvcurs = 0;
9479 goto_cost = GOTO_COST + noinvcurs;
9480
9481 /*
9482 * Plan how to do the positioning:
9483 * 1. Use CR to move it to column 0, same row.
9484 * 2. Use T_LE to move it a few columns to the left.
9485 * 3. Use NL to move a few lines down, column 0.
9486 * 4. Move a few columns to the right with T_ND or by writing chars.
9487 *
9488 * Don't do this if the cursor went beyond the last column, the cursor
9489 * position is unknown then (some terminals wrap, some don't )
9490 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009491 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 * characters to move the cursor to the right.
9493 */
9494 if (row >= screen_cur_row && screen_cur_col < Columns)
9495 {
9496 /*
9497 * If the cursor is in the same row, bigger col, we can use CR
9498 * or T_LE.
9499 */
9500 bs = NULL; /* init for GCC */
9501 attr = screen_attr;
9502 if (row == screen_cur_row && col < screen_cur_col)
9503 {
9504 /* "le" is preferred over "bc", because "bc" is obsolete */
9505 if (*T_LE)
9506 bs = T_LE; /* "cursor left" */
9507 else
9508 bs = T_BC; /* "backspace character (old) */
9509 if (*bs)
9510 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9511 else
9512 cost = 999;
9513 if (col + 1 < cost) /* using CR is less characters */
9514 {
9515 plan = PLAN_CR;
9516 wouldbe_col = 0;
9517 cost = 1; /* CR is just one character */
9518 }
9519 else
9520 {
9521 plan = PLAN_LE;
9522 wouldbe_col = col;
9523 }
9524 if (noinvcurs) /* will stop highlighting */
9525 {
9526 cost += noinvcurs;
9527 attr = 0;
9528 }
9529 }
9530
9531 /*
9532 * If the cursor is above where we want to be, we can use CR LF.
9533 */
9534 else if (row > screen_cur_row)
9535 {
9536 plan = PLAN_NL;
9537 wouldbe_col = 0;
9538 cost = (row - screen_cur_row) * 2; /* CR LF */
9539 if (noinvcurs) /* will stop highlighting */
9540 {
9541 cost += noinvcurs;
9542 attr = 0;
9543 }
9544 }
9545
9546 /*
9547 * If the cursor is in the same row, smaller col, just use write.
9548 */
9549 else
9550 {
9551 plan = PLAN_WRITE;
9552 wouldbe_col = screen_cur_col;
9553 cost = 0;
9554 }
9555
9556 /*
9557 * Check if any characters that need to be written have the
9558 * correct attributes. Also avoid UTF-8 characters.
9559 */
9560 i = col - wouldbe_col;
9561 if (i > 0)
9562 cost += i;
9563 if (cost < goto_cost && i > 0)
9564 {
9565 /*
9566 * Check if the attributes are correct without additionally
9567 * stopping highlighting.
9568 */
9569 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9570 while (i && *p++ == attr)
9571 --i;
9572 if (i != 0)
9573 {
9574 /*
9575 * Try if it works when highlighting is stopped here.
9576 */
9577 if (*--p == 0)
9578 {
9579 cost += noinvcurs;
9580 while (i && *p++ == 0)
9581 --i;
9582 }
9583 if (i != 0)
9584 cost = 999; /* different attributes, don't do it */
9585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 if (enc_utf8)
9587 {
9588 /* Don't use an UTF-8 char for positioning, it's slow. */
9589 for (i = wouldbe_col; i < col; ++i)
9590 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9591 {
9592 cost = 999;
9593 break;
9594 }
9595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 }
9597
9598 /*
9599 * We can do it without term_windgoto()!
9600 */
9601 if (cost < goto_cost)
9602 {
9603 if (plan == PLAN_LE)
9604 {
9605 if (noinvcurs)
9606 screen_stop_highlight();
9607 while (screen_cur_col > col)
9608 {
9609 out_str(bs);
9610 --screen_cur_col;
9611 }
9612 }
9613 else if (plan == PLAN_CR)
9614 {
9615 if (noinvcurs)
9616 screen_stop_highlight();
9617 out_char('\r');
9618 screen_cur_col = 0;
9619 }
9620 else if (plan == PLAN_NL)
9621 {
9622 if (noinvcurs)
9623 screen_stop_highlight();
9624 while (screen_cur_row < row)
9625 {
9626 out_char('\n');
9627 ++screen_cur_row;
9628 }
9629 screen_cur_col = 0;
9630 }
9631
9632 i = col - screen_cur_col;
9633 if (i > 0)
9634 {
9635 /*
9636 * Use cursor-right if it's one character only. Avoids
9637 * removing a line of pixels from the last bold char, when
9638 * using the bold trick in the GUI.
9639 */
9640 if (T_ND[0] != NUL && T_ND[1] == NUL)
9641 {
9642 while (i-- > 0)
9643 out_char(*T_ND);
9644 }
9645 else
9646 {
9647 int off;
9648
9649 off = LineOffset[row] + screen_cur_col;
9650 while (i-- > 0)
9651 {
9652 if (ScreenAttrs[off] != screen_attr)
9653 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 if (enc_dbcs == DBCS_JPNU
9657 && ScreenLines[off] == 0x8e)
9658 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 ++off;
9660 }
9661 }
9662 }
9663 }
9664 }
9665 else
9666 cost = 999;
9667
9668 if (cost >= goto_cost)
9669 {
9670 if (noinvcurs)
9671 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009672 if (row == screen_cur_row && (col > screen_cur_col)
9673 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 term_cursor_right(col - screen_cur_col);
9675 else
9676 term_windgoto(row, col);
9677 }
9678 screen_cur_row = row;
9679 screen_cur_col = col;
9680 }
9681}
9682
9683/*
9684 * Set cursor to its position in the current window.
9685 */
9686 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009687setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009689 setcursor_mayforce(FALSE);
9690}
9691
9692/*
9693 * Set cursor to its position in the current window.
9694 * When "force" is TRUE also when not redrawing.
9695 */
9696 void
9697setcursor_mayforce(int force)
9698{
9699 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 {
9701 validate_cursor();
9702 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009703 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009705 /* With 'rightleft' set and the cursor on a double-wide
9706 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009707 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9708 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009709 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009710 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711#endif
9712 curwin->w_wcol));
9713 }
9714}
9715
9716
9717/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009718 * Insert 'line_count' lines at 'row' in window 'wp'.
9719 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9720 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 * scrolling.
9722 * Returns FAIL if the lines are not inserted, OK for success.
9723 */
9724 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009725win_ins_lines(
9726 win_T *wp,
9727 int row,
9728 int line_count,
9729 int invalid,
9730 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731{
9732 int did_delete;
9733 int nextrow;
9734 int lastrow;
9735 int retval;
9736
9737 if (invalid)
9738 wp->w_lines_valid = 0;
9739
9740 if (wp->w_height < 5)
9741 return FAIL;
9742
9743 if (line_count > wp->w_height - row)
9744 line_count = wp->w_height - row;
9745
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009746 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 if (retval != MAYBE)
9748 return retval;
9749
9750 /*
9751 * If there is a next window or a status line, we first try to delete the
9752 * lines at the bottom to avoid messing what is after the window.
9753 * If this fails and there are following windows, don't do anything to avoid
9754 * messing up those windows, better just redraw.
9755 */
9756 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 if (wp->w_next != NULL || wp->w_status_height)
9758 {
9759 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009760 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 did_delete = TRUE;
9762 else if (wp->w_next)
9763 return FAIL;
9764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 /*
9766 * if no lines deleted, blank the lines that will end up below the window
9767 */
9768 if (!did_delete)
9769 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009772 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 lastrow = nextrow + line_count;
9774 if (lastrow > Rows)
9775 lastrow = Rows;
9776 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009777 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 ' ', ' ', 0);
9779 }
9780
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009781 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 == FAIL)
9783 {
9784 /* deletion will have messed up other windows */
9785 if (did_delete)
9786 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 win_rest_invalid(W_NEXT(wp));
9789 }
9790 return FAIL;
9791 }
9792
9793 return OK;
9794}
9795
9796/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009797 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9799 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9800 * scrolling
9801 * Return OK for success, FAIL if the lines are not deleted.
9802 */
9803 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009804win_del_lines(
9805 win_T *wp,
9806 int row,
9807 int line_count,
9808 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009809 int mayclear,
9810 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811{
9812 int retval;
9813
9814 if (invalid)
9815 wp->w_lines_valid = 0;
9816
9817 if (line_count > wp->w_height - row)
9818 line_count = wp->w_height - row;
9819
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009820 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 if (retval != MAYBE)
9822 return retval;
9823
9824 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009825 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 return FAIL;
9827
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 /*
9829 * If there are windows or status lines below, try to put them at the
9830 * correct place. If we can't do that, they have to be redrawn.
9831 */
9832 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9833 {
9834 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009835 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 {
9837 wp->w_redr_status = TRUE;
9838 win_rest_invalid(wp->w_next);
9839 }
9840 }
9841 /*
9842 * If this is the last window and there is no status line, redraw the
9843 * command line later.
9844 */
9845 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 redraw_cmdline = TRUE;
9847 return OK;
9848}
9849
9850/*
9851 * Common code for win_ins_lines() and win_del_lines().
9852 * Returns OK or FAIL when the work has been done.
9853 * Returns MAYBE when not finished yet.
9854 */
9855 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009856win_do_lines(
9857 win_T *wp,
9858 int row,
9859 int line_count,
9860 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009861 int del,
9862 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863{
9864 int retval;
9865
9866 if (!redrawing() || line_count <= 0)
9867 return FAIL;
9868
Bram Moolenaar33796b32019-06-08 16:01:13 +02009869 // When inserting lines would result in loss of command output, just redraw
9870 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009871 if (no_win_do_lines_ins && !del)
9872 return FAIL;
9873
Bram Moolenaar33796b32019-06-08 16:01:13 +02009874 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02009875 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009877 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009878 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 return FAIL;
9880 }
9881
Bram Moolenaar33796b32019-06-08 16:01:13 +02009882#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009883 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02009884 if (popup_visible)
9885 return FAIL;
9886#endif
9887
9888 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 if (row + line_count >= wp->w_height)
9890 {
9891 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009892 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 ' ', ' ', 0);
9894 return OK;
9895 }
9896
9897 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009898 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009900 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009902 if (!no_win_do_lines_ins)
9903 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904
9905 /*
9906 * If the terminal can set a scroll region, use that.
9907 * Always do this in a vertically split window. This will redraw from
9908 * ScreenLines[] when t_CV isn't defined. That's faster than using
9909 * win_line().
9910 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009911 * a character in the lower right corner of the scroll region may cause a
9912 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009914 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 scroll_region_set(wp, row);
9918 if (del)
9919 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009920 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 else
9922 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009923 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 scroll_region_reset();
9926 return retval;
9927 }
9928
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9930 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931
9932 return MAYBE;
9933}
9934
9935/*
9936 * window 'wp' and everything after it is messed up, mark it for redraw
9937 */
9938 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009939win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 {
9943 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 wp->w_redr_status = TRUE;
9945 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 }
9947 redraw_cmdline = TRUE;
9948}
9949
9950/*
9951 * The rest of the routines in this file perform screen manipulations. The
9952 * given operation is performed physically on the screen. The corresponding
9953 * change is also made to the internal screen image. In this way, the editor
9954 * anticipates the effect of editing changes on the appearance of the screen.
9955 * That way, when we call screenupdate a complete redraw isn't usually
9956 * necessary. Another advantage is that we can keep adding code to anticipate
9957 * screen changes, and in the meantime, everything still works.
9958 */
9959
9960/*
9961 * types for inserting or deleting lines
9962 */
9963#define USE_T_CAL 1
9964#define USE_T_CDL 2
9965#define USE_T_AL 3
9966#define USE_T_CE 4
9967#define USE_T_DL 5
9968#define USE_T_SR 6
9969#define USE_NL 7
9970#define USE_T_CD 8
9971#define USE_REDRAW 9
9972
9973/*
9974 * insert lines on the screen and update ScreenLines[]
9975 * 'end' is the line after the scrolled part. Normally it is Rows.
9976 * When scrolling region used 'off' is the offset from the top for the region.
9977 * 'row' and 'end' are relative to the start of the region.
9978 *
9979 * return FAIL for failure, OK for success.
9980 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009981 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009982screen_ins_lines(
9983 int off,
9984 int row,
9985 int line_count,
9986 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009987 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009988 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989{
9990 int i;
9991 int j;
9992 unsigned temp;
9993 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009994 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 int type;
9996 int result_empty;
9997 int can_ce = can_clear(T_CE);
9998
9999 /*
10000 * FAIL if
10001 * - there is no valid screen
10002 * - the screen has to be redrawn completely
10003 * - the line count is less than one
10004 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010005 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +020010006 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 */
Bram Moolenaar33796b32019-06-08 16:01:13 +020010008 if (!screen_valid(TRUE)
10009 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010010#ifdef FEAT_CLIPBOARD
10011 || (clip_star.state != SELECT_CLEARED
10012 && redrawing_for_callback > 0)
10013#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +020010014#ifdef FEAT_TEXT_PROP
10015 || popup_visible
10016#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010017 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 return FAIL;
10019
10020 /*
10021 * There are seven ways to insert lines:
10022 * 0. When in a vertically split window and t_CV isn't set, redraw the
10023 * characters from ScreenLines[].
10024 * 1. Use T_CD (clear to end of display) if it exists and the result of
10025 * the insert is just empty lines
10026 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
10027 * present or line_count > 1. It looks better if we do all the inserts
10028 * at once.
10029 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
10030 * insert is just empty lines and T_CE is not present or line_count >
10031 * 1.
10032 * 4. Use T_AL (insert line) if it exists.
10033 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10034 * just empty lines.
10035 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10036 * just empty lines.
10037 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10038 * the 'da' flag is not set or we have clear line capability.
10039 * 8. redraw the characters from ScreenLines[].
10040 *
10041 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10042 * the scrollbar for the window. It does have insert line, use that if it
10043 * exists.
10044 */
10045 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10047 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010048 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 type = USE_T_CD;
10050 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10051 type = USE_T_CAL;
10052 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10053 type = USE_T_CDL;
10054 else if (*T_AL != NUL)
10055 type = USE_T_AL;
10056 else if (can_ce && result_empty)
10057 type = USE_T_CE;
10058 else if (*T_DL != NUL && result_empty)
10059 type = USE_T_DL;
10060 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10061 type = USE_T_SR;
10062 else
10063 return FAIL;
10064
10065 /*
10066 * For clearing the lines screen_del_lines() is used. This will also take
10067 * care of t_db if necessary.
10068 */
10069 if (type == USE_T_CD || type == USE_T_CDL ||
10070 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010071 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072
10073 /*
10074 * If text is retained below the screen, first clear or delete as many
10075 * lines at the bottom of the window as are about to be inserted so that
10076 * the deleted lines won't later surface during a screen_del_lines.
10077 */
10078 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010079 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080
10081#ifdef FEAT_CLIPBOARD
10082 /* Remove a modeless selection when inserting lines halfway the screen
10083 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010084 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010085 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 else
10087 clip_scroll_selection(-line_count);
10088#endif
10089
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090#ifdef FEAT_GUI
10091 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10092 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010093 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094#endif
10095
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010096 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10097 cursor_col = wp->w_wincol;
10098
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 if (*T_CCS != NUL) /* cursor relative to region */
10100 cursor_row = row;
10101 else
10102 cursor_row = row + off;
10103
10104 /*
10105 * Shift LineOffset[] line_count down to reflect the inserted lines.
10106 * Clear the inserted lines in ScreenLines[].
10107 */
10108 row += off;
10109 end += off;
10110 for (i = 0; i < line_count; ++i)
10111 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 if (wp != NULL && wp->w_width != Columns)
10113 {
10114 /* need to copy part of a line */
10115 j = end - 1 - i;
10116 while ((j -= line_count) >= row)
10117 linecopy(j + line_count, j, wp);
10118 j += line_count;
10119 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010120 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10121 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 else
10123 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10124 LineWraps[j] = FALSE;
10125 }
10126 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 {
10128 j = end - 1 - i;
10129 temp = LineOffset[j];
10130 while ((j -= line_count) >= row)
10131 {
10132 LineOffset[j + line_count] = LineOffset[j];
10133 LineWraps[j + line_count] = LineWraps[j];
10134 }
10135 LineOffset[j + line_count] = temp;
10136 LineWraps[j + line_count] = FALSE;
10137 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010138 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 else
10140 lineinvalid(temp, (int)Columns);
10141 }
10142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143
10144 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010145 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010146 if (clear_attr != 0)
10147 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 /* redraw the characters */
10150 if (type == USE_REDRAW)
10151 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010152 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 {
10154 term_append_lines(line_count);
10155 screen_start(); /* don't know where cursor is now */
10156 }
10157 else
10158 {
10159 for (i = 0; i < line_count; i++)
10160 {
10161 if (type == USE_T_AL)
10162 {
10163 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010164 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 out_str(T_AL);
10166 }
10167 else /* type == USE_T_SR */
10168 out_str(T_SR);
10169 screen_start(); /* don't know where cursor is now */
10170 }
10171 }
10172
10173 /*
10174 * With scroll-reverse and 'da' flag set we need to clear the lines that
10175 * have been scrolled down into the region.
10176 */
10177 if (type == USE_T_SR && *T_DA)
10178 {
10179 for (i = 0; i < line_count; ++i)
10180 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010181 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 out_str(T_CE);
10183 screen_start(); /* don't know where cursor is now */
10184 }
10185 }
10186
10187#ifdef FEAT_GUI
10188 gui_can_update_cursor();
10189 if (gui.in_use)
10190 out_flush(); /* always flush after a scroll */
10191#endif
10192 return OK;
10193}
10194
10195/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010196 * Delete lines on the screen and update ScreenLines[].
10197 * "end" is the line after the scrolled part. Normally it is Rows.
10198 * When scrolling region used "off" is the offset from the top for the region.
10199 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 *
10201 * Return OK for success, FAIL if the lines are not deleted.
10202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010204screen_del_lines(
10205 int off,
10206 int row,
10207 int line_count,
10208 int end,
10209 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010210 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010211 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212{
10213 int j;
10214 int i;
10215 unsigned temp;
10216 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010217 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 int cursor_end;
10219 int result_empty; /* result is empty until end of region */
10220 int can_delete; /* deleting line codes can be used */
10221 int type;
10222
10223 /*
10224 * FAIL if
10225 * - there is no valid screen
10226 * - the screen has to be redrawn completely
10227 * - the line count is less than one
10228 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010229 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010231 if (!screen_valid(TRUE) || line_count <= 0
10232 || (!force && line_count > p_ttyscroll)
10233#ifdef FEAT_CLIPBOARD
10234 || (clip_star.state != SELECT_CLEARED
10235 && redrawing_for_callback > 0)
10236#endif
10237 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 return FAIL;
10239
10240 /*
10241 * Check if the rest of the current region will become empty.
10242 */
10243 result_empty = row + line_count >= end;
10244
10245 /*
10246 * We can delete lines only when 'db' flag not set or when 'ce' option
10247 * available.
10248 */
10249 can_delete = (*T_DB == NUL || can_clear(T_CE));
10250
10251 /*
10252 * There are six ways to delete lines:
10253 * 0. When in a vertically split window and t_CV isn't set, redraw the
10254 * characters from ScreenLines[].
10255 * 1. Use T_CD if it exists and the result is empty.
10256 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10257 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10258 * none of the other ways work.
10259 * 4. Use T_CE (erase line) if the result is empty.
10260 * 5. Use T_DL (delete line) if it exists.
10261 * 6. redraw the characters from ScreenLines[].
10262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10264 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010265 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 type = USE_T_CD;
10267#if defined(__BEOS__) && defined(BEOS_DR8)
10268 /*
10269 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10270 * its internal termcap... this works okay for tests which test *T_DB !=
10271 * NUL. It has the disadvantage that the user cannot use any :set t_*
10272 * command to get T_DB (back) to empty_option, only :set term=... will do
10273 * the trick...
10274 * Anyway, this hack will hopefully go away with the next OS release.
10275 * (Olaf Seibert)
10276 */
10277 else if (row == 0 && T_DB == empty_option
10278 && (line_count == 1 || *T_CDL == NUL))
10279#else
10280 else if (row == 0 && (
10281#ifndef AMIGA
10282 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10283 * up, so use delete-line command */
10284 line_count == 1 ||
10285#endif
10286 *T_CDL == NUL))
10287#endif
10288 type = USE_NL;
10289 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10290 type = USE_T_CDL;
10291 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010292 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 type = USE_T_CE;
10294 else if (*T_DL != NUL && can_delete)
10295 type = USE_T_DL;
10296 else if (*T_CDL != NUL && can_delete)
10297 type = USE_T_CDL;
10298 else
10299 return FAIL;
10300
10301#ifdef FEAT_CLIPBOARD
10302 /* Remove a modeless selection when deleting lines halfway the screen or
10303 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010304 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010305 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 else
10307 clip_scroll_selection(line_count);
10308#endif
10309
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310#ifdef FEAT_GUI
10311 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10312 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010313 gui_dont_update_cursor(gui.cursor_row >= row + off
10314 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315#endif
10316
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010317 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10318 cursor_col = wp->w_wincol;
10319
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 if (*T_CCS != NUL) /* cursor relative to region */
10321 {
10322 cursor_row = row;
10323 cursor_end = end;
10324 }
10325 else
10326 {
10327 cursor_row = row + off;
10328 cursor_end = end + off;
10329 }
10330
10331 /*
10332 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10333 * Clear the inserted lines in ScreenLines[].
10334 */
10335 row += off;
10336 end += off;
10337 for (i = 0; i < line_count; ++i)
10338 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 if (wp != NULL && wp->w_width != Columns)
10340 {
10341 /* need to copy part of a line */
10342 j = row + i;
10343 while ((j += line_count) <= end - 1)
10344 linecopy(j - line_count, j, wp);
10345 j -= line_count;
10346 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010347 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10348 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 else
10350 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10351 LineWraps[j] = FALSE;
10352 }
10353 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 {
10355 /* whole width, moving the line pointers is faster */
10356 j = row + i;
10357 temp = LineOffset[j];
10358 while ((j += line_count) <= end - 1)
10359 {
10360 LineOffset[j - line_count] = LineOffset[j];
10361 LineWraps[j - line_count] = LineWraps[j];
10362 }
10363 LineOffset[j - line_count] = temp;
10364 LineWraps[j - line_count] = FALSE;
10365 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010366 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 else
10368 lineinvalid(temp, (int)Columns);
10369 }
10370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010372 if (screen_attr != clear_attr)
10373 screen_stop_highlight();
10374 if (clear_attr != 0)
10375 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 /* redraw the characters */
10378 if (type == USE_REDRAW)
10379 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010380 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010382 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 out_str(T_CD);
10384 screen_start(); /* don't know where cursor is now */
10385 }
10386 else if (type == USE_T_CDL)
10387 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010388 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 term_delete_lines(line_count);
10390 screen_start(); /* don't know where cursor is now */
10391 }
10392 /*
10393 * Deleting lines at top of the screen or scroll region: Just scroll
10394 * the whole screen (scroll region) up by outputting newlines on the
10395 * last line.
10396 */
10397 else if (type == USE_NL)
10398 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010399 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 for (i = line_count; --i >= 0; )
10401 out_char('\n'); /* cursor will remain on same line */
10402 }
10403 else
10404 {
10405 for (i = line_count; --i >= 0; )
10406 {
10407 if (type == USE_T_DL)
10408 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010409 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 out_str(T_DL); /* delete a line */
10411 }
10412 else /* type == USE_T_CE */
10413 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010414 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 out_str(T_CE); /* erase a line */
10416 }
10417 screen_start(); /* don't know where cursor is now */
10418 }
10419 }
10420
10421 /*
10422 * If the 'db' flag is set, we need to clear the lines that have been
10423 * scrolled up at the bottom of the region.
10424 */
10425 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10426 {
10427 for (i = line_count; i > 0; --i)
10428 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010429 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 out_str(T_CE); /* erase a line */
10431 screen_start(); /* don't know where cursor is now */
10432 }
10433 }
10434
10435#ifdef FEAT_GUI
10436 gui_can_update_cursor();
10437 if (gui.in_use)
10438 out_flush(); /* always flush after a scroll */
10439#endif
10440
10441 return OK;
10442}
10443
10444/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010445 * Return TRUE when postponing displaying the mode message: when not redrawing
10446 * or inside a mapping.
10447 */
10448 int
10449skip_showmode()
10450{
10451 // Call char_avail() only when we are going to show something, because it
10452 // takes a bit of time. redrawing() may also call char_avail_avail().
10453 if (global_busy
10454 || msg_silent != 0
10455 || !redrawing()
10456 || (char_avail() && !KeyTyped))
10457 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010458 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010459 return TRUE;
10460 }
10461 return FALSE;
10462}
10463
10464/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010465 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 *
10467 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10468 * If clear_cmdline is FALSE there may be a message there that needs to be
10469 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010470 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 * Return the length of the message (0 if no message).
10472 */
10473 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010474showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475{
10476 int need_clear;
10477 int length = 0;
10478 int do_mode;
10479 int attr;
10480 int nwr_save;
10481#ifdef FEAT_INS_EXPAND
10482 int sub_attr;
10483#endif
10484
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010485 do_mode = ((p_smd && msg_silent == 0)
10486 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010487 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010488 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010489 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010491 if (skip_showmode())
10492 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493
10494 nwr_save = need_wait_return;
10495
10496 /* wait a bit before overwriting an important message */
10497 check_for_delay(FALSE);
10498
10499 /* if the cmdline is more than one line high, erase top lines */
10500 need_clear = clear_cmdline;
10501 if (clear_cmdline && cmdline_row < Rows - 1)
10502 msg_clr_cmdline(); /* will reset clear_cmdline */
10503
10504 /* Position on the last line in the window, column 0 */
10505 msg_pos_mode();
10506 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010507 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 if (do_mode)
10509 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010510 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010512 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010513# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010514 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010515# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010516 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010517# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010518 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010519# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010520 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010522 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523# endif
10524#endif
10525#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10526 if (gui.in_use)
10527 {
10528 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010529 {
10530 /* HANGUL */
10531 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010532 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010533 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010534 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 }
10537#endif
10538#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010539 /* CTRL-X in Insert mode */
10540 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 {
10542 /* These messages can get long, avoid a wrap in a narrow
10543 * window. Prefer showing edit_submode_extra. */
10544 length = (Rows - msg_row) * Columns - 3;
10545 if (edit_submode_extra != NULL)
10546 length -= vim_strsize(edit_submode_extra);
10547 if (length > 0)
10548 {
10549 if (edit_submode_pre != NULL)
10550 length -= vim_strsize(edit_submode_pre);
10551 if (length - vim_strsize(edit_submode) > 0)
10552 {
10553 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010554 msg_puts_attr((char *)edit_submode_pre, attr);
10555 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 }
10557 if (edit_submode_extra != NULL)
10558 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010559 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010561 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 else
10563 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010564 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 }
10566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 }
10568 else
10569#endif
10570 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010572 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010573 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010574 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 else if (State & INSERT)
10576 {
10577#ifdef FEAT_RIGHTLEFT
10578 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010579 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010581 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010583 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010584 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010586 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010588 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589#ifdef FEAT_RIGHTLEFT
10590 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010591 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592#endif
10593#ifdef FEAT_KEYMAP
10594 if (State & LANGMAP)
10595 {
10596# ifdef FEAT_ARABIC
10597 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010598 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 else
10600# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010601 if (get_keymap_str(curwin, (char_u *)" (%s)",
10602 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010603 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604 }
10605#endif
10606 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010607 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 if (VIsual_active)
10610 {
10611 char *p;
10612
10613 /* Don't concatenate separate words to avoid translation
10614 * problems. */
10615 switch ((VIsual_select ? 4 : 0)
10616 + (VIsual_mode == Ctrl_V) * 2
10617 + (VIsual_mode == 'V'))
10618 {
10619 case 0: p = N_(" VISUAL"); break;
10620 case 1: p = N_(" VISUAL LINE"); break;
10621 case 2: p = N_(" VISUAL BLOCK"); break;
10622 case 4: p = N_(" SELECT"); break;
10623 case 5: p = N_(" SELECT LINE"); break;
10624 default: p = N_(" SELECT BLOCK"); break;
10625 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010626 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010628 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010630
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 need_clear = TRUE;
10632 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010633 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634#ifdef FEAT_INS_EXPAND
10635 && edit_submode == NULL /* otherwise it gets too long */
10636#endif
10637 )
10638 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010639 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 need_clear = TRUE;
10641 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010642
10643 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010644 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 msg_clr_eos();
10646 msg_didout = FALSE; /* overwrite this message */
10647 length = msg_col;
10648 msg_col = 0;
10649 need_wait_return = nwr_save; /* never ask for hit-return for this */
10650 }
10651 else if (clear_cmdline && msg_silent == 0)
10652 /* Clear the whole command line. Will reset "clear_cmdline". */
10653 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010654 else if (redraw_mode)
10655 {
10656 msg_pos_mode();
10657 msg_clr_eos();
10658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659
10660#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 /* In Visual mode the size of the selected area must be redrawn. */
10662 if (VIsual_active)
10663 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664
10665 /* If the last window has no status line, the ruler is after the mode
10666 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010667 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010668 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669#endif
10670 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010671 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 clear_cmdline = FALSE;
10673
10674 return length;
10675}
10676
10677/*
10678 * Position for a mode message.
10679 */
10680 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010681msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682{
10683 msg_col = 0;
10684 msg_row = Rows - 1;
10685}
10686
10687/*
10688 * Delete mode message. Used when ESC is typed which is expected to end
10689 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010690 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 */
10692 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010693unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694{
10695 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010696 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 */
10698 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10699 redraw_cmdline = TRUE; /* delete mode later */
10700 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010701 clearmode();
10702}
10703
10704/*
10705 * Clear the mode message.
10706 */
10707 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010708clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010709{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010710 int save_msg_row = msg_row;
10711 int save_msg_col = msg_col;
10712
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010713 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010714 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010715 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010716 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010717
10718 msg_col = save_msg_col;
10719 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720}
10721
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010722 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010723recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010724{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010725 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010726 if (!shortmess(SHM_RECORDING))
10727 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010728 char s[4];
10729
10730 sprintf(s, " @%c", reg_recording);
10731 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010732 }
10733}
10734
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010735/*
10736 * Draw the tab pages line at the top of the Vim window.
10737 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010738 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010739draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010740{
10741 int tabcount = 0;
10742 tabpage_T *tp;
10743 int tabwidth;
10744 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010745 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010746 int attr;
10747 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010748 win_T *cwp;
10749 int wincount;
10750 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010751 int c;
10752 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010753 int attr_sel = HL_ATTR(HLF_TPS);
10754 int attr_nosel = HL_ATTR(HLF_TP);
10755 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010756 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010757 int room;
10758 int use_sep_chars = (t_colors < 8
10759#ifdef FEAT_GUI
10760 && !gui.in_use
10761#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010762#ifdef FEAT_TERMGUICOLORS
10763 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010764#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010765 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010766
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010767 if (ScreenLines == NULL)
10768 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010769 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010770
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010771#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010772 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010773 if (gui_use_tabline())
10774 {
10775 gui_update_tabline();
10776 return;
10777 }
10778#endif
10779
10780 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010781 return;
10782
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010783#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010784 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010785
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010786 /* Use the 'tabline' option if it's set. */
10787 if (*p_tal != NUL)
10788 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010789 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010790
10791 /* Check for an error. If there is one we would loop in redrawing the
10792 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010793 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010794 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010795 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010796 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010797 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010798 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010799 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010800 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010801#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010802 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010803 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010804 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010805
Bram Moolenaar238a5642006-02-21 22:12:05 +000010806 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10807 if (tabwidth < 6)
10808 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010809
Bram Moolenaar238a5642006-02-21 22:12:05 +000010810 attr = attr_nosel;
10811 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010812 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10813 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010814 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010815 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010816
Bram Moolenaar238a5642006-02-21 22:12:05 +000010817 if (tp->tp_topframe == topframe)
10818 attr = attr_sel;
10819 if (use_sep_chars && col > 0)
10820 screen_putchar('|', 0, col++, attr);
10821
10822 if (tp->tp_topframe != topframe)
10823 attr = attr_nosel;
10824
10825 screen_putchar(' ', 0, col++, attr);
10826
10827 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010828 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010829 cwp = curwin;
10830 wp = firstwin;
10831 }
10832 else
10833 {
10834 cwp = tp->tp_curwin;
10835 wp = tp->tp_firstwin;
10836 }
10837
10838 modified = FALSE;
10839 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10840 if (bufIsChanged(wp->w_buffer))
10841 modified = TRUE;
10842 if (modified || wincount > 1)
10843 {
10844 if (wincount > 1)
10845 {
10846 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010847 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010848 if (col + len >= Columns - 3)
10849 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010850 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010851#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010852 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010853#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010854 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010855#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010856 );
10857 col += len;
10858 }
10859 if (modified)
10860 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10861 screen_putchar(' ', 0, col++, attr);
10862 }
10863
10864 room = scol - col + tabwidth - 1;
10865 if (room > 0)
10866 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010867 /* Get buffer name in NameBuff[] */
10868 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010869 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010870 len = vim_strsize(NameBuff);
10871 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010872 if (has_mbyte)
10873 while (len > room)
10874 {
10875 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010876 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010877 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010878 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010879 {
10880 p += len - room;
10881 len = room;
10882 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010883 if (len > Columns - col - 1)
10884 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010885
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010886 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010887 col += len;
10888 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010889 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010890
10891 /* Store the tab page number in TabPageIdxs[], so that
10892 * jump_to_mouse() knows where each one is. */
10893 ++tabcount;
10894 while (scol < col)
10895 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010896 }
10897
Bram Moolenaar238a5642006-02-21 22:12:05 +000010898 if (use_sep_chars)
10899 c = '_';
10900 else
10901 c = ' ';
10902 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010903
10904 /* Put an "X" for closing the current tab if there are several. */
10905 if (first_tabpage->tp_next != NULL)
10906 {
10907 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10908 TabPageIdxs[Columns - 1] = -999;
10909 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010910 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010911
10912 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10913 * set. */
10914 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010915}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010916
10917/*
10918 * Get buffer name for "buf" into NameBuff[].
10919 * Takes care of special buffer names and translates special characters.
10920 */
10921 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010922get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010923{
10924 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010925 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010926 else
10927 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10928 trans_characters(NameBuff, MAXPATHL);
10929}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010930
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931/*
10932 * Get the character to use in a status line. Get its attributes in "*attr".
10933 */
10934 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010935fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936{
10937 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010938
10939#ifdef FEAT_TERMINAL
10940 if (bt_terminal(wp->w_buffer))
10941 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010942 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010943 {
10944 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010945 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010946 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010947 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010948 {
10949 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010950 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010951 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010952 }
10953 else
10954#endif
10955 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010957 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 fill = fill_stl;
10959 }
10960 else
10961 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010962 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 fill = fill_stlnc;
10964 }
10965 /* Use fill when there is highlighting, and highlighting of current
10966 * window differs, or the fillchars differ, or this is not the
10967 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010968 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010969 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 || (fill_stl != fill_stlnc)))
10971 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010972 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 return '^';
10974 return '=';
10975}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977/*
10978 * Get the character to use in a separator between vertically split windows.
10979 * Get its attributes in "*attr".
10980 */
10981 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010982fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010984 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 if (*attr == 0 && fill_vert == ' ')
10986 return '|';
10987 else
10988 return fill_vert;
10989}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990
10991/*
10992 * Return TRUE if redrawing should currently be done.
10993 */
10994 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010995redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010997#ifdef FEAT_EVAL
10998 if (disable_redraw_for_testing)
10999 return 0;
11000 else
11001#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020011002 return ((!RedrawingDisabled
11003#ifdef FEAT_EVAL
11004 || ignore_redraw_flag_for_testing
11005#endif
11006 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007}
11008
11009/*
11010 * Return TRUE if printing messages should currently be done.
11011 */
11012 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011013messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014{
11015 return (!(p_lz && char_avail() && !KeyTyped));
11016}
11017
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011018#ifdef FEAT_MENU
11019/*
11020 * Draw the window toolbar.
11021 */
11022 static void
11023redraw_win_toolbar(win_T *wp)
11024{
11025 vimmenu_T *menu;
11026 int item_idx = 0;
11027 int item_count = 0;
11028 int col = 0;
11029 int next_col;
11030 int off = (int)(current_ScreenLine - ScreenLines);
11031 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
11032 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
11033
11034 vim_free(wp->w_winbar_items);
11035 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
11036 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011037 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011038
11039 /* TODO: use fewer spaces if there is not enough room */
11040 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020011041 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011042 {
11043 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011044 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011045 break;
11046 if (col > 1)
11047 {
11048 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011049 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011050 break;
11051 }
11052
11053 wp->w_winbar_items[item_idx].wb_startcol = col;
11054 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011055 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011056 break;
11057
11058 next_col = text_to_screenline(wp, menu->name, col);
11059 while (col < next_col)
11060 {
11061 ScreenAttrs[off + col] = button_attr;
11062 ++col;
11063 }
11064 wp->w_winbar_items[item_idx].wb_endcol = col;
11065 wp->w_winbar_items[item_idx].wb_menu = menu;
11066 ++item_idx;
11067
Bram Moolenaar02631462017-09-22 15:20:32 +020011068 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011069 break;
11070 space_to_screenline(off + col, button_attr);
11071 ++col;
11072 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011073 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011074 {
11075 space_to_screenline(off + col, fill_attr);
11076 ++col;
11077 }
11078 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11079
Bram Moolenaar02631462017-09-22 15:20:32 +020011080 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020011081 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011082}
11083#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011084
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085/*
11086 * Show current status info in ruler and various other places
11087 * If always is FALSE, only show ruler if position has changed.
11088 */
11089 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011090showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091{
11092 if (!always && !redrawing())
11093 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011094#ifdef FEAT_INS_EXPAND
11095 if (pum_visible())
11096 {
11097 /* Don't redraw right now, do it later. */
11098 curwin->w_redr_status = TRUE;
11099 return;
11100 }
11101#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011102#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011103 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000011104 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 else
11106#endif
11107#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011108 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109#endif
11110
11111#ifdef FEAT_TITLE
11112 if (need_maketitle
11113# ifdef FEAT_STL_OPT
11114 || (p_icon && (stl_syntax & STL_IN_ICON))
11115 || (p_title && (stl_syntax & STL_IN_TITLE))
11116# endif
11117 )
11118 maketitle();
11119#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011120 /* Redraw the tab pages line if needed. */
11121 if (redraw_tabline)
11122 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123}
11124
11125#ifdef FEAT_CMDL_INFO
11126 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011127win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011129#define RULER_BUF_LEN 70
11130 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 int row;
11132 int fillchar;
11133 int attr;
11134 int empty_line = FALSE;
11135 colnr_T virtcol;
11136 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011137 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 int this_ru_col;
11140 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010011141 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142
11143 /* If 'ruler' off or redrawing disabled, don't do anything */
11144 if (!p_ru)
11145 return;
11146
11147 /*
11148 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11149 * after deleting lines, before cursor.lnum is corrected.
11150 */
11151 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11152 return;
11153
11154#ifdef FEAT_INS_EXPAND
11155 /* Don't draw the ruler while doing insert-completion, it might overwrite
11156 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 if (edit_submode != NULL)
11159 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011160 // Don't draw the ruler when the popup menu is visible, it may overlap.
11161 // Except when the popup menu will be redrawn anyway.
11162 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011163 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164#endif
11165
11166#ifdef FEAT_STL_OPT
11167 if (*p_ruf)
11168 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011169 int save_called_emsg = called_emsg;
11170
11171 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011173 if (called_emsg)
11174 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011175 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011176 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177 return;
11178 }
11179#endif
11180
11181 /*
11182 * Check if not in Insert mode and the line is empty (will show "0-1").
11183 */
11184 if (!(State & INSERT)
11185 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11186 empty_line = TRUE;
11187
11188 /*
11189 * Only draw the ruler when something changed.
11190 */
11191 validate_virtcol_win(wp);
11192 if ( redraw_cmdline
11193 || always
11194 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11195 || wp->w_cursor.col != wp->w_ru_cursor.col
11196 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 || wp->w_topline != wp->w_ru_topline
11199 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11200#ifdef FEAT_DIFF
11201 || wp->w_topfill != wp->w_ru_topfill
11202#endif
11203 || empty_line != wp->w_ru_empty)
11204 {
11205 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 if (wp->w_status_height)
11207 {
11208 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011209 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011210 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011211 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 }
11213 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214 {
11215 row = Rows - 1;
11216 fillchar = ' ';
11217 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218 width = Columns;
11219 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 }
11221
11222 /* In list mode virtcol needs to be recomputed */
11223 virtcol = wp->w_virtcol;
11224 if (wp->w_p_list && lcs_tab1 == NUL)
11225 {
11226 wp->w_p_list = FALSE;
11227 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11228 wp->w_p_list = TRUE;
11229 }
11230
11231 /*
11232 * Some sprintfs return the length, some return a pointer.
11233 * To avoid portability problems we use strlen() here.
11234 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011235 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11237 ? 0L
11238 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011239 len = STRLEN(buffer);
11240 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11242 (int)virtcol + 1);
11243
11244 /*
11245 * Add a "50%" if there is room for it.
11246 * On the last line, don't print in the last column (scrolls the
11247 * screen up on some terminals).
11248 */
11249 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011250 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 this_ru_col = ru_col - (Columns - width);
11255 if (this_ru_col < 0)
11256 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 /* Never use more than half the window/screen width, leave the other
11258 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011259 if (this_ru_col < (width + 1) / 2)
11260 this_ru_col = (width + 1) / 2;
11261 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011263 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011264 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266 if (has_mbyte)
11267 i += (*mb_char2bytes)(fillchar, buffer + i);
11268 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 buffer[i++] = fillchar;
11270 ++o;
11271 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011272 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 }
11274 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275 if (has_mbyte)
11276 {
11277 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011278 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279 {
11280 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011281 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 {
11283 buffer[i] = NUL;
11284 break;
11285 }
11286 }
11287 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011288 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011289 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290
Bram Moolenaar4033c552017-09-16 20:54:51 +020011291 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 i = redraw_cmdline;
11293 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011294 this_ru_col + off + (int)STRLEN(buffer),
11295 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 fillchar, fillchar, attr);
11297 /* don't redraw the cmdline because of showing the ruler */
11298 redraw_cmdline = i;
11299 wp->w_ru_cursor = wp->w_cursor;
11300 wp->w_ru_virtcol = wp->w_virtcol;
11301 wp->w_ru_empty = empty_line;
11302 wp->w_ru_topline = wp->w_topline;
11303 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11304#ifdef FEAT_DIFF
11305 wp->w_ru_topfill = wp->w_topfill;
11306#endif
11307 }
11308}
11309#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011310
11311#if defined(FEAT_LINEBREAK) || defined(PROTO)
11312/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011313 * Return the width of the 'number' and 'relativenumber' column.
11314 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011315 * Otherwise it depends on 'numberwidth' and the line count.
11316 */
11317 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011318number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011319{
11320 int n;
11321 linenr_T lnum;
11322
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011323 if (wp->w_p_rnu && !wp->w_p_nu)
11324 /* cursor line shows "0" */
11325 lnum = wp->w_height;
11326 else
11327 /* cursor line shows absolute line number */
11328 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011329
Bram Moolenaar6b314672015-03-20 15:42:10 +010011330 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011331 return wp->w_nrwidth_width;
11332 wp->w_nrwidth_line_count = lnum;
11333
11334 n = 0;
11335 do
11336 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011337 lnum /= 10;
11338 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011339 } while (lnum > 0);
11340
11341 /* 'numberwidth' gives the minimal width plus one */
11342 if (n < wp->w_p_nuw - 1)
11343 n = wp->w_p_nuw - 1;
11344
Bram Moolenaare4b407f2019-07-04 11:59:28 +020011345# ifdef FEAT_SIGNS
11346 // If 'signcolumn' is set to 'number' and there is a sign to display, then
11347 // the minimal width for the number column is 2.
11348 if (n < 2 && (wp->w_buffer->b_signlist != NULL)
11349 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
11350 n = 2;
11351# endif
11352
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011353 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011354 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011355 return n;
11356}
11357#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011358
Bram Moolenaar113e1072019-01-20 15:30:40 +010011359#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011360/*
11361 * Return the current cursor column. This is the actual position on the
11362 * screen. First column is 0.
11363 */
11364 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011365screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011366{
11367 return screen_cur_col;
11368}
11369
11370/*
11371 * Return the current cursor row. This is the actual position on the screen.
11372 * First row is 0.
11373 */
11374 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011375screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011376{
11377 return screen_cur_row;
11378}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011379#endif