blob: 5cdbd2c87f56b970b3c86b827064bad44c076799 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200125static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100126static void win_draw_end(win_T *wp, int c1, int c2, int draw_margin, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
129static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
130static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200132static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000134#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200138# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void start_search_hl(void);
140static void end_search_hl(void);
141static void init_search_hl(win_T *wp);
142static void prepare_search_hl(win_T *wp, linenr_T lnum);
143static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
144static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static void screen_char(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200149static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200151static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void win_rest_invalid(win_T *wp);
153static void msg_pos_mode(void);
154static void recording_mode(int attr);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200155static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200157#ifdef FEAT_MENU
158static void redraw_win_toolbar(win_T *wp);
159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100161static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
163#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200164static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167/* Ugly global: overrule attribute used by screen_char() */
168static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200170#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
171/* Can limit syntax highlight time to 'redrawtime'. */
172# define SYN_TIME_LIMIT 1
173#endif
174
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200175#ifdef FEAT_RIGHTLEFT
176# define HAS_RIGHTLEFT(x) x
177#else
178# define HAS_RIGHTLEFT(x) FALSE
179#endif
180
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181/*
182 * Redraw the current window later, with update_screen(type).
183 * Set must_redraw only if not already set to a higher value.
Bram Moolenaarae654382019-01-17 21:09:05 +0100184 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 */
186 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100187redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188{
189 redraw_win_later(curwin, type);
190}
191
192 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100193redraw_win_later(
194 win_T *wp,
195 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200197 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {
199 wp->w_redr_type = type;
200 if (type >= NOT_VALID)
201 wp->w_lines_valid = 0;
202 if (must_redraw < type) /* must_redraw is the maximum of all windows */
203 must_redraw = type;
204 }
205}
206
207/*
208 * Force a complete redraw later. Also resets the highlighting. To be used
209 * after executing a shell command that messes up the screen.
210 */
211 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100212redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213{
214 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000215#ifdef FEAT_GUI
216 if (gui.in_use)
217 /* Use a code that will reset gui.highlight_mask in
218 * gui_stop_highlight(). */
219 screen_attr = HL_ALL + 1;
220 else
221#endif
222 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200223 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224}
225
226/*
227 * Mark all windows to be redrawn later.
228 */
229 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100230redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231{
232 win_T *wp;
233
234 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 redraw_win_later(wp, type);
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100236 // This may be needed when switching tabs.
237 if (must_redraw < type)
238 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239}
240
241/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000242 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 */
244 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100245redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246{
247 redraw_buf_later(curbuf, type);
248}
249
250 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100251redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252{
253 win_T *wp;
254
255 FOR_ALL_WINDOWS(wp)
256 {
257 if (wp->w_buffer == buf)
258 redraw_win_later(wp, type);
259 }
260}
261
Bram Moolenaar113e1072019-01-20 15:30:40 +0100262#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200263 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100264redraw_buf_line_later(buf_T *buf, linenr_T lnum)
265{
266 win_T *wp;
267
268 FOR_ALL_WINDOWS(wp)
269 if (wp->w_buffer == buf && lnum >= wp->w_topline
270 && lnum < wp->w_botline)
271 redrawWinline(wp, lnum);
272}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100273#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100274
Bram Moolenaar113e1072019-01-20 15:30:40 +0100275#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100276 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200277redraw_buf_and_status_later(buf_T *buf, int type)
278{
279 win_T *wp;
280
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200281#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200282 if (wild_menu_showing != 0)
283 /* Don't redraw while the command line completion is displayed, it
284 * would disappear. */
285 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200286#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200287 FOR_ALL_WINDOWS(wp)
288 {
289 if (wp->w_buffer == buf)
290 {
291 redraw_win_later(wp, type);
292 wp->w_redr_status = TRUE;
293 }
294 }
295}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100296#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200297
Bram Moolenaar113e1072019-01-20 15:30:40 +0100298#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200300 * Redraw as soon as possible. When the command line is not scrolled redraw
301 * right away and restore what was on the command line.
302 * Return a code indicating what happened.
303 */
304 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100305redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306{
307 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200308 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200309 int r;
310 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200311 schar_T *screenline; /* copy from ScreenLines[] */
312 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200314 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200315 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200316 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200317
318 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200319 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 return ret;
321
322 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200323 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200324 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200325 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200326 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200327 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200328 if (screenline == NULL || screenattr == NULL)
329 ret = 2;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200330 if (enc_utf8)
331 {
332 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200333 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200334 if (screenlineUC == NULL)
335 ret = 2;
336 for (i = 0; i < p_mco; ++i)
337 {
338 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200339 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200340 if (screenlineC[i] == NULL)
341 ret = 2;
342 }
343 }
344 if (enc_dbcs == DBCS_JPNU)
345 {
346 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200347 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200348 if (screenline2 == NULL)
349 ret = 2;
350 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351
352 if (ret != 2)
353 {
354 /* Save the text displayed in the command line area. */
355 for (r = 0; r < rows; ++r)
356 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200357 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200358 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200359 (size_t)cols * sizeof(schar_T));
360 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200361 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200362 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 if (enc_utf8)
364 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 mch_memmove(screenlineC[i] + r * cols,
370 ScreenLinesC[i] + LineOffset[cmdline_row + r],
371 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200372 }
373 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200375 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200376 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200377 }
378
379 update_screen(0);
380 ret = 3;
381
382 if (must_redraw == 0)
383 {
384 int off = (int)(current_ScreenLine - ScreenLines);
385
386 /* Restore the text displayed in the command line area. */
387 for (r = 0; r < rows; ++r)
388 {
389 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200390 screenline + r * cols,
391 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200392 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200393 screenattr + r * cols,
394 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200395 if (enc_utf8)
396 {
397 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200398 screenlineUC + r * cols,
399 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200400 for (i = 0; i < p_mco; ++i)
401 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200402 screenlineC[i] + r * cols,
403 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200404 }
405 if (enc_dbcs == DBCS_JPNU)
406 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200407 screenline2 + r * cols,
408 (size_t)cols * sizeof(schar_T));
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200409 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200410 }
411 ret = 4;
412 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200413 }
414
415 vim_free(screenline);
416 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200417 if (enc_utf8)
418 {
419 vim_free(screenlineUC);
420 for (i = 0; i < p_mco; ++i)
421 vim_free(screenlineC[i]);
422 }
423 if (enc_dbcs == DBCS_JPNU)
424 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200425
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200426 /* Show the intro message when appropriate. */
427 maybe_intro_message();
428
429 setcursor();
430
Bram Moolenaar2951b772013-07-03 12:45:31 +0200431 return ret;
432}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100433#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200434
435/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100436 * Invoked after an asynchronous callback is called.
437 * If an echo command was used the cursor needs to be put back where
438 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200439 * If "call_update_screen" is FALSE don't call update_screen() when at the
440 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100441 */
442 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200443redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100444{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200445 ++redrawing_for_callback;
446
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100447 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200448 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100449 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200450 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200451 // Don't redraw when in prompt_for_number().
452 if (cmdline_row > 0)
453 {
454 // Redrawing only works when the screen didn't scroll. Don't clear
455 // wildmenu entries.
456 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200457#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200458 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200459#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200460 && call_update_screen)
461 update_screen(0);
462
463 // Redraw in the same position, so that the user can continue
464 // editing the command.
465 redrawcmdline_ex(FALSE);
466 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200467 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200468 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100469 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200470 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200471 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100472 setcursor();
473 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100474 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100475#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100476 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200477 // Don't update the cursor when it is blinking and off to avoid
478 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100479 out_flush_cursor(FALSE, FALSE);
480 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100481#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100482 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200483
484 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100485}
486
487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 * Changed something in the current window, at buffer line "lnum", that
489 * requires that line and possibly other lines to be redrawn.
490 * Used when entering/leaving Insert mode with the cursor on a folded line.
491 * Used to remove the "$" from a change command.
492 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
493 * may become invalid and the whole window will have to be redrawn.
494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100496redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200497 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100498 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200500 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
501 wp->w_redraw_top = lnum;
502 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
503 wp->w_redraw_bot = lnum;
504 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505}
506
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200507 void
508reset_updating_screen(int may_resize_shell UNUSED)
509{
510 updating_screen = FALSE;
511#ifdef FEAT_GUI
512 if (may_resize_shell)
513 gui_may_resize_shell();
514#endif
515#ifdef FEAT_TERMINAL
516 term_check_channel_closed_recently();
517#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200518
519#ifdef HAVE_DROP_FILE
520 // If handle_drop() was called while updating_screen was TRUE need to
521 // handle the drop now.
522 handle_any_postponed_drop();
523#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200524}
525
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200527 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 */
529 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100530update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531{
532 redraw_curbuf_later(type);
533 update_screen(type);
534}
535
536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 * Based on the current value of curwin->w_topline, transfer a screenfull
538 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200539 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200541 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200542update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200544 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 win_T *wp;
546 static int did_intro = FALSE;
547#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
548 int did_one;
549#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200550#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200551 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200552 int gui_cursor_col = 0;
553 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200554#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200555 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000557 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200559 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200561 if (type == VALID_NO_UPDATE)
562 {
563 no_update = TRUE;
564 type = 0;
565 }
566
Bram Moolenaara3347722019-05-11 21:14:24 +0200567#ifdef FEAT_EVAL
568 // Before updating the screen, notify any listeners of changed text.
569 invoke_listeners();
570#endif
571
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 if (must_redraw)
573 {
574 if (type < must_redraw) /* use maximal type */
575 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000576
577 /* must_redraw is reset here, so that when we run into some weird
578 * reason to redraw while busy redrawing (e.g., asynchronous
579 * scrolling), or update_topline() in win_update() will cause a
580 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 must_redraw = 0;
582 }
583
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200584 /* May need to update w_lines[]. */
585 if (curwin->w_lines_valid == 0 && type < NOT_VALID
586#ifdef FEAT_TERMINAL
587 && !term_do_update_window(curwin)
588#endif
589 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 type = NOT_VALID;
591
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000592 /* Postpone the redrawing when it's not needed and when being called
593 * recursively. */
594 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 {
596 redraw_later(type); /* remember type for next time */
597 must_redraw = type;
598 if (type > INVERTED_ALL)
599 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200600 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 }
602
603 updating_screen = TRUE;
604#ifdef FEAT_SYN_HL
605 ++display_tick; /* let syntax code know we're in a next round of
606 * display updating */
607#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200608 if (no_update)
609 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611 /*
612 * if the screen was scrolled up when displaying a message, scroll it down
613 */
614 if (msg_scrolled)
615 {
616 clear_cmdline = TRUE;
617 if (msg_scrolled > Rows - 5) /* clearing is faster */
618 type = CLEAR;
619 else if (type != CLEAR)
620 {
621 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200622 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
623 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 type = CLEAR;
625 FOR_ALL_WINDOWS(wp)
626 {
627 if (W_WINROW(wp) < msg_scrolled)
628 {
629 if (W_WINROW(wp) + wp->w_height > msg_scrolled
630 && wp->w_redr_type < REDRAW_TOP
631 && wp->w_lines_valid > 0
632 && wp->w_topline == wp->w_lines[0].wl_lnum)
633 {
634 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
635 wp->w_redr_type = REDRAW_TOP;
636 }
637 else
638 {
639 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200640 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
641 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 }
644 }
645 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200646 if (!no_update)
647 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000648 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 }
650 msg_scrolled = 0;
651 need_wait_return = FALSE;
652 }
653
654 /* reset cmdline_row now (may have been changed temporarily) */
655 compute_cmdrow();
656
657 /* Check for changed highlighting */
658 if (need_highlight_changed)
659 highlight_changed();
660
661 if (type == CLEAR) /* first clear screen */
662 {
663 screenclear(); /* will reset clear_cmdline */
664 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200665 /* must_redraw may be set indirectly, avoid another redraw later */
666 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 }
668
669 if (clear_cmdline) /* going to clear cmdline (done below) */
670 check_for_delay(FALSE);
671
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000672#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200673 /* Force redraw when width of 'number' or 'relativenumber' column
674 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000675 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200676 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
677 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000678 curwin->w_redr_type = NOT_VALID;
679#endif
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 /*
682 * Only start redrawing if there is really something to do.
683 */
684 if (type == INVERTED)
685 update_curswant();
686 if (curwin->w_redr_type < type
687 && !((type == VALID
688 && curwin->w_lines[0].wl_valid
689#ifdef FEAT_DIFF
690 && curwin->w_topfill == curwin->w_old_topfill
691 && curwin->w_botfill == curwin->w_old_botfill
692#endif
693 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000695 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
697 && curwin->w_old_visual_mode == VIsual_mode
698 && (curwin->w_valid & VALID_VIRTCOL)
699 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 ))
701 curwin->w_redr_type = type;
702
Bram Moolenaar5a305422006-04-28 22:38:25 +0000703 /* Redraw the tab pages line if needed. */
704 if (redraw_tabline || type >= NOT_VALID)
705 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000706
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707#ifdef FEAT_SYN_HL
708 /*
709 * Correct stored syntax highlighting info for changes in each displayed
710 * buffer. Each buffer must only be done once.
711 */
712 FOR_ALL_WINDOWS(wp)
713 {
714 if (wp->w_buffer->b_mod_set)
715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 win_T *wwp;
717
718 /* Check if we already did this buffer. */
719 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
720 if (wwp->w_buffer == wp->w_buffer)
721 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200722 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 syn_stack_apply_changes(wp->w_buffer);
724 }
725 }
726#endif
727
728 /*
729 * Go from top to bottom through the windows, redrawing the ones that need
730 * it.
731 */
732#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
733 did_one = FALSE;
734#endif
735#ifdef FEAT_SEARCH_EXTRA
736 search_hl.rm.regprog = NULL;
737#endif
738 FOR_ALL_WINDOWS(wp)
739 {
740 if (wp->w_redr_type != 0)
741 {
742 cursor_off();
743#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
744 if (!did_one)
745 {
746 did_one = TRUE;
747# ifdef FEAT_SEARCH_EXTRA
748 start_search_hl();
749# endif
750# ifdef FEAT_CLIPBOARD
751 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200752 if (clip_star.available && clip_isautosel_star())
753 clip_update_selection(&clip_star);
754 if (clip_plus.available && clip_isautosel_plus())
755 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756# endif
757#ifdef FEAT_GUI
758 /* Remove the cursor before starting to do anything, because
759 * scrolling may make it difficult to redraw the text under
760 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200761 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200762 {
763 gui_cursor_col = gui.cursor_col;
764 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200766 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768#endif
769 }
770#endif
771 win_update(wp);
772 }
773
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 /* redraw status line after the window to minimize cursor movement */
775 if (wp->w_redr_status)
776 {
777 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200778 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 }
781#if defined(FEAT_SEARCH_EXTRA)
782 end_search_hl();
783#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100784#ifdef FEAT_INS_EXPAND
785 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200786 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 /* Reset b_mod_set flags. Going through all windows is probably faster
790 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200791 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200794 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
796 /* Clear or redraw the command line. Done last, because scrolling may
797 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200798 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 showmode();
800
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200801 if (no_update)
802 --no_win_do_lines_ins;
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200805 if (!did_intro)
806 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 did_intro = TRUE;
808
809#ifdef FEAT_GUI
810 /* Redraw the cursor and update the scrollbars when all screen updating is
811 * done. */
812 if (gui.in_use)
813 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200814 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200815 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100816 mch_disable_flush();
817 out_flush(); /* required before updating the cursor */
818 mch_enable_flush();
819
Bram Moolenaar144445d2016-07-08 21:41:54 +0200820 /* Put the GUI position where the cursor was, gui_update_cursor()
821 * uses that. */
822 gui.col = gui_cursor_col;
823 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200824 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100826 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200827 screen_cur_col = gui.col;
828 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200829 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100830 else
831 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 gui_update_scrollbars(FALSE);
833 }
834#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200835 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836}
837
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100838#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100839/*
840 * Prepare for updating one or more windows.
841 * Caller must check for "updating_screen" already set to avoid recursiveness.
842 */
843 static void
844update_prepare(void)
845{
846 cursor_off();
847 updating_screen = TRUE;
848#ifdef FEAT_GUI
849 /* Remove the cursor before starting to do anything, because scrolling may
850 * make it difficult to redraw the text under it. */
851 if (gui.in_use)
852 gui_undraw_cursor();
853#endif
854#ifdef FEAT_SEARCH_EXTRA
855 start_search_hl();
856#endif
857}
858
859/*
860 * Finish updating one or more windows.
861 */
862 static void
863update_finish(void)
864{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200865 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100866 showmode();
867
868# ifdef FEAT_SEARCH_EXTRA
869 end_search_hl();
870# endif
871
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200872 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100873
874# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100875 /* Redraw the cursor and update the scrollbars when all screen updating is
876 * done. */
877 if (gui.in_use)
878 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100879 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100880 gui_update_scrollbars(FALSE);
881 }
882# endif
883}
884#endif
885
Bram Moolenaar860cae12010-06-05 23:22:07 +0200886#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200887/*
888 * Return TRUE if the cursor line in window "wp" may be concealed, according
889 * to the 'concealcursor' option.
890 */
891 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100892conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200893{
894 int c;
895
896 if (*wp->w_p_cocu == NUL)
897 return FALSE;
898 if (get_real_state() & VISUAL)
899 c = 'v';
900 else if (State & INSERT)
901 c = 'i';
902 else if (State & NORMAL)
903 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200904 else if (State & CMDLINE)
905 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200906 else
907 return FALSE;
908 return vim_strchr(wp->w_p_cocu, c) != NULL;
909}
910
911/*
912 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
913 */
914 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200915conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200916{
917 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
918 {
919 need_cursor_line_redraw = TRUE;
920 /* Need to recompute cursor column, e.g., when starting Visual mode
921 * without concealing. */
922 curs_columns(TRUE);
923 }
924}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925#endif
926
Bram Moolenaar113e1072019-01-20 15:30:40 +0100927#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100929update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930{
931 win_T *wp;
932 int doit = FALSE;
933
934# ifdef FEAT_FOLDING
935 win_foldinfo.fi_level = 0;
936# endif
937
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100938 // update/delete a specific sign
939 redraw_buf_line_later(buf, lnum);
940
941 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (wp->w_redr_type != 0)
944 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100946 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200947 * happening (recursive call), messages on the screen or still starting up.
948 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100949 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200950 || State == ASKMORE || State == HITRETURN
951 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100952#ifdef FEAT_GUI
953 || gui.starting
954#endif
955 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 return;
957
958 /* update all windows that need updating */
959 update_prepare();
960
Bram Moolenaar29323592016-07-24 22:04:11 +0200961 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 {
963 if (wp->w_redr_type != 0)
964 win_update(wp);
965 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200966 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
969 update_finish();
970}
971#endif
972
973
974#if defined(FEAT_GUI) || defined(PROTO)
975/*
976 * Update a single window, its status line and maybe the command line msg.
977 * Used for the GUI scrollbar.
978 */
979 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100980updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000982 /* return if already busy updating */
983 if (updating_screen)
984 return;
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 update_prepare();
987
988#ifdef FEAT_CLIPBOARD
989 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200990 if (clip_star.available && clip_isautosel_star())
991 clip_update_selection(&clip_star);
992 if (clip_plus.available && clip_isautosel_plus())
993 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000997
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000998 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000999 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001000 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 if (wp->w_redr_status
1003# ifdef FEAT_CMDL_INFO
1004 || p_ru
1005# endif
1006# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001007 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008# endif
1009 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001010 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
1012 update_finish();
1013}
1014#endif
1015
1016/*
1017 * Update a single window.
1018 *
1019 * This may cause the windows below it also to be redrawn (when clearing the
1020 * screen or scrolling lines).
1021 *
1022 * How the window is redrawn depends on wp->w_redr_type. Each type also
1023 * implies the one below it.
1024 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001025 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1027 * INVERTED redraw the changed part of the Visual area
1028 * INVERTED_ALL redraw the whole Visual area
1029 * VALID 1. scroll up/down to adjust for a changed w_topline
1030 * 2. update lines at the top when scrolled down
1031 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001032 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 * b_mod_top and b_mod_bot.
1034 * - if wp->w_redraw_top non-zero, redraw lines between
1035 * wp->w_redraw_top and wp->w_redr_bot.
1036 * - continue redrawing when syntax status is invalid.
1037 * 4. if scrolled up, update lines at the bottom.
1038 * This results in three areas that may need updating:
1039 * top: from first row to top_end (when scrolled down)
1040 * mid: from mid_start to mid_end (update inversion or changed text)
1041 * bot: from bot_start to last row (when scrolled up)
1042 */
1043 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001044win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045{
1046 buf_T *buf = wp->w_buffer;
1047 int type;
1048 int top_end = 0; /* Below last row of the top area that needs
1049 updating. 0 when no top area updating. */
1050 int mid_start = 999;/* first row of the mid area that needs
1051 updating. 999 when no mid area updating. */
1052 int mid_end = 0; /* Below last row of the mid area that needs
1053 updating. 0 when no mid area updating. */
1054 int bot_start = 999;/* first row of the bot area that needs
1055 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 int scrolled_down = FALSE; /* TRUE when scrolled down when
1057 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001059 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 int top_to_mod = FALSE; /* redraw above mod_top */
1061#endif
1062
1063 int row; /* current window row to display */
1064 linenr_T lnum; /* current buffer lnum to display */
1065 int idx; /* current index in w_lines[] */
1066 int srow; /* starting row of the current line */
1067
1068 int eof = FALSE; /* if TRUE, we hit the end of the file */
1069 int didline = FALSE; /* if TRUE, we finished the last line */
1070 int i;
1071 long j;
1072 static int recursive = FALSE; /* being called recursively */
1073 int old_botline = wp->w_botline;
1074#ifdef FEAT_FOLDING
1075 long fold_count;
1076#endif
1077#ifdef FEAT_SYN_HL
1078 /* remember what happened to the previous line, to know if
1079 * check_visual_highlight() can be used */
1080#define DID_NONE 1 /* didn't update a line */
1081#define DID_LINE 2 /* updated a normal line */
1082#define DID_FOLD 3 /* updated a folded line */
1083 int did_update = DID_NONE;
1084 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1085#endif
1086 linenr_T mod_top = 0;
1087 linenr_T mod_bot = 0;
1088#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1089 int save_got_int;
1090#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001091#ifdef SYN_TIME_LIMIT
1092 proftime_T syntax_tm;
1093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
1095 type = wp->w_redr_type;
1096
1097 if (type == NOT_VALID)
1098 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 wp->w_lines_valid = 0;
1101 }
1102
1103 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001104 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 {
1106 wp->w_redr_type = 0;
1107 return;
1108 }
1109
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 /* Window is zero-width: Only need to draw the separator. */
1111 if (wp->w_width == 0)
1112 {
1113 /* draw the vertical separator right of this window */
1114 draw_vsep_win(wp, 0);
1115 wp->w_redr_type = 0;
1116 return;
1117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001119#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001120 // If this window contains a terminal, redraw works completely differently.
1121 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001122 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001123 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001124# ifdef FEAT_MENU
1125 /* Draw the window toolbar, if there is one. */
1126 if (winbar_height(wp) > 0)
1127 redraw_win_toolbar(wp);
1128# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001129 wp->w_redr_type = 0;
1130 return;
1131 }
1132#endif
1133
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001135 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136#endif
1137
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001138#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001139 /* Force redraw when width of 'number' or 'relativenumber' column
1140 * changes. */
1141 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001142 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001143 {
1144 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001145 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001146 }
1147 else
1148#endif
1149
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1151 {
1152 /*
1153 * When there are both inserted/deleted lines and specific lines to be
1154 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1155 * everything (only happens when redrawing is off for while).
1156 */
1157 type = NOT_VALID;
1158 }
1159 else
1160 {
1161 /*
1162 * Set mod_top to the first line that needs displaying because of
1163 * changes. Set mod_bot to the first line after the changes.
1164 */
1165 mod_top = wp->w_redraw_top;
1166 if (wp->w_redraw_bot != 0)
1167 mod_bot = wp->w_redraw_bot + 1;
1168 else
1169 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 if (buf->b_mod_set)
1171 {
1172 if (mod_top == 0 || mod_top > buf->b_mod_top)
1173 {
1174 mod_top = buf->b_mod_top;
1175#ifdef FEAT_SYN_HL
1176 /* Need to redraw lines above the change that may be included
1177 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001178 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001180 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 if (mod_top < 1)
1182 mod_top = 1;
1183 }
1184#endif
1185 }
1186 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1187 mod_bot = buf->b_mod_bot;
1188
1189#ifdef FEAT_SEARCH_EXTRA
1190 /* When 'hlsearch' is on and using a multi-line search pattern, a
1191 * change in one line may make the Search highlighting in a
1192 * previous line invalid. Simple solution: redraw all visible
1193 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001194 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001196 if (search_hl.rm.regprog != NULL
1197 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001199 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001200 {
1201 cur = wp->w_match_head;
1202 while (cur != NULL)
1203 {
1204 if (cur->match.regprog != NULL
1205 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001206 {
1207 top_to_mod = TRUE;
1208 break;
1209 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001210 cur = cur->next;
1211 }
1212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213#endif
1214 }
1215#ifdef FEAT_FOLDING
1216 if (mod_top != 0 && hasAnyFolding(wp))
1217 {
1218 linenr_T lnumt, lnumb;
1219
1220 /*
1221 * A change in a line can cause lines above it to become folded or
1222 * unfolded. Find the top most buffer line that may be affected.
1223 * If the line was previously folded and displayed, get the first
1224 * line of that fold. If the line is folded now, get the first
1225 * folded line. Use the minimum of these two.
1226 */
1227
1228 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1229 * the line below it. If there is no valid entry, use w_topline.
1230 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1231 * to this line. If there is no valid entry, use MAXLNUM. */
1232 lnumt = wp->w_topline;
1233 lnumb = MAXLNUM;
1234 for (i = 0; i < wp->w_lines_valid; ++i)
1235 if (wp->w_lines[i].wl_valid)
1236 {
1237 if (wp->w_lines[i].wl_lastlnum < mod_top)
1238 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1239 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1240 {
1241 lnumb = wp->w_lines[i].wl_lnum;
1242 /* When there is a fold column it might need updating
1243 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001244 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 ++lnumb;
1246 }
1247 }
1248
1249 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1250 if (mod_top > lnumt)
1251 mod_top = lnumt;
1252
1253 /* Now do the same for the bottom line (one above mod_bot). */
1254 --mod_bot;
1255 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1256 ++mod_bot;
1257 if (mod_bot < lnumb)
1258 mod_bot = lnumb;
1259 }
1260#endif
1261
1262 /* When a change starts above w_topline and the end is below
1263 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001264 * If the end of the change is above w_topline: do like no change was
1265 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 if (mod_top != 0 && mod_top < wp->w_topline)
1267 {
1268 if (mod_bot > wp->w_topline)
1269 mod_top = wp->w_topline;
1270#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001271 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 top_end = 1;
1273#endif
1274 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001275
1276 /* When line numbers are displayed need to redraw all lines below
1277 * inserted/deleted lines. */
1278 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1279 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001281 wp->w_redraw_top = 0; // reset for next time
1282 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283
1284 /*
1285 * When only displaying the lines at the top, set top_end. Used when
1286 * window has scrolled down for msg_scrolled.
1287 */
1288 if (type == REDRAW_TOP)
1289 {
1290 j = 0;
1291 for (i = 0; i < wp->w_lines_valid; ++i)
1292 {
1293 j += wp->w_lines[i].wl_size;
1294 if (j >= wp->w_upd_rows)
1295 {
1296 top_end = j;
1297 break;
1298 }
1299 }
1300 if (top_end == 0)
1301 /* not found (cannot happen?): redraw everything */
1302 type = NOT_VALID;
1303 else
1304 /* top area defined, the rest is VALID */
1305 type = VALID;
1306 }
1307
Bram Moolenaar367329b2007-08-30 11:53:22 +00001308 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001309 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1310 * non-zero and thus not FALSE) will indicate that screenclear() was not
1311 * called. */
1312 if (screen_cleared)
1313 screen_cleared = MAYBE;
1314
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 /*
1316 * If there are no changes on the screen that require a complete redraw,
1317 * handle three cases:
1318 * 1: we are off the top of the screen by a few lines: scroll down
1319 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1320 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1321 * w_lines[] that needs updating.
1322 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001323 if ((type == VALID || type == SOME_VALID
1324 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325#ifdef FEAT_DIFF
1326 && !wp->w_botfill && !wp->w_old_botfill
1327#endif
1328 )
1329 {
1330 if (mod_top != 0 && wp->w_topline == mod_top)
1331 {
1332 /*
1333 * w_topline is the first changed line, the scrolling will be done
1334 * further down.
1335 */
1336 }
1337 else if (wp->w_lines[0].wl_valid
1338 && (wp->w_topline < wp->w_lines[0].wl_lnum
1339#ifdef FEAT_DIFF
1340 || (wp->w_topline == wp->w_lines[0].wl_lnum
1341 && wp->w_topfill > wp->w_old_topfill)
1342#endif
1343 ))
1344 {
1345 /*
1346 * New topline is above old topline: May scroll down.
1347 */
1348#ifdef FEAT_FOLDING
1349 if (hasAnyFolding(wp))
1350 {
1351 linenr_T ln;
1352
1353 /* count the number of lines we are off, counting a sequence
1354 * of folded lines as one */
1355 j = 0;
1356 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1357 {
1358 ++j;
1359 if (j >= wp->w_height - 2)
1360 break;
1361 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1362 }
1363 }
1364 else
1365#endif
1366 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1367 if (j < wp->w_height - 2) /* not too far off */
1368 {
1369 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1370#ifdef FEAT_DIFF
1371 /* insert extra lines for previously invisible filler lines */
1372 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1373 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1374 - wp->w_old_topfill;
1375#endif
1376 if (i < wp->w_height - 2) /* less than a screen off */
1377 {
1378 /*
1379 * Try to insert the correct number of lines.
1380 * If not the last window, delete the lines at the bottom.
1381 * win_ins_lines may fail when the terminal can't do it.
1382 */
1383 if (i > 0)
1384 check_for_delay(FALSE);
1385 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1386 {
1387 if (wp->w_lines_valid != 0)
1388 {
1389 /* Need to update rows that are new, stop at the
1390 * first one that scrolled down. */
1391 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
1394 /* Move the entries that were scrolled, disable
1395 * the entries for the lines to be redrawn. */
1396 if ((wp->w_lines_valid += j) > wp->w_height)
1397 wp->w_lines_valid = wp->w_height;
1398 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1399 wp->w_lines[idx] = wp->w_lines[idx - j];
1400 while (idx >= 0)
1401 wp->w_lines[idx--].wl_valid = FALSE;
1402 }
1403 }
1404 else
1405 mid_start = 0; /* redraw all lines */
1406 }
1407 else
1408 mid_start = 0; /* redraw all lines */
1409 }
1410 else
1411 mid_start = 0; /* redraw all lines */
1412 }
1413 else
1414 {
1415 /*
1416 * New topline is at or below old topline: May scroll up.
1417 * When topline didn't change, find first entry in w_lines[] that
1418 * needs updating.
1419 */
1420
1421 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1422 j = -1;
1423 row = 0;
1424 for (i = 0; i < wp->w_lines_valid; i++)
1425 {
1426 if (wp->w_lines[i].wl_valid
1427 && wp->w_lines[i].wl_lnum == wp->w_topline)
1428 {
1429 j = i;
1430 break;
1431 }
1432 row += wp->w_lines[i].wl_size;
1433 }
1434 if (j == -1)
1435 {
1436 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1437 * lines */
1438 mid_start = 0;
1439 }
1440 else
1441 {
1442 /*
1443 * Try to delete the correct number of lines.
1444 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1445 */
1446#ifdef FEAT_DIFF
1447 /* If the topline didn't change, delete old filler lines,
1448 * otherwise delete filler lines of the new topline... */
1449 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1450 row += wp->w_old_topfill;
1451 else
1452 row += diff_check_fill(wp, wp->w_topline);
1453 /* ... but don't delete new filler lines. */
1454 row -= wp->w_topfill;
1455#endif
1456 if (row > 0)
1457 {
1458 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001459 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1460 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 bot_start = wp->w_height - row;
1462 else
1463 mid_start = 0; /* redraw all lines */
1464 }
1465 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1466 {
1467 /*
1468 * Skip the lines (below the deleted lines) that are still
1469 * valid and don't need redrawing. Copy their info
1470 * upwards, to compensate for the deleted lines. Set
1471 * bot_start to the first row that needs redrawing.
1472 */
1473 bot_start = 0;
1474 idx = 0;
1475 for (;;)
1476 {
1477 wp->w_lines[idx] = wp->w_lines[j];
1478 /* stop at line that didn't fit, unless it is still
1479 * valid (no lines deleted) */
1480 if (row > 0 && bot_start + row
1481 + (int)wp->w_lines[j].wl_size > wp->w_height)
1482 {
1483 wp->w_lines_valid = idx + 1;
1484 break;
1485 }
1486 bot_start += wp->w_lines[idx++].wl_size;
1487
1488 /* stop at the last valid entry in w_lines[].wl_size */
1489 if (++j >= wp->w_lines_valid)
1490 {
1491 wp->w_lines_valid = idx;
1492 break;
1493 }
1494 }
1495#ifdef FEAT_DIFF
1496 /* Correct the first entry for filler lines at the top
1497 * when it won't get updated below. */
1498 if (wp->w_p_diff && bot_start > 0)
1499 wp->w_lines[0].wl_size =
1500 plines_win_nofill(wp, wp->w_topline, TRUE)
1501 + wp->w_topfill;
1502#endif
1503 }
1504 }
1505 }
1506
1507 /* When starting redraw in the first line, redraw all lines. When
1508 * there is only one window it's probably faster to clear the screen
1509 * first. */
1510 if (mid_start == 0)
1511 {
1512 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001513 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001514 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001515 /* Clear the screen when it was not done by win_del_lines() or
1516 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1517 * then. */
1518 if (screen_cleared != TRUE)
1519 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001520 /* The screen was cleared, redraw the tab pages line. */
1521 if (redraw_tabline)
1522 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001525
1526 /* When win_del_lines() or win_ins_lines() caused the screen to be
1527 * cleared (only happens for the first window) or when screenclear()
1528 * was called directly above, "must_redraw" will have been set to
1529 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1530 if (screen_cleared == TRUE)
1531 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 }
1533 else
1534 {
1535 /* Not VALID or INVERTED: redraw all lines. */
1536 mid_start = 0;
1537 mid_end = wp->w_height;
1538 }
1539
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001540 if (type == SOME_VALID)
1541 {
1542 /* SOME_VALID: redraw all lines. */
1543 mid_start = 0;
1544 mid_end = wp->w_height;
1545 type = NOT_VALID;
1546 }
1547
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 /* check if we are updating or removing the inverted part */
1549 if ((VIsual_active && buf == curwin->w_buffer)
1550 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1551 {
1552 linenr_T from, to;
1553
1554 if (VIsual_active)
1555 {
1556 if (VIsual_active
1557 && (VIsual_mode != wp->w_old_visual_mode
1558 || type == INVERTED_ALL))
1559 {
1560 /*
1561 * If the type of Visual selection changed, redraw the whole
1562 * selection. Also when the ownership of the X selection is
1563 * gained or lost.
1564 */
1565 if (curwin->w_cursor.lnum < VIsual.lnum)
1566 {
1567 from = curwin->w_cursor.lnum;
1568 to = VIsual.lnum;
1569 }
1570 else
1571 {
1572 from = VIsual.lnum;
1573 to = curwin->w_cursor.lnum;
1574 }
1575 /* redraw more when the cursor moved as well */
1576 if (wp->w_old_cursor_lnum < from)
1577 from = wp->w_old_cursor_lnum;
1578 if (wp->w_old_cursor_lnum > to)
1579 to = wp->w_old_cursor_lnum;
1580 if (wp->w_old_visual_lnum < from)
1581 from = wp->w_old_visual_lnum;
1582 if (wp->w_old_visual_lnum > to)
1583 to = wp->w_old_visual_lnum;
1584 }
1585 else
1586 {
1587 /*
1588 * Find the line numbers that need to be updated: The lines
1589 * between the old cursor position and the current cursor
1590 * position. Also check if the Visual position changed.
1591 */
1592 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1593 {
1594 from = curwin->w_cursor.lnum;
1595 to = wp->w_old_cursor_lnum;
1596 }
1597 else
1598 {
1599 from = wp->w_old_cursor_lnum;
1600 to = curwin->w_cursor.lnum;
1601 if (from == 0) /* Visual mode just started */
1602 from = to;
1603 }
1604
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001605 if (VIsual.lnum != wp->w_old_visual_lnum
1606 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {
1608 if (wp->w_old_visual_lnum < from
1609 && wp->w_old_visual_lnum != 0)
1610 from = wp->w_old_visual_lnum;
1611 if (wp->w_old_visual_lnum > to)
1612 to = wp->w_old_visual_lnum;
1613 if (VIsual.lnum < from)
1614 from = VIsual.lnum;
1615 if (VIsual.lnum > to)
1616 to = VIsual.lnum;
1617 }
1618 }
1619
1620 /*
1621 * If in block mode and changed column or curwin->w_curswant:
1622 * update all lines.
1623 * First compute the actual start and end column.
1624 */
1625 if (VIsual_mode == Ctrl_V)
1626 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001627 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001628#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001629 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
Bram Moolenaar404406a2014-10-09 13:24:43 +02001631 if (curwin->w_p_lbr)
1632 ve_flags = VE_ALL;
1633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001635#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001636 ve_flags = save_ve_flags;
1637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 ++toc;
1639 if (curwin->w_curswant == MAXCOL)
1640 toc = MAXCOL;
1641
1642 if (fromc != wp->w_old_cursor_fcol
1643 || toc != wp->w_old_cursor_lcol)
1644 {
1645 if (from > VIsual.lnum)
1646 from = VIsual.lnum;
1647 if (to < VIsual.lnum)
1648 to = VIsual.lnum;
1649 }
1650 wp->w_old_cursor_fcol = fromc;
1651 wp->w_old_cursor_lcol = toc;
1652 }
1653 }
1654 else
1655 {
1656 /* Use the line numbers of the old Visual area. */
1657 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1658 {
1659 from = wp->w_old_cursor_lnum;
1660 to = wp->w_old_visual_lnum;
1661 }
1662 else
1663 {
1664 from = wp->w_old_visual_lnum;
1665 to = wp->w_old_cursor_lnum;
1666 }
1667 }
1668
1669 /*
1670 * There is no need to update lines above the top of the window.
1671 */
1672 if (from < wp->w_topline)
1673 from = wp->w_topline;
1674
1675 /*
1676 * If we know the value of w_botline, use it to restrict the update to
1677 * the lines that are visible in the window.
1678 */
1679 if (wp->w_valid & VALID_BOTLINE)
1680 {
1681 if (from >= wp->w_botline)
1682 from = wp->w_botline - 1;
1683 if (to >= wp->w_botline)
1684 to = wp->w_botline - 1;
1685 }
1686
1687 /*
1688 * Find the minimal part to be updated.
1689 * Watch out for scrolling that made entries in w_lines[] invalid.
1690 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1691 * top_end; need to redraw from top_end to the "to" line.
1692 * A middle mouse click with a Visual selection may change the text
1693 * above the Visual area and reset wl_valid, do count these for
1694 * mid_end (in srow).
1695 */
1696 if (mid_start > 0)
1697 {
1698 lnum = wp->w_topline;
1699 idx = 0;
1700 srow = 0;
1701 if (scrolled_down)
1702 mid_start = top_end;
1703 else
1704 mid_start = 0;
1705 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1706 {
1707 if (wp->w_lines[idx].wl_valid)
1708 mid_start += wp->w_lines[idx].wl_size;
1709 else if (!scrolled_down)
1710 srow += wp->w_lines[idx].wl_size;
1711 ++idx;
1712# ifdef FEAT_FOLDING
1713 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1714 lnum = wp->w_lines[idx].wl_lnum;
1715 else
1716# endif
1717 ++lnum;
1718 }
1719 srow += mid_start;
1720 mid_end = wp->w_height;
1721 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1722 {
1723 if (wp->w_lines[idx].wl_valid
1724 && wp->w_lines[idx].wl_lnum >= to + 1)
1725 {
1726 /* Only update until first row of this line */
1727 mid_end = srow;
1728 break;
1729 }
1730 srow += wp->w_lines[idx].wl_size;
1731 }
1732 }
1733 }
1734
1735 if (VIsual_active && buf == curwin->w_buffer)
1736 {
1737 wp->w_old_visual_mode = VIsual_mode;
1738 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1739 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001740 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 wp->w_old_curswant = curwin->w_curswant;
1742 }
1743 else
1744 {
1745 wp->w_old_visual_mode = 0;
1746 wp->w_old_cursor_lnum = 0;
1747 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001748 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750
1751#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1752 /* reset got_int, otherwise regexp won't work */
1753 save_got_int = got_int;
1754 got_int = 0;
1755#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001756#ifdef SYN_TIME_LIMIT
1757 /* Set the time limit to 'redrawtime'. */
1758 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001759 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761#ifdef FEAT_FOLDING
1762 win_foldinfo.fi_level = 0;
1763#endif
1764
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001765#ifdef FEAT_MENU
1766 /*
1767 * Draw the window toolbar, if there is one.
1768 * TODO: only when needed.
1769 */
1770 if (winbar_height(wp) > 0)
1771 redraw_win_toolbar(wp);
1772#endif
1773
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 /*
1775 * Update all the window rows.
1776 */
1777 idx = 0; /* first entry in w_lines[].wl_size */
1778 row = 0;
1779 srow = 0;
1780 lnum = wp->w_topline; /* first line shown in window */
1781 for (;;)
1782 {
1783 /* stop updating when reached the end of the window (check for _past_
1784 * the end of the window is at the end of the loop) */
1785 if (row == wp->w_height)
1786 {
1787 didline = TRUE;
1788 break;
1789 }
1790
1791 /* stop updating when hit the end of the file */
1792 if (lnum > buf->b_ml.ml_line_count)
1793 {
1794 eof = TRUE;
1795 break;
1796 }
1797
1798 /* Remember the starting row of the line that is going to be dealt
1799 * with. It is used further down when the line doesn't fit. */
1800 srow = row;
1801
1802 /*
1803 * Update a line when it is in an area that needs updating, when it
1804 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001805 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 * win_del_lines(), check if the current line includes it.
1807 * When syntax folding is being used, the saved syntax states will
1808 * already have been updated, we can't see where the syntax state is
1809 * the same again, just update until the end of the window.
1810 */
1811 if (row < top_end
1812 || (row >= mid_start && row < mid_end)
1813#ifdef FEAT_SEARCH_EXTRA
1814 || top_to_mod
1815#endif
1816 || idx >= wp->w_lines_valid
1817 || (row + wp->w_lines[idx].wl_size > bot_start)
1818 || (mod_top != 0
1819 && (lnum == mod_top
1820 || (lnum >= mod_top
1821 && (lnum < mod_bot
1822#ifdef FEAT_SYN_HL
1823 || did_update == DID_FOLD
1824 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001825 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 && (
1827# ifdef FEAT_FOLDING
1828 (foldmethodIsSyntax(wp)
1829 && hasAnyFolding(wp)) ||
1830# endif
1831 syntax_check_changed(lnum)))
1832#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001833#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001834 /* match in fixed position might need redraw
1835 * if lines were inserted or deleted */
1836 || (wp->w_match_head != NULL
1837 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 )))))
1840 {
1841#ifdef FEAT_SEARCH_EXTRA
1842 if (lnum == mod_top)
1843 top_to_mod = FALSE;
1844#endif
1845
1846 /*
1847 * When at start of changed lines: May scroll following lines
1848 * up or down to minimize redrawing.
1849 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001850 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 */
1852 if (lnum == mod_top
1853 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001854 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 {
1856 int old_rows = 0;
1857 int new_rows = 0;
1858 int xtra_rows;
1859 linenr_T l;
1860
1861 /* Count the old number of window rows, using w_lines[], which
1862 * should still contain the sizes for the lines as they are
1863 * currently displayed. */
1864 for (i = idx; i < wp->w_lines_valid; ++i)
1865 {
1866 /* Only valid lines have a meaningful wl_lnum. Invalid
1867 * lines are part of the changed area. */
1868 if (wp->w_lines[i].wl_valid
1869 && wp->w_lines[i].wl_lnum == mod_bot)
1870 break;
1871 old_rows += wp->w_lines[i].wl_size;
1872#ifdef FEAT_FOLDING
1873 if (wp->w_lines[i].wl_valid
1874 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1875 {
1876 /* Must have found the last valid entry above mod_bot.
1877 * Add following invalid entries. */
1878 ++i;
1879 while (i < wp->w_lines_valid
1880 && !wp->w_lines[i].wl_valid)
1881 old_rows += wp->w_lines[i++].wl_size;
1882 break;
1883 }
1884#endif
1885 }
1886
1887 if (i >= wp->w_lines_valid)
1888 {
1889 /* We can't find a valid line below the changed lines,
1890 * need to redraw until the end of the window.
1891 * Inserting/deleting lines has no use. */
1892 bot_start = 0;
1893 }
1894 else
1895 {
1896 /* Able to count old number of rows: Count new window
1897 * rows, and may insert/delete lines */
1898 j = idx;
1899 for (l = lnum; l < mod_bot; ++l)
1900 {
1901#ifdef FEAT_FOLDING
1902 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1903 ++new_rows;
1904 else
1905#endif
1906#ifdef FEAT_DIFF
1907 if (l == wp->w_topline)
1908 new_rows += plines_win_nofill(wp, l, TRUE)
1909 + wp->w_topfill;
1910 else
1911#endif
1912 new_rows += plines_win(wp, l, TRUE);
1913 ++j;
1914 if (new_rows > wp->w_height - row - 2)
1915 {
1916 /* it's getting too much, must redraw the rest */
1917 new_rows = 9999;
1918 break;
1919 }
1920 }
1921 xtra_rows = new_rows - old_rows;
1922 if (xtra_rows < 0)
1923 {
1924 /* May scroll text up. If there is not enough
1925 * remaining text or scrolling fails, must redraw the
1926 * rest. If scrolling works, must redraw the text
1927 * below the scrolled text. */
1928 if (row - xtra_rows >= wp->w_height - 2)
1929 mod_bot = MAXLNUM;
1930 else
1931 {
1932 check_for_delay(FALSE);
1933 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001934 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 mod_bot = MAXLNUM;
1936 else
1937 bot_start = wp->w_height + xtra_rows;
1938 }
1939 }
1940 else if (xtra_rows > 0)
1941 {
1942 /* May scroll text down. If there is not enough
1943 * remaining text of scrolling fails, must redraw the
1944 * rest. */
1945 if (row + xtra_rows >= wp->w_height - 2)
1946 mod_bot = MAXLNUM;
1947 else
1948 {
1949 check_for_delay(FALSE);
1950 if (win_ins_lines(wp, row + old_rows,
1951 xtra_rows, FALSE, FALSE) == FAIL)
1952 mod_bot = MAXLNUM;
1953 else if (top_end > row + old_rows)
1954 /* Scrolled the part at the top that requires
1955 * updating down. */
1956 top_end += xtra_rows;
1957 }
1958 }
1959
1960 /* When not updating the rest, may need to move w_lines[]
1961 * entries. */
1962 if (mod_bot != MAXLNUM && i != j)
1963 {
1964 if (j < i)
1965 {
1966 int x = row + new_rows;
1967
1968 /* move entries in w_lines[] upwards */
1969 for (;;)
1970 {
1971 /* stop at last valid entry in w_lines[] */
1972 if (i >= wp->w_lines_valid)
1973 {
1974 wp->w_lines_valid = j;
1975 break;
1976 }
1977 wp->w_lines[j] = wp->w_lines[i];
1978 /* stop at a line that won't fit */
1979 if (x + (int)wp->w_lines[j].wl_size
1980 > wp->w_height)
1981 {
1982 wp->w_lines_valid = j + 1;
1983 break;
1984 }
1985 x += wp->w_lines[j++].wl_size;
1986 ++i;
1987 }
1988 if (bot_start > x)
1989 bot_start = x;
1990 }
1991 else /* j > i */
1992 {
1993 /* move entries in w_lines[] downwards */
1994 j -= i;
1995 wp->w_lines_valid += j;
1996 if (wp->w_lines_valid > wp->w_height)
1997 wp->w_lines_valid = wp->w_height;
1998 for (i = wp->w_lines_valid; i - j >= idx; --i)
1999 wp->w_lines[i] = wp->w_lines[i - j];
2000
2001 /* The w_lines[] entries for inserted lines are
2002 * now invalid, but wl_size may be used above.
2003 * Reset to zero. */
2004 while (i >= idx)
2005 {
2006 wp->w_lines[i].wl_size = 0;
2007 wp->w_lines[i--].wl_valid = FALSE;
2008 }
2009 }
2010 }
2011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 }
2013
2014#ifdef FEAT_FOLDING
2015 /*
2016 * When lines are folded, display one line for all of them.
2017 * Otherwise, display normally (can be several display lines when
2018 * 'wrap' is on).
2019 */
2020 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2021 if (fold_count != 0)
2022 {
2023 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2024 ++row;
2025 --fold_count;
2026 wp->w_lines[idx].wl_folded = TRUE;
2027 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2028# ifdef FEAT_SYN_HL
2029 did_update = DID_FOLD;
2030# endif
2031 }
2032 else
2033#endif
2034 if (idx < wp->w_lines_valid
2035 && wp->w_lines[idx].wl_valid
2036 && wp->w_lines[idx].wl_lnum == lnum
2037 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002038 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 && srow + wp->w_lines[idx].wl_size > wp->w_height
2040#ifdef FEAT_DIFF
2041 && diff_check_fill(wp, lnum) == 0
2042#endif
2043 )
2044 {
2045 /* This line is not going to fit. Don't draw anything here,
2046 * will draw "@ " lines below. */
2047 row = wp->w_height + 1;
2048 }
2049 else
2050 {
2051#ifdef FEAT_SEARCH_EXTRA
2052 prepare_search_hl(wp, lnum);
2053#endif
2054#ifdef FEAT_SYN_HL
2055 /* Let the syntax stuff know we skipped a few lines. */
2056 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002057 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 syntax_end_parsing(syntax_last_parsed + 1);
2059#endif
2060
2061 /*
2062 * Display one line.
2063 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002064 row = win_line(wp, lnum, srow, wp->w_height,
2065 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
2067#ifdef FEAT_FOLDING
2068 wp->w_lines[idx].wl_folded = FALSE;
2069 wp->w_lines[idx].wl_lastlnum = lnum;
2070#endif
2071#ifdef FEAT_SYN_HL
2072 did_update = DID_LINE;
2073 syntax_last_parsed = lnum;
2074#endif
2075 }
2076
2077 wp->w_lines[idx].wl_lnum = lnum;
2078 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002079
2080 /* Past end of the window or end of the screen. Note that after
2081 * resizing wp->w_height may be end up too big. That's a problem
2082 * elsewhere, but prevent a crash here. */
2083 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
2085 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002086 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2088 ++idx;
2089 break;
2090 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002091 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 wp->w_lines[idx].wl_size = row - srow;
2093 ++idx;
2094#ifdef FEAT_FOLDING
2095 lnum += fold_count + 1;
2096#else
2097 ++lnum;
2098#endif
2099 }
2100 else
2101 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002102 if (wp->w_p_rnu)
2103 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002104#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002105 // 'relativenumber' set: The text doesn't need to be drawn, but
2106 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002107 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2108 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002109 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002110 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002111#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002112 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002113 }
2114
2115 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 row += wp->w_lines[idx++].wl_size;
2117 if (row > wp->w_height) /* past end of screen */
2118 break;
2119#ifdef FEAT_FOLDING
2120 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2121#else
2122 ++lnum;
2123#endif
2124#ifdef FEAT_SYN_HL
2125 did_update = DID_NONE;
2126#endif
2127 }
2128
2129 if (lnum > buf->b_ml.ml_line_count)
2130 {
2131 eof = TRUE;
2132 break;
2133 }
2134 }
2135 /*
2136 * End of loop over all window lines.
2137 */
2138
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002139#ifdef FEAT_VTP
2140 /* Rewrite the character at the end of the screen line. */
2141 if (use_vtp())
2142 {
2143 int i;
2144
2145 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002146 if (enc_utf8)
2147 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2148 LineOffset[i] + screen_Columns) > 1)
2149 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2150 else
2151 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2152 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002153 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2154 }
2155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
2157 if (idx > wp->w_lines_valid)
2158 wp->w_lines_valid = idx;
2159
2160#ifdef FEAT_SYN_HL
2161 /*
2162 * Let the syntax stuff know we stop parsing here.
2163 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002164 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 syntax_end_parsing(syntax_last_parsed + 1);
2166#endif
2167
2168 /*
2169 * If we didn't hit the end of the file, and we didn't finish the last
2170 * line we were working on, then the line didn't fit.
2171 */
2172 wp->w_empty_rows = 0;
2173#ifdef FEAT_DIFF
2174 wp->w_filler_rows = 0;
2175#endif
2176 if (!eof && !didline)
2177 {
2178 if (lnum == wp->w_topline)
2179 {
2180 /*
2181 * Single line that does not fit!
2182 * Don't overwrite it, it can be edited.
2183 */
2184 wp->w_botline = lnum + 1;
2185 }
2186#ifdef FEAT_DIFF
2187 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2188 {
2189 /* Window ends in filler lines. */
2190 wp->w_botline = lnum;
2191 wp->w_filler_rows = wp->w_height - srow;
2192 }
2193#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002194 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2195 {
2196 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2197
2198 /*
2199 * Last line isn't finished: Display "@@@" in the last screen line.
2200 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002201 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002202 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002203 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002204 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002205 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002206 set_empty_rows(wp, srow);
2207 wp->w_botline = lnum;
2208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2210 {
2211 /*
2212 * Last line isn't finished: Display "@@@" at the end.
2213 */
2214 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2215 W_WINROW(wp) + wp->w_height,
2216 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002217 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 set_empty_rows(wp, srow);
2219 wp->w_botline = lnum;
2220 }
2221 else
2222 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002223 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 wp->w_botline = lnum;
2225 }
2226 }
2227 else
2228 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 if (eof) /* we hit the end of the file */
2231 {
2232 wp->w_botline = buf->b_ml.ml_line_count + 1;
2233#ifdef FEAT_DIFF
2234 j = diff_check_fill(wp, wp->w_botline);
2235 if (j > 0 && !wp->w_botfill)
2236 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002237 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 if (char2cells(fill_diff) > 1)
2239 i = '-';
2240 else
2241 i = fill_diff;
2242 if (row + j > wp->w_height)
2243 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002244 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 row += j;
2246 }
2247#endif
2248 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002249 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 wp->w_botline = lnum;
2251
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002252 // Make sure the rest of the screen is blank
2253 // put '~'s on rows that aren't part of the file.
2254 win_draw_end(wp, '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 }
2256
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002257#ifdef SYN_TIME_LIMIT
2258 syn_set_timeout(NULL);
2259#endif
2260
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 /* Reset the type of redrawing required, the window has been updated. */
2262 wp->w_redr_type = 0;
2263#ifdef FEAT_DIFF
2264 wp->w_old_topfill = wp->w_topfill;
2265 wp->w_old_botfill = wp->w_botfill;
2266#endif
2267
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002268 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
2270 /*
2271 * There is a trick with w_botline. If we invalidate it on each
2272 * change that might modify it, this will cause a lot of expensive
2273 * calls to plines() in update_topline() each time. Therefore the
2274 * value of w_botline is often approximated, and this value is used to
2275 * compute the value of w_topline. If the value of w_botline was
2276 * wrong, check that the value of w_topline is correct (cursor is on
2277 * the visible part of the text). If it's not, we need to redraw
2278 * again. Mostly this just means scrolling up a few lines, so it
2279 * doesn't look too bad. Only do this for the current window (where
2280 * changes are relevant).
2281 */
2282 wp->w_valid |= VALID_BOTLINE;
2283 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2284 {
2285 recursive = TRUE;
2286 curwin->w_valid &= ~VALID_TOPLINE;
2287 update_topline(); /* may invalidate w_botline again */
2288 if (must_redraw != 0)
2289 {
2290 /* Don't update for changes in buffer again. */
2291 i = curbuf->b_mod_set;
2292 curbuf->b_mod_set = FALSE;
2293 win_update(curwin);
2294 must_redraw = 0;
2295 curbuf->b_mod_set = i;
2296 }
2297 recursive = FALSE;
2298 }
2299 }
2300
2301#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2302 /* restore got_int, unless CTRL-C was hit while redrawing */
2303 if (!got_int)
2304 got_int = save_got_int;
2305#endif
2306}
2307
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002309 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2310 * Return the new offset.
2311 */
2312 static int
2313screen_fill_end(
2314 win_T *wp,
2315 int c1,
2316 int c2,
2317 int off,
2318 int width,
2319 int row,
2320 int endrow,
2321 int attr)
2322{
2323 int nn = off + width;
2324
2325 if (nn > wp->w_width)
2326 nn = wp->w_width;
2327#ifdef FEAT_RIGHTLEFT
2328 if (wp->w_p_rl)
2329 {
2330 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2331 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2332 c1, c2, attr);
2333 }
2334 else
2335#endif
2336 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2337 wp->w_wincol + off, (int)wp->w_wincol + nn,
2338 c1, c2, attr);
2339 return nn;
2340}
2341
2342/*
2343 * Clear lines near the end the window and mark the unused lines with "c1".
2344 * use "c2" as the filler character.
2345 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 */
2347 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002348win_draw_end(
2349 win_T *wp,
2350 int c1,
2351 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002352 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002353 int row,
2354 int endrow,
2355 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 int n = 0;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002358
2359 if (draw_margin)
2360 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002361#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002362 int fdc = compute_foldcolumn(wp, 0);
2363
2364 if (fdc > 0)
2365 // draw the fold column
2366 n = screen_fill_end(wp, ' ', ' ', n, fdc,
2367 row, endrow, HL_ATTR(HLF_FC));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002368#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002369#ifdef FEAT_SIGNS
2370 if (signcolumn_on(wp))
2371 // draw the sign column
2372 n = screen_fill_end(wp, ' ', ' ', n, 2,
2373 row, endrow, HL_ATTR(HLF_SC));
2374#endif
2375 if ((wp->w_p_nu || wp->w_p_rnu)
2376 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2377 // draw the number column
2378 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
2379 row, endrow, HL_ATTR(HLF_N));
2380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381
2382#ifdef FEAT_RIGHTLEFT
2383 if (wp->w_p_rl)
2384 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002386 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002387 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002389 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002390 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
2392 else
2393#endif
2394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002396 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002397 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002399
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 set_empty_rows(wp, row);
2401}
2402
Bram Moolenaar1a384422010-07-14 19:53:30 +02002403#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002404/*
2405 * Advance **color_cols and return TRUE when there are columns to draw.
2406 */
2407 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002408advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002409{
2410 while (**color_cols >= 0 && vcol > **color_cols)
2411 ++*color_cols;
2412 return (**color_cols >= 0);
2413}
2414#endif
2415
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002416#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2417/*
2418 * Copy "text" to ScreenLines using "attr".
2419 * Returns the next screen column.
2420 */
2421 static int
2422text_to_screenline(win_T *wp, char_u *text, int col)
2423{
2424 int off = (int)(current_ScreenLine - ScreenLines);
2425
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002426 if (has_mbyte)
2427 {
2428 int cells;
2429 int u8c, u8cc[MAX_MCO];
2430 int i;
2431 int idx;
2432 int c_len;
2433 char_u *p;
2434# ifdef FEAT_ARABIC
2435 int prev_c = 0; /* previous Arabic character */
2436 int prev_c1 = 0; /* first composing char for prev_c */
2437# endif
2438
2439# ifdef FEAT_RIGHTLEFT
2440 if (wp->w_p_rl)
2441 idx = off;
2442 else
2443# endif
2444 idx = off + col;
2445
2446 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2447 for (p = text; *p != NUL; )
2448 {
2449 cells = (*mb_ptr2cells)(p);
2450 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002451 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002452# ifdef FEAT_RIGHTLEFT
2453 - (wp->w_p_rl ? col : 0)
2454# endif
2455 )
2456 break;
2457 ScreenLines[idx] = *p;
2458 if (enc_utf8)
2459 {
2460 u8c = utfc_ptr2char(p, u8cc);
2461 if (*p < 0x80 && u8cc[0] == 0)
2462 {
2463 ScreenLinesUC[idx] = 0;
2464#ifdef FEAT_ARABIC
2465 prev_c = u8c;
2466#endif
2467 }
2468 else
2469 {
2470#ifdef FEAT_ARABIC
2471 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2472 {
2473 /* Do Arabic shaping. */
2474 int pc, pc1, nc;
2475 int pcc[MAX_MCO];
2476 int firstbyte = *p;
2477
2478 /* The idea of what is the previous and next
2479 * character depends on 'rightleft'. */
2480 if (wp->w_p_rl)
2481 {
2482 pc = prev_c;
2483 pc1 = prev_c1;
2484 nc = utf_ptr2char(p + c_len);
2485 prev_c1 = u8cc[0];
2486 }
2487 else
2488 {
2489 pc = utfc_ptr2char(p + c_len, pcc);
2490 nc = prev_c;
2491 pc1 = pcc[0];
2492 }
2493 prev_c = u8c;
2494
2495 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2496 pc, pc1, nc);
2497 ScreenLines[idx] = firstbyte;
2498 }
2499 else
2500 prev_c = u8c;
2501#endif
2502 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002503 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002504 for (i = 0; i < Screen_mco; ++i)
2505 {
2506 ScreenLinesC[i][idx] = u8cc[i];
2507 if (u8cc[i] == 0)
2508 break;
2509 }
2510 }
2511 if (cells > 1)
2512 ScreenLines[idx + 1] = 0;
2513 }
2514 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2515 /* double-byte single width character */
2516 ScreenLines2[idx] = p[1];
2517 else if (cells > 1)
2518 /* double-width character */
2519 ScreenLines[idx + 1] = p[1];
2520 col += cells;
2521 idx += cells;
2522 p += c_len;
2523 }
2524 }
2525 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002526 {
2527 int len = (int)STRLEN(text);
2528
Bram Moolenaar02631462017-09-22 15:20:32 +02002529 if (len > wp->w_width - col)
2530 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002531 if (len > 0)
2532 {
2533#ifdef FEAT_RIGHTLEFT
2534 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002535 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002536 else
2537#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002538 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002539 col += len;
2540 }
2541 }
2542 return col;
2543}
2544#endif
2545
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546#ifdef FEAT_FOLDING
2547/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002548 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2549 * space is available for window "wp", minus "col".
2550 */
2551 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002552compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002553{
2554 int fdc = wp->w_p_fdc;
2555 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002556 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002557
2558 if (fdc > wwidth - (col + wmw))
2559 fdc = wwidth - (col + wmw);
2560 return fdc;
2561}
2562
2563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 * Display one folded line.
2565 */
2566 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002567fold_line(
2568 win_T *wp,
2569 long fold_count,
2570 foldinfo_T *foldinfo,
2571 linenr_T lnum,
2572 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002574 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 pos_T *top, *bot;
2576 linenr_T lnume = lnum + fold_count - 1;
2577 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002578 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 int col;
2581 int txtcol;
2582 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002583 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
2585 /* Build the fold line:
2586 * 1. Add the cmdwin_type for the command-line window
2587 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002588 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 * 4. Compose the text
2590 * 5. Add the text
2591 * 6. set highlighting for the Visual area an other text
2592 */
2593 col = 0;
2594
2595 /*
2596 * 1. Add the cmdwin_type for the command-line window
2597 * Ignores 'rightleft', this window is never right-left.
2598 */
2599#ifdef FEAT_CMDWIN
2600 if (cmdwin_type != 0 && wp == curwin)
2601 {
2602 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002603 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 if (enc_utf8)
2605 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 ++col;
2607 }
2608#endif
2609
2610 /*
2611 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002612 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002614 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 if (fdc > 0)
2616 {
2617 fill_foldcolumn(buf, wp, TRUE, lnum);
2618#ifdef FEAT_RIGHTLEFT
2619 if (wp->w_p_rl)
2620 {
2621 int i;
2622
Bram Moolenaar02631462017-09-22 15:20:32 +02002623 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002624 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 /* reverse the fold column */
2626 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002627 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 }
2629 else
2630#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002631 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 col += fdc;
2633 }
2634
2635#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002636# define RL_MEMSET(p, v, l) \
2637 do { \
2638 if (wp->w_p_rl) \
2639 for (ri = 0; ri < l; ++ri) \
2640 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2641 else \
2642 for (ri = 0; ri < l; ++ri) \
2643 ScreenAttrs[off + (p) + ri] = v; \
2644 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002646# define RL_MEMSET(p, v, l) \
2647 do { \
2648 for (ri = 0; ri < l; ++ri) \
2649 ScreenAttrs[off + (p) + ri] = v; \
2650 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651#endif
2652
Bram Moolenaar64486672010-05-16 15:46:46 +02002653 /* Set all attributes of the 'number' or 'relativenumber' column and the
2654 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002655 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656
2657#ifdef FEAT_SIGNS
2658 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002659 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002661 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (len > 0)
2663 {
2664 if (len > 2)
2665 len = 2;
2666# ifdef FEAT_RIGHTLEFT
2667 if (wp->w_p_rl)
2668 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002669 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002670 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 else
2672# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002673 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 col += len;
2675 }
2676 }
2677#endif
2678
2679 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002680 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002682 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002684 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 if (len > 0)
2686 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002687 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002688 long num;
2689 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002690
2691 if (len > w + 1)
2692 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002693
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002694 if (wp->w_p_nu && !wp->w_p_rnu)
2695 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002696 num = (long)lnum;
2697 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002698 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002699 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002700 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002701 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002702 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002703 /* 'number' + 'relativenumber': cursor line shows absolute
2704 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002705 num = lnum;
2706 fmt = "%-*ld ";
2707 }
2708 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002709
Bram Moolenaar700e7342013-01-30 12:31:36 +01002710 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711#ifdef FEAT_RIGHTLEFT
2712 if (wp->w_p_rl)
2713 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002714 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002715 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 else
2717#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002718 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 col += len;
2720 }
2721 }
2722
2723 /*
2724 * 4. Compose the folded-line string with 'foldtext', if set.
2725 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002726 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
2728 txtcol = col; /* remember where text starts */
2729
2730 /*
2731 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2732 * Right-left text is put in columns 0 - number-col, normal text is put
2733 * in columns number-col - window-width.
2734 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002735 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
2737 /* Fill the rest of the line with the fold filler */
2738#ifdef FEAT_RIGHTLEFT
2739 if (wp->w_p_rl)
2740 col -= txtcol;
2741#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002742 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743#ifdef FEAT_RIGHTLEFT
2744 - (wp->w_p_rl ? txtcol : 0)
2745#endif
2746 )
2747 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 if (enc_utf8)
2749 {
2750 if (fill_fold >= 0x80)
2751 {
2752 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002753 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002754 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 }
2756 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002757 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002759 ScreenLines[off + col] = fill_fold;
2760 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002761 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002763 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002764 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 }
2766
2767 if (text != buf)
2768 vim_free(text);
2769
2770 /*
2771 * 6. set highlighting for the Visual area an other text.
2772 * If all folded lines are in the Visual area, highlight the line.
2773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2775 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002776 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 {
2778 /* Visual is after curwin->w_cursor */
2779 top = &curwin->w_cursor;
2780 bot = &VIsual;
2781 }
2782 else
2783 {
2784 /* Visual is before curwin->w_cursor */
2785 top = &VIsual;
2786 bot = &curwin->w_cursor;
2787 }
2788 if (lnum >= top->lnum
2789 && lnume <= bot->lnum
2790 && (VIsual_mode != 'v'
2791 || ((lnum > top->lnum
2792 || (lnum == top->lnum
2793 && top->col == 0))
2794 && (lnume < bot->lnum
2795 || (lnume == bot->lnum
2796 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002797 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
2799 if (VIsual_mode == Ctrl_V)
2800 {
2801 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002802 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002804 if (wp->w_old_cursor_lcol != MAXCOL
2805 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002806 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 len = wp->w_old_cursor_lcol;
2808 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002809 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002810 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002811 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 }
2813 }
2814 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002817 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 }
2820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002822#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002823 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2824 if (wp->w_p_cc_cols)
2825 {
2826 int i = 0;
2827 int j = wp->w_p_cc_cols[i];
2828 int old_txtcol = txtcol;
2829
2830 while (j > -1)
2831 {
2832 txtcol += j;
2833 if (wp->w_p_wrap)
2834 txtcol -= wp->w_skipcol;
2835 else
2836 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002837 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002838 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002839 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002840 txtcol = old_txtcol;
2841 j = wp->w_p_cc_cols[++i];
2842 }
2843 }
2844
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002845 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002846 if (wp->w_p_cuc)
2847 {
2848 txtcol += wp->w_virtcol;
2849 if (wp->w_p_wrap)
2850 txtcol -= wp->w_skipcol;
2851 else
2852 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002853 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002854 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002855 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002856 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
Bram Moolenaar02631462017-09-22 15:20:32 +02002859 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2860 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861
2862 /*
2863 * Update w_cline_height and w_cline_folded if the cursor line was
2864 * updated (saves a call to plines() later).
2865 */
2866 if (wp == curwin
2867 && lnum <= curwin->w_cursor.lnum
2868 && lnume >= curwin->w_cursor.lnum)
2869 {
2870 curwin->w_cline_row = row;
2871 curwin->w_cline_height = 1;
2872 curwin->w_cline_folded = TRUE;
2873 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2874 }
2875}
2876
2877/*
2878 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2879 */
2880 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002881copy_text_attr(
2882 int off,
2883 char_u *buf,
2884 int len,
2885 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002887 int i;
2888
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 if (enc_utf8)
2891 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002892 for (i = 0; i < len; ++i)
2893 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894}
2895
2896/*
2897 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002898 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 */
2900 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002901fill_foldcolumn(
2902 char_u *p,
2903 win_T *wp,
2904 int closed, /* TRUE of FALSE */
2905 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906{
2907 int i = 0;
2908 int level;
2909 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002910 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002911 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912
2913 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002914 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915
2916 level = win_foldinfo.fi_level;
2917 if (level > 0)
2918 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002919 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002920 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002921
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 /* If the column is too narrow, we start at the lowest level that
2923 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002924 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 if (first_level < 1)
2926 first_level = 1;
2927
Bram Moolenaar1c934292015-01-27 16:39:29 +01002928 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 {
2930 if (win_foldinfo.fi_lnum == lnum
2931 && first_level + i >= win_foldinfo.fi_low_level)
2932 p[i] = '-';
2933 else if (first_level == 1)
2934 p[i] = '|';
2935 else if (first_level + i <= 9)
2936 p[i] = '0' + first_level + i;
2937 else
2938 p[i] = '>';
2939 if (first_level + i == level)
2940 break;
2941 }
2942 }
2943 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002944 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945}
2946#endif /* FEAT_FOLDING */
2947
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01002948#ifdef FEAT_TEXT_PROP
2949static textprop_T *current_text_props = NULL;
2950static buf_T *current_buf = NULL;
2951
2952 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01002953text_prop_compare(const void *s1, const void *s2)
2954{
2955 int idx1, idx2;
2956 proptype_T *pt1, *pt2;
2957 colnr_T col1, col2;
2958
2959 idx1 = *(int *)s1;
2960 idx2 = *(int *)s2;
2961 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
2962 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
2963 if (pt1 == pt2)
2964 return 0;
2965 if (pt1 == NULL)
2966 return -1;
2967 if (pt2 == NULL)
2968 return 1;
2969 if (pt1->pt_priority != pt2->pt_priority)
2970 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
2971 col1 = current_text_props[idx1].tp_col;
2972 col2 = current_text_props[idx2].tp_col;
2973 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
2974}
2975#endif
2976
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977/*
2978 * Display line "lnum" of window 'wp' on the screen.
2979 * Start at row "startrow", stop when "endrow" is reached.
2980 * wp->w_virtcol needs to be valid.
2981 *
2982 * Return the number of last row the line occupies.
2983 */
2984 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002985win_line(
2986 win_T *wp,
2987 linenr_T lnum,
2988 int startrow,
2989 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002990 int nochange UNUSED, // not updating for changed text
2991 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01002993 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2995 int c = 0; /* init for GCC */
2996 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002997#ifdef FEAT_LINEBREAK
2998 long vcol_sbr = -1; /* virtual column after showbreak */
2999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 long vcol_prev = -1; /* "vcol" of previous character */
3001 char_u *line; /* current line */
3002 char_u *ptr; /* current position in "line" */
3003 int row; /* row in the window, excl w_winrow */
3004 int screen_row; /* row on the screen, incl w_winrow */
3005
3006 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3007 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003008 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003009 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003011 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 int extra_attr = 0; /* attributes when n_extra != 0 */
3013 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3014 displaying lcs_eol at end-of-line */
3015 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3016 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3017
3018 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3019 int saved_n_extra = 0;
3020 char_u *saved_p_extra = NULL;
3021 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003022 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 int saved_char_attr = 0;
3024
3025 int n_attr = 0; /* chars with special attr */
3026 int saved_attr2 = 0; /* char_attr saved for n_attr */
3027 int n_attr3 = 0; /* chars with overruling special attr */
3028 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3029
3030 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3031
3032 int fromcol, tocol; /* start/end of inverting */
3033 int fromcol_prev = -2; /* start of inverting after cursor */
3034 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003036 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 pos_T pos;
3038 long v;
3039
3040 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003041 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3043 in this line */
3044 int attr = 0; /* attributes for area highlighting */
3045 int area_attr = 0; /* attributes desired by highlighting */
3046 int search_attr = 0; /* attributes desired by 'hlsearch' */
3047#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003048 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 int syntax_attr = 0; /* attributes desired by syntax */
3050 int has_syntax = FALSE; /* this buffer has syntax highl. */
3051 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003052 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003053 int draw_color_col = FALSE; /* highlight colorcolumn */
3054 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003055#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003056#ifdef FEAT_TEXT_PROP
3057 int text_prop_count;
3058 int text_prop_next = 0; // next text property to use
3059 textprop_T *text_props = NULL;
3060 int *text_prop_idxs = NULL;
3061 int text_props_active = 0;
3062 proptype_T *text_prop_type = NULL;
3063 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003064 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003065#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003066#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003067 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003068# define SPWORDLEN 150
3069 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003070 int nextlinecol = 0; /* column where nextline[] starts */
3071 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003072 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003073 int spell_attr = 0; /* attributes desired by spelling */
3074 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003075 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3076 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003077 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003078 static int cap_col = -1; /* column to check for Cap word */
3079 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003080 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003082 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 int multi_attr = 0; /* attributes desired by multibyte */
3084 int mb_l = 1; /* multi-byte byte length */
3085 int mb_c = 0; /* decoded multi-byte character */
3086 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003087 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088#ifdef FEAT_DIFF
3089 int filler_lines; /* nr of filler lines to be drawn */
3090 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003091 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 int change_start = MAXCOL; /* first col of changed area */
3093 int change_end = -1; /* last col of changed area */
3094#endif
3095 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3096#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003097 int need_showbreak = FALSE; /* overlong line, skipping first x
3098 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003100#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003101 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003103 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104#endif
3105#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003106 matchitem_T *cur; /* points to the match list */
3107 match_T *shl; /* points to search_hl or a match */
3108 int shl_flag; /* flag to indicate whether search_hl
3109 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003110 int pos_inprogress; /* marks that position match search is
3111 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003112 int prevcol_hl_flag; /* flag to indicate whether prevcol
3113 equals startcol of search_hl or one
3114 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115#endif
3116#ifdef FEAT_ARABIC
3117 int prev_c = 0; /* previous Arabic character */
3118 int prev_c1 = 0; /* first composing char for prev_c */
3119#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003120#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003121 int did_line_attr = 0;
3122#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003123#ifdef FEAT_TERMINAL
3124 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003125 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
3128 /* draw_state: items that are drawn in sequence: */
3129#define WL_START 0 /* nothing done yet */
3130#ifdef FEAT_CMDWIN
3131# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3132#else
3133# define WL_CMDLINE WL_START
3134#endif
3135#ifdef FEAT_FOLDING
3136# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3137#else
3138# define WL_FOLD WL_CMDLINE
3139#endif
3140#ifdef FEAT_SIGNS
3141# define WL_SIGN WL_FOLD + 1 /* column for signs */
3142#else
3143# define WL_SIGN WL_FOLD /* column for signs */
3144#endif
3145#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003146#ifdef FEAT_LINEBREAK
3147# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003149# define WL_BRI WL_NR
3150#endif
3151#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3152# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3153#else
3154# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#endif
3156#define WL_LINE WL_SBR + 1 /* text in the line */
3157 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003158#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 int feedback_col = 0;
3160 int feedback_old_attr = -1;
3161#endif
3162
Bram Moolenaar860cae12010-06-05 23:22:07 +02003163#ifdef FEAT_CONCEAL
3164 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003165 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003166 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003167 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003168 int is_concealing = FALSE;
3169 int boguscols = 0; /* nonexistent columns added to force
3170 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003171 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003172 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003173 int match_conc = 0; /* cchar for match functions */
3174 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003175 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003176# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003177# define FIX_FOR_BOGUSCOLS \
3178 { \
3179 n_extra += vcol_off; \
3180 vcol -= vcol_off; \
3181 vcol_off = 0; \
3182 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003183 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003184 boguscols = 0; \
3185 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003186#else
3187# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
3190 if (startrow > endrow) /* past the end already! */
3191 return startrow;
3192
3193 row = startrow;
3194 screen_row = row + W_WINROW(wp);
3195
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003196 if (!number_only)
3197 {
3198 /*
3199 * To speed up the loop below, set extra_check when there is linebreak,
3200 * trailing white space and/or syntax processing to be done.
3201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003203 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204#endif
3205#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003206 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003207# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003208 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003209# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003210 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003212 /* Prepare for syntax highlighting in this line. When there is an
3213 * error, stop syntax highlighting. */
3214 save_did_emsg = did_emsg;
3215 did_emsg = FALSE;
3216 syntax_start(wp, lnum);
3217 if (did_emsg)
3218 wp->w_s->b_syn_error = TRUE;
3219 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003220 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003221 did_emsg = save_did_emsg;
3222#ifdef SYN_TIME_LIMIT
3223 if (!wp->w_s->b_syn_slow)
3224#endif
3225 {
3226 has_syntax = TRUE;
3227 extra_check = TRUE;
3228 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003231
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003232 /* Check for columns to display for 'colorcolumn'. */
3233 color_cols = wp->w_p_cc_cols;
3234 if (color_cols != NULL)
3235 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003236#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003237
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003238#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003239 if (term_show_buffer(wp->w_buffer))
3240 {
3241 extra_check = TRUE;
3242 get_term_attr = TRUE;
3243 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3244 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003245#endif
3246
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003247#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003248 if (wp->w_p_spell
3249 && *wp->w_s->b_p_spl != NUL
3250 && wp->w_s->b_langp.ga_len > 0
3251 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003252 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003253 /* Prepare for spell checking. */
3254 has_spell = TRUE;
3255 extra_check = TRUE;
3256
Bram Moolenaar7701f302018-10-02 21:20:32 +02003257 /* Get the start of the next line, so that words that wrap to the
3258 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003259 * Trick: skip a few chars for C/shell/Vim comments */
3260 nextline[SPWORDLEN] = NUL;
3261 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3262 {
3263 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3264 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3265 }
3266
Bram Moolenaar7701f302018-10-02 21:20:32 +02003267 /* When a word wrapped from the previous line the start of the
3268 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003269 if (lnum == checked_lnum)
3270 cur_checked_col = checked_col;
3271 checked_lnum = 0;
3272
3273 /* When there was a sentence end in the previous line may require a
3274 * word starting with capital in this line. In line 1 always check
3275 * the first word. */
3276 if (lnum != capcol_lnum)
3277 cap_col = -1;
3278 if (lnum == 1)
3279 cap_col = 0;
3280 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282#endif
3283
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003284 /*
3285 * handle visual active in this window
3286 */
3287 fromcol = -10;
3288 tocol = MAXCOL;
3289 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003291 /* Visual is after curwin->w_cursor */
3292 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003294 top = &curwin->w_cursor;
3295 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003297 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003299 top = &VIsual;
3300 bot = &curwin->w_cursor;
3301 }
3302 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3303 if (VIsual_mode == Ctrl_V) /* block mode */
3304 {
3305 if (lnum_in_visual_area)
3306 {
3307 fromcol = wp->w_old_cursor_fcol;
3308 tocol = wp->w_old_cursor_lcol;
3309 }
3310 }
3311 else /* non-block mode */
3312 {
3313 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003315 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003317 if (VIsual_mode == 'V') /* linewise */
3318 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 else
3320 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003321 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3322 if (gchar_pos(top) == NUL)
3323 tocol = fromcol + 1;
3324 }
3325 }
3326 if (VIsual_mode != 'V' && lnum == bot->lnum)
3327 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003328 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003329 {
3330 fromcol = -10;
3331 tocol = MAXCOL;
3332 }
3333 else if (bot->col == MAXCOL)
3334 tocol = MAXCOL;
3335 else
3336 {
3337 pos = *bot;
3338 if (*p_sel == 'e')
3339 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3340 else
3341 {
3342 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3343 ++tocol;
3344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 }
3346 }
3347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003349 /* Check if the character under the cursor should not be inverted */
3350 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003351#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003352 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003353#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003354 )
3355 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003357 /* if inverting in this line set area_highlighting */
3358 if (fromcol >= 0)
3359 {
3360 area_highlighting = TRUE;
3361 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003363 if ((clip_star.available && !clip_star.owned
3364 && clip_isautosel_star())
3365 || (clip_plus.available && !clip_plus.owned
3366 && clip_isautosel_plus()))
3367 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003372 /*
3373 * handle 'incsearch' and ":s///c" highlighting
3374 */
3375 else if (highlight_match
3376 && wp == curwin
3377 && lnum >= curwin->w_cursor.lnum
3378 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003380 if (lnum == curwin->w_cursor.lnum)
3381 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003382 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003383 else
3384 fromcol = 0;
3385 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3386 {
3387 pos.lnum = lnum;
3388 pos.col = search_match_endcol;
3389 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3390 }
3391 else
3392 tocol = MAXCOL;
3393 /* do at least one character; happens when past end of line */
3394 if (fromcol == tocol)
3395 tocol = fromcol + 1;
3396 area_highlighting = TRUE;
3397 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 }
3400
3401#ifdef FEAT_DIFF
3402 filler_lines = diff_check(wp, lnum);
3403 if (filler_lines < 0)
3404 {
3405 if (filler_lines == -1)
3406 {
3407 if (diff_find_change(wp, lnum, &change_start, &change_end))
3408 diff_hlf = HLF_ADD; /* added line */
3409 else if (change_start == 0)
3410 diff_hlf = HLF_TXD; /* changed text */
3411 else
3412 diff_hlf = HLF_CHD; /* changed line */
3413 }
3414 else
3415 diff_hlf = HLF_ADD; /* added line */
3416 filler_lines = 0;
3417 area_highlighting = TRUE;
3418 }
3419 if (lnum == wp->w_topline)
3420 filler_lines = wp->w_topfill;
3421 filler_todo = filler_lines;
3422#endif
3423
3424#ifdef LINE_ATTR
3425# ifdef FEAT_SIGNS
3426 /* If this line has a sign with line highlighting set line_attr. */
3427 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3428 if (v != 0)
3429 line_attr = sign_get_attr((int)v, TRUE);
3430# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003431# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003433 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003434 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435# endif
3436 if (line_attr != 0)
3437 area_highlighting = TRUE;
3438#endif
3439
3440 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3441 ptr = line;
3442
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003443#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003444 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003445 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003446 /* For checking first word with a capital skip white space. */
3447 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003448 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003449
Bram Moolenaar30abd282005-06-22 22:35:10 +00003450 /* To be able to spell-check over line boundaries copy the end of the
3451 * current line into nextline[]. Above the start of the next line was
3452 * copied to nextline[SPWORDLEN]. */
3453 if (nextline[SPWORDLEN] == NUL)
3454 {
3455 /* No next line or it is empty. */
3456 nextlinecol = MAXCOL;
3457 nextline_idx = 0;
3458 }
3459 else
3460 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003461 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003462 if (v < SPWORDLEN)
3463 {
3464 /* Short line, use it completely and append the start of the
3465 * next line. */
3466 nextlinecol = 0;
3467 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003468 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003469 nextline_idx = v + 1;
3470 }
3471 else
3472 {
3473 /* Long line, use only the last SPWORDLEN bytes. */
3474 nextlinecol = v - SPWORDLEN;
3475 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3476 nextline_idx = SPWORDLEN + 1;
3477 }
3478 }
3479 }
3480#endif
3481
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003482 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003484 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003485 extra_check = TRUE;
3486 /* find start of trailing whitespace */
3487 if (lcs_trail)
3488 {
3489 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003490 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003491 --trailcol;
3492 trailcol += (colnr_T) (ptr - line);
3493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
3495
3496 /*
3497 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3498 * first character to be displayed.
3499 */
3500 if (wp->w_p_wrap)
3501 v = wp->w_skipcol;
3502 else
3503 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003504 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003507
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 while (vcol < v && *ptr != NUL)
3509 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003510 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003513 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 }
3515
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003516 /* When:
3517 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003518 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003519 * - 'virtualedit' is set, or
3520 * - the visual mode is active,
3521 * the end of the line may be before the start of the displayed part.
3522 */
3523 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003524#ifdef FEAT_SYN_HL
3525 wp->w_p_cuc || draw_color_col ||
3526#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003527 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003528 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
3531 /* Handle a character that's not completely on the screen: Put ptr at
3532 * that character but skip the first few screen characters. */
3533 if (vcol > v)
3534 {
3535 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003537 /* If the character fits on the screen, don't need to skip it.
3538 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003539 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003540 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 }
3542
3543 /*
3544 * Adjust for when the inverted text is before the screen,
3545 * and when the start of the inverted text is before the screen.
3546 */
3547 if (tocol <= vcol)
3548 fromcol = 0;
3549 else if (fromcol >= 0 && fromcol < vcol)
3550 fromcol = vcol;
3551
3552#ifdef FEAT_LINEBREAK
3553 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3554 if (wp->w_p_wrap)
3555 need_showbreak = TRUE;
3556#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003557#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003558 /* When spell checking a word we need to figure out the start of the
3559 * word and if it's badly spelled or not. */
3560 if (has_spell)
3561 {
3562 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003563 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003564 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003565
3566 pos = wp->w_cursor;
3567 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003568 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003569 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003570
3571 /* spell_move_to() may call ml_get() and make "line" invalid */
3572 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3573 ptr = line + linecol;
3574
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003575 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003576 {
3577 /* no bad word found at line start, don't check until end of a
3578 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003579 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003580 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003581 }
3582 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003583 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003584 /* bad word found, use attributes until end of word */
3585 word_end = wp->w_cursor.col + len + 1;
3586
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003587 /* Turn index into actual attributes. */
3588 if (spell_hlf != HLF_COUNT)
3589 spell_attr = highlight_attr[spell_hlf];
3590 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003591 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003592
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003593# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003594 /* Need to restart syntax highlighting for this line. */
3595 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003596 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003597# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003598 }
3599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 }
3601
3602 /*
3603 * Correct highlighting for cursor that can't be disabled.
3604 * Avoids having to check this for each character.
3605 */
3606 if (fromcol >= 0)
3607 {
3608 if (noinvcur)
3609 {
3610 if ((colnr_T)fromcol == wp->w_virtcol)
3611 {
3612 /* highlighting starts at cursor, let it start just after the
3613 * cursor */
3614 fromcol_prev = fromcol;
3615 fromcol = -1;
3616 }
3617 else if ((colnr_T)fromcol < wp->w_virtcol)
3618 /* restart highlighting after the cursor */
3619 fromcol_prev = wp->w_virtcol;
3620 }
3621 if (fromcol >= tocol)
3622 fromcol = -1;
3623 }
3624
3625#ifdef FEAT_SEARCH_EXTRA
3626 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003627 * Handle highlighting the last used search pattern and matches.
3628 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003630 cur = wp->w_match_head;
3631 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003632 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003634 if (shl_flag == FALSE)
3635 {
3636 shl = &search_hl;
3637 shl_flag = TRUE;
3638 }
3639 else
3640 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003641 shl->startcol = MAXCOL;
3642 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003644 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003645 v = (long)(ptr - line);
3646 if (cur != NULL)
3647 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003648 next_search_hl(wp, shl, lnum, (colnr_T)v,
3649 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003650
3651 /* Need to get the line again, a multi-line regexp may have made it
3652 * invalid. */
3653 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3654 ptr = line + v;
3655
3656 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003658 if (shl->lnum == lnum)
3659 shl->startcol = shl->rm.startpos[0].col;
3660 else
3661 shl->startcol = 0;
3662 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3663 - shl->rm.startpos[0].lnum)
3664 shl->endcol = shl->rm.endpos[0].col;
3665 else
3666 shl->endcol = MAXCOL;
3667 /* Highlight one character for an empty match. */
3668 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003670 if (has_mbyte && line[shl->endcol] != NUL)
3671 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3672 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003673 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003675 if ((long)shl->startcol < v) /* match at leftcol */
3676 {
3677 shl->attr_cur = shl->attr;
3678 search_attr = shl->attr;
3679 }
3680 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003682 if (shl != &search_hl && cur != NULL)
3683 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
3685#endif
3686
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003687#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003688 // Cursor line highlighting for 'cursorline' in the current window.
3689 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003690 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003691 // Do not show the cursor line when Visual mode is active, because it's
3692 // not clear what is selected then. Do update w_last_cursorline.
3693 if (!(wp == curwin && VIsual_active))
3694 {
3695 line_attr = HL_ATTR(HLF_CUL);
3696 area_highlighting = TRUE;
3697 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003698 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003699 }
3700#endif
3701
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003702#ifdef FEAT_TEXT_PROP
3703 {
3704 char_u *prop_start;
3705
3706 text_prop_count = get_text_props(wp->w_buffer, lnum,
3707 &prop_start, FALSE);
3708 if (text_prop_count > 0)
3709 {
3710 // Make a copy of the properties, so that they are properly
3711 // aligned.
3712 text_props = (textprop_T *)alloc(
3713 text_prop_count * sizeof(textprop_T));
3714 if (text_props != NULL)
3715 mch_memmove(text_props, prop_start,
3716 text_prop_count * sizeof(textprop_T));
3717
3718 // Allocate an array for the indexes.
3719 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3720 area_highlighting = TRUE;
3721 extra_check = TRUE;
3722 }
3723 }
3724#endif
3725
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003726 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 col = 0;
3728#ifdef FEAT_RIGHTLEFT
3729 if (wp->w_p_rl)
3730 {
3731 /* Rightleft window: process the text in the normal direction, but put
3732 * it in current_ScreenLine[] from right to left. Start at the
3733 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003734 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 off += col;
3736 }
3737#endif
3738
3739 /*
3740 * Repeat for the whole displayed line.
3741 */
3742 for (;;)
3743 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003744#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003745 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 /* Skip this quickly when working on the text. */
3748 if (draw_state != WL_LINE)
3749 {
3750#ifdef FEAT_CMDWIN
3751 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3752 {
3753 draw_state = WL_CMDLINE;
3754 if (cmdwin_type != 0 && wp == curwin)
3755 {
3756 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003758 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003759 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003760 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
3762 }
3763#endif
3764
3765#ifdef FEAT_FOLDING
3766 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3767 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003768 int fdc = compute_foldcolumn(wp, 0);
3769
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003771 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003773 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003774 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003775 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003776 p_extra_free = alloc(12 + 1);
3777
3778 if (p_extra_free != NULL)
3779 {
3780 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3781 n_extra = fdc;
3782 p_extra_free[n_extra] = NUL;
3783 p_extra = p_extra_free;
3784 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003785 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003786 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 }
3789 }
3790#endif
3791
3792#ifdef FEAT_SIGNS
3793 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3794 {
3795 draw_state = WL_SIGN;
3796 /* Show the sign column when there are any signs in this
3797 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003798 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003800 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003802 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803# endif
3804
3805 /* Draw two cells with the sign value or blank. */
3806 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003807 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003808 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 n_extra = 2;
3810
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003811 if (row == startrow
3812#ifdef FEAT_DIFF
3813 + filler_lines && filler_todo <= 0
3814#endif
3815 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 {
3817 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3818 SIGN_TEXT);
3819# ifdef FEAT_SIGN_ICONS
3820 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3821 SIGN_ICON);
3822 if (gui.in_use && icon_sign != 0)
3823 {
3824 /* Use the image in this position. */
3825 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003826 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827# ifdef FEAT_NETBEANS_INTG
3828 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003829 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003831 c_final = NUL;
3832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833# endif
3834 char_attr = icon_sign;
3835 }
3836 else
3837# endif
3838 if (text_sign != 0)
3839 {
3840 p_extra = sign_get_text(text_sign);
3841 if (p_extra != NULL)
3842 {
3843 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003844 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003845 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 }
3847 char_attr = sign_get_attr(text_sign, FALSE);
3848 }
3849 }
3850 }
3851 }
3852#endif
3853
3854 if (draw_state == WL_NR - 1 && n_extra == 0)
3855 {
3856 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003857 /* Display the absolute or relative line number. After the
3858 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3859 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 && (row == startrow
3861#ifdef FEAT_DIFF
3862 + filler_lines
3863#endif
3864 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3865 {
3866 /* Draw the line number (empty space after wrapping). */
3867 if (row == startrow
3868#ifdef FEAT_DIFF
3869 + filler_lines
3870#endif
3871 )
3872 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003873 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003874 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003875
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003876 if (wp->w_p_nu && !wp->w_p_rnu)
3877 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003878 num = (long)lnum;
3879 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003880 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003881 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003882 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003883 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003884 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003885 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003886 num = lnum;
3887 fmt = "%-*ld ";
3888 }
3889 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003890
Bram Moolenaar700e7342013-01-30 12:31:36 +01003891 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003892 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 if (wp->w_skipcol > 0)
3894 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3895 *p_extra = '-';
3896#ifdef FEAT_RIGHTLEFT
3897 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01003898 {
3899 char_u *p1, *p2;
3900 int t;
3901
3902 // like rl_mirror(), but keep the space at the end
3903 p2 = skiptowhite(extra) - 1;
3904 for (p1 = extra; p1 < p2; ++p1, --p2)
3905 {
3906 t = *p1;
3907 *p1 = *p2;
3908 *p2 = t;
3909 }
3910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911#endif
3912 p_extra = extra;
3913 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003914 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01003917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003919 c_final = NUL;
3920 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003921 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003922 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003923#ifdef FEAT_SYN_HL
3924 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003925 * the current line differently.
3926 * TODO: Can we use CursorLine instead of CursorLineNr
3927 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003928 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003929 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003930 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
3933 }
3934
Bram Moolenaar597a4222014-06-25 14:39:50 +02003935#ifdef FEAT_LINEBREAK
3936 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3937 && n_extra == 0 && *p_sbr != NUL)
3938 /* draw indent after showbreak value */
3939 draw_state = WL_BRI;
3940 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3941 /* After the showbreak, draw the breakindent */
3942 draw_state = WL_BRI - 1;
3943
3944 /* draw 'breakindent': indent wrapped text accordingly */
3945 if (draw_state == WL_BRI - 1 && n_extra == 0)
3946 {
3947 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003948 /* if need_showbreak is set, breakindent also applies */
3949 if (wp->w_p_bri && n_extra == 0
3950 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003951# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003952 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003953# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003954 )
3955 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003956 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003957# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003958 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003959 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003960 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003961# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003962 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3963 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003964 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003965# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003966 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003967# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003968 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003969 c_extra = ' ';
3970 n_extra = get_breakindent_win(wp,
3971 ml_get_buf(wp->w_buffer, lnum, FALSE));
3972 /* Correct end of highlighted area for 'breakindent',
3973 * required when 'linebreak' is also set. */
3974 if (tocol == vcol)
3975 tocol += n_extra;
3976 }
3977 }
3978#endif
3979
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3981 if (draw_state == WL_SBR - 1 && n_extra == 0)
3982 {
3983 draw_state = WL_SBR;
3984# ifdef FEAT_DIFF
3985 if (filler_todo > 0)
3986 {
3987 /* Draw "deleted" diff line(s). */
3988 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003989 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003991 c_final = NUL;
3992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01003994 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003996 c_final = NUL;
3997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998# ifdef FEAT_RIGHTLEFT
3999 if (wp->w_p_rl)
4000 n_extra = col + 1;
4001 else
4002# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004003 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004004 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 }
4006# endif
4007# ifdef FEAT_LINEBREAK
4008 if (*p_sbr != NUL && need_showbreak)
4009 {
4010 /* Draw 'showbreak' at the start of each broken line. */
4011 p_extra = p_sbr;
4012 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004013 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004015 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004017 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 /* Correct end of highlighted area for 'showbreak',
4019 * required when 'linebreak' is also set. */
4020 if (tocol == vcol)
4021 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004022#ifdef FEAT_SYN_HL
4023 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004024 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004025 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004026 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
4029# endif
4030 }
4031#endif
4032
4033 if (draw_state == WL_LINE - 1 && n_extra == 0)
4034 {
4035 draw_state = WL_LINE;
4036 if (saved_n_extra)
4037 {
4038 /* Continue item from end of wrapped line. */
4039 n_extra = saved_n_extra;
4040 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004041 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 p_extra = saved_p_extra;
4043 char_attr = saved_char_attr;
4044 }
4045 else
4046 char_attr = 0;
4047 }
4048 }
4049
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004050 // When still displaying '$' of change command, stop at cursor.
4051 // When only displaying the (relative) line number and that's done,
4052 // stop here.
4053 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004054 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055#ifdef FEAT_DIFF
4056 && filler_todo <= 0
4057#endif
4058 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004059 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004061 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004062 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004063 /* Pretend we have finished updating the window. Except when
4064 * 'cursorcolumn' is set. */
4065#ifdef FEAT_SYN_HL
4066 if (wp->w_p_cuc)
4067 row = wp->w_cline_row + wp->w_cline_height;
4068 else
4069#endif
4070 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 break;
4072 }
4073
Bram Moolenaar637532b2019-01-03 21:44:40 +01004074 if (draw_state == WL_LINE && (area_highlighting
4075#ifdef FEAT_SPELL
4076 || has_spell
4077#endif
4078 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 {
4080 /* handle Visual or match highlighting in this line */
4081 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4083 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004085 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 && vcol < tocol))
4087 area_attr = attr; /* start highlighting */
4088 else if (area_attr != 0
4089 && (vcol == tocol
4090 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092
4093#ifdef FEAT_SEARCH_EXTRA
4094 if (!n_extra)
4095 {
4096 /*
4097 * Check for start/end of search pattern match.
4098 * After end, check for start/end of next match.
4099 * When another match, have to check for start again.
4100 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004101 * Do this for 'search_hl' and the match list (ordered by
4102 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004104 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004105 cur = wp->w_match_head;
4106 shl_flag = FALSE;
4107 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004109 if (shl_flag == FALSE
4110 && ((cur != NULL
4111 && cur->priority > SEARCH_HL_PRIORITY)
4112 || cur == NULL))
4113 {
4114 shl = &search_hl;
4115 shl_flag = TRUE;
4116 }
4117 else
4118 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004119 if (cur != NULL)
4120 cur->pos.cur = 0;
4121 pos_inprogress = TRUE;
4122 while (shl->rm.regprog != NULL
4123 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004125 if (shl->startcol != MAXCOL
4126 && v >= (long)shl->startcol
4127 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004129 int tmp_col = v + MB_PTR2LEN(ptr);
4130
4131 if (shl->endcol < tmp_col)
4132 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004134#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004135 // Match with the "Conceal" group results in hiding
4136 // the match.
4137 if (cur != NULL
4138 && shl != &search_hl
4139 && syn_name2id((char_u *)"Conceal")
4140 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004141 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004142 has_match_conc =
4143 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004144 match_conc = cur->conceal_char;
4145 }
4146 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004147 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004150 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 {
4152 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004153 next_search_hl(wp, shl, lnum, (colnr_T)v,
4154 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004155 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004156 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157
4158 /* Need to get the line again, a multi-line regexp
4159 * may have made it invalid. */
4160 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4161 ptr = line + v;
4162
4163 if (shl->lnum == lnum)
4164 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004165 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004167 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004169 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004171 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 {
4173 /* highlight empty match, try again after
4174 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004176 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004177 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004179 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181
4182 /* Loop to check if the match starts at the
4183 * current position */
4184 continue;
4185 }
4186 }
4187 break;
4188 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004189 if (shl != &search_hl && cur != NULL)
4190 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004192
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004193 /* Use attributes from match with highest priority among
4194 * 'search_hl' and the match list. */
4195 search_attr = search_hl.attr_cur;
4196 cur = wp->w_match_head;
4197 shl_flag = FALSE;
4198 while (cur != NULL || shl_flag == FALSE)
4199 {
4200 if (shl_flag == FALSE
4201 && ((cur != NULL
4202 && cur->priority > SEARCH_HL_PRIORITY)
4203 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004204 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004205 shl = &search_hl;
4206 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004207 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004208 else
4209 shl = &cur->hl;
4210 if (shl->attr_cur != 0)
4211 search_attr = shl->attr_cur;
4212 if (shl != &search_hl && cur != NULL)
4213 cur = cur->next;
4214 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004215 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004216 if (*ptr == NUL && (did_line_attr >= 1
4217 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004218 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
4220#endif
4221
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004223 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004225 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4226 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004228 if (diff_hlf == HLF_TXD && ptr - line > change_end
4229 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004231 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004232 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004233 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
4235#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004236
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004237#ifdef FEAT_TEXT_PROP
4238 if (text_props != NULL)
4239 {
4240 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004241 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004242
4243 // Check if any active property ends.
4244 for (pi = 0; pi < text_props_active; ++pi)
4245 {
4246 int tpi = text_prop_idxs[pi];
4247
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004248 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004249 + text_props[tpi].tp_len)
4250 {
4251 if (pi + 1 < text_props_active)
4252 mch_memmove(text_prop_idxs + pi,
4253 text_prop_idxs + pi + 1,
4254 sizeof(int)
4255 * (text_props_active - (pi + 1)));
4256 --text_props_active;
4257 --pi;
4258 }
4259 }
4260
4261 // Add any text property that starts in this column.
4262 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004263 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004264 text_prop_idxs[text_props_active++] = text_prop_next++;
4265
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004266 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004267 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004268 if (text_props_active > 0)
4269 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004270 // Sort the properties on priority and/or starting last.
4271 // Then combine the attributes, highest priority last.
4272 current_text_props = text_props;
4273 current_buf = wp->w_buffer;
4274 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4275 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004276
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004277 for (pi = 0; pi < text_props_active; ++pi)
4278 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004279 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004280 proptype_T *pt = text_prop_type_by_id(
4281 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004282
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004283 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004284 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004285 int pt_attr = syn_id2attr(pt->pt_hl_id);
4286
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004287 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004288 text_prop_attr =
4289 hl_combine_attr(text_prop_attr, pt_attr);
4290 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004291 }
4292 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004293 }
4294 }
4295#endif
4296
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004297 /* Decide which of the highlight attributes to use. */
4298 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004299#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004300 if (area_attr != 0)
4301 char_attr = hl_combine_attr(line_attr, area_attr);
4302 else if (search_attr != 0)
4303 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004304 /* Use line_attr when not in the Visual or 'incsearch' area
4305 * (area_attr may be 0 when "noinvcur" is set). */
4306 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004307 || vcol < fromcol || vcol_prev < fromcol_prev
4308 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004309 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004310#else
4311 if (area_attr != 0)
4312 char_attr = area_attr;
4313 else if (search_attr != 0)
4314 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004315#endif
4316 else
4317 {
4318 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004319#ifdef FEAT_TEXT_PROP
4320 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004321 {
4322 if (text_prop_combine)
4323 char_attr = hl_combine_attr(
4324 syntax_attr, text_prop_attr);
4325 else
4326 char_attr = text_prop_attr;
4327 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004328 else
4329#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004330#ifdef FEAT_SYN_HL
4331 if (has_syntax)
4332 char_attr = syntax_attr;
4333 else
4334#endif
4335 char_attr = 0;
4336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 }
4338
4339 /*
4340 * Get the next character to put on the screen.
4341 */
4342 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004343 * The "p_extra" points to the extra stuff that is inserted to
4344 * represent special characters (non-printable stuff) and other
4345 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004346 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004347 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4348 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4350 */
4351 if (n_extra > 0)
4352 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004353 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004355 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004357 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 {
4359 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004360 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004361 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363 else
4364 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 else
4367 {
4368 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 if (has_mbyte)
4370 {
4371 mb_c = c;
4372 if (enc_utf8)
4373 {
4374 /* If the UTF-8 character is more than one byte:
4375 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004376 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 mb_utf8 = FALSE;
4378 if (mb_l > n_extra)
4379 mb_l = 1;
4380 else if (mb_l > 1)
4381 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004382 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004384 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 }
4386 }
4387 else
4388 {
4389 /* if this is a DBCS character, put it in "mb_c" */
4390 mb_l = MB_BYTE2LEN(c);
4391 if (mb_l >= n_extra)
4392 mb_l = 1;
4393 else if (mb_l > 1)
4394 mb_c = (c << 8) + p_extra[1];
4395 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004396 if (mb_l == 0) /* at the NUL at end-of-line */
4397 mb_l = 1;
4398
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 /* If a double-width char doesn't fit display a '>' in the
4400 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004401 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402# ifdef FEAT_RIGHTLEFT
4403 wp->w_p_rl ? (col <= 0) :
4404# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004405 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 && (*mb_char2cells)(mb_c) == 2)
4407 {
4408 c = '>';
4409 mb_c = c;
4410 mb_l = 1;
4411 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004412 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 /* put the pointer back to output the double-width
4414 * character at the start of the next line. */
4415 ++n_extra;
4416 --p_extra;
4417 }
4418 else
4419 {
4420 n_extra -= mb_l - 1;
4421 p_extra += mb_l - 1;
4422 }
4423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 ++p_extra;
4425 }
4426 --n_extra;
4427 }
4428 else
4429 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004430#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004431 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004432#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004433
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004434 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004435 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 /*
4437 * Get a character from the line itself.
4438 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004439 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004440#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004441 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 if (has_mbyte)
4444 {
4445 mb_c = c;
4446 if (enc_utf8)
4447 {
4448 /* If the UTF-8 character is more than one byte: Decode it
4449 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004450 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 mb_utf8 = FALSE;
4452 if (mb_l > 1)
4453 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004454 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 /* Overlong encoded ASCII or ASCII with composing char
4456 * is displayed normally, except a NUL. */
4457 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004458 {
4459 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004460#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004461 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004462#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004465
4466 /* At start of the line we can have a composing char.
4467 * Draw it as a space with a composing char. */
4468 if (utf_iscomposing(mb_c))
4469 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004470 int i;
4471
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004472 for (i = Screen_mco - 1; i > 0; --i)
4473 u8cc[i] = u8cc[i - 1];
4474 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004475 mb_c = ' ';
4476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478
4479 if ((mb_l == 1 && c >= 0x80)
4480 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004481 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 {
4483 /*
4484 * Illegal UTF-8 byte: display as <xx>.
4485 * Non-BMP character : display as ? or fullwidth ?.
4486 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004487 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004488# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004489 if (wp->w_p_rl) /* reverse */
4490 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004491# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 p_extra = extra;
4493 c = *p_extra;
4494 mb_c = mb_ptr2char_adv(&p_extra);
4495 mb_utf8 = (c >= 0x80);
4496 n_extra = (int)STRLEN(p_extra);
4497 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004498 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 if (area_attr == 0 && search_attr == 0)
4500 {
4501 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004502 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 saved_attr2 = char_attr; /* save current attr */
4504 }
4505 }
4506 else if (mb_l == 0) /* at the NUL at end-of-line */
4507 mb_l = 1;
4508#ifdef FEAT_ARABIC
4509 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4510 {
4511 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004512 int pc, pc1, nc;
4513 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514
4515 /* The idea of what is the previous and next
4516 * character depends on 'rightleft'. */
4517 if (wp->w_p_rl)
4518 {
4519 pc = prev_c;
4520 pc1 = prev_c1;
4521 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004522 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524 else
4525 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004526 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004528 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 }
4530 prev_c = mb_c;
4531
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004532 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534 else
4535 prev_c = mb_c;
4536#endif
4537 }
4538 else /* enc_dbcs */
4539 {
4540 mb_l = MB_BYTE2LEN(c);
4541 if (mb_l == 0) /* at the NUL at end-of-line */
4542 mb_l = 1;
4543 else if (mb_l > 1)
4544 {
4545 /* We assume a second byte below 32 is illegal.
4546 * Hopefully this is OK for all double-byte encodings!
4547 */
4548 if (ptr[1] >= 32)
4549 mb_c = (c << 8) + ptr[1];
4550 else
4551 {
4552 if (ptr[1] == NUL)
4553 {
4554 /* head byte at end of line */
4555 mb_l = 1;
4556 transchar_nonprint(extra, c);
4557 }
4558 else
4559 {
4560 /* illegal tail byte */
4561 mb_l = 2;
4562 STRCPY(extra, "XX");
4563 }
4564 p_extra = extra;
4565 n_extra = (int)STRLEN(extra) - 1;
4566 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004567 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 c = *p_extra++;
4569 if (area_attr == 0 && search_attr == 0)
4570 {
4571 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004572 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 saved_attr2 = char_attr; /* save current attr */
4574 }
4575 mb_c = c;
4576 }
4577 }
4578 }
4579 /* If a double-width char doesn't fit display a '>' in the
4580 * last column; the character is displayed at the start of the
4581 * next line. */
4582 if ((
4583# ifdef FEAT_RIGHTLEFT
4584 wp->w_p_rl ? (col <= 0) :
4585# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004586 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 && (*mb_char2cells)(mb_c) == 2)
4588 {
4589 c = '>';
4590 mb_c = c;
4591 mb_utf8 = FALSE;
4592 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004593 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 /* Put pointer back so that the character will be
4595 * displayed at the start of the next line. */
4596 --ptr;
4597 }
4598 else if (*ptr != NUL)
4599 ptr += mb_l - 1;
4600
4601 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004602 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004603 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004604 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004607 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004608 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 c = ' ';
4610 if (area_attr == 0 && search_attr == 0)
4611 {
4612 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004613 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 saved_attr2 = char_attr; /* save current attr */
4615 }
4616 mb_c = c;
4617 mb_utf8 = FALSE;
4618 mb_l = 1;
4619 }
4620
4621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 ++ptr;
4623
4624 if (extra_check)
4625 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004626#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004627 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004628#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004629
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004630#ifdef FEAT_TERMINAL
4631 if (get_term_attr)
4632 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004633 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004634
4635 if (!attr_pri)
4636 char_attr = syntax_attr;
4637 else
4638 char_attr = hl_combine_attr(syntax_attr, char_attr);
4639 }
4640#endif
4641
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004642#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004643 // Get syntax attribute, unless still at the start of the line
4644 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004645 v = (long)(ptr - line);
4646 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 {
4648 /* Get the syntax attribute for the character. If there
4649 * is an error, disable syntax highlighting. */
4650 save_did_emsg = did_emsg;
4651 did_emsg = FALSE;
4652
Bram Moolenaar217ad922005-03-20 22:37:15 +00004653 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004654# ifdef FEAT_SPELL
4655 has_spell ? &can_spell :
4656# endif
4657 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
4659 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004660 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004661 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004662 has_syntax = FALSE;
4663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 else
4665 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004666#ifdef SYN_TIME_LIMIT
4667 if (wp->w_s->b_syn_slow)
4668 has_syntax = FALSE;
4669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670
4671 /* Need to get the line again, a multi-line regexp may
4672 * have made it invalid. */
4673 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4674 ptr = line + v;
4675
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004676# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004677 // Text properties overrule syntax highlighting or combine.
4678 if (text_prop_attr == 0 || text_prop_combine)
4679# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004680 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004681 int comb_attr = syntax_attr;
4682# ifdef FEAT_TEXT_PROP
4683 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4684# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004685 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004686 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004687 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004688 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004689 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004690# ifdef FEAT_CONCEAL
4691 /* no concealing past the end of the line, it interferes
4692 * with line highlighting */
4693 if (c == NUL)
4694 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004695 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004696 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004697# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004699#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004700
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004701#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004702 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004703 * Only do this when there is no syntax highlighting, the
4704 * @Spell cluster is not used or the current syntax item
4705 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004706 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004707 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004708 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004709 if (c != 0 && (
4710# ifdef FEAT_SYN_HL
4711 !has_syntax ||
4712# endif
4713 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004714 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004715 char_u *prev_ptr, *p;
4716 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004717 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004718 if (has_mbyte)
4719 {
4720 prev_ptr = ptr - mb_l;
4721 v -= mb_l - 1;
4722 }
4723 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004724 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004725
4726 /* Use nextline[] if possible, it has the start of the
4727 * next line concatenated. */
4728 if ((prev_ptr - line) - nextlinecol >= 0)
4729 p = nextline + (prev_ptr - line) - nextlinecol;
4730 else
4731 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004732 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004733 len = spell_check(wp, p, &spell_hlf, &cap_col,
4734 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004735 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004736
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004737 /* In Insert mode only highlight a word that
4738 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004739 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004740 && (State & INSERT) != 0
4741 && wp->w_cursor.lnum == lnum
4742 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004743 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004744 && wp->w_cursor.col < (colnr_T)word_end)
4745 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004746 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004747 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004748 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004749
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004750 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004751 && (p - nextline) + len > nextline_idx)
4752 {
4753 /* Remember that the good word continues at the
4754 * start of the next line. */
4755 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004756 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004757 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004758
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004759 /* Turn index into actual attributes. */
4760 if (spell_hlf != HLF_COUNT)
4761 spell_attr = highlight_attr[spell_hlf];
4762
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004763 if (cap_col > 0)
4764 {
4765 if (p != prev_ptr
4766 && (p - nextline) + cap_col >= nextline_idx)
4767 {
4768 /* Remember that the word in the next line
4769 * must start with a capital. */
4770 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004771 cap_col = (int)((p - nextline) + cap_col
4772 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004773 }
4774 else
4775 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004776 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004777 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004778 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004779 }
4780 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004781 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004782 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004783 char_attr = hl_combine_attr(char_attr, spell_attr);
4784 else
4785 char_attr = hl_combine_attr(spell_attr, char_attr);
4786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787#endif
4788#ifdef FEAT_LINEBREAK
4789 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004790 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004792 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004793 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004795 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004796 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004797
Bram Moolenaar597a4222014-06-25 14:39:50 +02004798 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004799 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004800 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004801 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004802# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004803 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004804 wp->w_buffer->b_p_vts_array) - 1;
4805# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004806 n_extra = (int)wp->w_buffer->b_p_ts
4807 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004808# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004809
Bram Moolenaar4df70292015-03-21 14:20:16 +01004810 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004811 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004812 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004813 {
4814#ifdef FEAT_CONCEAL
4815 if (c == TAB)
4816 /* See "Tab alignment" below. */
4817 FIX_FOR_BOGUSCOLS;
4818#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004819 if (!wp->w_p_list)
4820 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 }
4823#endif
4824
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004825 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4826 // But not when the character is followed by a composing
4827 // character (use mb_l to check that).
4828 if (wp->w_p_list
4829 && ((((c == 160 && mb_l == 1)
4830 || (mb_utf8
4831 && ((mb_c == 160 && mb_l == 2)
4832 || (mb_c == 0x202f && mb_l == 3))))
4833 && lcs_nbsp)
4834 || (c == ' '
4835 && mb_l == 1
4836 && lcs_space
4837 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004838 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004839 c = (c == ' ') ? lcs_space : lcs_nbsp;
4840 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004841 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004842 n_attr = 1;
4843 extra_attr = HL_ATTR(HLF_8);
4844 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004845 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004846 mb_c = c;
4847 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004848 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004849 mb_utf8 = TRUE;
4850 u8cc[0] = 0;
4851 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004852 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004853 else
4854 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004855 }
4856
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4858 {
4859 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004860 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 {
4862 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004863 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 saved_attr2 = char_attr; /* save current attr */
4865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004867 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 {
4869 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004870 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004871 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 }
4873 else
4874 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 }
4876 }
4877
4878 /*
4879 * Handling of non-printable characters.
4880 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004881 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 {
4883 /*
4884 * when getting a character from the file, we may have to
4885 * turn it into something else on the way to putting it
4886 * into "ScreenLines".
4887 */
4888 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4889 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004890 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004891 long vcol_adjusted = vcol; /* removed showbreak length */
4892#ifdef FEAT_LINEBREAK
4893 /* only adjust the tab_len, when at the first column
4894 * after the showbreak value was drawn */
4895 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4896 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004899#ifdef FEAT_VARTABS
4900 tab_len = tabstop_padding(vcol_adjusted,
4901 wp->w_buffer->b_p_ts,
4902 wp->w_buffer->b_p_vts_array) - 1;
4903#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004904 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004905 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4906#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004907
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004908#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004909 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004910#endif
4911 /* tab amount depends on current column */
4912 n_extra = tab_len;
4913#ifdef FEAT_LINEBREAK
4914 else
4915 {
4916 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01004917 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004918 int i;
4919 int saved_nextra = n_extra;
4920
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004921#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004922 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004923 /* there are characters to conceal */
4924 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004925 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4926 */
4927 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4928 && n_extra > tab_len)
4929 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004930#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004931
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004932 /* if n_extra > 0, it gives the number of chars, to
4933 * use for a tab, else we need to calculate the width
4934 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004935 len = (tab_len * mb_char2len(lcs_tab2));
4936 if (n_extra > 0)
4937 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004938 c = lcs_tab1;
4939 p = alloc((unsigned)(len + 1));
4940 vim_memset(p, ' ', len);
4941 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004942 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004943 p_extra_free = p;
4944 for (i = 0; i < tab_len; i++)
4945 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004946 if (*p == NUL)
4947 {
4948 tab_len = i;
4949 break;
4950 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004951 mb_char2bytes(lcs_tab2, p);
4952 p += mb_char2len(lcs_tab2);
4953 n_extra += mb_char2len(lcs_tab2)
4954 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004955 }
4956 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004957#ifdef FEAT_CONCEAL
4958 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4959 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004960 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004961 n_extra -= vcol_off;
4962#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004963 }
4964#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004965#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004966 {
4967 int vc_saved = vcol_off;
4968
4969 /* Tab alignment should be identical regardless of
4970 * 'conceallevel' value. So tab compensates of all
4971 * previous concealed characters, and thus resets
4972 * vcol_off and boguscols accumulated so far in the
4973 * line. Note that the tab can be longer than
4974 * 'tabstop' when there are concealed characters. */
4975 FIX_FOR_BOGUSCOLS;
4976
4977 /* Make sure, the highlighting for the tab char will be
4978 * correctly set further below (effectively reverts the
4979 * FIX_FOR_BOGSUCOLS macro */
4980 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004981 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004982 tab_len += vc_saved;
4983 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 if (wp->w_p_list)
4987 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004988 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004989#ifdef FEAT_LINEBREAK
4990 if (wp->w_p_lbr)
4991 c_extra = NUL; /* using p_extra from above */
4992 else
4993#endif
4994 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004995 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004996 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004997 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005000 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 {
5002 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005003 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005004 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
5007 else
5008 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005009 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 c_extra = ' ';
5011 c = ' ';
5012 }
5013 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005014 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005015 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005016 || ((fromcol >= 0 || fromcol_prev >= 0)
5017 && tocol > vcol
5018 && VIsual_mode != Ctrl_V
5019 && (
5020# ifdef FEAT_RIGHTLEFT
5021 wp->w_p_rl ? (col >= 0) :
5022# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005023 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005024 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005025 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005026 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005027 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005029 /* Display a '$' after the line or highlight an extra
5030 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5032 /* For a diff line the highlighting continues after the
5033 * "$". */
5034 if (
5035# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005036 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037# ifdef LINE_ATTR
5038 &&
5039# endif
5040# endif
5041# ifdef LINE_ATTR
5042 line_attr == 0
5043# endif
5044 )
5045#endif
5046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 /* In virtualedit, visual selections may extend
5048 * beyond end of line. */
5049 if (area_highlighting && virtual_active()
5050 && tocol != MAXCOL && vcol < tocol)
5051 n_extra = 0;
5052 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 {
5054 p_extra = at_end_str;
5055 n_extra = 1;
5056 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005057 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 }
5059 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005060 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005061 c = lcs_eol;
5062 else
5063 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 lcs_eol_one = -1;
5065 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005066 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005068 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 n_attr = 1;
5070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005072 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 {
5074 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005075 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005076 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 }
5078 else
5079 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 }
5081 else if (c != NUL)
5082 {
5083 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005084 if (n_extra == 0)
5085 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086#ifdef FEAT_RIGHTLEFT
5087 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5088 rl_mirror(p_extra); /* reverse "<12>" */
5089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005091 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005092#ifdef FEAT_LINEBREAK
5093 if (wp->w_p_lbr)
5094 {
5095 char_u *p;
5096
5097 c = *p_extra;
5098 p = alloc((unsigned)n_extra + 1);
5099 vim_memset(p, ' ', n_extra);
5100 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5101 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005102 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005103 p_extra_free = p_extra = p;
5104 }
5105 else
5106#endif
5107 {
5108 n_extra = byte2cells(c) - 1;
5109 c = *p_extra++;
5110 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005111 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
5113 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005114 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 saved_attr2 = char_attr; /* save current attr */
5116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 else if (VIsual_active
5120 && (VIsual_mode == Ctrl_V
5121 || VIsual_mode == 'v')
5122 && virtual_active()
5123 && tocol != MAXCOL
5124 && vcol < tocol
5125 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005126#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005128#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005129 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
5131 c = ' ';
5132 --ptr; /* put it back at the NUL */
5133 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005134#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 else if ((
5136# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005137 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005139# ifdef FEAT_TERMINAL
5140 term_attr != 0 ||
5141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 ) && (
5144# ifdef FEAT_RIGHTLEFT
5145 wp->w_p_rl ? (col >= 0) :
5146# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005147 (col
5148# ifdef FEAT_CONCEAL
5149 - boguscols
5150# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005151 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 {
5153 /* Highlight until the right side of the window */
5154 c = ' ';
5155 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005156
5157 /* Remember we do the char for line highlighting. */
5158 ++did_line_attr;
5159
5160 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005161 if (line_attr != 0 && char_attr == search_attr
5162 && (did_line_attr > 1
5163 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005164 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165# ifdef FEAT_DIFF
5166 if (diff_hlf == HLF_TXD)
5167 {
5168 diff_hlf = HLF_CHD;
5169 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005170 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005171 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005172 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5173 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005174 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
5177# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005178# ifdef FEAT_TERMINAL
5179 if (term_attr != 0)
5180 {
5181 char_attr = term_attr;
5182 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5183 char_attr = hl_combine_attr(char_attr,
5184 HL_ATTR(HLF_CUL));
5185 }
5186# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 }
5188#endif
5189 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005190
5191#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005192 if ( wp->w_p_cole > 0
5193 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005194 conceal_cursor_line(wp))
5195 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005196 && !(lnum_in_visual_area
5197 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005198 {
5199 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005200 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005201 && (syn_get_sub_char() != NUL || match_conc
5202 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005203 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005204 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005205 /* First time at this concealed item: display one
5206 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005207 if (match_conc)
5208 c = match_conc;
5209 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005210 c = syn_get_sub_char();
5211 else if (lcs_conceal != NUL)
5212 c = lcs_conceal;
5213 else
5214 c = ' ';
5215
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005216 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005217
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005218 if (n_extra > 0)
5219 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005220 vcol += n_extra;
5221 if (wp->w_p_wrap && n_extra > 0)
5222 {
5223# ifdef FEAT_RIGHTLEFT
5224 if (wp->w_p_rl)
5225 {
5226 col -= n_extra;
5227 boguscols -= n_extra;
5228 }
5229 else
5230# endif
5231 {
5232 boguscols += n_extra;
5233 col += n_extra;
5234 }
5235 }
5236 n_extra = 0;
5237 n_attr = 0;
5238 }
5239 else if (n_skip == 0)
5240 {
5241 is_concealing = TRUE;
5242 n_skip = 1;
5243 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005244 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005245 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005246 {
5247 mb_utf8 = TRUE;
5248 u8cc[0] = 0;
5249 c = 0xc0;
5250 }
5251 else
5252 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005253 }
5254 else
5255 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005256 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005257 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005258 }
5259#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 }
5261
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005262#ifdef FEAT_CONCEAL
5263 /* In the cursor line and we may be concealing characters: correct
5264 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005265 if (!did_wcol && draw_state == WL_LINE
5266 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005267 && conceal_cursor_line(wp)
5268 && (int)wp->w_virtcol <= vcol + n_skip)
5269 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005270# ifdef FEAT_RIGHTLEFT
5271 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005272 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005273 else
5274# endif
5275 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005276 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005277 did_wcol = TRUE;
5278 }
5279#endif
5280
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 /* Don't override visual selection highlighting. */
5282 if (n_attr > 0
5283 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005284 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 char_attr = extra_attr;
5286
Bram Moolenaar81695252004-12-29 20:58:21 +00005287#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 /* XIM don't send preedit_start and preedit_end, but they send
5289 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5290 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005291 if (p_imst == IM_ON_THE_SPOT
5292 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005293 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 && (State & INSERT)
5295 && !p_imdisable
5296 && im_is_preediting()
5297 && draw_state == WL_LINE)
5298 {
5299 colnr_T tcol;
5300
5301 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005302 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 else
5304 tcol = preedit_end_col;
5305 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5306 {
5307 if (feedback_old_attr < 0)
5308 {
5309 feedback_col = 0;
5310 feedback_old_attr = char_attr;
5311 }
5312 char_attr = im_get_feedback_attr(feedback_col);
5313 if (char_attr < 0)
5314 char_attr = feedback_old_attr;
5315 feedback_col++;
5316 }
5317 else if (feedback_old_attr >= 0)
5318 {
5319 char_attr = feedback_old_attr;
5320 feedback_old_attr = -1;
5321 feedback_col = 0;
5322 }
5323 }
5324#endif
5325 /*
5326 * Handle the case where we are in column 0 but not on the first
5327 * character of the line and the user wants us to show us a
5328 * special character (via 'listchars' option "precedes:<char>".
5329 */
5330 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005331 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5333#ifdef FEAT_DIFF
5334 && filler_todo <= 0
5335#endif
5336 && draw_state > WL_NR
5337 && c != NUL)
5338 {
5339 c = lcs_prec;
5340 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005341 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5342 {
5343 /* Double-width character being overwritten by the "precedes"
5344 * character, need to fill up half the character. */
5345 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005346 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005347 n_extra = 1;
5348 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005349 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005352 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 {
5354 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005355 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005356 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 }
5358 else
5359 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005360 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
5362 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005363 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 n_attr3 = 1;
5365 }
5366 }
5367
5368 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005369 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005371 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005372#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005373 || did_line_attr == 1
5374#endif
5375 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005377#ifdef FEAT_SEARCH_EXTRA
5378 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005379
5380 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005381 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005382 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005383#endif
5384
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005385 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 * highlight match at end of line. If it's beyond the last
5387 * char on the screen, just overwrite that one (tricky!) Not
5388 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005389#ifdef FEAT_SEARCH_EXTRA
5390 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005391 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005392 prevcol_hl_flag = TRUE;
5393 else
5394 {
5395 cur = wp->w_match_head;
5396 while (cur != NULL)
5397 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005398 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005399 {
5400 prevcol_hl_flag = TRUE;
5401 break;
5402 }
5403 cur = cur->next;
5404 }
5405 }
5406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005408 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005409 && (VIsual_mode != Ctrl_V
5410 || lnum == VIsual.lnum
5411 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005412 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413#ifdef FEAT_SEARCH_EXTRA
5414 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005415 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005416# ifdef FEAT_SYN_HL
5417 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5418 && !(wp == curwin && VIsual_active))
5419# endif
5420# ifdef FEAT_DIFF
5421 && diff_hlf == (hlf_T)0
5422# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005423# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005424 && did_line_attr <= 1
5425# endif
5426 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427#endif
5428 ))
5429 {
5430 int n = 0;
5431
5432#ifdef FEAT_RIGHTLEFT
5433 if (wp->w_p_rl)
5434 {
5435 if (col < 0)
5436 n = 1;
5437 }
5438 else
5439#endif
5440 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005441 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 n = -1;
5443 }
5444 if (n != 0)
5445 {
5446 /* At the window boundary, highlight the last character
5447 * instead (better than nothing). */
5448 off += n;
5449 col += n;
5450 }
5451 else
5452 {
5453 /* Add a blank character to highlight. */
5454 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 if (enc_utf8)
5456 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 }
5458#ifdef FEAT_SEARCH_EXTRA
5459 if (area_attr == 0)
5460 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005461 /* Use attributes from match with highest priority among
5462 * 'search_hl' and the match list. */
5463 char_attr = search_hl.attr;
5464 cur = wp->w_match_head;
5465 shl_flag = FALSE;
5466 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005467 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005468 if (shl_flag == FALSE
5469 && ((cur != NULL
5470 && cur->priority > SEARCH_HL_PRIORITY)
5471 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005472 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005473 shl = &search_hl;
5474 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005475 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005476 else
5477 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005478 if ((ptr - line) - 1 == (long)shl->startcol
5479 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005480 char_attr = shl->attr;
5481 if (shl != &search_hl && cur != NULL)
5482 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 }
5485#endif
5486 ScreenAttrs[off] = char_attr;
5487#ifdef FEAT_RIGHTLEFT
5488 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005491 --off;
5492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 else
5494#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005497 ++off;
5498 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005499 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005500#ifdef FEAT_SYN_HL
5501 eol_hl_off = 1;
5502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
Bram Moolenaar91170f82006-05-05 21:15:17 +00005506 /*
5507 * At end of the text line.
5508 */
5509 if (c == NUL)
5510 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005511#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005512 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005513 if (wp->w_p_wrap)
5514 v = wp->w_skipcol;
5515 else
5516 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005517
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005518 /* check if line ends before left margin */
5519 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005520 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005521#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005522 // Get rid of the boguscols now, we want to draw until the right
5523 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005524 col -= boguscols;
5525 boguscols = 0;
5526#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005527
5528 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005529 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005530
5531 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005532 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5533 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005534 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005535 && lnum != wp->w_cursor.lnum)
5536 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005537# ifdef FEAT_RIGHTLEFT
5538 && !wp->w_p_rl
5539# endif
5540 )
5541 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005542 int rightmost_vcol = 0;
5543 int i;
5544
5545 if (wp->w_p_cuc)
5546 rightmost_vcol = wp->w_virtcol;
5547 if (draw_color_col)
5548 /* determine rightmost colorcolumn to possibly draw */
5549 for (i = 0; color_cols[i] >= 0; ++i)
5550 if (rightmost_vcol < color_cols[i])
5551 rightmost_vcol = color_cols[i];
5552
Bram Moolenaar02631462017-09-22 15:20:32 +02005553 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005554 {
5555 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005556 if (enc_utf8)
5557 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005558 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005559 if (draw_color_col)
5560 draw_color_col = advance_color_col(VCOL_HLC,
5561 &color_cols);
5562
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005563 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005564 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005565 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005566 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005567 else
5568 ScreenAttrs[off++] = 0;
5569
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005570 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005571 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005572
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005573 ++vcol;
5574 }
5575 }
5576#endif
5577
Bram Moolenaar53f81742017-09-22 14:35:51 +02005578 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005579 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 row++;
5581
5582 /*
5583 * Update w_cline_height and w_cline_folded if the cursor line was
5584 * updated (saves a call to plines() later).
5585 */
5586 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5587 {
5588 curwin->w_cline_row = startrow;
5589 curwin->w_cline_height = row - startrow;
5590#ifdef FEAT_FOLDING
5591 curwin->w_cline_folded = FALSE;
5592#endif
5593 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5594 }
5595
5596 break;
5597 }
5598
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005599 // Show "extends" character from 'listchars' if beyond the line end and
5600 // 'list' is set.
5601 if (lcs_ext != NUL
5602 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 && !wp->w_p_wrap
5604#ifdef FEAT_DIFF
5605 && filler_todo <= 0
5606#endif
5607 && (
5608#ifdef FEAT_RIGHTLEFT
5609 wp->w_p_rl ? col == 0 :
5610#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005611 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005613 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5615 {
5616 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005617 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005619 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
5621 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005622 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005623 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 }
5625 else
5626 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 }
5628
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005629#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005630 /* advance to the next 'colorcolumn' */
5631 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005632 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005633
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005634 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005635 * highlight the cursor position itself.
5636 * Also highlight the 'colorcolumn' if it is different than
5637 * 'cursorcolumn' */
5638 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005639 if (draw_state == WL_LINE && !lnum_in_visual_area
5640 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005641 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005642 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005643 && lnum != wp->w_cursor.lnum)
5644 {
5645 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005646 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005647 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005648 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005649 {
5650 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005651 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005652 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005653 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005654#endif
5655
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 /*
5657 * Store character to be displayed.
5658 * Skip characters that are left of the screen for 'nowrap'.
5659 */
5660 vcol_prev = vcol;
5661 if (draw_state < WL_LINE || n_skip <= 0)
5662 {
5663 /*
5664 * Store the character.
5665 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005666#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5668 {
5669 /* A double-wide character is: put first halve in left cell. */
5670 --off;
5671 --col;
5672 }
5673#endif
5674 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005676 {
5677 if ((mb_c & 0xff00) == 0x8e00)
5678 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 else if (enc_utf8)
5682 {
5683 if (mb_utf8)
5684 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005685 int i;
5686
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005688 if ((c & 0xff) == 0)
5689 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005690 for (i = 0; i < Screen_mco; ++i)
5691 {
5692 ScreenLinesC[i][off] = u8cc[i];
5693 if (u8cc[i] == 0)
5694 break;
5695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
5697 else
5698 ScreenLinesUC[off] = 0;
5699 }
5700 if (multi_attr)
5701 {
5702 ScreenAttrs[off] = multi_attr;
5703 multi_attr = 0;
5704 }
5705 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 ScreenAttrs[off] = char_attr;
5707
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5709 {
5710 /* Need to fill two screen columns. */
5711 ++off;
5712 ++col;
5713 if (enc_utf8)
5714 /* UTF-8: Put a 0 in the second screen char. */
5715 ScreenLines[off] = 0;
5716 else
5717 /* DBCS: Put second byte in the second screen char. */
5718 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005719 if (draw_state > WL_NR
5720#ifdef FEAT_DIFF
5721 && filler_todo <= 0
5722#endif
5723 )
5724 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 /* When "tocol" is halfway a character, set it to the end of
5726 * the character, otherwise highlighting won't stop. */
5727 if (tocol == vcol)
5728 ++tocol;
5729#ifdef FEAT_RIGHTLEFT
5730 if (wp->w_p_rl)
5731 {
5732 /* now it's time to backup one cell */
5733 --off;
5734 --col;
5735 }
5736#endif
5737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738#ifdef FEAT_RIGHTLEFT
5739 if (wp->w_p_rl)
5740 {
5741 --off;
5742 --col;
5743 }
5744 else
5745#endif
5746 {
5747 ++off;
5748 ++col;
5749 }
5750 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005751#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005752 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005753 {
5754 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005755 ++vcol_off;
5756 if (n_extra > 0)
5757 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005758 if (wp->w_p_wrap)
5759 {
5760 /*
5761 * Special voodoo required if 'wrap' is on.
5762 *
5763 * Advance the column indicator to force the line
5764 * drawing to wrap early. This will make the line
5765 * take up the same screen space when parts are concealed,
5766 * so that cursor line computations aren't messed up.
5767 *
5768 * To avoid the fictitious advance of 'col' causing
5769 * trailing junk to be written out of the screen line
5770 * we are building, 'boguscols' keeps track of the number
5771 * of bad columns we have advanced.
5772 */
5773 if (n_extra > 0)
5774 {
5775 vcol += n_extra;
5776# ifdef FEAT_RIGHTLEFT
5777 if (wp->w_p_rl)
5778 {
5779 col -= n_extra;
5780 boguscols -= n_extra;
5781 }
5782 else
5783# endif
5784 {
5785 col += n_extra;
5786 boguscols += n_extra;
5787 }
5788 n_extra = 0;
5789 n_attr = 0;
5790 }
5791
5792
Bram Moolenaar860cae12010-06-05 23:22:07 +02005793 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5794 {
5795 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005796# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005797 if (wp->w_p_rl)
5798 {
5799 --boguscols;
5800 --col;
5801 }
5802 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005803# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005804 {
5805 ++boguscols;
5806 ++col;
5807 }
5808 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005809
5810# ifdef FEAT_RIGHTLEFT
5811 if (wp->w_p_rl)
5812 {
5813 --boguscols;
5814 --col;
5815 }
5816 else
5817# endif
5818 {
5819 ++boguscols;
5820 ++col;
5821 }
5822 }
5823 else
5824 {
5825 if (n_extra > 0)
5826 {
5827 vcol += n_extra;
5828 n_extra = 0;
5829 n_attr = 0;
5830 }
5831 }
5832
5833 }
5834#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 else
5836 --n_skip;
5837
Bram Moolenaar64486672010-05-16 15:46:46 +02005838 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5839 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005840 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841#ifdef FEAT_DIFF
5842 && filler_todo <= 0
5843#endif
5844 )
5845 ++vcol;
5846
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005847#ifdef FEAT_SYN_HL
5848 if (vcol_save_attr >= 0)
5849 char_attr = vcol_save_attr;
5850#endif
5851
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 /* restore attributes after "predeces" in 'listchars' */
5853 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5854 char_attr = saved_attr3;
5855
5856 /* restore attributes after last 'listchars' or 'number' char */
5857 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5858 char_attr = saved_attr2;
5859
5860 /*
5861 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005862 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 */
5864 if ((
5865#ifdef FEAT_RIGHTLEFT
5866 wp->w_p_rl ? (col < 0) :
5867#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005868 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 && (*ptr != NUL
5870#ifdef FEAT_DIFF
5871 || filler_todo > 0
5872#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005873 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5875 )
5876 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005877#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005878 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005879 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005880 boguscols = 0;
5881#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005882 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005883 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 ++row;
5886 ++screen_row;
5887
5888 /* When not wrapping and finished diff lines, or when displayed
5889 * '$' and highlighting until last column, break here. */
5890 if ((!wp->w_p_wrap
5891#ifdef FEAT_DIFF
5892 && filler_todo <= 0
5893#endif
5894 ) || lcs_eol_one == -1)
5895 break;
5896
5897 /* When the window is too narrow draw all "@" lines. */
5898 if (draw_state != WL_LINE
5899#ifdef FEAT_DIFF
5900 && filler_todo <= 0
5901#endif
5902 )
5903 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01005904 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 row = endrow;
5907 }
5908
5909 /* When line got too long for screen break here. */
5910 if (row == endrow)
5911 {
5912 ++row;
5913 break;
5914 }
5915
5916 if (screen_cur_row == screen_row - 1
5917#ifdef FEAT_DIFF
5918 && filler_todo <= 0
5919#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005920 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 {
5922 /* Remember that the line wraps, used for modeless copy. */
5923 LineWraps[screen_row - 1] = TRUE;
5924
5925 /*
5926 * Special trick to make copy/paste of wrapped lines work with
5927 * xterm/screen: write an extra character beyond the end of
5928 * the line. This will work with all terminal types
5929 * (regardless of the xn,am settings).
5930 * Only do this on a fast tty.
5931 * Only do this if the cursor is on the current line
5932 * (something has been written in it).
5933 * Don't do this for the GUI.
5934 * Don't do this for double-width characters.
5935 * Don't do this for a window not at the right screen border.
5936 */
5937 if (p_tf
5938#ifdef FEAT_GUI
5939 && !gui.in_use
5940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005942 && ((*mb_off2cells)(LineOffset[screen_row],
5943 LineOffset[screen_row] + screen_Columns)
5944 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005946 + (int)Columns - 2,
5947 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01005948 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 {
5950 /* First make sure we are at the end of the screen line,
5951 * then output the same character again to let the
5952 * terminal know about the wrap. If the terminal doesn't
5953 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005954 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 screen_char(LineOffset[screen_row - 1]
5956 + (unsigned)Columns - 1,
5957 screen_row - 1, (int)(Columns - 1));
5958
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 /* When there is a multi-byte character, just output a
5960 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005961 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5962 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 out_char(' ');
5964 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 out_char(ScreenLines[LineOffset[screen_row - 1]
5966 + (Columns - 1)]);
5967 /* force a redraw of the first char on the next line */
5968 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5969 screen_start(); /* don't know where cursor is now */
5970 }
5971 }
5972
5973 col = 0;
5974 off = (unsigned)(current_ScreenLine - ScreenLines);
5975#ifdef FEAT_RIGHTLEFT
5976 if (wp->w_p_rl)
5977 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005978 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 off += col;
5980 }
5981#endif
5982
5983 /* reset the drawing state for the start of a wrapped line */
5984 draw_state = WL_START;
5985 saved_n_extra = n_extra;
5986 saved_p_extra = p_extra;
5987 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005988 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 saved_char_attr = char_attr;
5990 n_extra = 0;
5991 lcs_prec_todo = lcs_prec;
5992#ifdef FEAT_LINEBREAK
5993# ifdef FEAT_DIFF
5994 if (filler_todo <= 0)
5995# endif
5996 need_showbreak = TRUE;
5997#endif
5998#ifdef FEAT_DIFF
5999 --filler_todo;
6000 /* When the filler lines are actually below the last line of the
6001 * file, don't draw the line itself, break here. */
6002 if (filler_todo == 0 && wp->w_botfill)
6003 break;
6004#endif
6005 }
6006
6007 } /* for every character in the line */
6008
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006009#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006010 /* After an empty line check first word for capital. */
6011 if (*skipwhite(line) == NUL)
6012 {
6013 capcol_lnum = lnum + 1;
6014 cap_col = 0;
6015 }
6016#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006017#ifdef FEAT_TEXT_PROP
6018 vim_free(text_props);
6019 vim_free(text_prop_idxs);
6020#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006021
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006022 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 return row;
6024}
6025
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006026/*
6027 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006028 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006029 */
6030 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006031comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006032{
6033 int i;
6034
6035 for (i = 0; i < Screen_mco; ++i)
6036 {
6037 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6038 return TRUE;
6039 if (ScreenLinesC[i][off_from] == 0)
6040 break;
6041 }
6042 return FALSE;
6043}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006044
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045/*
6046 * Check whether the given character needs redrawing:
6047 * - the (first byte of the) character is different
6048 * - the attributes are different
6049 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006050 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 */
6052 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006053char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
6055 if (cols > 0
6056 && ((ScreenLines[off_from] != ScreenLines[off_to]
6057 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 || (enc_dbcs != 0
6059 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6060 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6061 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6062 : (cols > 1 && ScreenLines[off_from + 1]
6063 != ScreenLines[off_to + 1])))
6064 || (enc_utf8
6065 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6066 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006067 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006068 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6069 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006070 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 return TRUE;
6072 return FALSE;
6073}
6074
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006075#if defined(FEAT_TERMINAL) || defined(PROTO)
6076/*
6077 * Return the index in ScreenLines[] for the current screen line.
6078 */
6079 int
6080screen_get_current_line_off()
6081{
6082 return (int)(current_ScreenLine - ScreenLines);
6083}
6084#endif
6085
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086/*
6087 * Move one "cooked" screen line to the screen, but only the characters that
6088 * have actually changed. Handle insert/delete character.
6089 * "coloff" gives the first column on the screen for this line.
6090 * "endcol" gives the columns where valid characters are.
6091 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6092 * needs to be cleared, negative otherwise.
6093 * "rlflag" is TRUE in a rightleft window:
6094 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6095 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6096 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006097 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006098screen_line(
6099 int row,
6100 int coloff,
6101 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006102 int clear_width,
6103 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
6105 unsigned off_from;
6106 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006107 unsigned max_off_from;
6108 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 int force = FALSE; /* force update rest of the line */
6112 int redraw_this /* bool: does character need redraw? */
6113#ifdef FEAT_GUI
6114 = TRUE /* For GUI when while-loop empty */
6115#endif
6116 ;
6117 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 int clear_next = FALSE;
6119 int char_cells; /* 1: normal char */
6120 /* 2: occupies two display cells */
6121# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006123 /* Check for illegal row and col, just in case. */
6124 if (row >= Rows)
6125 row = Rows - 1;
6126 if (endcol > Columns)
6127 endcol = Columns;
6128
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129# ifdef FEAT_CLIPBOARD
6130 clip_may_clear_selection(row, row);
6131# endif
6132
6133 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6134 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006135 max_off_from = off_from + screen_Columns;
6136 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137
6138#ifdef FEAT_RIGHTLEFT
6139 if (rlflag)
6140 {
6141 /* Clear rest first, because it's left of the text. */
6142 if (clear_width > 0)
6143 {
6144 while (col <= endcol && ScreenLines[off_to] == ' '
6145 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006146 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 {
6148 ++off_to;
6149 ++col;
6150 }
6151 if (col <= endcol)
6152 screen_fill(row, row + 1, col + coloff,
6153 endcol + coloff + 1, ' ', ' ', 0);
6154 }
6155 col = endcol + 1;
6156 off_to = LineOffset[row] + col + coloff;
6157 off_from += col;
6158 endcol = (clear_width > 0 ? clear_width : -clear_width);
6159 }
6160#endif /* FEAT_RIGHTLEFT */
6161
6162 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6163
6164 while (col < endcol)
6165 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006167 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 else
6169 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170
6171 redraw_this = redraw_next;
6172 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6173 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6174
6175#ifdef FEAT_GUI
6176 /* If the next character was bold, then redraw the current character to
6177 * remove any pixels that might have spilt over into us. This only
6178 * happens in the GUI.
6179 */
6180 if (redraw_next && gui.in_use)
6181 {
6182 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006183 if (hl > HL_ALL)
6184 hl = syn_attr2attr(hl);
6185 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 redraw_this = TRUE;
6187 }
6188#endif
6189
6190 if (redraw_this)
6191 {
6192 /*
6193 * Special handling when 'xs' termcap flag set (hpterm):
6194 * Attributes for characters are stored at the position where the
6195 * cursor is when writing the highlighting code. The
6196 * start-highlighting code must be written with the cursor on the
6197 * first highlighted character. The stop-highlighting code must
6198 * be written with the cursor just after the last highlighted
6199 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006200 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 * to clear the rest of the line, and force redrawing it
6202 * completely.
6203 */
6204 if ( p_wiv
6205 && !force
6206#ifdef FEAT_GUI
6207 && !gui.in_use
6208#endif
6209 && ScreenAttrs[off_to] != 0
6210 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6211 {
6212 /*
6213 * Need to remove highlighting attributes here.
6214 */
6215 windgoto(row, col + coloff);
6216 out_str(T_CE); /* clear rest of this screen line */
6217 screen_start(); /* don't know where cursor is now */
6218 force = TRUE; /* force redraw of rest of the line */
6219 redraw_next = TRUE; /* or else next char would miss out */
6220
6221 /*
6222 * If the previous character was highlighted, need to stop
6223 * highlighting at this character.
6224 */
6225 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6226 {
6227 screen_attr = ScreenAttrs[off_to - 1];
6228 term_windgoto(row, col + coloff);
6229 screen_stop_highlight();
6230 }
6231 else
6232 screen_attr = 0; /* highlighting has stopped */
6233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 if (enc_dbcs != 0)
6235 {
6236 /* Check if overwriting a double-byte with a single-byte or
6237 * the other way around requires another character to be
6238 * redrawn. For UTF-8 this isn't needed, because comparing
6239 * ScreenLinesUC[] is sufficient. */
6240 if (char_cells == 1
6241 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006242 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 {
6244 /* Writing a single-cell character over a double-cell
6245 * character: need to redraw the next cell. */
6246 ScreenLines[off_to + 1] = 0;
6247 redraw_next = TRUE;
6248 }
6249 else if (char_cells == 2
6250 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006251 && (*mb_off2cells)(off_to, max_off_to) == 1
6252 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 {
6254 /* Writing the second half of a double-cell character over
6255 * a double-cell character: need to redraw the second
6256 * cell. */
6257 ScreenLines[off_to + 2] = 0;
6258 redraw_next = TRUE;
6259 }
6260
6261 if (enc_dbcs == DBCS_JPNU)
6262 ScreenLines2[off_to] = ScreenLines2[off_from];
6263 }
6264 /* When writing a single-width character over a double-width
6265 * character and at the end of the redrawn text, need to clear out
6266 * the right halve of the old character.
6267 * Also required when writing the right halve of a double-width
6268 * char over the left halve of an existing one. */
6269 if (has_mbyte && col + char_cells == endcol
6270 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006271 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006273 && (*mb_off2cells)(off_to, max_off_to) == 1
6274 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276
6277 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 if (enc_utf8)
6279 {
6280 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6281 if (ScreenLinesUC[off_from] != 0)
6282 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006283 int i;
6284
6285 for (i = 0; i < Screen_mco; ++i)
6286 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 }
6288 }
6289 if (char_cells == 2)
6290 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291
6292#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006293 /* The bold trick makes a single column of pixels appear in the
6294 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 * character should be redrawn too. This happens for our own GUI
6296 * and for some xterms. */
6297 if (
6298# ifdef FEAT_GUI
6299 gui.in_use
6300# endif
6301# if defined(FEAT_GUI) && defined(UNIX)
6302 ||
6303# endif
6304# ifdef UNIX
6305 term_is_xterm
6306# endif
6307 )
6308 {
6309 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006310 if (hl > HL_ALL)
6311 hl = syn_attr2attr(hl);
6312 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 redraw_next = TRUE;
6314 }
6315#endif
6316 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006317
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006318 /* For simplicity set the attributes of second half of a
6319 * double-wide character equal to the first half. */
6320 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006322
6323 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 screen_char(off_to, row, col + coloff);
6327 }
6328 else if ( p_wiv
6329#ifdef FEAT_GUI
6330 && !gui.in_use
6331#endif
6332 && col + coloff > 0)
6333 {
6334 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6335 {
6336 /*
6337 * Don't output stop-highlight when moving the cursor, it will
6338 * stop the highlighting when it should continue.
6339 */
6340 screen_attr = 0;
6341 }
6342 else if (screen_attr != 0)
6343 screen_stop_highlight();
6344 }
6345
6346 off_to += CHAR_CELLS;
6347 off_from += CHAR_CELLS;
6348 col += CHAR_CELLS;
6349 }
6350
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 if (clear_next)
6352 {
6353 /* Clear the second half of a double-wide character of which the left
6354 * half was overwritten with a single-wide character. */
6355 ScreenLines[off_to] = ' ';
6356 if (enc_utf8)
6357 ScreenLinesUC[off_to] = 0;
6358 screen_char(off_to, row, col + coloff);
6359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360
6361 if (clear_width > 0
6362#ifdef FEAT_RIGHTLEFT
6363 && !rlflag
6364#endif
6365 )
6366 {
6367#ifdef FEAT_GUI
6368 int startCol = col;
6369#endif
6370
6371 /* blank out the rest of the line */
6372 while (col < clear_width && ScreenLines[off_to] == ' '
6373 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006374 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 {
6376 ++off_to;
6377 ++col;
6378 }
6379 if (col < clear_width)
6380 {
6381#ifdef FEAT_GUI
6382 /*
6383 * In the GUI, clearing the rest of the line may leave pixels
6384 * behind if the first character cleared was bold. Some bold
6385 * fonts spill over the left. In this case we redraw the previous
6386 * character too. If we didn't skip any blanks above, then we
6387 * only redraw if the character wasn't already redrawn anyway.
6388 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006389 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 {
6391 hl = ScreenAttrs[off_to];
6392 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006393 {
6394 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006395
Bram Moolenaar9c697322006-10-09 20:11:17 +00006396 if (enc_utf8)
6397 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6398 * that its width is 2. */
6399 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6400 else if (enc_dbcs != 0)
6401 {
6402 /* find previous character by counting from first
6403 * column and get its width. */
6404 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006405 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006406
6407 while (off < off_to)
6408 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006409 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006410 off += prev_cells;
6411 }
6412 }
6413
6414 if (enc_dbcs != 0 && prev_cells > 1)
6415 screen_char_2(off_to - prev_cells, row,
6416 col + coloff - prev_cells);
6417 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006418 screen_char(off_to - prev_cells, row,
6419 col + coloff - prev_cells);
6420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 }
6422#endif
6423 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6424 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 off_to += clear_width - col;
6426 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 }
6428 }
6429
6430 if (clear_width > 0)
6431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 /* For a window that's left of another, draw the separator char. */
6433 if (col + coloff < Columns)
6434 {
6435 int c;
6436
6437 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006438 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006439 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6440 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 || ScreenAttrs[off_to] != hl)
6442 {
6443 ScreenLines[off_to] = c;
6444 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 if (enc_utf8)
6446 {
6447 if (c >= 0x80)
6448 {
6449 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006450 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 }
6452 else
6453 ScreenLinesUC[off_to] = 0;
6454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 screen_char(off_to, row, col + coloff);
6456 }
6457 }
6458 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 LineWraps[row] = FALSE;
6460 }
6461}
6462
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006463#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006465 * Mirror text "str" for right-left displaying.
6466 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006468 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006469rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470{
6471 char_u *p1, *p2;
6472 int t;
6473
6474 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6475 {
6476 t = *p1;
6477 *p1 = *p2;
6478 *p2 = t;
6479 }
6480}
6481#endif
6482
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483/*
6484 * mark all status lines for redraw; used after first :cd
6485 */
6486 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006487status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488{
6489 win_T *wp;
6490
Bram Moolenaar29323592016-07-24 22:04:11 +02006491 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 if (wp->w_status_height)
6493 {
6494 wp->w_redr_status = TRUE;
6495 redraw_later(VALID);
6496 }
6497}
6498
6499/*
6500 * mark all status lines of the current buffer for redraw
6501 */
6502 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006503status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504{
6505 win_T *wp;
6506
Bram Moolenaar29323592016-07-24 22:04:11 +02006507 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6509 {
6510 wp->w_redr_status = TRUE;
6511 redraw_later(VALID);
6512 }
6513}
6514
6515/*
6516 * Redraw all status lines that need to be redrawn.
6517 */
6518 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006519redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520{
6521 win_T *wp;
6522
Bram Moolenaar29323592016-07-24 22:04:11 +02006523 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006525 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006526 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006527 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529
Bram Moolenaar4033c552017-09-16 20:54:51 +02006530#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531/*
6532 * Redraw all status lines at the bottom of frame "frp".
6533 */
6534 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006535win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536{
6537 if (frp->fr_layout == FR_LEAF)
6538 frp->fr_win->w_redr_status = TRUE;
6539 else if (frp->fr_layout == FR_ROW)
6540 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006541 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 win_redraw_last_status(frp);
6543 }
6544 else /* frp->fr_layout == FR_COL */
6545 {
6546 frp = frp->fr_child;
6547 while (frp->fr_next != NULL)
6548 frp = frp->fr_next;
6549 win_redraw_last_status(frp);
6550 }
6551}
6552#endif
6553
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554/*
6555 * Draw the verticap separator right of window "wp" starting with line "row".
6556 */
6557 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006558draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559{
6560 int hl;
6561 int c;
6562
6563 if (wp->w_vsep_width)
6564 {
6565 /* draw the vertical separator right of this window */
6566 c = fillchar_vsep(&hl);
6567 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6568 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6569 c, ' ', hl);
6570 }
6571}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572
6573#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006574static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575
6576/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006577 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 */
6579 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006580status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581{
6582 int len = 0;
6583
6584#ifdef FEAT_MENU
6585 int emenu = (xp->xp_context == EXPAND_MENUS
6586 || xp->xp_context == EXPAND_MENUNAMES);
6587
6588 /* Check for menu separators - replace with '|'. */
6589 if (emenu && menu_is_separator(s))
6590 return 1;
6591#endif
6592
6593 while (*s != NUL)
6594 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006595 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006596 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006597 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 }
6599
6600 return len;
6601}
6602
6603/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006604 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006605 * These are backslashes used for escaping. Do show backslashes in help tags.
6606 */
6607 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006608skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006609{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006610 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006611#ifdef FEAT_MENU
6612 || ((xp->xp_context == EXPAND_MENUS
6613 || xp->xp_context == EXPAND_MENUNAMES)
6614 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6615#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006616 )
6617 {
6618#ifndef BACKSLASH_IN_FILENAME
6619 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6620 return 2;
6621#endif
6622 return 1;
6623 }
6624 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006625}
6626
6627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 * Show wildchar matches in the status line.
6629 * Show at least the "match" item.
6630 * We start at item 'first_match' in the list and show all matches that fit.
6631 *
6632 * If inversion is possible we use it. Else '=' characters are used.
6633 */
6634 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006635win_redr_status_matches(
6636 expand_T *xp,
6637 int num_matches,
6638 char_u **matches, /* list of matches */
6639 int match,
6640 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641{
6642#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6643 int row;
6644 char_u *buf;
6645 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006646 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 int fillchar;
6648 int attr;
6649 int i;
6650 int highlight = TRUE;
6651 char_u *selstart = NULL;
6652 int selstart_col = 0;
6653 char_u *selend = NULL;
6654 static int first_match = 0;
6655 int add_left = FALSE;
6656 char_u *s;
6657#ifdef FEAT_MENU
6658 int emenu;
6659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661
6662 if (matches == NULL) /* interrupted completion? */
6663 return;
6664
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006665 if (has_mbyte)
6666 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6667 else
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006668 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 if (buf == NULL)
6670 return;
6671
6672 if (match == -1) /* don't show match but original text */
6673 {
6674 match = 0;
6675 highlight = FALSE;
6676 }
6677 /* count 1 for the ending ">" */
6678 clen = status_match_len(xp, L_MATCH(match)) + 3;
6679 if (match == 0)
6680 first_match = 0;
6681 else if (match < first_match)
6682 {
6683 /* jumping left, as far as we can go */
6684 first_match = match;
6685 add_left = TRUE;
6686 }
6687 else
6688 {
6689 /* check if match fits on the screen */
6690 for (i = first_match; i < match; ++i)
6691 clen += status_match_len(xp, L_MATCH(i)) + 2;
6692 if (first_match > 0)
6693 clen += 2;
6694 /* jumping right, put match at the left */
6695 if ((long)clen > Columns)
6696 {
6697 first_match = match;
6698 /* if showing the last match, we can add some on the left */
6699 clen = 2;
6700 for (i = match; i < num_matches; ++i)
6701 {
6702 clen += status_match_len(xp, L_MATCH(i)) + 2;
6703 if ((long)clen >= Columns)
6704 break;
6705 }
6706 if (i == num_matches)
6707 add_left = TRUE;
6708 }
6709 }
6710 if (add_left)
6711 while (first_match > 0)
6712 {
6713 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6714 if ((long)clen >= Columns)
6715 break;
6716 --first_match;
6717 }
6718
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006719 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720
6721 if (first_match == 0)
6722 {
6723 *buf = NUL;
6724 len = 0;
6725 }
6726 else
6727 {
6728 STRCPY(buf, "< ");
6729 len = 2;
6730 }
6731 clen = len;
6732
6733 i = first_match;
6734 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6735 {
6736 if (i == match)
6737 {
6738 selstart = buf + len;
6739 selstart_col = clen;
6740 }
6741
6742 s = L_MATCH(i);
6743 /* Check for menu separators - replace with '|' */
6744#ifdef FEAT_MENU
6745 emenu = (xp->xp_context == EXPAND_MENUS
6746 || xp->xp_context == EXPAND_MENUNAMES);
6747 if (emenu && menu_is_separator(s))
6748 {
6749 STRCPY(buf + len, transchar('|'));
6750 l = (int)STRLEN(buf + len);
6751 len += l;
6752 clen += l;
6753 }
6754 else
6755#endif
6756 for ( ; *s != NUL; ++s)
6757 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006758 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006760 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 {
6762 STRNCPY(buf + len, s, l);
6763 s += l - 1;
6764 len += l;
6765 }
6766 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 {
6768 STRCPY(buf + len, transchar_byte(*s));
6769 len += (int)STRLEN(buf + len);
6770 }
6771 }
6772 if (i == match)
6773 selend = buf + len;
6774
6775 *(buf + len++) = ' ';
6776 *(buf + len++) = ' ';
6777 clen += 2;
6778 if (++i == num_matches)
6779 break;
6780 }
6781
6782 if (i != num_matches)
6783 {
6784 *(buf + len++) = '>';
6785 ++clen;
6786 }
6787
6788 buf[len] = NUL;
6789
6790 row = cmdline_row - 1;
6791 if (row >= 0)
6792 {
6793 if (wild_menu_showing == 0)
6794 {
6795 if (msg_scrolled > 0)
6796 {
6797 /* Put the wildmenu just above the command line. If there is
6798 * no room, scroll the screen one line up. */
6799 if (cmdline_row == Rows - 1)
6800 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006801 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 ++msg_scrolled;
6803 }
6804 else
6805 {
6806 ++cmdline_row;
6807 ++row;
6808 }
6809 wild_menu_showing = WM_SCROLLED;
6810 }
6811 else
6812 {
6813 /* Create status line if needed by setting 'laststatus' to 2.
6814 * Set 'winminheight' to zero to avoid that the window is
6815 * resized. */
6816 if (lastwin->w_status_height == 0)
6817 {
6818 save_p_ls = p_ls;
6819 save_p_wmh = p_wmh;
6820 p_ls = 2;
6821 p_wmh = 0;
6822 last_status(FALSE);
6823 }
6824 wild_menu_showing = WM_SHOWN;
6825 }
6826 }
6827
6828 screen_puts(buf, row, 0, attr);
6829 if (selstart != NULL && highlight)
6830 {
6831 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006832 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 }
6834
6835 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6836 }
6837
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 vim_free(buf);
6840}
6841#endif
6842
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843/*
6844 * Redraw the status line of window wp.
6845 *
6846 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006847 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6848 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006850 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006851win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852{
6853 int row;
6854 char_u *p;
6855 int len;
6856 int fillchar;
6857 int attr;
6858 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006859 static int busy = FALSE;
6860
6861 /* It's possible to get here recursively when 'statusline' (indirectly)
6862 * invokes ":redrawstatus". Simply ignore the call then. */
6863 if (busy)
6864 return;
6865 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866
6867 wp->w_redr_status = FALSE;
6868 if (wp->w_status_height == 0)
6869 {
6870 /* no status line, can only be last window */
6871 redraw_cmdline = TRUE;
6872 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006873 else if (!redrawing()
6874#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006875 // don't update status line when popup menu is visible and may be
6876 // drawn over it, unless it will be redrawn later
6877 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006878#endif
6879 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {
6881 /* Don't redraw right now, do it later. */
6882 wp->w_redr_status = TRUE;
6883 }
6884#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006885 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
6887 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006888 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 }
6890#endif
6891 else
6892 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006893 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006895 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 p = NameBuff;
6897 len = (int)STRLEN(p);
6898
Bram Moolenaard85f2712017-07-28 21:51:57 +02006899 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900#ifdef FEAT_QUICKFIX
6901 || wp->w_p_pvw
6902#endif
6903 || bufIsChanged(wp->w_buffer)
6904 || wp->w_buffer->b_p_ro)
6905 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006906 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006908 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 len += (int)STRLEN(p + len);
6910 }
6911#ifdef FEAT_QUICKFIX
6912 if (wp->w_p_pvw)
6913 {
6914 STRCPY(p + len, _("[Preview]"));
6915 len += (int)STRLEN(p + len);
6916 }
6917#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006918 if (bufIsChanged(wp->w_buffer)
6919#ifdef FEAT_TERMINAL
6920 && !bt_terminal(wp->w_buffer)
6921#endif
6922 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 {
6924 STRCPY(p + len, "[+]");
6925 len += 3;
6926 }
6927 if (wp->w_buffer->b_p_ro)
6928 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006929 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006930 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 }
6932
Bram Moolenaar02631462017-09-22 15:20:32 +02006933 this_ru_col = ru_col - (Columns - wp->w_width);
6934 if (this_ru_col < (wp->w_width + 1) / 2)
6935 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 if (this_ru_col <= 1)
6937 {
6938 p = (char_u *)"<"; /* No room for file name! */
6939 len = 1;
6940 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01006941 else if (has_mbyte)
6942 {
6943 int clen = 0, i;
6944
6945 /* Count total number of display cells. */
6946 clen = mb_string2cells(p, -1);
6947
6948 /* Find first character that will fit.
6949 * Going from start to end is much faster for DBCS. */
6950 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
6951 i += (*mb_ptr2len)(p + i))
6952 clen -= (*mb_ptr2cells)(p + i);
6953 len = clen;
6954 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01006956 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01006958 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 }
6960
Bram Moolenaara12a1612019-01-24 16:39:02 +01006961 }
6962 else if (len > this_ru_col - 1)
6963 {
6964 p += len - (this_ru_col - 1);
6965 *p = '<';
6966 len = this_ru_col - 1;
6967 }
6968
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006970 screen_puts(p, row, wp->w_wincol, attr);
6971 screen_fill(row, row + 1, len + wp->w_wincol,
6972 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006974 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6976 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006977 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978
6979#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02006980 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981#endif
6982 }
6983
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 /*
6985 * May need to draw the character below the vertical separator.
6986 */
6987 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6988 {
6989 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006990 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 else
6992 fillchar = fillchar_vsep(&attr);
6993 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6994 attr);
6995 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006996 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997}
6998
Bram Moolenaar238a5642006-02-21 22:12:05 +00006999#ifdef FEAT_STL_OPT
7000/*
7001 * Redraw the status line according to 'statusline' and take care of any
7002 * errors encountered.
7003 */
7004 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007005redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007006{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007007 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007008 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007009
7010 /* When called recursively return. This can happen when the statusline
7011 * contains an expression that triggers a redraw. */
7012 if (entered)
7013 return;
7014 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007015
Bram Moolenaara742e082016-04-05 21:10:38 +02007016 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007017 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007018 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007019 {
7020 /* When there is an error disable the statusline, otherwise the
7021 * display is messed up with errors and a redraw triggers the problem
7022 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007023 set_string_option_direct((char_u *)"statusline", -1,
7024 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007025 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007026 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007027 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007028 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007029}
7030#endif
7031
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032/*
7033 * Return TRUE if the status line of window "wp" is connected to the status
7034 * line of the window right of it. If not, then it's a vertical separator.
7035 * Only call if (wp->w_vsep_width != 0).
7036 */
7037 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007038stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039{
7040 frame_T *fr;
7041
7042 fr = wp->w_frame;
7043 while (fr->fr_parent != NULL)
7044 {
7045 if (fr->fr_parent->fr_layout == FR_COL)
7046 {
7047 if (fr->fr_next != NULL)
7048 break;
7049 }
7050 else
7051 {
7052 if (fr->fr_next != NULL)
7053 return TRUE;
7054 }
7055 fr = fr->fr_parent;
7056 }
7057 return FALSE;
7058}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061/*
7062 * Get the value to show for the language mappings, active 'keymap'.
7063 */
7064 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007065get_keymap_str(
7066 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007067 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007068 char_u *buf, /* buffer for the result */
7069 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
7071 char_u *p;
7072
7073 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7074 return FALSE;
7075
7076 {
7077#ifdef FEAT_EVAL
7078 buf_T *old_curbuf = curbuf;
7079 win_T *old_curwin = curwin;
7080 char_u *s;
7081
7082 curbuf = wp->w_buffer;
7083 curwin = wp;
7084 STRCPY(buf, "b:keymap_name"); /* must be writable */
7085 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007086 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 --emsg_skip;
7088 curbuf = old_curbuf;
7089 curwin = old_curwin;
7090 if (p == NULL || *p == NUL)
7091#endif
7092 {
7093#ifdef FEAT_KEYMAP
7094 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7095 p = wp->w_buffer->b_p_keymap;
7096 else
7097#endif
7098 p = (char_u *)"lang";
7099 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007100 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 buf[0] = NUL;
7102#ifdef FEAT_EVAL
7103 vim_free(s);
7104#endif
7105 }
7106 return buf[0] != NUL;
7107}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108
7109#if defined(FEAT_STL_OPT) || defined(PROTO)
7110/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007111 * Redraw the status line or ruler of window "wp".
7112 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 */
7114 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007115win_redr_custom(
7116 win_T *wp,
7117 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007119 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 int attr;
7121 int curattr;
7122 int row;
7123 int col = 0;
7124 int maxwidth;
7125 int width;
7126 int n;
7127 int len;
7128 int fillchar;
7129 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007130 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007132 struct stl_hlrec hltab[STL_MAX_ITEM];
7133 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007134 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007135 win_T *ewp;
7136 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137
Bram Moolenaar1d633412013-12-11 15:52:01 +01007138 /* There is a tiny chance that this gets called recursively: When
7139 * redrawing a status line triggers redrawing the ruler or tabline.
7140 * Avoid trouble by not allowing recursion. */
7141 if (entered)
7142 return;
7143 entered = TRUE;
7144
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007146 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007148 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007149 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007150 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007151 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007152 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007153 maxwidth = Columns;
7154# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007155 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007156# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007158 else
7159 {
7160 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007161 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007162 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007163
7164 if (draw_ruler)
7165 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007166 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007167 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007168 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007169 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007170 if (*++stl == '-')
7171 stl++;
7172 if (atoi((char *)stl))
7173 while (VIM_ISDIGIT(*stl))
7174 stl++;
7175 if (*stl++ != '(')
7176 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007177 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007178 col = ru_col - (Columns - wp->w_width);
7179 if (col < (wp->w_width + 1) / 2)
7180 col = (wp->w_width + 1) / 2;
7181 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007182 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007183 {
7184 row = Rows - 1;
7185 --maxwidth; /* writing in last column may cause scrolling */
7186 fillchar = ' ';
7187 attr = 0;
7188 }
7189
7190# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007191 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007192# endif
7193 }
7194 else
7195 {
7196 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007197 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007198 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007199 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007200# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007201 use_sandbox = was_set_insecurely((char_u *)"statusline",
7202 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007203# endif
7204 }
7205
Bram Moolenaar53f81742017-09-22 14:35:51 +02007206 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007207 }
7208
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007210 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211
Bram Moolenaar61452852011-02-01 18:01:11 +01007212 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7213 * the cursor away and back. */
7214 ewp = wp == NULL ? curwin : wp;
7215 p_crb_save = ewp->w_p_crb;
7216 ewp->w_p_crb = FALSE;
7217
Bram Moolenaar362f3562009-11-03 16:20:34 +00007218 /* Make a copy, because the statusline may include a function call that
7219 * might change the option value and free the memory. */
7220 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007221 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007222 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007223 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007224 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007225 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007227 /* Make all characters printable. */
7228 p = transstr(buf);
7229 if (p != NULL)
7230 {
7231 vim_strncpy(buf, p, sizeof(buf) - 1);
7232 vim_free(p);
7233 }
7234
7235 /* fill up with "fillchar" */
7236 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007237 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 ++width;
7241 }
7242 buf[len] = NUL;
7243
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007244 /*
7245 * Draw each snippet with the specified highlighting.
7246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 curattr = attr;
7248 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007249 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007251 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 screen_puts_len(p, len, row, col, curattr);
7253 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007254 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007256 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007258 else if (hltab[n].userhl < 0)
7259 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007260#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007261 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7262 && wp->w_status_height != 0)
7263 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007264 else if (wp != NULL && bt_terminal(wp->w_buffer)
7265 && wp->w_status_height != 0)
7266 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007267#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007268 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007269 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007271 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 }
7273 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007274
7275 if (wp == NULL)
7276 {
7277 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7278 col = 0;
7279 len = 0;
7280 p = buf;
7281 fillchar = 0;
7282 for (n = 0; tabtab[n].start != NULL; n++)
7283 {
7284 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7285 while (col < len)
7286 TabPageIdxs[col++] = fillchar;
7287 p = tabtab[n].start;
7288 fillchar = tabtab[n].userhl;
7289 }
7290 while (col < Columns)
7291 TabPageIdxs[col++] = fillchar;
7292 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007293
7294theend:
7295 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296}
7297
7298#endif /* FEAT_STL_OPT */
7299
7300/*
7301 * Output a single character directly to the screen and update ScreenLines.
7302 */
7303 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007304screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 char_u buf[MB_MAXBYTES + 1];
7307
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007308 if (has_mbyte)
7309 buf[(*mb_char2bytes)(c, buf)] = NUL;
7310 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007311 {
7312 buf[0] = c;
7313 buf[1] = NUL;
7314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 screen_puts(buf, row, col, attr);
7316}
7317
7318/*
7319 * Get a single character directly from ScreenLines into "bytes[]".
7320 * Also return its attribute in *attrp;
7321 */
7322 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007323screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324{
7325 unsigned off;
7326
7327 /* safety check */
7328 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7329 {
7330 off = LineOffset[row] + col;
7331 *attrp = ScreenAttrs[off];
7332 bytes[0] = ScreenLines[off];
7333 bytes[1] = NUL;
7334
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 if (enc_utf8 && ScreenLinesUC[off] != 0)
7336 bytes[utfc_char2bytes(off, bytes)] = NUL;
7337 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7338 {
7339 bytes[0] = ScreenLines[off];
7340 bytes[1] = ScreenLines2[off];
7341 bytes[2] = NUL;
7342 }
7343 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7344 {
7345 bytes[1] = ScreenLines[off + 1];
7346 bytes[2] = NUL;
7347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 }
7349}
7350
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007351/*
7352 * Return TRUE if composing characters for screen posn "off" differs from
7353 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007354 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007355 */
7356 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007357screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007358{
7359 int i;
7360
7361 for (i = 0; i < Screen_mco; ++i)
7362 {
7363 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7364 return TRUE;
7365 if (u8cc[i] == 0)
7366 break;
7367 }
7368 return FALSE;
7369}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007370
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371/*
7372 * Put string '*text' on the screen at position 'row' and 'col', with
7373 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7374 * Note: only outputs within one row, message is truncated at screen boundary!
7375 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7376 */
7377 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007378screen_puts(
7379 char_u *text,
7380 int row,
7381 int col,
7382 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383{
7384 screen_puts_len(text, -1, row, col, attr);
7385}
7386
7387/*
7388 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7389 * a NUL.
7390 */
7391 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007392screen_puts_len(
7393 char_u *text,
7394 int textlen,
7395 int row,
7396 int col,
7397 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398{
7399 unsigned off;
7400 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007401 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007403 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 int mbyte_blen = 1;
7405 int mbyte_cells = 1;
7406 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007407 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007409#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007411 int pc, nc, nc1;
7412 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007414 int force_redraw_this;
7415 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007416 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417
7418 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7419 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007420 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421
Bram Moolenaarc236c162008-07-13 17:41:49 +00007422 /* When drawing over the right halve of a double-wide char clear out the
7423 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007424 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007425#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007426 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007427#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007428 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007429 {
7430 ScreenLines[off - 1] = ' ';
7431 ScreenAttrs[off - 1] = 0;
7432 if (enc_utf8)
7433 {
7434 ScreenLinesUC[off - 1] = 0;
7435 ScreenLinesC[0][off - 1] = 0;
7436 }
7437 /* redraw the previous cell, make it empty */
7438 screen_char(off - 1, row, col - 1);
7439 /* force the cell at "col" to be redrawn */
7440 force_redraw_next = TRUE;
7441 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007442
Bram Moolenaar367329b2007-08-30 11:53:22 +00007443 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007444 while (col < screen_Columns
7445 && (len < 0 || (int)(ptr - text) < len)
7446 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 {
7448 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 /* check if this is the first byte of a multibyte */
7450 if (has_mbyte)
7451 {
7452 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007453 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007455 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7457 mbyte_cells = 1;
7458 else if (enc_dbcs != 0)
7459 mbyte_cells = mbyte_blen;
7460 else /* enc_utf8 */
7461 {
7462 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007463 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 (int)((text + len) - ptr));
7465 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007466 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007468#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7470 {
7471 /* Do Arabic shaping. */
7472 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7473 {
7474 /* Past end of string to be displayed. */
7475 nc = NUL;
7476 nc1 = NUL;
7477 }
7478 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007479 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007480 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7481 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007482 nc1 = pcc[0];
7483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 pc = prev_c;
7485 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007486 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 }
7488 else
7489 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007490#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007491 if (col + mbyte_cells > screen_Columns)
7492 {
7493 /* Only 1 cell left, but character requires 2 cells:
7494 * display a '>' in the last column to avoid wrapping. */
7495 c = '>';
7496 mbyte_cells = 1;
7497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 }
7499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007501 force_redraw_this = force_redraw_next;
7502 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007503
7504 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 || (mbyte_cells == 2
7506 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7507 || (enc_dbcs == DBCS_JPNU
7508 && c == 0x8e
7509 && ScreenLines2[off] != ptr[1])
7510 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007511 && (ScreenLinesUC[off] !=
7512 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7513 || (ScreenLinesUC[off] != 0
7514 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007516 || exmode_active;
7517
Bram Moolenaara12a1612019-01-24 16:39:02 +01007518 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 {
7520#if defined(FEAT_GUI) || defined(UNIX)
7521 /* The bold trick makes a single row of pixels appear in the next
7522 * character. When a bold character is removed, the next
7523 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007524 * and for some xterms. */
7525 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526# ifdef FEAT_GUI
7527 gui.in_use
7528# endif
7529# if defined(FEAT_GUI) && defined(UNIX)
7530 ||
7531# endif
7532# ifdef UNIX
7533 term_is_xterm
7534# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007535 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007537 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007539 if (n > HL_ALL)
7540 n = syn_attr2attr(n);
7541 if (n & HL_BOLD)
7542 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 }
7544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 /* When at the end of the text and overwriting a two-cell
7546 * character with a one-cell character, need to clear the next
7547 * cell. Also when overwriting the left halve of a two-cell char
7548 * with the right halve of a two-cell char. Do this only once
7549 * (mb_off2cells() may return 2 on the right halve). */
7550 if (clear_next_cell)
7551 clear_next_cell = FALSE;
7552 else if (has_mbyte
7553 && (len < 0 ? ptr[mbyte_blen] == NUL
7554 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007555 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007557 && (*mb_off2cells)(off, max_off) == 1
7558 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 clear_next_cell = TRUE;
7560
7561 /* Make sure we never leave a second byte of a double-byte behind,
7562 * it confuses mb_off2cells(). */
7563 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007564 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007566 && (*mb_off2cells)(off, max_off) == 1
7567 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 ScreenLines[off] = c;
7570 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 if (enc_utf8)
7572 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007573 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 ScreenLinesUC[off] = 0;
7575 else
7576 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007577 int i;
7578
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007580 for (i = 0; i < Screen_mco; ++i)
7581 {
7582 ScreenLinesC[i][off] = u8cc[i];
7583 if (u8cc[i] == 0)
7584 break;
7585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 }
7587 if (mbyte_cells == 2)
7588 {
7589 ScreenLines[off + 1] = 0;
7590 ScreenAttrs[off + 1] = attr;
7591 }
7592 screen_char(off, row, col);
7593 }
7594 else if (mbyte_cells == 2)
7595 {
7596 ScreenLines[off + 1] = ptr[1];
7597 ScreenAttrs[off + 1] = attr;
7598 screen_char_2(off, row, col);
7599 }
7600 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7601 {
7602 ScreenLines2[off] = ptr[1];
7603 screen_char(off, row, col);
7604 }
7605 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 screen_char(off, row, col);
7607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 if (has_mbyte)
7609 {
7610 off += mbyte_cells;
7611 col += mbyte_cells;
7612 ptr += mbyte_blen;
7613 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007614 {
7615 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007617 len = -1;
7618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 }
7620 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 {
7622 ++off;
7623 ++col;
7624 ++ptr;
7625 }
7626 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007627
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007628 /* If we detected the next character needs to be redrawn, but the text
7629 * doesn't extend up to there, update the character here. */
7630 if (force_redraw_next && col < screen_Columns)
7631 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007632 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7633 screen_char_2(off, row, col);
7634 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007635 screen_char(off, row, col);
7636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637}
7638
7639#ifdef FEAT_SEARCH_EXTRA
7640/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007641 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 */
7643 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007644start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645{
7646 if (p_hls && !no_hlsearch)
7647 {
7648 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007649 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007650# ifdef FEAT_RELTIME
7651 /* Set the time limit to 'redrawtime'. */
7652 profile_setlimit(p_rdt, &search_hl.tm);
7653# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 }
7655}
7656
7657/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007658 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 */
7660 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007661end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662{
7663 if (search_hl.rm.regprog != NULL)
7664 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007665 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 search_hl.rm.regprog = NULL;
7667 }
7668}
7669
7670/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007671 * Init for calling prepare_search_hl().
7672 */
7673 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007674init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007675{
7676 matchitem_T *cur;
7677
7678 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7679 * match */
7680 cur = wp->w_match_head;
7681 while (cur != NULL)
7682 {
7683 cur->hl.rm = cur->match;
7684 if (cur->hlg_id == 0)
7685 cur->hl.attr = 0;
7686 else
7687 cur->hl.attr = syn_id2attr(cur->hlg_id);
7688 cur->hl.buf = wp->w_buffer;
7689 cur->hl.lnum = 0;
7690 cur->hl.first_lnum = 0;
7691# ifdef FEAT_RELTIME
7692 /* Set the time limit to 'redrawtime'. */
7693 profile_setlimit(p_rdt, &(cur->hl.tm));
7694# endif
7695 cur = cur->next;
7696 }
7697 search_hl.buf = wp->w_buffer;
7698 search_hl.lnum = 0;
7699 search_hl.first_lnum = 0;
7700 /* time limit is set at the toplevel, for all windows */
7701}
7702
7703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 * Advance to the match in window "wp" line "lnum" or past it.
7705 */
7706 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007707prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007709 matchitem_T *cur; /* points to the match list */
7710 match_T *shl; /* points to search_hl or a match */
7711 int shl_flag; /* flag to indicate whether search_hl
7712 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007713 int pos_inprogress; /* marks that position match search is
7714 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 int n;
7716
7717 /*
7718 * When using a multi-line pattern, start searching at the top
7719 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007720 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007722 cur = wp->w_match_head;
7723 shl_flag = FALSE;
7724 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007726 if (shl_flag == FALSE)
7727 {
7728 shl = &search_hl;
7729 shl_flag = TRUE;
7730 }
7731 else
7732 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 if (shl->rm.regprog != NULL
7734 && shl->lnum == 0
7735 && re_multiline(shl->rm.regprog))
7736 {
7737 if (shl->first_lnum == 0)
7738 {
7739# ifdef FEAT_FOLDING
7740 for (shl->first_lnum = lnum;
7741 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7742 if (hasFoldingWin(wp, shl->first_lnum - 1,
7743 NULL, NULL, TRUE, NULL))
7744 break;
7745# else
7746 shl->first_lnum = wp->w_topline;
7747# endif
7748 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007749 if (cur != NULL)
7750 cur->pos.cur = 0;
7751 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007753 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7754 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007756 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7757 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007758 pos_inprogress = cur == NULL || cur->pos.cur == 0
7759 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 if (shl->lnum != 0)
7761 {
7762 shl->first_lnum = shl->lnum
7763 + shl->rm.endpos[0].lnum
7764 - shl->rm.startpos[0].lnum;
7765 n = shl->rm.endpos[0].col;
7766 }
7767 else
7768 {
7769 ++shl->first_lnum;
7770 n = 0;
7771 }
7772 }
7773 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007774 if (shl != &search_hl && cur != NULL)
7775 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 }
7777}
7778
7779/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007780 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 * Uses shl->buf.
7782 * Sets shl->lnum and shl->rm contents.
7783 * Note: Assumes a previous match is always before "lnum", unless
7784 * shl->lnum is zero.
7785 * Careful: Any pointers for buffer lines will become invalid.
7786 */
7787 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007788next_search_hl(
7789 win_T *win,
7790 match_T *shl, /* points to search_hl or a match */
7791 linenr_T lnum,
7792 colnr_T mincol, /* minimal column for a match */
7793 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794{
7795 linenr_T l;
7796 colnr_T matchcol;
7797 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007798 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007800 // for :{range}s/pat only highlight inside the range
7801 if (lnum < search_first_line || lnum > search_last_line)
7802 {
7803 shl->lnum = 0;
7804 return;
7805 }
7806
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 if (shl->lnum != 0)
7808 {
7809 /* Check for three situations:
7810 * 1. If the "lnum" is below a previous match, start a new search.
7811 * 2. If the previous match includes "mincol", use it.
7812 * 3. Continue after the previous match.
7813 */
7814 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7815 if (lnum > l)
7816 shl->lnum = 0;
7817 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7818 return;
7819 }
7820
7821 /*
7822 * Repeat searching for a match until one is found that includes "mincol"
7823 * or none is found in this line.
7824 */
7825 called_emsg = FALSE;
7826 for (;;)
7827 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007828#ifdef FEAT_RELTIME
7829 /* Stop searching after passing the time limit. */
7830 if (profile_passed_limit(&(shl->tm)))
7831 {
7832 shl->lnum = 0; /* no match found in time */
7833 break;
7834 }
7835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 /* Three situations:
7837 * 1. No useful previous match: search from start of line.
7838 * 2. Not Vi compatible or empty match: continue at next character.
7839 * Break the loop if this is beyond the end of the line.
7840 * 3. Vi compatible searching: continue at end of previous match.
7841 */
7842 if (shl->lnum == 0)
7843 matchcol = 0;
7844 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7845 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007846 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007848 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007849
7850 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007851 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007852 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007854 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 shl->lnum = 0;
7856 break;
7857 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007858 if (has_mbyte)
7859 matchcol += mb_ptr2len(ml);
7860 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007861 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 }
7863 else
7864 matchcol = shl->rm.endpos[0].col;
7865
7866 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007867 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007869 /* Remember whether shl->rm is using a copy of the regprog in
7870 * cur->match. */
7871 int regprog_is_copy = (shl != &search_hl && cur != NULL
7872 && shl == &cur->hl
7873 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007874 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007875
Bram Moolenaarb3414592014-06-17 17:48:32 +02007876 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7877 matchcol,
7878#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007879 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007880#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007881 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007882#endif
7883 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007884 /* Copy the regprog, in case it got freed and recompiled. */
7885 if (regprog_is_copy)
7886 cur->match.regprog = cur->hl.rm.regprog;
7887
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007888 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007889 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007890 /* Error while handling regexp: stop using this regexp. */
7891 if (shl == &search_hl)
7892 {
7893 /* don't free regprog in the match list, it's a copy */
7894 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02007895 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007896 }
7897 shl->rm.regprog = NULL;
7898 shl->lnum = 0;
7899 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7900 message */
7901 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007902 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007903 }
7904 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007905 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007906 else
7907 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 if (nmatched == 0)
7909 {
7910 shl->lnum = 0; /* no match found */
7911 break;
7912 }
7913 if (shl->rm.startpos[0].lnum > 0
7914 || shl->rm.startpos[0].col >= mincol
7915 || nmatched > 1
7916 || shl->rm.endpos[0].col > mincol)
7917 {
7918 shl->lnum += shl->rm.startpos[0].lnum;
7919 break; /* useful match found */
7920 }
7921 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007922
7923 // Restore called_emsg for assert_fails().
7924 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926
Bram Moolenaar85077472016-10-16 14:35:48 +02007927/*
7928 * If there is a match fill "shl" and return one.
7929 * Return zero otherwise.
7930 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007931 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007932next_search_hl_pos(
7933 match_T *shl, /* points to a match */
7934 linenr_T lnum,
7935 posmatch_T *posmatch, /* match positions */
7936 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007937{
7938 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007939 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007940
Bram Moolenaarb3414592014-06-17 17:48:32 +02007941 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7942 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007943 llpos_T *pos = &posmatch->pos[i];
7944
7945 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007946 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007947 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007948 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007949 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007950 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007951 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007952 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007953 /* if this match comes before the one at "found" then swap
7954 * them */
7955 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007956 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007957 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007958
Bram Moolenaar85077472016-10-16 14:35:48 +02007959 *pos = posmatch->pos[found];
7960 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007961 }
7962 }
7963 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007964 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007965 }
7966 }
7967 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007968 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007969 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007970 colnr_T start = posmatch->pos[found].col == 0
7971 ? 0 : posmatch->pos[found].col - 1;
7972 colnr_T end = posmatch->pos[found].col == 0
7973 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007974
Bram Moolenaar85077472016-10-16 14:35:48 +02007975 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007976 shl->rm.startpos[0].lnum = 0;
7977 shl->rm.startpos[0].col = start;
7978 shl->rm.endpos[0].lnum = 0;
7979 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007980 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007981 posmatch->cur = found + 1;
7982 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007983 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007984 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007985}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007986#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007987
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007989screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990{
7991 attrentry_T *aep = NULL;
7992
7993 screen_attr = attr;
7994 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01007995#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 && termcap_active
7997#endif
7998 )
7999 {
8000#ifdef FEAT_GUI
8001 if (gui.in_use)
8002 {
8003 char buf[20];
8004
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008005 /* The GUI handles this internally. */
8006 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 OUT_STR(buf);
8008 }
8009 else
8010#endif
8011 {
8012 if (attr > HL_ALL) /* special HL attr. */
8013 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008014 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 aep = syn_cterm_attr2entry(attr);
8016 else
8017 aep = syn_term_attr2entry(attr);
8018 if (aep == NULL) /* did ":syntax clear" */
8019 attr = 0;
8020 else
8021 attr = aep->ae_attr;
8022 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008023 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008025 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008026#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008027 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8028 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8029 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008030#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008031 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008032 /* If the Normal FG color has BOLD attribute and the new HL
8033 * has a FG color defined, clear BOLD. */
8034 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008035 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008037 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008038 out_str(T_UCS);
8039 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008040 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8041 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008043 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008045 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008047 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008048 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049
8050 /*
8051 * Output the color or start string after bold etc., in case the
8052 * bold etc. override the color setting.
8053 */
8054 if (aep != NULL)
8055 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008056#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008057 /* When 'termguicolors' is set but fg or bg is unset,
8058 * fall back to the cterm colors. This helps for SpellBad,
8059 * where the GUI uses a red undercurl. */
8060 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008062 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008063 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008064 }
8065 else
8066#endif
8067 if (t_colors > 1)
8068 {
8069 if (aep->ae_u.cterm.fg_color)
8070 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8071 }
8072#ifdef FEAT_TERMGUICOLORS
8073 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8074 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008075 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008076 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 }
8078 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008079#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008080 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008082 if (aep->ae_u.cterm.bg_color)
8083 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8084 }
8085
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008086 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008087 {
8088 if (aep->ae_u.term.start != NULL)
8089 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 }
8091 }
8092 }
8093 }
8094}
8095
8096 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008097screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
8099 int do_ME = FALSE; /* output T_ME code */
8100
8101 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008102#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 && termcap_active
8104#endif
8105 )
8106 {
8107#ifdef FEAT_GUI
8108 if (gui.in_use)
8109 {
8110 char buf[20];
8111
8112 /* use internal GUI code */
8113 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8114 OUT_STR(buf);
8115 }
8116 else
8117#endif
8118 {
8119 if (screen_attr > HL_ALL) /* special HL attr. */
8120 {
8121 attrentry_T *aep;
8122
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008123 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {
8125 /*
8126 * Assume that t_me restores the original colors!
8127 */
8128 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008129 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008130#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008131 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8132 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8133 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008134#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008135 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008136#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008137 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8138 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8139 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008140#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008141 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 do_ME = TRUE;
8143 }
8144 else
8145 {
8146 aep = syn_term_attr2entry(screen_attr);
8147 if (aep != NULL && aep->ae_u.term.stop != NULL)
8148 {
8149 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8150 do_ME = TRUE;
8151 else
8152 out_str(aep->ae_u.term.stop);
8153 }
8154 }
8155 if (aep == NULL) /* did ":syntax clear" */
8156 screen_attr = 0;
8157 else
8158 screen_attr = aep->ae_attr;
8159 }
8160
8161 /*
8162 * Often all ending-codes are equal to T_ME. Avoid outputting the
8163 * same sequence several times.
8164 */
8165 if (screen_attr & HL_STANDOUT)
8166 {
8167 if (STRCMP(T_SE, T_ME) == 0)
8168 do_ME = TRUE;
8169 else
8170 out_str(T_SE);
8171 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008172 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008173 {
8174 if (STRCMP(T_UCE, T_ME) == 0)
8175 do_ME = TRUE;
8176 else
8177 out_str(T_UCE);
8178 }
8179 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008180 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {
8182 if (STRCMP(T_UE, T_ME) == 0)
8183 do_ME = TRUE;
8184 else
8185 out_str(T_UE);
8186 }
8187 if (screen_attr & HL_ITALIC)
8188 {
8189 if (STRCMP(T_CZR, T_ME) == 0)
8190 do_ME = TRUE;
8191 else
8192 out_str(T_CZR);
8193 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008194 if (screen_attr & HL_STRIKETHROUGH)
8195 {
8196 if (STRCMP(T_STE, T_ME) == 0)
8197 do_ME = TRUE;
8198 else
8199 out_str(T_STE);
8200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8202 out_str(T_ME);
8203
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008204#ifdef FEAT_TERMGUICOLORS
8205 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008207 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008208 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008209 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008210 term_bg_rgb_color(cterm_normal_bg_gui_color);
8211 }
8212 else
8213#endif
8214 {
8215 if (t_colors > 1)
8216 {
8217 /* set Normal cterm colors */
8218 if (cterm_normal_fg_color != 0)
8219 term_fg_color(cterm_normal_fg_color - 1);
8220 if (cterm_normal_bg_color != 0)
8221 term_bg_color(cterm_normal_bg_color - 1);
8222 if (cterm_normal_fg_bold)
8223 out_str(T_MD);
8224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 }
8226 }
8227 }
8228 screen_attr = 0;
8229}
8230
8231/*
8232 * Reset the colors for a cterm. Used when leaving Vim.
8233 * The machine specific code may override this again.
8234 */
8235 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008236reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008238 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {
8240 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008241#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008242 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8243 || cterm_normal_bg_gui_color != INVALCOLOR)
8244 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008245#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 {
8249 out_str(T_OP);
8250 screen_attr = -1;
8251 }
8252 if (cterm_normal_fg_bold)
8253 {
8254 out_str(T_ME);
8255 screen_attr = -1;
8256 }
8257 }
8258}
8259
8260/*
8261 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8262 * using the attributes from ScreenAttrs["off"].
8263 */
8264 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008265screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266{
8267 int attr;
8268
8269 /* Check for illegal values, just in case (could happen just after
8270 * resizing). */
8271 if (row >= screen_Rows || col >= screen_Columns)
8272 return;
8273
Bram Moolenaarae654382019-01-17 21:09:05 +01008274#ifdef FEAT_INS_EXPAND
8275 if (pum_under_menu(row, col))
8276 return;
8277#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008278 /* Outputting a character in the last cell on the screen may scroll the
8279 * screen up. Only do it when the "xn" termcap property is set, otherwise
8280 * mark the character invalid (update it when scrolled up). */
8281 if (*T_XN == NUL
8282 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283#ifdef FEAT_RIGHTLEFT
8284 /* account for first command-line character in rightleft mode */
8285 && !cmdmsg_rl
8286#endif
8287 )
8288 {
8289 ScreenAttrs[off] = (sattr_T)-1;
8290 return;
8291 }
8292
8293 /*
8294 * Stop highlighting first, so it's easier to move the cursor.
8295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 if (screen_char_attr != 0)
8297 attr = screen_char_attr;
8298 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 attr = ScreenAttrs[off];
8300 if (screen_attr != attr)
8301 screen_stop_highlight();
8302
8303 windgoto(row, col);
8304
8305 if (screen_attr != attr)
8306 screen_start_highlight(attr);
8307
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 if (enc_utf8 && ScreenLinesUC[off] != 0)
8309 {
8310 char_u buf[MB_MAXBYTES + 1];
8311
Bram Moolenaarcb070082016-04-02 22:14:51 +02008312 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008313 {
8314 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008315#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008316 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008317#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008318 )
8319 {
8320 /* Clear the two screen cells. If the character is actually
8321 * single width it won't change the second cell. */
8322 out_str((char_u *)" ");
8323 term_windgoto(row, col);
8324 }
8325 /* not sure where the cursor is after drawing the ambiguous width
8326 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008327 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008328 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008329 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008331
8332 /* Convert the UTF-8 character to bytes and write it. */
8333 buf[utfc_char2bytes(off, buf)] = NUL;
8334 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 }
8336 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 /* double-byte character in single-width cell */
8341 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8342 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 }
8344
8345 screen_cur_col++;
8346}
8347
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348/*
8349 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8350 * on the screen at position 'row' and 'col'.
8351 * The attributes of the first byte is used for all. This is required to
8352 * output the two bytes of a double-byte character with nothing in between.
8353 */
8354 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008355screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356{
8357 /* Check for illegal values (could be wrong when screen was resized). */
8358 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8359 return;
8360
8361 /* Outputting the last character on the screen may scrollup the screen.
8362 * Don't to it! Mark the character invalid (update it when scrolled up) */
8363 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8364 {
8365 ScreenAttrs[off] = (sattr_T)-1;
8366 return;
8367 }
8368
8369 /* Output the first byte normally (positions the cursor), then write the
8370 * second byte directly. */
8371 screen_char(off, row, col);
8372 out_char(ScreenLines[off + 1]);
8373 ++screen_cur_col;
8374}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376/*
8377 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8378 * This uses the contents of ScreenLines[] and doesn't change it.
8379 */
8380 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008381screen_draw_rectangle(
8382 int row,
8383 int col,
8384 int height,
8385 int width,
8386 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387{
8388 int r, c;
8389 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008390 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008392 /* Can't use ScreenLines unless initialized */
8393 if (ScreenLines == NULL)
8394 return;
8395
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 if (invert)
8397 screen_char_attr = HL_INVERSE;
8398 for (r = row; r < row + height; ++r)
8399 {
8400 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008401 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 for (c = col; c < col + width; ++c)
8403 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008404 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 {
8406 screen_char_2(off + c, r, c);
8407 ++c;
8408 }
8409 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {
8411 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008412 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 }
8415 }
8416 }
8417 screen_char_attr = 0;
8418}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420/*
8421 * Redraw the characters for a vertically split window.
8422 */
8423 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008424redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425{
8426 int col;
8427 int width;
8428
8429# ifdef FEAT_CLIPBOARD
8430 clip_may_clear_selection(row, end - 1);
8431# endif
8432
8433 if (wp == NULL)
8434 {
8435 col = 0;
8436 width = Columns;
8437 }
8438 else
8439 {
8440 col = wp->w_wincol;
8441 width = wp->w_width;
8442 }
8443 screen_draw_rectangle(row, col, end - row, width, FALSE);
8444}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008446 static void
8447space_to_screenline(int off, int attr)
8448{
8449 ScreenLines[off] = ' ';
8450 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008451 if (enc_utf8)
8452 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008453}
8454
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455/*
8456 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8457 * with character 'c1' in first column followed by 'c2' in the other columns.
8458 * Use attributes 'attr'.
8459 */
8460 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008461screen_fill(
8462 int start_row,
8463 int end_row,
8464 int start_col,
8465 int end_col,
8466 int c1,
8467 int c2,
8468 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469{
8470 int row;
8471 int col;
8472 int off;
8473 int end_off;
8474 int did_delete;
8475 int c;
8476 int norm_term;
8477#if defined(FEAT_GUI) || defined(UNIX)
8478 int force_next = FALSE;
8479#endif
8480
8481 if (end_row > screen_Rows) /* safety check */
8482 end_row = screen_Rows;
8483 if (end_col > screen_Columns) /* safety check */
8484 end_col = screen_Columns;
8485 if (ScreenLines == NULL
8486 || start_row >= end_row
8487 || start_col >= end_col) /* nothing to do */
8488 return;
8489
8490 /* it's a "normal" terminal when not in a GUI or cterm */
8491 norm_term = (
8492#ifdef FEAT_GUI
8493 !gui.in_use &&
8494#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008495 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 for (row = start_row; row < end_row; ++row)
8497 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008498 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008499#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008500 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008501#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008502 )
8503 {
8504 /* When drawing over the right halve of a double-wide char clear
8505 * out the left halve. When drawing over the left halve of a
8506 * double wide-char clear out the right halve. Only needed in a
8507 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008508 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008509 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008510 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008511 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 /*
8514 * Try to use delete-line termcap code, when no attributes or in a
8515 * "normal" terminal, where a bold/italic space is just a
8516 * space.
8517 */
8518 did_delete = FALSE;
8519 if (c2 == ' '
8520 && end_col == Columns
8521 && can_clear(T_CE)
8522 && (attr == 0
8523 || (norm_term
8524 && attr <= HL_ALL
8525 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8526 {
8527 /*
8528 * check if we really need to clear something
8529 */
8530 col = start_col;
8531 if (c1 != ' ') /* don't clear first char */
8532 ++col;
8533
8534 off = LineOffset[row] + col;
8535 end_off = LineOffset[row] + end_col;
8536
8537 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 if (enc_utf8)
8539 while (off < end_off && ScreenLines[off] == ' '
8540 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8541 ++off;
8542 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 while (off < end_off && ScreenLines[off] == ' '
8544 && ScreenAttrs[off] == 0)
8545 ++off;
8546 if (off < end_off) /* something to be cleared */
8547 {
8548 col = off - LineOffset[row];
8549 screen_stop_highlight();
8550 term_windgoto(row, col);/* clear rest of this screen line */
8551 out_str(T_CE);
8552 screen_start(); /* don't know where cursor is now */
8553 col = end_col - col;
8554 while (col--) /* clear chars in ScreenLines */
8555 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008556 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 ++off;
8558 }
8559 }
8560 did_delete = TRUE; /* the chars are cleared now */
8561 }
8562
8563 off = LineOffset[row] + start_col;
8564 c = c1;
8565 for (col = start_col; col < end_col; ++col)
8566 {
8567 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008568 || (enc_utf8 && (int)ScreenLinesUC[off]
8569 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 || ScreenAttrs[off] != attr
8571#if defined(FEAT_GUI) || defined(UNIX)
8572 || force_next
8573#endif
8574 )
8575 {
8576#if defined(FEAT_GUI) || defined(UNIX)
8577 /* The bold trick may make a single row of pixels appear in
8578 * the next character. When a bold character is removed, the
8579 * next character should be redrawn too. This happens for our
8580 * own GUI and for some xterms. */
8581 if (
8582# ifdef FEAT_GUI
8583 gui.in_use
8584# endif
8585# if defined(FEAT_GUI) && defined(UNIX)
8586 ||
8587# endif
8588# ifdef UNIX
8589 term_is_xterm
8590# endif
8591 )
8592 {
8593 if (ScreenLines[off] != ' '
8594 && (ScreenAttrs[off] > HL_ALL
8595 || ScreenAttrs[off] & HL_BOLD))
8596 force_next = TRUE;
8597 else
8598 force_next = FALSE;
8599 }
8600#endif
8601 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 if (enc_utf8)
8603 {
8604 if (c >= 0x80)
8605 {
8606 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008607 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 }
8609 else
8610 ScreenLinesUC[off] = 0;
8611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 ScreenAttrs[off] = attr;
8613 if (!did_delete || c != ' ')
8614 screen_char(off, row, col);
8615 }
8616 ++off;
8617 if (col == start_col)
8618 {
8619 if (did_delete)
8620 break;
8621 c = c2;
8622 }
8623 }
8624 if (end_col == Columns)
8625 LineWraps[row] = FALSE;
8626 if (row == Rows - 1) /* overwritten the command line */
8627 {
8628 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008629 if (start_col == 0 && end_col == Columns
8630 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008632 if (start_col == 0)
8633 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 }
8635 }
8636}
8637
8638/*
8639 * Check if there should be a delay. Used before clearing or redrawing the
8640 * screen or the command line.
8641 */
8642 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008643check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644{
8645 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8646 && !did_wait_return
8647 && emsg_silent == 0)
8648 {
8649 out_flush();
8650 ui_delay(1000L, TRUE);
8651 emsg_on_display = FALSE;
8652 if (check_msg_scroll)
8653 msg_scroll = FALSE;
8654 }
8655}
8656
8657/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008658 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8659 */
8660 static void
8661clear_TabPageIdxs(void)
8662{
8663 int scol;
8664
8665 for (scol = 0; scol < Columns; ++scol)
8666 TabPageIdxs[scol] = 0;
8667}
8668
8669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008671 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 * Returns TRUE if there is a valid screen to write to.
8673 * Returns FALSE when starting up and screen not initialized yet.
8674 */
8675 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008676screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008678 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 return (ScreenLines != NULL);
8680}
8681
8682/*
8683 * Resize the shell to Rows and Columns.
8684 * Allocate ScreenLines[] and associated items.
8685 *
8686 * There may be some time between setting Rows and Columns and (re)allocating
8687 * ScreenLines[]. This happens when starting up and when (manually) changing
8688 * the shell size. Always use screen_Rows and screen_Columns to access items
8689 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8690 * final size of the shell is needed.
8691 */
8692 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008693screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694{
8695 int new_row, old_row;
8696#ifdef FEAT_GUI
8697 int old_Rows;
8698#endif
8699 win_T *wp;
8700 int outofmem = FALSE;
8701 int len;
8702 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008704 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008706 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 sattr_T *new_ScreenAttrs;
8708 unsigned *new_LineOffset;
8709 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008710 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008711 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008713 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008714 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008716retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 /*
8718 * Allocation of the screen buffers is done only when the size changes and
8719 * when Rows and Columns have been set and we have started doing full
8720 * screen stuff.
8721 */
8722 if ((ScreenLines != NULL
8723 && Rows == screen_Rows
8724 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 && enc_utf8 == (ScreenLinesUC != NULL)
8726 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008727 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 || Rows == 0
8729 || Columns == 0
8730 || (!full_screen && ScreenLines == NULL))
8731 return;
8732
8733 /*
8734 * It's possible that we produce an out-of-memory message below, which
8735 * will cause this function to be called again. To break the loop, just
8736 * return here.
8737 */
8738 if (entered)
8739 return;
8740 entered = TRUE;
8741
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008742 /*
8743 * Note that the window sizes are updated before reallocating the arrays,
8744 * thus we must not redraw here!
8745 */
8746 ++RedrawingDisabled;
8747
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 win_new_shellsize(); /* fit the windows in the new sized shell */
8749
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 comp_col(); /* recompute columns for shown command and ruler */
8751
8752 /*
8753 * We're changing the size of the screen.
8754 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8755 * - Move lines from the old arrays into the new arrays, clear extra
8756 * lines (unless the screen is going to be cleared).
8757 * - Free the old arrays.
8758 *
8759 * If anything fails, make ScreenLines NULL, so we don't do anything!
8760 * Continuing with the old ScreenLines may result in a crash, because the
8761 * size is wrong.
8762 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008763 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008765 if (aucmd_win != NULL)
8766 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767
8768 new_ScreenLines = (schar_T *)lalloc((long_u)(
8769 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008770 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 if (enc_utf8)
8772 {
8773 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8774 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008775 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008776 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8778 }
8779 if (enc_dbcs == DBCS_JPNU)
8780 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8781 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8783 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8784 new_LineOffset = (unsigned *)lalloc((long_u)(
8785 Rows * sizeof(unsigned)), FALSE);
8786 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008787 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008789 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 {
8791 if (win_alloc_lines(wp) == FAIL)
8792 {
8793 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008794 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 }
8796 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008797 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8798 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008799 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008800give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008802 for (i = 0; i < p_mco; ++i)
8803 if (new_ScreenLinesC[i] == NULL)
8804 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008806 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 || new_ScreenAttrs == NULL
8809 || new_LineOffset == NULL
8810 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008811 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 || outofmem)
8813 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008814 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008815 {
8816 /* guess the size */
8817 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8818
8819 /* Remember we did this to avoid getting outofmem messages over
8820 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008821 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008822 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008823 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008824 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008825 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008826 VIM_CLEAR(new_ScreenLinesC[i]);
8827 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008828 VIM_CLEAR(new_ScreenAttrs);
8829 VIM_CLEAR(new_LineOffset);
8830 VIM_CLEAR(new_LineWraps);
8831 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 }
8833 else
8834 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008835 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008836
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 for (new_row = 0; new_row < Rows; ++new_row)
8838 {
8839 new_LineOffset[new_row] = new_row * Columns;
8840 new_LineWraps[new_row] = FALSE;
8841
8842 /*
8843 * If the screen is not going to be cleared, copy as much as
8844 * possible from the old screen to the new one and clear the rest
8845 * (used when resizing the window at the "--more--" prompt or when
8846 * executing an external command, for the GUI).
8847 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008848 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 {
8850 (void)vim_memset(new_ScreenLines + new_row * Columns,
8851 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 if (enc_utf8)
8853 {
8854 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8855 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008856 for (i = 0; i < p_mco; ++i)
8857 (void)vim_memset(new_ScreenLinesC[i]
8858 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 0, (size_t)Columns * sizeof(u8char_T));
8860 }
8861 if (enc_dbcs == DBCS_JPNU)
8862 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8863 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8865 0, (size_t)Columns * sizeof(sattr_T));
8866 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008867 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 {
8869 if (screen_Columns < Columns)
8870 len = screen_Columns;
8871 else
8872 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008873 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008874 * may be invalid now. Also when p_mco changes. */
8875 if (!(enc_utf8 && ScreenLinesUC == NULL)
8876 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008877 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8878 ScreenLines + LineOffset[old_row],
8879 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008880 if (enc_utf8 && ScreenLinesUC != NULL
8881 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 {
8883 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8884 ScreenLinesUC + LineOffset[old_row],
8885 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008886 for (i = 0; i < p_mco; ++i)
8887 mch_memmove(new_ScreenLinesC[i]
8888 + new_LineOffset[new_row],
8889 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 (size_t)len * sizeof(u8char_T));
8891 }
8892 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8893 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8894 ScreenLines2 + LineOffset[old_row],
8895 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8897 ScreenAttrs + LineOffset[old_row],
8898 (size_t)len * sizeof(sattr_T));
8899 }
8900 }
8901 }
8902 /* Use the last line of the screen for the current line. */
8903 current_ScreenLine = new_ScreenLines + Rows * Columns;
8904 }
8905
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008906 free_screenlines();
8907
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008910 for (i = 0; i < p_mco; ++i)
8911 ScreenLinesC[i] = new_ScreenLinesC[i];
8912 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 ScreenAttrs = new_ScreenAttrs;
8915 LineOffset = new_LineOffset;
8916 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008917 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918
8919 /* It's important that screen_Rows and screen_Columns reflect the actual
8920 * size of ScreenLines[]. Set them before calling anything. */
8921#ifdef FEAT_GUI
8922 old_Rows = screen_Rows;
8923#endif
8924 screen_Rows = Rows;
8925 screen_Columns = Columns;
8926
8927 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008928 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930#ifdef FEAT_GUI
8931 else if (gui.in_use
8932 && !gui.starting
8933 && ScreenLines != NULL
8934 && old_Rows != Rows)
8935 {
8936 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8937 /*
8938 * Adjust the position of the cursor, for when executing an external
8939 * command.
8940 */
8941 if (msg_row >= Rows) /* Rows got smaller */
8942 msg_row = Rows - 1; /* put cursor at last row */
8943 else if (Rows > old_Rows) /* Rows got bigger */
8944 msg_row += Rows - old_Rows; /* put cursor in same place */
8945 if (msg_col >= Columns) /* Columns got smaller */
8946 msg_col = Columns - 1; /* put cursor at last column */
8947 }
8948#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008949 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008952 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008953
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008954 /*
8955 * Do not apply autocommands more than 3 times to avoid an endless loop
8956 * in case applying autocommands always changes Rows or Columns.
8957 */
8958 if (starting == 0 && ++retry_count <= 3)
8959 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008960 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008961 /* In rare cases, autocommands may have altered Rows or Columns,
8962 * jump back to check if we need to allocate the screen again. */
8963 goto retry;
8964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965}
8966
8967 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008968free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008969{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008970 int i;
8971
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008972 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008973 for (i = 0; i < Screen_mco; ++i)
8974 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008975 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008976 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008977 vim_free(ScreenAttrs);
8978 vim_free(LineOffset);
8979 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008980 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008981}
8982
8983 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008984screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985{
8986 check_for_delay(FALSE);
8987 screenalloc(FALSE); /* allocate screen buffers if size changed */
8988 screenclear2(); /* clear the screen */
8989}
8990
8991 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008992screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993{
8994 int i;
8995
8996 if (starting == NO_SCREEN || ScreenLines == NULL
8997#ifdef FEAT_GUI
8998 || (gui.in_use && gui.starting)
8999#endif
9000 )
9001 return;
9002
9003#ifdef FEAT_GUI
9004 if (!gui.in_use)
9005#endif
9006 screen_attr = -1; /* force setting the Normal colors */
9007 screen_stop_highlight(); /* don't want highlighting here */
9008
9009#ifdef FEAT_CLIPBOARD
9010 /* disable selection without redrawing it */
9011 clip_scroll_selection(9999);
9012#endif
9013
9014 /* blank out ScreenLines */
9015 for (i = 0; i < Rows; ++i)
9016 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009017 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 LineWraps[i] = FALSE;
9019 }
9020
9021 if (can_clear(T_CL))
9022 {
9023 out_str(T_CL); /* clear the display */
9024 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009025 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 }
9027 else
9028 {
9029 /* can't clear the screen, mark all chars with invalid attributes */
9030 for (i = 0; i < Rows; ++i)
9031 lineinvalid(LineOffset[i], (int)Columns);
9032 clear_cmdline = TRUE;
9033 }
9034
9035 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9036
9037 win_rest_invalid(firstwin);
9038 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009039 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 if (must_redraw == CLEAR) /* no need to clear again */
9041 must_redraw = NOT_VALID;
9042 compute_cmdrow();
9043 msg_row = cmdline_row; /* put cursor on last line for messages */
9044 msg_col = 0;
9045 screen_start(); /* don't know where cursor is now */
9046 msg_scrolled = 0; /* can't scroll back */
9047 msg_didany = FALSE;
9048 msg_didout = FALSE;
9049}
9050
9051/*
9052 * Clear one line in ScreenLines.
9053 */
9054 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009055lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056{
9057 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 if (enc_utf8)
9059 (void)vim_memset(ScreenLinesUC + off, 0,
9060 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009061 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062}
9063
9064/*
9065 * Mark one line in ScreenLines invalid by setting the attributes to an
9066 * invalid value.
9067 */
9068 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009069lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070{
9071 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9072}
9073
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074/*
9075 * Copy part of a Screenline for vertically split window "wp".
9076 */
9077 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009078linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079{
9080 unsigned off_to = LineOffset[to] + wp->w_wincol;
9081 unsigned off_from = LineOffset[from] + wp->w_wincol;
9082
9083 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9084 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 if (enc_utf8)
9086 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009087 int i;
9088
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9090 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009091 for (i = 0; i < p_mco; ++i)
9092 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9093 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 }
9095 if (enc_dbcs == DBCS_JPNU)
9096 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9097 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9099 wp->w_width * sizeof(sattr_T));
9100}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101
9102/*
9103 * Return TRUE if clearing with term string "p" would work.
9104 * It can't work when the string is empty or it won't set the right background.
9105 */
9106 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009107can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108{
9109 return (*p != NUL && (t_colors <= 1
9110#ifdef FEAT_GUI
9111 || gui.in_use
9112#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009113#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009114 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009115 || (!p_tgc && cterm_normal_bg_color == 0)
9116#else
9117 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009118#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009119 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120}
9121
9122/*
9123 * Reset cursor position. Use whenever cursor was moved because of outputting
9124 * something directly to the screen (shell commands) or a terminal control
9125 * code.
9126 */
9127 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009128screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129{
9130 screen_cur_row = screen_cur_col = 9999;
9131}
9132
9133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 * Move the cursor to position "row","col" in the screen.
9135 * This tries to find the most efficient way to move, minimizing the number of
9136 * characters sent to the terminal.
9137 */
9138 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009139windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009141 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 int i;
9143 int plan;
9144 int cost;
9145 int wouldbe_col;
9146 int noinvcurs;
9147 char_u *bs;
9148 int goto_cost;
9149 int attr;
9150
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009151#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9153
9154#define PLAN_LE 1
9155#define PLAN_CR 2
9156#define PLAN_NL 3
9157#define PLAN_WRITE 4
9158 /* Can't use ScreenLines unless initialized */
9159 if (ScreenLines == NULL)
9160 return;
9161
9162 if (col != screen_cur_col || row != screen_cur_row)
9163 {
9164 /* Check for valid position. */
9165 if (row < 0) /* window without text lines? */
9166 row = 0;
9167 if (row >= screen_Rows)
9168 row = screen_Rows - 1;
9169 if (col >= screen_Columns)
9170 col = screen_Columns - 1;
9171
9172 /* check if no cursor movement is allowed in highlight mode */
9173 if (screen_attr && *T_MS == NUL)
9174 noinvcurs = HIGHL_COST;
9175 else
9176 noinvcurs = 0;
9177 goto_cost = GOTO_COST + noinvcurs;
9178
9179 /*
9180 * Plan how to do the positioning:
9181 * 1. Use CR to move it to column 0, same row.
9182 * 2. Use T_LE to move it a few columns to the left.
9183 * 3. Use NL to move a few lines down, column 0.
9184 * 4. Move a few columns to the right with T_ND or by writing chars.
9185 *
9186 * Don't do this if the cursor went beyond the last column, the cursor
9187 * position is unknown then (some terminals wrap, some don't )
9188 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009189 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 * characters to move the cursor to the right.
9191 */
9192 if (row >= screen_cur_row && screen_cur_col < Columns)
9193 {
9194 /*
9195 * If the cursor is in the same row, bigger col, we can use CR
9196 * or T_LE.
9197 */
9198 bs = NULL; /* init for GCC */
9199 attr = screen_attr;
9200 if (row == screen_cur_row && col < screen_cur_col)
9201 {
9202 /* "le" is preferred over "bc", because "bc" is obsolete */
9203 if (*T_LE)
9204 bs = T_LE; /* "cursor left" */
9205 else
9206 bs = T_BC; /* "backspace character (old) */
9207 if (*bs)
9208 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9209 else
9210 cost = 999;
9211 if (col + 1 < cost) /* using CR is less characters */
9212 {
9213 plan = PLAN_CR;
9214 wouldbe_col = 0;
9215 cost = 1; /* CR is just one character */
9216 }
9217 else
9218 {
9219 plan = PLAN_LE;
9220 wouldbe_col = col;
9221 }
9222 if (noinvcurs) /* will stop highlighting */
9223 {
9224 cost += noinvcurs;
9225 attr = 0;
9226 }
9227 }
9228
9229 /*
9230 * If the cursor is above where we want to be, we can use CR LF.
9231 */
9232 else if (row > screen_cur_row)
9233 {
9234 plan = PLAN_NL;
9235 wouldbe_col = 0;
9236 cost = (row - screen_cur_row) * 2; /* CR LF */
9237 if (noinvcurs) /* will stop highlighting */
9238 {
9239 cost += noinvcurs;
9240 attr = 0;
9241 }
9242 }
9243
9244 /*
9245 * If the cursor is in the same row, smaller col, just use write.
9246 */
9247 else
9248 {
9249 plan = PLAN_WRITE;
9250 wouldbe_col = screen_cur_col;
9251 cost = 0;
9252 }
9253
9254 /*
9255 * Check if any characters that need to be written have the
9256 * correct attributes. Also avoid UTF-8 characters.
9257 */
9258 i = col - wouldbe_col;
9259 if (i > 0)
9260 cost += i;
9261 if (cost < goto_cost && i > 0)
9262 {
9263 /*
9264 * Check if the attributes are correct without additionally
9265 * stopping highlighting.
9266 */
9267 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9268 while (i && *p++ == attr)
9269 --i;
9270 if (i != 0)
9271 {
9272 /*
9273 * Try if it works when highlighting is stopped here.
9274 */
9275 if (*--p == 0)
9276 {
9277 cost += noinvcurs;
9278 while (i && *p++ == 0)
9279 --i;
9280 }
9281 if (i != 0)
9282 cost = 999; /* different attributes, don't do it */
9283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 if (enc_utf8)
9285 {
9286 /* Don't use an UTF-8 char for positioning, it's slow. */
9287 for (i = wouldbe_col; i < col; ++i)
9288 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9289 {
9290 cost = 999;
9291 break;
9292 }
9293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 }
9295
9296 /*
9297 * We can do it without term_windgoto()!
9298 */
9299 if (cost < goto_cost)
9300 {
9301 if (plan == PLAN_LE)
9302 {
9303 if (noinvcurs)
9304 screen_stop_highlight();
9305 while (screen_cur_col > col)
9306 {
9307 out_str(bs);
9308 --screen_cur_col;
9309 }
9310 }
9311 else if (plan == PLAN_CR)
9312 {
9313 if (noinvcurs)
9314 screen_stop_highlight();
9315 out_char('\r');
9316 screen_cur_col = 0;
9317 }
9318 else if (plan == PLAN_NL)
9319 {
9320 if (noinvcurs)
9321 screen_stop_highlight();
9322 while (screen_cur_row < row)
9323 {
9324 out_char('\n');
9325 ++screen_cur_row;
9326 }
9327 screen_cur_col = 0;
9328 }
9329
9330 i = col - screen_cur_col;
9331 if (i > 0)
9332 {
9333 /*
9334 * Use cursor-right if it's one character only. Avoids
9335 * removing a line of pixels from the last bold char, when
9336 * using the bold trick in the GUI.
9337 */
9338 if (T_ND[0] != NUL && T_ND[1] == NUL)
9339 {
9340 while (i-- > 0)
9341 out_char(*T_ND);
9342 }
9343 else
9344 {
9345 int off;
9346
9347 off = LineOffset[row] + screen_cur_col;
9348 while (i-- > 0)
9349 {
9350 if (ScreenAttrs[off] != screen_attr)
9351 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 if (enc_dbcs == DBCS_JPNU
9355 && ScreenLines[off] == 0x8e)
9356 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 ++off;
9358 }
9359 }
9360 }
9361 }
9362 }
9363 else
9364 cost = 999;
9365
9366 if (cost >= goto_cost)
9367 {
9368 if (noinvcurs)
9369 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009370 if (row == screen_cur_row && (col > screen_cur_col)
9371 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 term_cursor_right(col - screen_cur_col);
9373 else
9374 term_windgoto(row, col);
9375 }
9376 screen_cur_row = row;
9377 screen_cur_col = col;
9378 }
9379}
9380
9381/*
9382 * Set cursor to its position in the current window.
9383 */
9384 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009385setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009387 setcursor_mayforce(FALSE);
9388}
9389
9390/*
9391 * Set cursor to its position in the current window.
9392 * When "force" is TRUE also when not redrawing.
9393 */
9394 void
9395setcursor_mayforce(int force)
9396{
9397 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 {
9399 validate_cursor();
9400 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009401 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009403 /* With 'rightleft' set and the cursor on a double-wide
9404 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009405 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9406 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009407 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009408 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409#endif
9410 curwin->w_wcol));
9411 }
9412}
9413
9414
9415/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009416 * Insert 'line_count' lines at 'row' in window 'wp'.
9417 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9418 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 * scrolling.
9420 * Returns FAIL if the lines are not inserted, OK for success.
9421 */
9422 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009423win_ins_lines(
9424 win_T *wp,
9425 int row,
9426 int line_count,
9427 int invalid,
9428 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429{
9430 int did_delete;
9431 int nextrow;
9432 int lastrow;
9433 int retval;
9434
9435 if (invalid)
9436 wp->w_lines_valid = 0;
9437
9438 if (wp->w_height < 5)
9439 return FAIL;
9440
9441 if (line_count > wp->w_height - row)
9442 line_count = wp->w_height - row;
9443
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009444 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 if (retval != MAYBE)
9446 return retval;
9447
9448 /*
9449 * If there is a next window or a status line, we first try to delete the
9450 * lines at the bottom to avoid messing what is after the window.
9451 * If this fails and there are following windows, don't do anything to avoid
9452 * messing up those windows, better just redraw.
9453 */
9454 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 if (wp->w_next != NULL || wp->w_status_height)
9456 {
9457 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009458 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 did_delete = TRUE;
9460 else if (wp->w_next)
9461 return FAIL;
9462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 /*
9464 * if no lines deleted, blank the lines that will end up below the window
9465 */
9466 if (!did_delete)
9467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009470 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 lastrow = nextrow + line_count;
9472 if (lastrow > Rows)
9473 lastrow = Rows;
9474 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009475 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 ' ', ' ', 0);
9477 }
9478
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009479 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 == FAIL)
9481 {
9482 /* deletion will have messed up other windows */
9483 if (did_delete)
9484 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 win_rest_invalid(W_NEXT(wp));
9487 }
9488 return FAIL;
9489 }
9490
9491 return OK;
9492}
9493
9494/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009495 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9497 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9498 * scrolling
9499 * Return OK for success, FAIL if the lines are not deleted.
9500 */
9501 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009502win_del_lines(
9503 win_T *wp,
9504 int row,
9505 int line_count,
9506 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009507 int mayclear,
9508 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509{
9510 int retval;
9511
9512 if (invalid)
9513 wp->w_lines_valid = 0;
9514
9515 if (line_count > wp->w_height - row)
9516 line_count = wp->w_height - row;
9517
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009518 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 if (retval != MAYBE)
9520 return retval;
9521
9522 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009523 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 return FAIL;
9525
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 /*
9527 * If there are windows or status lines below, try to put them at the
9528 * correct place. If we can't do that, they have to be redrawn.
9529 */
9530 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9531 {
9532 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009533 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 {
9535 wp->w_redr_status = TRUE;
9536 win_rest_invalid(wp->w_next);
9537 }
9538 }
9539 /*
9540 * If this is the last window and there is no status line, redraw the
9541 * command line later.
9542 */
9543 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 redraw_cmdline = TRUE;
9545 return OK;
9546}
9547
9548/*
9549 * Common code for win_ins_lines() and win_del_lines().
9550 * Returns OK or FAIL when the work has been done.
9551 * Returns MAYBE when not finished yet.
9552 */
9553 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009554win_do_lines(
9555 win_T *wp,
9556 int row,
9557 int line_count,
9558 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009559 int del,
9560 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561{
9562 int retval;
9563
9564 if (!redrawing() || line_count <= 0)
9565 return FAIL;
9566
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009567 /* When inserting lines would result in loss of command output, just redraw
9568 * the lines. */
9569 if (no_win_do_lines_ins && !del)
9570 return FAIL;
9571
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009573 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009575 if (!no_win_do_lines_ins)
9576 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 return FAIL;
9578 }
9579
9580 /*
9581 * Delete all remaining lines
9582 */
9583 if (row + line_count >= wp->w_height)
9584 {
9585 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009586 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 ' ', ' ', 0);
9588 return OK;
9589 }
9590
9591 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009592 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009594 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009596 if (!no_win_do_lines_ins)
9597 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598
9599 /*
9600 * If the terminal can set a scroll region, use that.
9601 * Always do this in a vertically split window. This will redraw from
9602 * ScreenLines[] when t_CV isn't defined. That's faster than using
9603 * win_line().
9604 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009605 * a character in the lower right corner of the scroll region may cause a
9606 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009608 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 scroll_region_set(wp, row);
9612 if (del)
9613 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009614 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 else
9616 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009617 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 scroll_region_reset();
9620 return retval;
9621 }
9622
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9624 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625
9626 return MAYBE;
9627}
9628
9629/*
9630 * window 'wp' and everything after it is messed up, mark it for redraw
9631 */
9632 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009633win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 {
9637 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 wp->w_redr_status = TRUE;
9639 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 }
9641 redraw_cmdline = TRUE;
9642}
9643
9644/*
9645 * The rest of the routines in this file perform screen manipulations. The
9646 * given operation is performed physically on the screen. The corresponding
9647 * change is also made to the internal screen image. In this way, the editor
9648 * anticipates the effect of editing changes on the appearance of the screen.
9649 * That way, when we call screenupdate a complete redraw isn't usually
9650 * necessary. Another advantage is that we can keep adding code to anticipate
9651 * screen changes, and in the meantime, everything still works.
9652 */
9653
9654/*
9655 * types for inserting or deleting lines
9656 */
9657#define USE_T_CAL 1
9658#define USE_T_CDL 2
9659#define USE_T_AL 3
9660#define USE_T_CE 4
9661#define USE_T_DL 5
9662#define USE_T_SR 6
9663#define USE_NL 7
9664#define USE_T_CD 8
9665#define USE_REDRAW 9
9666
9667/*
9668 * insert lines on the screen and update ScreenLines[]
9669 * 'end' is the line after the scrolled part. Normally it is Rows.
9670 * When scrolling region used 'off' is the offset from the top for the region.
9671 * 'row' and 'end' are relative to the start of the region.
9672 *
9673 * return FAIL for failure, OK for success.
9674 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009675 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009676screen_ins_lines(
9677 int off,
9678 int row,
9679 int line_count,
9680 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009681 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009682 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683{
9684 int i;
9685 int j;
9686 unsigned temp;
9687 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009688 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 int type;
9690 int result_empty;
9691 int can_ce = can_clear(T_CE);
9692
9693 /*
9694 * FAIL if
9695 * - there is no valid screen
9696 * - the screen has to be redrawn completely
9697 * - the line count is less than one
9698 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009699 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009701 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9702#ifdef FEAT_CLIPBOARD
9703 || (clip_star.state != SELECT_CLEARED
9704 && redrawing_for_callback > 0)
9705#endif
9706 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 return FAIL;
9708
9709 /*
9710 * There are seven ways to insert lines:
9711 * 0. When in a vertically split window and t_CV isn't set, redraw the
9712 * characters from ScreenLines[].
9713 * 1. Use T_CD (clear to end of display) if it exists and the result of
9714 * the insert is just empty lines
9715 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9716 * present or line_count > 1. It looks better if we do all the inserts
9717 * at once.
9718 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9719 * insert is just empty lines and T_CE is not present or line_count >
9720 * 1.
9721 * 4. Use T_AL (insert line) if it exists.
9722 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9723 * just empty lines.
9724 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9725 * just empty lines.
9726 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9727 * the 'da' flag is not set or we have clear line capability.
9728 * 8. redraw the characters from ScreenLines[].
9729 *
9730 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9731 * the scrollbar for the window. It does have insert line, use that if it
9732 * exists.
9733 */
9734 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9736 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009737 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 type = USE_T_CD;
9739 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9740 type = USE_T_CAL;
9741 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9742 type = USE_T_CDL;
9743 else if (*T_AL != NUL)
9744 type = USE_T_AL;
9745 else if (can_ce && result_empty)
9746 type = USE_T_CE;
9747 else if (*T_DL != NUL && result_empty)
9748 type = USE_T_DL;
9749 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9750 type = USE_T_SR;
9751 else
9752 return FAIL;
9753
9754 /*
9755 * For clearing the lines screen_del_lines() is used. This will also take
9756 * care of t_db if necessary.
9757 */
9758 if (type == USE_T_CD || type == USE_T_CDL ||
9759 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009760 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761
9762 /*
9763 * If text is retained below the screen, first clear or delete as many
9764 * lines at the bottom of the window as are about to be inserted so that
9765 * the deleted lines won't later surface during a screen_del_lines.
9766 */
9767 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009768 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769
9770#ifdef FEAT_CLIPBOARD
9771 /* Remove a modeless selection when inserting lines halfway the screen
9772 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009773 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009774 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 else
9776 clip_scroll_selection(-line_count);
9777#endif
9778
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779#ifdef FEAT_GUI
9780 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9781 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009782 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783#endif
9784
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009785 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9786 cursor_col = wp->w_wincol;
9787
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 if (*T_CCS != NUL) /* cursor relative to region */
9789 cursor_row = row;
9790 else
9791 cursor_row = row + off;
9792
9793 /*
9794 * Shift LineOffset[] line_count down to reflect the inserted lines.
9795 * Clear the inserted lines in ScreenLines[].
9796 */
9797 row += off;
9798 end += off;
9799 for (i = 0; i < line_count; ++i)
9800 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 if (wp != NULL && wp->w_width != Columns)
9802 {
9803 /* need to copy part of a line */
9804 j = end - 1 - i;
9805 while ((j -= line_count) >= row)
9806 linecopy(j + line_count, j, wp);
9807 j += line_count;
9808 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009809 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9810 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 else
9812 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9813 LineWraps[j] = FALSE;
9814 }
9815 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 {
9817 j = end - 1 - i;
9818 temp = LineOffset[j];
9819 while ((j -= line_count) >= row)
9820 {
9821 LineOffset[j + line_count] = LineOffset[j];
9822 LineWraps[j + line_count] = LineWraps[j];
9823 }
9824 LineOffset[j + line_count] = temp;
9825 LineWraps[j + line_count] = FALSE;
9826 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009827 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 else
9829 lineinvalid(temp, (int)Columns);
9830 }
9831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832
9833 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009834 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009835 if (clear_attr != 0)
9836 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 /* redraw the characters */
9839 if (type == USE_REDRAW)
9840 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009841 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 {
9843 term_append_lines(line_count);
9844 screen_start(); /* don't know where cursor is now */
9845 }
9846 else
9847 {
9848 for (i = 0; i < line_count; i++)
9849 {
9850 if (type == USE_T_AL)
9851 {
9852 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009853 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 out_str(T_AL);
9855 }
9856 else /* type == USE_T_SR */
9857 out_str(T_SR);
9858 screen_start(); /* don't know where cursor is now */
9859 }
9860 }
9861
9862 /*
9863 * With scroll-reverse and 'da' flag set we need to clear the lines that
9864 * have been scrolled down into the region.
9865 */
9866 if (type == USE_T_SR && *T_DA)
9867 {
9868 for (i = 0; i < line_count; ++i)
9869 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009870 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 out_str(T_CE);
9872 screen_start(); /* don't know where cursor is now */
9873 }
9874 }
9875
9876#ifdef FEAT_GUI
9877 gui_can_update_cursor();
9878 if (gui.in_use)
9879 out_flush(); /* always flush after a scroll */
9880#endif
9881 return OK;
9882}
9883
9884/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009885 * Delete lines on the screen and update ScreenLines[].
9886 * "end" is the line after the scrolled part. Normally it is Rows.
9887 * When scrolling region used "off" is the offset from the top for the region.
9888 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 *
9890 * Return OK for success, FAIL if the lines are not deleted.
9891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009893screen_del_lines(
9894 int off,
9895 int row,
9896 int line_count,
9897 int end,
9898 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009899 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009900 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901{
9902 int j;
9903 int i;
9904 unsigned temp;
9905 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009906 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 int cursor_end;
9908 int result_empty; /* result is empty until end of region */
9909 int can_delete; /* deleting line codes can be used */
9910 int type;
9911
9912 /*
9913 * FAIL if
9914 * - there is no valid screen
9915 * - the screen has to be redrawn completely
9916 * - the line count is less than one
9917 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009918 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009920 if (!screen_valid(TRUE) || line_count <= 0
9921 || (!force && line_count > p_ttyscroll)
9922#ifdef FEAT_CLIPBOARD
9923 || (clip_star.state != SELECT_CLEARED
9924 && redrawing_for_callback > 0)
9925#endif
9926 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 return FAIL;
9928
9929 /*
9930 * Check if the rest of the current region will become empty.
9931 */
9932 result_empty = row + line_count >= end;
9933
9934 /*
9935 * We can delete lines only when 'db' flag not set or when 'ce' option
9936 * available.
9937 */
9938 can_delete = (*T_DB == NUL || can_clear(T_CE));
9939
9940 /*
9941 * There are six ways to delete lines:
9942 * 0. When in a vertically split window and t_CV isn't set, redraw the
9943 * characters from ScreenLines[].
9944 * 1. Use T_CD if it exists and the result is empty.
9945 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9946 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9947 * none of the other ways work.
9948 * 4. Use T_CE (erase line) if the result is empty.
9949 * 5. Use T_DL (delete line) if it exists.
9950 * 6. redraw the characters from ScreenLines[].
9951 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9953 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009954 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 type = USE_T_CD;
9956#if defined(__BEOS__) && defined(BEOS_DR8)
9957 /*
9958 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9959 * its internal termcap... this works okay for tests which test *T_DB !=
9960 * NUL. It has the disadvantage that the user cannot use any :set t_*
9961 * command to get T_DB (back) to empty_option, only :set term=... will do
9962 * the trick...
9963 * Anyway, this hack will hopefully go away with the next OS release.
9964 * (Olaf Seibert)
9965 */
9966 else if (row == 0 && T_DB == empty_option
9967 && (line_count == 1 || *T_CDL == NUL))
9968#else
9969 else if (row == 0 && (
9970#ifndef AMIGA
9971 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9972 * up, so use delete-line command */
9973 line_count == 1 ||
9974#endif
9975 *T_CDL == NUL))
9976#endif
9977 type = USE_NL;
9978 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9979 type = USE_T_CDL;
9980 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02009981 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 type = USE_T_CE;
9983 else if (*T_DL != NUL && can_delete)
9984 type = USE_T_DL;
9985 else if (*T_CDL != NUL && can_delete)
9986 type = USE_T_CDL;
9987 else
9988 return FAIL;
9989
9990#ifdef FEAT_CLIPBOARD
9991 /* Remove a modeless selection when deleting lines halfway the screen or
9992 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009993 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009994 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 else
9996 clip_scroll_selection(line_count);
9997#endif
9998
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999#ifdef FEAT_GUI
10000 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10001 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010002 gui_dont_update_cursor(gui.cursor_row >= row + off
10003 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004#endif
10005
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010006 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10007 cursor_col = wp->w_wincol;
10008
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 if (*T_CCS != NUL) /* cursor relative to region */
10010 {
10011 cursor_row = row;
10012 cursor_end = end;
10013 }
10014 else
10015 {
10016 cursor_row = row + off;
10017 cursor_end = end + off;
10018 }
10019
10020 /*
10021 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10022 * Clear the inserted lines in ScreenLines[].
10023 */
10024 row += off;
10025 end += off;
10026 for (i = 0; i < line_count; ++i)
10027 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 if (wp != NULL && wp->w_width != Columns)
10029 {
10030 /* need to copy part of a line */
10031 j = row + i;
10032 while ((j += line_count) <= end - 1)
10033 linecopy(j - line_count, j, wp);
10034 j -= line_count;
10035 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010036 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10037 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 else
10039 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10040 LineWraps[j] = FALSE;
10041 }
10042 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 {
10044 /* whole width, moving the line pointers is faster */
10045 j = row + i;
10046 temp = LineOffset[j];
10047 while ((j += line_count) <= end - 1)
10048 {
10049 LineOffset[j - line_count] = LineOffset[j];
10050 LineWraps[j - line_count] = LineWraps[j];
10051 }
10052 LineOffset[j - line_count] = temp;
10053 LineWraps[j - line_count] = FALSE;
10054 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010055 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 else
10057 lineinvalid(temp, (int)Columns);
10058 }
10059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010061 if (screen_attr != clear_attr)
10062 screen_stop_highlight();
10063 if (clear_attr != 0)
10064 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 /* redraw the characters */
10067 if (type == USE_REDRAW)
10068 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010069 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010071 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 out_str(T_CD);
10073 screen_start(); /* don't know where cursor is now */
10074 }
10075 else if (type == USE_T_CDL)
10076 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010077 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 term_delete_lines(line_count);
10079 screen_start(); /* don't know where cursor is now */
10080 }
10081 /*
10082 * Deleting lines at top of the screen or scroll region: Just scroll
10083 * the whole screen (scroll region) up by outputting newlines on the
10084 * last line.
10085 */
10086 else if (type == USE_NL)
10087 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010088 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 for (i = line_count; --i >= 0; )
10090 out_char('\n'); /* cursor will remain on same line */
10091 }
10092 else
10093 {
10094 for (i = line_count; --i >= 0; )
10095 {
10096 if (type == USE_T_DL)
10097 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010098 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 out_str(T_DL); /* delete a line */
10100 }
10101 else /* type == USE_T_CE */
10102 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010103 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 out_str(T_CE); /* erase a line */
10105 }
10106 screen_start(); /* don't know where cursor is now */
10107 }
10108 }
10109
10110 /*
10111 * If the 'db' flag is set, we need to clear the lines that have been
10112 * scrolled up at the bottom of the region.
10113 */
10114 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10115 {
10116 for (i = line_count; i > 0; --i)
10117 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010118 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 out_str(T_CE); /* erase a line */
10120 screen_start(); /* don't know where cursor is now */
10121 }
10122 }
10123
10124#ifdef FEAT_GUI
10125 gui_can_update_cursor();
10126 if (gui.in_use)
10127 out_flush(); /* always flush after a scroll */
10128#endif
10129
10130 return OK;
10131}
10132
10133/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010134 * Return TRUE when postponing displaying the mode message: when not redrawing
10135 * or inside a mapping.
10136 */
10137 int
10138skip_showmode()
10139{
10140 // Call char_avail() only when we are going to show something, because it
10141 // takes a bit of time. redrawing() may also call char_avail_avail().
10142 if (global_busy
10143 || msg_silent != 0
10144 || !redrawing()
10145 || (char_avail() && !KeyTyped))
10146 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010147 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010148 return TRUE;
10149 }
10150 return FALSE;
10151}
10152
10153/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010154 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 *
10156 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10157 * If clear_cmdline is FALSE there may be a message there that needs to be
10158 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010159 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 * Return the length of the message (0 if no message).
10161 */
10162 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010163showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164{
10165 int need_clear;
10166 int length = 0;
10167 int do_mode;
10168 int attr;
10169 int nwr_save;
10170#ifdef FEAT_INS_EXPAND
10171 int sub_attr;
10172#endif
10173
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010174 do_mode = ((p_smd && msg_silent == 0)
10175 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010176 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010177 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010178 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010180 if (skip_showmode())
10181 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182
10183 nwr_save = need_wait_return;
10184
10185 /* wait a bit before overwriting an important message */
10186 check_for_delay(FALSE);
10187
10188 /* if the cmdline is more than one line high, erase top lines */
10189 need_clear = clear_cmdline;
10190 if (clear_cmdline && cmdline_row < Rows - 1)
10191 msg_clr_cmdline(); /* will reset clear_cmdline */
10192
10193 /* Position on the last line in the window, column 0 */
10194 msg_pos_mode();
10195 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010196 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 if (do_mode)
10198 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010199 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010201 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010202# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010203 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010204# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010205 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010206# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010207 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010208# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010209 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010211 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212# endif
10213#endif
10214#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10215 if (gui.in_use)
10216 {
10217 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010218 {
10219 /* HANGUL */
10220 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010221 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010222 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010223 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 }
10226#endif
10227#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010228 /* CTRL-X in Insert mode */
10229 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 {
10231 /* These messages can get long, avoid a wrap in a narrow
10232 * window. Prefer showing edit_submode_extra. */
10233 length = (Rows - msg_row) * Columns - 3;
10234 if (edit_submode_extra != NULL)
10235 length -= vim_strsize(edit_submode_extra);
10236 if (length > 0)
10237 {
10238 if (edit_submode_pre != NULL)
10239 length -= vim_strsize(edit_submode_pre);
10240 if (length - vim_strsize(edit_submode) > 0)
10241 {
10242 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010243 msg_puts_attr((char *)edit_submode_pre, attr);
10244 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 }
10246 if (edit_submode_extra != NULL)
10247 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010248 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010250 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 else
10252 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010253 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 }
10255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 }
10257 else
10258#endif
10259 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010261 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010262 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010263 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 else if (State & INSERT)
10265 {
10266#ifdef FEAT_RIGHTLEFT
10267 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010268 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010270 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010272 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010273 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010275 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010277 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278#ifdef FEAT_RIGHTLEFT
10279 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010280 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281#endif
10282#ifdef FEAT_KEYMAP
10283 if (State & LANGMAP)
10284 {
10285# ifdef FEAT_ARABIC
10286 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010287 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 else
10289# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010290 if (get_keymap_str(curwin, (char_u *)" (%s)",
10291 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010292 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 }
10294#endif
10295 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010296 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 if (VIsual_active)
10299 {
10300 char *p;
10301
10302 /* Don't concatenate separate words to avoid translation
10303 * problems. */
10304 switch ((VIsual_select ? 4 : 0)
10305 + (VIsual_mode == Ctrl_V) * 2
10306 + (VIsual_mode == 'V'))
10307 {
10308 case 0: p = N_(" VISUAL"); break;
10309 case 1: p = N_(" VISUAL LINE"); break;
10310 case 2: p = N_(" VISUAL BLOCK"); break;
10311 case 4: p = N_(" SELECT"); break;
10312 case 5: p = N_(" SELECT LINE"); break;
10313 default: p = N_(" SELECT BLOCK"); break;
10314 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010315 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010317 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010319
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 need_clear = TRUE;
10321 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010322 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323#ifdef FEAT_INS_EXPAND
10324 && edit_submode == NULL /* otherwise it gets too long */
10325#endif
10326 )
10327 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010328 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 need_clear = TRUE;
10330 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010331
10332 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010333 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 msg_clr_eos();
10335 msg_didout = FALSE; /* overwrite this message */
10336 length = msg_col;
10337 msg_col = 0;
10338 need_wait_return = nwr_save; /* never ask for hit-return for this */
10339 }
10340 else if (clear_cmdline && msg_silent == 0)
10341 /* Clear the whole command line. Will reset "clear_cmdline". */
10342 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010343 else if (redraw_mode)
10344 {
10345 msg_pos_mode();
10346 msg_clr_eos();
10347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348
10349#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 /* In Visual mode the size of the selected area must be redrawn. */
10351 if (VIsual_active)
10352 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353
10354 /* If the last window has no status line, the ruler is after the mode
10355 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010356 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010357 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358#endif
10359 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010360 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 clear_cmdline = FALSE;
10362
10363 return length;
10364}
10365
10366/*
10367 * Position for a mode message.
10368 */
10369 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010370msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371{
10372 msg_col = 0;
10373 msg_row = Rows - 1;
10374}
10375
10376/*
10377 * Delete mode message. Used when ESC is typed which is expected to end
10378 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010379 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 */
10381 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010382unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383{
10384 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010385 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 */
10387 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10388 redraw_cmdline = TRUE; /* delete mode later */
10389 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010390 clearmode();
10391}
10392
10393/*
10394 * Clear the mode message.
10395 */
10396 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010397clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010398{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010399 int save_msg_row = msg_row;
10400 int save_msg_col = msg_col;
10401
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010402 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010403 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010404 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010405 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010406
10407 msg_col = save_msg_col;
10408 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409}
10410
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010411 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010412recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010413{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010414 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010415 if (!shortmess(SHM_RECORDING))
10416 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010417 char s[4];
10418
10419 sprintf(s, " @%c", reg_recording);
10420 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010421 }
10422}
10423
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010424/*
10425 * Draw the tab pages line at the top of the Vim window.
10426 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010427 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010428draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010429{
10430 int tabcount = 0;
10431 tabpage_T *tp;
10432 int tabwidth;
10433 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010434 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010435 int attr;
10436 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010437 win_T *cwp;
10438 int wincount;
10439 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010440 int c;
10441 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010442 int attr_sel = HL_ATTR(HLF_TPS);
10443 int attr_nosel = HL_ATTR(HLF_TP);
10444 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010445 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010446 int room;
10447 int use_sep_chars = (t_colors < 8
10448#ifdef FEAT_GUI
10449 && !gui.in_use
10450#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010451#ifdef FEAT_TERMGUICOLORS
10452 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010453#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010454 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010455
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010456 if (ScreenLines == NULL)
10457 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010458 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010459
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010460#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010461 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010462 if (gui_use_tabline())
10463 {
10464 gui_update_tabline();
10465 return;
10466 }
10467#endif
10468
10469 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010470 return;
10471
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010472#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010473 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010474
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010475 /* Use the 'tabline' option if it's set. */
10476 if (*p_tal != NUL)
10477 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010478 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010479
10480 /* Check for an error. If there is one we would loop in redrawing the
10481 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010482 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010483 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010484 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010485 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010486 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010487 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010488 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010489 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010490#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010491 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010492 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010493 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010494
Bram Moolenaar238a5642006-02-21 22:12:05 +000010495 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10496 if (tabwidth < 6)
10497 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010498
Bram Moolenaar238a5642006-02-21 22:12:05 +000010499 attr = attr_nosel;
10500 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010501 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10502 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010503 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010504 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010505
Bram Moolenaar238a5642006-02-21 22:12:05 +000010506 if (tp->tp_topframe == topframe)
10507 attr = attr_sel;
10508 if (use_sep_chars && col > 0)
10509 screen_putchar('|', 0, col++, attr);
10510
10511 if (tp->tp_topframe != topframe)
10512 attr = attr_nosel;
10513
10514 screen_putchar(' ', 0, col++, attr);
10515
10516 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010517 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010518 cwp = curwin;
10519 wp = firstwin;
10520 }
10521 else
10522 {
10523 cwp = tp->tp_curwin;
10524 wp = tp->tp_firstwin;
10525 }
10526
10527 modified = FALSE;
10528 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10529 if (bufIsChanged(wp->w_buffer))
10530 modified = TRUE;
10531 if (modified || wincount > 1)
10532 {
10533 if (wincount > 1)
10534 {
10535 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010536 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010537 if (col + len >= Columns - 3)
10538 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010539 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010540#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010541 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010542#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010543 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010544#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010545 );
10546 col += len;
10547 }
10548 if (modified)
10549 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10550 screen_putchar(' ', 0, col++, attr);
10551 }
10552
10553 room = scol - col + tabwidth - 1;
10554 if (room > 0)
10555 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010556 /* Get buffer name in NameBuff[] */
10557 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010558 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010559 len = vim_strsize(NameBuff);
10560 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010561 if (has_mbyte)
10562 while (len > room)
10563 {
10564 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010565 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010566 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010567 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010568 {
10569 p += len - room;
10570 len = room;
10571 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010572 if (len > Columns - col - 1)
10573 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010574
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010575 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010576 col += len;
10577 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010578 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010579
10580 /* Store the tab page number in TabPageIdxs[], so that
10581 * jump_to_mouse() knows where each one is. */
10582 ++tabcount;
10583 while (scol < col)
10584 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010585 }
10586
Bram Moolenaar238a5642006-02-21 22:12:05 +000010587 if (use_sep_chars)
10588 c = '_';
10589 else
10590 c = ' ';
10591 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010592
10593 /* Put an "X" for closing the current tab if there are several. */
10594 if (first_tabpage->tp_next != NULL)
10595 {
10596 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10597 TabPageIdxs[Columns - 1] = -999;
10598 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010599 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010600
10601 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10602 * set. */
10603 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010604}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010605
10606/*
10607 * Get buffer name for "buf" into NameBuff[].
10608 * Takes care of special buffer names and translates special characters.
10609 */
10610 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010611get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010612{
10613 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010614 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010615 else
10616 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10617 trans_characters(NameBuff, MAXPATHL);
10618}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010619
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620/*
10621 * Get the character to use in a status line. Get its attributes in "*attr".
10622 */
10623 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010624fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625{
10626 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010627
10628#ifdef FEAT_TERMINAL
10629 if (bt_terminal(wp->w_buffer))
10630 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010631 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010632 {
10633 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010634 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010635 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010636 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010637 {
10638 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010639 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010640 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010641 }
10642 else
10643#endif
10644 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010646 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 fill = fill_stl;
10648 }
10649 else
10650 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010651 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 fill = fill_stlnc;
10653 }
10654 /* Use fill when there is highlighting, and highlighting of current
10655 * window differs, or the fillchars differ, or this is not the
10656 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010657 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010658 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 || (fill_stl != fill_stlnc)))
10660 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010661 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 return '^';
10663 return '=';
10664}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666/*
10667 * Get the character to use in a separator between vertically split windows.
10668 * Get its attributes in "*attr".
10669 */
10670 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010671fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010673 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674 if (*attr == 0 && fill_vert == ' ')
10675 return '|';
10676 else
10677 return fill_vert;
10678}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679
10680/*
10681 * Return TRUE if redrawing should currently be done.
10682 */
10683 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010684redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010686#ifdef FEAT_EVAL
10687 if (disable_redraw_for_testing)
10688 return 0;
10689 else
10690#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010691 return ((!RedrawingDisabled
10692#ifdef FEAT_EVAL
10693 || ignore_redraw_flag_for_testing
10694#endif
10695 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696}
10697
10698/*
10699 * Return TRUE if printing messages should currently be done.
10700 */
10701 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010702messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703{
10704 return (!(p_lz && char_avail() && !KeyTyped));
10705}
10706
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010707#ifdef FEAT_MENU
10708/*
10709 * Draw the window toolbar.
10710 */
10711 static void
10712redraw_win_toolbar(win_T *wp)
10713{
10714 vimmenu_T *menu;
10715 int item_idx = 0;
10716 int item_count = 0;
10717 int col = 0;
10718 int next_col;
10719 int off = (int)(current_ScreenLine - ScreenLines);
10720 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10721 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10722
10723 vim_free(wp->w_winbar_items);
10724 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10725 ++item_count;
10726 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10727 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10728
10729 /* TODO: use fewer spaces if there is not enough room */
10730 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010731 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010732 {
10733 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010734 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010735 break;
10736 if (col > 1)
10737 {
10738 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010739 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010740 break;
10741 }
10742
10743 wp->w_winbar_items[item_idx].wb_startcol = col;
10744 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010745 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010746 break;
10747
10748 next_col = text_to_screenline(wp, menu->name, col);
10749 while (col < next_col)
10750 {
10751 ScreenAttrs[off + col] = button_attr;
10752 ++col;
10753 }
10754 wp->w_winbar_items[item_idx].wb_endcol = col;
10755 wp->w_winbar_items[item_idx].wb_menu = menu;
10756 ++item_idx;
10757
Bram Moolenaar02631462017-09-22 15:20:32 +020010758 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010759 break;
10760 space_to_screenline(off + col, button_attr);
10761 ++col;
10762 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010763 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010764 {
10765 space_to_screenline(off + col, fill_attr);
10766 ++col;
10767 }
10768 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10769
Bram Moolenaar02631462017-09-22 15:20:32 +020010770 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10771 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010772}
10773#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010774
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775/*
10776 * Show current status info in ruler and various other places
10777 * If always is FALSE, only show ruler if position has changed.
10778 */
10779 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010780showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781{
10782 if (!always && !redrawing())
10783 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010784#ifdef FEAT_INS_EXPAND
10785 if (pum_visible())
10786 {
10787 /* Don't redraw right now, do it later. */
10788 curwin->w_redr_status = TRUE;
10789 return;
10790 }
10791#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010792#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010793 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010794 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 else
10796#endif
10797#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010798 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799#endif
10800
10801#ifdef FEAT_TITLE
10802 if (need_maketitle
10803# ifdef FEAT_STL_OPT
10804 || (p_icon && (stl_syntax & STL_IN_ICON))
10805 || (p_title && (stl_syntax & STL_IN_TITLE))
10806# endif
10807 )
10808 maketitle();
10809#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010810 /* Redraw the tab pages line if needed. */
10811 if (redraw_tabline)
10812 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813}
10814
10815#ifdef FEAT_CMDL_INFO
10816 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010817win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010819#define RULER_BUF_LEN 70
10820 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821 int row;
10822 int fillchar;
10823 int attr;
10824 int empty_line = FALSE;
10825 colnr_T virtcol;
10826 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010827 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829 int this_ru_col;
10830 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010831 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832
10833 /* If 'ruler' off or redrawing disabled, don't do anything */
10834 if (!p_ru)
10835 return;
10836
10837 /*
10838 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10839 * after deleting lines, before cursor.lnum is corrected.
10840 */
10841 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10842 return;
10843
10844#ifdef FEAT_INS_EXPAND
10845 /* Don't draw the ruler while doing insert-completion, it might overwrite
10846 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 if (edit_submode != NULL)
10849 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010850 // Don't draw the ruler when the popup menu is visible, it may overlap.
10851 // Except when the popup menu will be redrawn anyway.
10852 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010853 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854#endif
10855
10856#ifdef FEAT_STL_OPT
10857 if (*p_ruf)
10858 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010859 int save_called_emsg = called_emsg;
10860
10861 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010863 if (called_emsg)
10864 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010865 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010866 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 return;
10868 }
10869#endif
10870
10871 /*
10872 * Check if not in Insert mode and the line is empty (will show "0-1").
10873 */
10874 if (!(State & INSERT)
10875 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10876 empty_line = TRUE;
10877
10878 /*
10879 * Only draw the ruler when something changed.
10880 */
10881 validate_virtcol_win(wp);
10882 if ( redraw_cmdline
10883 || always
10884 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10885 || wp->w_cursor.col != wp->w_ru_cursor.col
10886 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 || wp->w_topline != wp->w_ru_topline
10889 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10890#ifdef FEAT_DIFF
10891 || wp->w_topfill != wp->w_ru_topfill
10892#endif
10893 || empty_line != wp->w_ru_empty)
10894 {
10895 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 if (wp->w_status_height)
10897 {
10898 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010899 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010900 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010901 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 }
10903 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 {
10905 row = Rows - 1;
10906 fillchar = ' ';
10907 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 width = Columns;
10909 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 }
10911
10912 /* In list mode virtcol needs to be recomputed */
10913 virtcol = wp->w_virtcol;
10914 if (wp->w_p_list && lcs_tab1 == NUL)
10915 {
10916 wp->w_p_list = FALSE;
10917 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10918 wp->w_p_list = TRUE;
10919 }
10920
10921 /*
10922 * Some sprintfs return the length, some return a pointer.
10923 * To avoid portability problems we use strlen() here.
10924 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010925 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10927 ? 0L
10928 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010929 len = STRLEN(buffer);
10930 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10932 (int)virtcol + 1);
10933
10934 /*
10935 * Add a "50%" if there is room for it.
10936 * On the last line, don't print in the last column (scrolls the
10937 * screen up on some terminals).
10938 */
10939 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010940 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 this_ru_col = ru_col - (Columns - width);
10945 if (this_ru_col < 0)
10946 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 /* Never use more than half the window/screen width, leave the other
10948 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010949 if (this_ru_col < (width + 1) / 2)
10950 this_ru_col = (width + 1) / 2;
10951 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010953 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010954 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 if (has_mbyte)
10957 i += (*mb_char2bytes)(fillchar, buffer + i);
10958 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 buffer[i++] = fillchar;
10960 ++o;
10961 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010962 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 }
10964 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 if (has_mbyte)
10966 {
10967 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010968 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 {
10970 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010971 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 {
10973 buffer[i] = NUL;
10974 break;
10975 }
10976 }
10977 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010978 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020010979 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980
Bram Moolenaar4033c552017-09-16 20:54:51 +020010981 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 i = redraw_cmdline;
10983 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020010984 this_ru_col + off + (int)STRLEN(buffer),
10985 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 fillchar, fillchar, attr);
10987 /* don't redraw the cmdline because of showing the ruler */
10988 redraw_cmdline = i;
10989 wp->w_ru_cursor = wp->w_cursor;
10990 wp->w_ru_virtcol = wp->w_virtcol;
10991 wp->w_ru_empty = empty_line;
10992 wp->w_ru_topline = wp->w_topline;
10993 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10994#ifdef FEAT_DIFF
10995 wp->w_ru_topfill = wp->w_topfill;
10996#endif
10997 }
10998}
10999#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011000
11001#if defined(FEAT_LINEBREAK) || defined(PROTO)
11002/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011003 * Return the width of the 'number' and 'relativenumber' column.
11004 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011005 * Otherwise it depends on 'numberwidth' and the line count.
11006 */
11007 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011008number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011009{
11010 int n;
11011 linenr_T lnum;
11012
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011013 if (wp->w_p_rnu && !wp->w_p_nu)
11014 /* cursor line shows "0" */
11015 lnum = wp->w_height;
11016 else
11017 /* cursor line shows absolute line number */
11018 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011019
Bram Moolenaar6b314672015-03-20 15:42:10 +010011020 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011021 return wp->w_nrwidth_width;
11022 wp->w_nrwidth_line_count = lnum;
11023
11024 n = 0;
11025 do
11026 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011027 lnum /= 10;
11028 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011029 } while (lnum > 0);
11030
11031 /* 'numberwidth' gives the minimal width plus one */
11032 if (n < wp->w_p_nuw - 1)
11033 n = wp->w_p_nuw - 1;
11034
11035 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011036 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011037 return n;
11038}
11039#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011040
Bram Moolenaar113e1072019-01-20 15:30:40 +010011041#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011042/*
11043 * Return the current cursor column. This is the actual position on the
11044 * screen. First column is 0.
11045 */
11046 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011047screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011048{
11049 return screen_cur_col;
11050}
11051
11052/*
11053 * Return the current cursor row. This is the actual position on the screen.
11054 * First row is 0.
11055 */
11056 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011057screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011058{
11059 return screen_cur_row;
11060}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011061#endif