blob: cb10f6ee9c9c61d04869412b5d0d4cd40a7c8c7a [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
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200568 {
569 buf_T *buf;
570
571 // Before updating the screen, notify any listeners of changed text.
572 FOR_ALL_BUFFERS(buf)
573 invoke_listeners(buf);
574 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200575#endif
576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 if (must_redraw)
578 {
579 if (type < must_redraw) /* use maximal type */
580 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000581
582 /* must_redraw is reset here, so that when we run into some weird
583 * reason to redraw while busy redrawing (e.g., asynchronous
584 * scrolling), or update_topline() in win_update() will cause a
585 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 must_redraw = 0;
587 }
588
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200589 /* May need to update w_lines[]. */
590 if (curwin->w_lines_valid == 0 && type < NOT_VALID
591#ifdef FEAT_TERMINAL
592 && !term_do_update_window(curwin)
593#endif
594 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 type = NOT_VALID;
596
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000597 /* Postpone the redrawing when it's not needed and when being called
598 * recursively. */
599 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 {
601 redraw_later(type); /* remember type for next time */
602 must_redraw = type;
603 if (type > INVERTED_ALL)
604 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200605 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 }
607
608 updating_screen = TRUE;
609#ifdef FEAT_SYN_HL
610 ++display_tick; /* let syntax code know we're in a next round of
611 * display updating */
612#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200613 if (no_update)
614 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615
616 /*
617 * if the screen was scrolled up when displaying a message, scroll it down
618 */
619 if (msg_scrolled)
620 {
621 clear_cmdline = TRUE;
622 if (msg_scrolled > Rows - 5) /* clearing is faster */
623 type = CLEAR;
624 else if (type != CLEAR)
625 {
626 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200627 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
628 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 type = CLEAR;
630 FOR_ALL_WINDOWS(wp)
631 {
632 if (W_WINROW(wp) < msg_scrolled)
633 {
634 if (W_WINROW(wp) + wp->w_height > msg_scrolled
635 && wp->w_redr_type < REDRAW_TOP
636 && wp->w_lines_valid > 0
637 && wp->w_topline == wp->w_lines[0].wl_lnum)
638 {
639 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
640 wp->w_redr_type = REDRAW_TOP;
641 }
642 else
643 {
644 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200645 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
646 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 }
649 }
650 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200651 if (!no_update)
652 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000653 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 }
655 msg_scrolled = 0;
656 need_wait_return = FALSE;
657 }
658
659 /* reset cmdline_row now (may have been changed temporarily) */
660 compute_cmdrow();
661
662 /* Check for changed highlighting */
663 if (need_highlight_changed)
664 highlight_changed();
665
666 if (type == CLEAR) /* first clear screen */
667 {
668 screenclear(); /* will reset clear_cmdline */
669 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200670 /* must_redraw may be set indirectly, avoid another redraw later */
671 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 }
673
674 if (clear_cmdline) /* going to clear cmdline (done below) */
675 check_for_delay(FALSE);
676
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000677#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200678 /* Force redraw when width of 'number' or 'relativenumber' column
679 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000680 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200681 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
682 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000683 curwin->w_redr_type = NOT_VALID;
684#endif
685
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 /*
687 * Only start redrawing if there is really something to do.
688 */
689 if (type == INVERTED)
690 update_curswant();
691 if (curwin->w_redr_type < type
692 && !((type == VALID
693 && curwin->w_lines[0].wl_valid
694#ifdef FEAT_DIFF
695 && curwin->w_topfill == curwin->w_old_topfill
696 && curwin->w_botfill == curwin->w_old_botfill
697#endif
698 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000700 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
702 && curwin->w_old_visual_mode == VIsual_mode
703 && (curwin->w_valid & VALID_VIRTCOL)
704 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 ))
706 curwin->w_redr_type = type;
707
Bram Moolenaar5a305422006-04-28 22:38:25 +0000708 /* Redraw the tab pages line if needed. */
709 if (redraw_tabline || type >= NOT_VALID)
710 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000711
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712#ifdef FEAT_SYN_HL
713 /*
714 * Correct stored syntax highlighting info for changes in each displayed
715 * buffer. Each buffer must only be done once.
716 */
717 FOR_ALL_WINDOWS(wp)
718 {
719 if (wp->w_buffer->b_mod_set)
720 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 win_T *wwp;
722
723 /* Check if we already did this buffer. */
724 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
725 if (wwp->w_buffer == wp->w_buffer)
726 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200727 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 syn_stack_apply_changes(wp->w_buffer);
729 }
730 }
731#endif
732
733 /*
734 * Go from top to bottom through the windows, redrawing the ones that need
735 * it.
736 */
737#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
738 did_one = FALSE;
739#endif
740#ifdef FEAT_SEARCH_EXTRA
741 search_hl.rm.regprog = NULL;
742#endif
743 FOR_ALL_WINDOWS(wp)
744 {
745 if (wp->w_redr_type != 0)
746 {
747 cursor_off();
748#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
749 if (!did_one)
750 {
751 did_one = TRUE;
752# ifdef FEAT_SEARCH_EXTRA
753 start_search_hl();
754# endif
755# ifdef FEAT_CLIPBOARD
756 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200757 if (clip_star.available && clip_isautosel_star())
758 clip_update_selection(&clip_star);
759 if (clip_plus.available && clip_isautosel_plus())
760 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761# endif
762#ifdef FEAT_GUI
763 /* Remove the cursor before starting to do anything, because
764 * scrolling may make it difficult to redraw the text under
765 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200766 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200767 {
768 gui_cursor_col = gui.cursor_col;
769 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200771 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773#endif
774 }
775#endif
776 win_update(wp);
777 }
778
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 /* redraw status line after the window to minimize cursor movement */
780 if (wp->w_redr_status)
781 {
782 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200783 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786#if defined(FEAT_SEARCH_EXTRA)
787 end_search_hl();
788#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100789#ifdef FEAT_INS_EXPAND
790 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200791 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 /* Reset b_mod_set flags. Going through all windows is probably faster
795 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200796 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200799 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801 /* Clear or redraw the command line. Done last, because scrolling may
802 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200803 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 showmode();
805
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200806 if (no_update)
807 --no_win_do_lines_ins;
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200810 if (!did_intro)
811 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 did_intro = TRUE;
813
814#ifdef FEAT_GUI
815 /* Redraw the cursor and update the scrollbars when all screen updating is
816 * done. */
817 if (gui.in_use)
818 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200819 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200820 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100821 mch_disable_flush();
822 out_flush(); /* required before updating the cursor */
823 mch_enable_flush();
824
Bram Moolenaar144445d2016-07-08 21:41:54 +0200825 /* Put the GUI position where the cursor was, gui_update_cursor()
826 * uses that. */
827 gui.col = gui_cursor_col;
828 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200829 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100831 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200832 screen_cur_col = gui.col;
833 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200834 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100835 else
836 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 gui_update_scrollbars(FALSE);
838 }
839#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200840 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841}
842
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100843#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100844/*
845 * Prepare for updating one or more windows.
846 * Caller must check for "updating_screen" already set to avoid recursiveness.
847 */
848 static void
849update_prepare(void)
850{
851 cursor_off();
852 updating_screen = TRUE;
853#ifdef FEAT_GUI
854 /* Remove the cursor before starting to do anything, because scrolling may
855 * make it difficult to redraw the text under it. */
856 if (gui.in_use)
857 gui_undraw_cursor();
858#endif
859#ifdef FEAT_SEARCH_EXTRA
860 start_search_hl();
861#endif
862}
863
864/*
865 * Finish updating one or more windows.
866 */
867 static void
868update_finish(void)
869{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200870 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100871 showmode();
872
873# ifdef FEAT_SEARCH_EXTRA
874 end_search_hl();
875# endif
876
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200877 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100878
879# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100880 /* Redraw the cursor and update the scrollbars when all screen updating is
881 * done. */
882 if (gui.in_use)
883 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100884 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100885 gui_update_scrollbars(FALSE);
886 }
887# endif
888}
889#endif
890
Bram Moolenaar860cae12010-06-05 23:22:07 +0200891#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200892/*
893 * Return TRUE if the cursor line in window "wp" may be concealed, according
894 * to the 'concealcursor' option.
895 */
896 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100897conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200898{
899 int c;
900
901 if (*wp->w_p_cocu == NUL)
902 return FALSE;
903 if (get_real_state() & VISUAL)
904 c = 'v';
905 else if (State & INSERT)
906 c = 'i';
907 else if (State & NORMAL)
908 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200909 else if (State & CMDLINE)
910 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200911 else
912 return FALSE;
913 return vim_strchr(wp->w_p_cocu, c) != NULL;
914}
915
916/*
917 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
918 */
919 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200920conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200921{
922 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
923 {
924 need_cursor_line_redraw = TRUE;
925 /* Need to recompute cursor column, e.g., when starting Visual mode
926 * without concealing. */
927 curs_columns(TRUE);
928 }
929}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930#endif
931
Bram Moolenaar113e1072019-01-20 15:30:40 +0100932#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100934update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935{
936 win_T *wp;
937 int doit = FALSE;
938
939# ifdef FEAT_FOLDING
940 win_foldinfo.fi_level = 0;
941# endif
942
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100943 // update/delete a specific sign
944 redraw_buf_line_later(buf, lnum);
945
946 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 if (wp->w_redr_type != 0)
949 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100951 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200952 * happening (recursive call), messages on the screen or still starting up.
953 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100954 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200955 || State == ASKMORE || State == HITRETURN
956 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100957#ifdef FEAT_GUI
958 || gui.starting
959#endif
960 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 return;
962
963 /* update all windows that need updating */
964 update_prepare();
965
Bram Moolenaar29323592016-07-24 22:04:11 +0200966 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 {
968 if (wp->w_redr_type != 0)
969 win_update(wp);
970 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200971 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973
974 update_finish();
975}
976#endif
977
978
979#if defined(FEAT_GUI) || defined(PROTO)
980/*
981 * Update a single window, its status line and maybe the command line msg.
982 * Used for the GUI scrollbar.
983 */
984 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100985updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000987 /* return if already busy updating */
988 if (updating_screen)
989 return;
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 update_prepare();
992
993#ifdef FEAT_CLIPBOARD
994 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200995 if (clip_star.available && clip_isautosel_star())
996 clip_update_selection(&clip_star);
997 if (clip_plus.available && clip_isautosel_plus())
998 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001002
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001003 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001004 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001005 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 if (wp->w_redr_status
1008# ifdef FEAT_CMDL_INFO
1009 || p_ru
1010# endif
1011# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001012 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013# endif
1014 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001015 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017 update_finish();
1018}
1019#endif
1020
1021/*
1022 * Update a single window.
1023 *
1024 * This may cause the windows below it also to be redrawn (when clearing the
1025 * screen or scrolling lines).
1026 *
1027 * How the window is redrawn depends on wp->w_redr_type. Each type also
1028 * implies the one below it.
1029 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001030 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1032 * INVERTED redraw the changed part of the Visual area
1033 * INVERTED_ALL redraw the whole Visual area
1034 * VALID 1. scroll up/down to adjust for a changed w_topline
1035 * 2. update lines at the top when scrolled down
1036 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001037 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 * b_mod_top and b_mod_bot.
1039 * - if wp->w_redraw_top non-zero, redraw lines between
1040 * wp->w_redraw_top and wp->w_redr_bot.
1041 * - continue redrawing when syntax status is invalid.
1042 * 4. if scrolled up, update lines at the bottom.
1043 * This results in three areas that may need updating:
1044 * top: from first row to top_end (when scrolled down)
1045 * mid: from mid_start to mid_end (update inversion or changed text)
1046 * bot: from bot_start to last row (when scrolled up)
1047 */
1048 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001049win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050{
1051 buf_T *buf = wp->w_buffer;
1052 int type;
1053 int top_end = 0; /* Below last row of the top area that needs
1054 updating. 0 when no top area updating. */
1055 int mid_start = 999;/* first row of the mid area that needs
1056 updating. 999 when no mid area updating. */
1057 int mid_end = 0; /* Below last row of the mid area that needs
1058 updating. 0 when no mid area updating. */
1059 int bot_start = 999;/* first row of the bot area that needs
1060 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 int scrolled_down = FALSE; /* TRUE when scrolled down when
1062 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001064 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 int top_to_mod = FALSE; /* redraw above mod_top */
1066#endif
1067
1068 int row; /* current window row to display */
1069 linenr_T lnum; /* current buffer lnum to display */
1070 int idx; /* current index in w_lines[] */
1071 int srow; /* starting row of the current line */
1072
1073 int eof = FALSE; /* if TRUE, we hit the end of the file */
1074 int didline = FALSE; /* if TRUE, we finished the last line */
1075 int i;
1076 long j;
1077 static int recursive = FALSE; /* being called recursively */
1078 int old_botline = wp->w_botline;
1079#ifdef FEAT_FOLDING
1080 long fold_count;
1081#endif
1082#ifdef FEAT_SYN_HL
1083 /* remember what happened to the previous line, to know if
1084 * check_visual_highlight() can be used */
1085#define DID_NONE 1 /* didn't update a line */
1086#define DID_LINE 2 /* updated a normal line */
1087#define DID_FOLD 3 /* updated a folded line */
1088 int did_update = DID_NONE;
1089 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1090#endif
1091 linenr_T mod_top = 0;
1092 linenr_T mod_bot = 0;
1093#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1094 int save_got_int;
1095#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001096#ifdef SYN_TIME_LIMIT
1097 proftime_T syntax_tm;
1098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099
1100 type = wp->w_redr_type;
1101
1102 if (type == NOT_VALID)
1103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 wp->w_lines_valid = 0;
1106 }
1107
1108 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001109 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 {
1111 wp->w_redr_type = 0;
1112 return;
1113 }
1114
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 /* Window is zero-width: Only need to draw the separator. */
1116 if (wp->w_width == 0)
1117 {
1118 /* draw the vertical separator right of this window */
1119 draw_vsep_win(wp, 0);
1120 wp->w_redr_type = 0;
1121 return;
1122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001124#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001125 // If this window contains a terminal, redraw works completely differently.
1126 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001127 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001128 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001129# ifdef FEAT_MENU
1130 /* Draw the window toolbar, if there is one. */
1131 if (winbar_height(wp) > 0)
1132 redraw_win_toolbar(wp);
1133# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001134 wp->w_redr_type = 0;
1135 return;
1136 }
1137#endif
1138
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001140 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141#endif
1142
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001143#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001144 /* Force redraw when width of 'number' or 'relativenumber' column
1145 * changes. */
1146 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001147 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001148 {
1149 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001150 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001151 }
1152 else
1153#endif
1154
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1156 {
1157 /*
1158 * When there are both inserted/deleted lines and specific lines to be
1159 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1160 * everything (only happens when redrawing is off for while).
1161 */
1162 type = NOT_VALID;
1163 }
1164 else
1165 {
1166 /*
1167 * Set mod_top to the first line that needs displaying because of
1168 * changes. Set mod_bot to the first line after the changes.
1169 */
1170 mod_top = wp->w_redraw_top;
1171 if (wp->w_redraw_bot != 0)
1172 mod_bot = wp->w_redraw_bot + 1;
1173 else
1174 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (buf->b_mod_set)
1176 {
1177 if (mod_top == 0 || mod_top > buf->b_mod_top)
1178 {
1179 mod_top = buf->b_mod_top;
1180#ifdef FEAT_SYN_HL
1181 /* Need to redraw lines above the change that may be included
1182 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001183 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001185 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 if (mod_top < 1)
1187 mod_top = 1;
1188 }
1189#endif
1190 }
1191 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1192 mod_bot = buf->b_mod_bot;
1193
1194#ifdef FEAT_SEARCH_EXTRA
1195 /* When 'hlsearch' is on and using a multi-line search pattern, a
1196 * change in one line may make the Search highlighting in a
1197 * previous line invalid. Simple solution: redraw all visible
1198 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001199 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001201 if (search_hl.rm.regprog != NULL
1202 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001204 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001205 {
1206 cur = wp->w_match_head;
1207 while (cur != NULL)
1208 {
1209 if (cur->match.regprog != NULL
1210 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001211 {
1212 top_to_mod = TRUE;
1213 break;
1214 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001215 cur = cur->next;
1216 }
1217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218#endif
1219 }
1220#ifdef FEAT_FOLDING
1221 if (mod_top != 0 && hasAnyFolding(wp))
1222 {
1223 linenr_T lnumt, lnumb;
1224
1225 /*
1226 * A change in a line can cause lines above it to become folded or
1227 * unfolded. Find the top most buffer line that may be affected.
1228 * If the line was previously folded and displayed, get the first
1229 * line of that fold. If the line is folded now, get the first
1230 * folded line. Use the minimum of these two.
1231 */
1232
1233 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1234 * the line below it. If there is no valid entry, use w_topline.
1235 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1236 * to this line. If there is no valid entry, use MAXLNUM. */
1237 lnumt = wp->w_topline;
1238 lnumb = MAXLNUM;
1239 for (i = 0; i < wp->w_lines_valid; ++i)
1240 if (wp->w_lines[i].wl_valid)
1241 {
1242 if (wp->w_lines[i].wl_lastlnum < mod_top)
1243 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1244 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1245 {
1246 lnumb = wp->w_lines[i].wl_lnum;
1247 /* When there is a fold column it might need updating
1248 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001249 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 ++lnumb;
1251 }
1252 }
1253
1254 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1255 if (mod_top > lnumt)
1256 mod_top = lnumt;
1257
1258 /* Now do the same for the bottom line (one above mod_bot). */
1259 --mod_bot;
1260 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1261 ++mod_bot;
1262 if (mod_bot < lnumb)
1263 mod_bot = lnumb;
1264 }
1265#endif
1266
1267 /* When a change starts above w_topline and the end is below
1268 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001269 * If the end of the change is above w_topline: do like no change was
1270 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 if (mod_top != 0 && mod_top < wp->w_topline)
1272 {
1273 if (mod_bot > wp->w_topline)
1274 mod_top = wp->w_topline;
1275#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001276 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 top_end = 1;
1278#endif
1279 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001280
1281 /* When line numbers are displayed need to redraw all lines below
1282 * inserted/deleted lines. */
1283 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1284 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001286 wp->w_redraw_top = 0; // reset for next time
1287 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288
1289 /*
1290 * When only displaying the lines at the top, set top_end. Used when
1291 * window has scrolled down for msg_scrolled.
1292 */
1293 if (type == REDRAW_TOP)
1294 {
1295 j = 0;
1296 for (i = 0; i < wp->w_lines_valid; ++i)
1297 {
1298 j += wp->w_lines[i].wl_size;
1299 if (j >= wp->w_upd_rows)
1300 {
1301 top_end = j;
1302 break;
1303 }
1304 }
1305 if (top_end == 0)
1306 /* not found (cannot happen?): redraw everything */
1307 type = NOT_VALID;
1308 else
1309 /* top area defined, the rest is VALID */
1310 type = VALID;
1311 }
1312
Bram Moolenaar367329b2007-08-30 11:53:22 +00001313 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001314 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1315 * non-zero and thus not FALSE) will indicate that screenclear() was not
1316 * called. */
1317 if (screen_cleared)
1318 screen_cleared = MAYBE;
1319
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 /*
1321 * If there are no changes on the screen that require a complete redraw,
1322 * handle three cases:
1323 * 1: we are off the top of the screen by a few lines: scroll down
1324 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1325 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1326 * w_lines[] that needs updating.
1327 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001328 if ((type == VALID || type == SOME_VALID
1329 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330#ifdef FEAT_DIFF
1331 && !wp->w_botfill && !wp->w_old_botfill
1332#endif
1333 )
1334 {
1335 if (mod_top != 0 && wp->w_topline == mod_top)
1336 {
1337 /*
1338 * w_topline is the first changed line, the scrolling will be done
1339 * further down.
1340 */
1341 }
1342 else if (wp->w_lines[0].wl_valid
1343 && (wp->w_topline < wp->w_lines[0].wl_lnum
1344#ifdef FEAT_DIFF
1345 || (wp->w_topline == wp->w_lines[0].wl_lnum
1346 && wp->w_topfill > wp->w_old_topfill)
1347#endif
1348 ))
1349 {
1350 /*
1351 * New topline is above old topline: May scroll down.
1352 */
1353#ifdef FEAT_FOLDING
1354 if (hasAnyFolding(wp))
1355 {
1356 linenr_T ln;
1357
1358 /* count the number of lines we are off, counting a sequence
1359 * of folded lines as one */
1360 j = 0;
1361 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1362 {
1363 ++j;
1364 if (j >= wp->w_height - 2)
1365 break;
1366 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1367 }
1368 }
1369 else
1370#endif
1371 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1372 if (j < wp->w_height - 2) /* not too far off */
1373 {
1374 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1375#ifdef FEAT_DIFF
1376 /* insert extra lines for previously invisible filler lines */
1377 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1378 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1379 - wp->w_old_topfill;
1380#endif
1381 if (i < wp->w_height - 2) /* less than a screen off */
1382 {
1383 /*
1384 * Try to insert the correct number of lines.
1385 * If not the last window, delete the lines at the bottom.
1386 * win_ins_lines may fail when the terminal can't do it.
1387 */
1388 if (i > 0)
1389 check_for_delay(FALSE);
1390 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1391 {
1392 if (wp->w_lines_valid != 0)
1393 {
1394 /* Need to update rows that are new, stop at the
1395 * first one that scrolled down. */
1396 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398
1399 /* Move the entries that were scrolled, disable
1400 * the entries for the lines to be redrawn. */
1401 if ((wp->w_lines_valid += j) > wp->w_height)
1402 wp->w_lines_valid = wp->w_height;
1403 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1404 wp->w_lines[idx] = wp->w_lines[idx - j];
1405 while (idx >= 0)
1406 wp->w_lines[idx--].wl_valid = FALSE;
1407 }
1408 }
1409 else
1410 mid_start = 0; /* redraw all lines */
1411 }
1412 else
1413 mid_start = 0; /* redraw all lines */
1414 }
1415 else
1416 mid_start = 0; /* redraw all lines */
1417 }
1418 else
1419 {
1420 /*
1421 * New topline is at or below old topline: May scroll up.
1422 * When topline didn't change, find first entry in w_lines[] that
1423 * needs updating.
1424 */
1425
1426 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1427 j = -1;
1428 row = 0;
1429 for (i = 0; i < wp->w_lines_valid; i++)
1430 {
1431 if (wp->w_lines[i].wl_valid
1432 && wp->w_lines[i].wl_lnum == wp->w_topline)
1433 {
1434 j = i;
1435 break;
1436 }
1437 row += wp->w_lines[i].wl_size;
1438 }
1439 if (j == -1)
1440 {
1441 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1442 * lines */
1443 mid_start = 0;
1444 }
1445 else
1446 {
1447 /*
1448 * Try to delete the correct number of lines.
1449 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1450 */
1451#ifdef FEAT_DIFF
1452 /* If the topline didn't change, delete old filler lines,
1453 * otherwise delete filler lines of the new topline... */
1454 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1455 row += wp->w_old_topfill;
1456 else
1457 row += diff_check_fill(wp, wp->w_topline);
1458 /* ... but don't delete new filler lines. */
1459 row -= wp->w_topfill;
1460#endif
1461 if (row > 0)
1462 {
1463 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001464 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1465 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 bot_start = wp->w_height - row;
1467 else
1468 mid_start = 0; /* redraw all lines */
1469 }
1470 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1471 {
1472 /*
1473 * Skip the lines (below the deleted lines) that are still
1474 * valid and don't need redrawing. Copy their info
1475 * upwards, to compensate for the deleted lines. Set
1476 * bot_start to the first row that needs redrawing.
1477 */
1478 bot_start = 0;
1479 idx = 0;
1480 for (;;)
1481 {
1482 wp->w_lines[idx] = wp->w_lines[j];
1483 /* stop at line that didn't fit, unless it is still
1484 * valid (no lines deleted) */
1485 if (row > 0 && bot_start + row
1486 + (int)wp->w_lines[j].wl_size > wp->w_height)
1487 {
1488 wp->w_lines_valid = idx + 1;
1489 break;
1490 }
1491 bot_start += wp->w_lines[idx++].wl_size;
1492
1493 /* stop at the last valid entry in w_lines[].wl_size */
1494 if (++j >= wp->w_lines_valid)
1495 {
1496 wp->w_lines_valid = idx;
1497 break;
1498 }
1499 }
1500#ifdef FEAT_DIFF
1501 /* Correct the first entry for filler lines at the top
1502 * when it won't get updated below. */
1503 if (wp->w_p_diff && bot_start > 0)
1504 wp->w_lines[0].wl_size =
1505 plines_win_nofill(wp, wp->w_topline, TRUE)
1506 + wp->w_topfill;
1507#endif
1508 }
1509 }
1510 }
1511
1512 /* When starting redraw in the first line, redraw all lines. When
1513 * there is only one window it's probably faster to clear the screen
1514 * first. */
1515 if (mid_start == 0)
1516 {
1517 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001518 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001519 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001520 /* Clear the screen when it was not done by win_del_lines() or
1521 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1522 * then. */
1523 if (screen_cleared != TRUE)
1524 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001525 /* The screen was cleared, redraw the tab pages line. */
1526 if (redraw_tabline)
1527 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001530
1531 /* When win_del_lines() or win_ins_lines() caused the screen to be
1532 * cleared (only happens for the first window) or when screenclear()
1533 * was called directly above, "must_redraw" will have been set to
1534 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1535 if (screen_cleared == TRUE)
1536 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 }
1538 else
1539 {
1540 /* Not VALID or INVERTED: redraw all lines. */
1541 mid_start = 0;
1542 mid_end = wp->w_height;
1543 }
1544
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001545 if (type == SOME_VALID)
1546 {
1547 /* SOME_VALID: redraw all lines. */
1548 mid_start = 0;
1549 mid_end = wp->w_height;
1550 type = NOT_VALID;
1551 }
1552
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 /* check if we are updating or removing the inverted part */
1554 if ((VIsual_active && buf == curwin->w_buffer)
1555 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1556 {
1557 linenr_T from, to;
1558
1559 if (VIsual_active)
1560 {
1561 if (VIsual_active
1562 && (VIsual_mode != wp->w_old_visual_mode
1563 || type == INVERTED_ALL))
1564 {
1565 /*
1566 * If the type of Visual selection changed, redraw the whole
1567 * selection. Also when the ownership of the X selection is
1568 * gained or lost.
1569 */
1570 if (curwin->w_cursor.lnum < VIsual.lnum)
1571 {
1572 from = curwin->w_cursor.lnum;
1573 to = VIsual.lnum;
1574 }
1575 else
1576 {
1577 from = VIsual.lnum;
1578 to = curwin->w_cursor.lnum;
1579 }
1580 /* redraw more when the cursor moved as well */
1581 if (wp->w_old_cursor_lnum < from)
1582 from = wp->w_old_cursor_lnum;
1583 if (wp->w_old_cursor_lnum > to)
1584 to = wp->w_old_cursor_lnum;
1585 if (wp->w_old_visual_lnum < from)
1586 from = wp->w_old_visual_lnum;
1587 if (wp->w_old_visual_lnum > to)
1588 to = wp->w_old_visual_lnum;
1589 }
1590 else
1591 {
1592 /*
1593 * Find the line numbers that need to be updated: The lines
1594 * between the old cursor position and the current cursor
1595 * position. Also check if the Visual position changed.
1596 */
1597 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1598 {
1599 from = curwin->w_cursor.lnum;
1600 to = wp->w_old_cursor_lnum;
1601 }
1602 else
1603 {
1604 from = wp->w_old_cursor_lnum;
1605 to = curwin->w_cursor.lnum;
1606 if (from == 0) /* Visual mode just started */
1607 from = to;
1608 }
1609
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001610 if (VIsual.lnum != wp->w_old_visual_lnum
1611 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
1613 if (wp->w_old_visual_lnum < from
1614 && wp->w_old_visual_lnum != 0)
1615 from = wp->w_old_visual_lnum;
1616 if (wp->w_old_visual_lnum > to)
1617 to = wp->w_old_visual_lnum;
1618 if (VIsual.lnum < from)
1619 from = VIsual.lnum;
1620 if (VIsual.lnum > to)
1621 to = VIsual.lnum;
1622 }
1623 }
1624
1625 /*
1626 * If in block mode and changed column or curwin->w_curswant:
1627 * update all lines.
1628 * First compute the actual start and end column.
1629 */
1630 if (VIsual_mode == Ctrl_V)
1631 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001632 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001633#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001634 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
Bram Moolenaar404406a2014-10-09 13:24:43 +02001636 if (curwin->w_p_lbr)
1637 ve_flags = VE_ALL;
1638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001640#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001641 ve_flags = save_ve_flags;
1642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 ++toc;
1644 if (curwin->w_curswant == MAXCOL)
1645 toc = MAXCOL;
1646
1647 if (fromc != wp->w_old_cursor_fcol
1648 || toc != wp->w_old_cursor_lcol)
1649 {
1650 if (from > VIsual.lnum)
1651 from = VIsual.lnum;
1652 if (to < VIsual.lnum)
1653 to = VIsual.lnum;
1654 }
1655 wp->w_old_cursor_fcol = fromc;
1656 wp->w_old_cursor_lcol = toc;
1657 }
1658 }
1659 else
1660 {
1661 /* Use the line numbers of the old Visual area. */
1662 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1663 {
1664 from = wp->w_old_cursor_lnum;
1665 to = wp->w_old_visual_lnum;
1666 }
1667 else
1668 {
1669 from = wp->w_old_visual_lnum;
1670 to = wp->w_old_cursor_lnum;
1671 }
1672 }
1673
1674 /*
1675 * There is no need to update lines above the top of the window.
1676 */
1677 if (from < wp->w_topline)
1678 from = wp->w_topline;
1679
1680 /*
1681 * If we know the value of w_botline, use it to restrict the update to
1682 * the lines that are visible in the window.
1683 */
1684 if (wp->w_valid & VALID_BOTLINE)
1685 {
1686 if (from >= wp->w_botline)
1687 from = wp->w_botline - 1;
1688 if (to >= wp->w_botline)
1689 to = wp->w_botline - 1;
1690 }
1691
1692 /*
1693 * Find the minimal part to be updated.
1694 * Watch out for scrolling that made entries in w_lines[] invalid.
1695 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1696 * top_end; need to redraw from top_end to the "to" line.
1697 * A middle mouse click with a Visual selection may change the text
1698 * above the Visual area and reset wl_valid, do count these for
1699 * mid_end (in srow).
1700 */
1701 if (mid_start > 0)
1702 {
1703 lnum = wp->w_topline;
1704 idx = 0;
1705 srow = 0;
1706 if (scrolled_down)
1707 mid_start = top_end;
1708 else
1709 mid_start = 0;
1710 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1711 {
1712 if (wp->w_lines[idx].wl_valid)
1713 mid_start += wp->w_lines[idx].wl_size;
1714 else if (!scrolled_down)
1715 srow += wp->w_lines[idx].wl_size;
1716 ++idx;
1717# ifdef FEAT_FOLDING
1718 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1719 lnum = wp->w_lines[idx].wl_lnum;
1720 else
1721# endif
1722 ++lnum;
1723 }
1724 srow += mid_start;
1725 mid_end = wp->w_height;
1726 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1727 {
1728 if (wp->w_lines[idx].wl_valid
1729 && wp->w_lines[idx].wl_lnum >= to + 1)
1730 {
1731 /* Only update until first row of this line */
1732 mid_end = srow;
1733 break;
1734 }
1735 srow += wp->w_lines[idx].wl_size;
1736 }
1737 }
1738 }
1739
1740 if (VIsual_active && buf == curwin->w_buffer)
1741 {
1742 wp->w_old_visual_mode = VIsual_mode;
1743 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1744 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001745 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 wp->w_old_curswant = curwin->w_curswant;
1747 }
1748 else
1749 {
1750 wp->w_old_visual_mode = 0;
1751 wp->w_old_cursor_lnum = 0;
1752 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001753 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
1756#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1757 /* reset got_int, otherwise regexp won't work */
1758 save_got_int = got_int;
1759 got_int = 0;
1760#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001761#ifdef SYN_TIME_LIMIT
1762 /* Set the time limit to 'redrawtime'. */
1763 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001764 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766#ifdef FEAT_FOLDING
1767 win_foldinfo.fi_level = 0;
1768#endif
1769
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001770#ifdef FEAT_MENU
1771 /*
1772 * Draw the window toolbar, if there is one.
1773 * TODO: only when needed.
1774 */
1775 if (winbar_height(wp) > 0)
1776 redraw_win_toolbar(wp);
1777#endif
1778
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 /*
1780 * Update all the window rows.
1781 */
1782 idx = 0; /* first entry in w_lines[].wl_size */
1783 row = 0;
1784 srow = 0;
1785 lnum = wp->w_topline; /* first line shown in window */
1786 for (;;)
1787 {
1788 /* stop updating when reached the end of the window (check for _past_
1789 * the end of the window is at the end of the loop) */
1790 if (row == wp->w_height)
1791 {
1792 didline = TRUE;
1793 break;
1794 }
1795
1796 /* stop updating when hit the end of the file */
1797 if (lnum > buf->b_ml.ml_line_count)
1798 {
1799 eof = TRUE;
1800 break;
1801 }
1802
1803 /* Remember the starting row of the line that is going to be dealt
1804 * with. It is used further down when the line doesn't fit. */
1805 srow = row;
1806
1807 /*
1808 * Update a line when it is in an area that needs updating, when it
1809 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001810 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 * win_del_lines(), check if the current line includes it.
1812 * When syntax folding is being used, the saved syntax states will
1813 * already have been updated, we can't see where the syntax state is
1814 * the same again, just update until the end of the window.
1815 */
1816 if (row < top_end
1817 || (row >= mid_start && row < mid_end)
1818#ifdef FEAT_SEARCH_EXTRA
1819 || top_to_mod
1820#endif
1821 || idx >= wp->w_lines_valid
1822 || (row + wp->w_lines[idx].wl_size > bot_start)
1823 || (mod_top != 0
1824 && (lnum == mod_top
1825 || (lnum >= mod_top
1826 && (lnum < mod_bot
1827#ifdef FEAT_SYN_HL
1828 || did_update == DID_FOLD
1829 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001830 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 && (
1832# ifdef FEAT_FOLDING
1833 (foldmethodIsSyntax(wp)
1834 && hasAnyFolding(wp)) ||
1835# endif
1836 syntax_check_changed(lnum)))
1837#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001838#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001839 /* match in fixed position might need redraw
1840 * if lines were inserted or deleted */
1841 || (wp->w_match_head != NULL
1842 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 )))))
1845 {
1846#ifdef FEAT_SEARCH_EXTRA
1847 if (lnum == mod_top)
1848 top_to_mod = FALSE;
1849#endif
1850
1851 /*
1852 * When at start of changed lines: May scroll following lines
1853 * up or down to minimize redrawing.
1854 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001855 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 */
1857 if (lnum == mod_top
1858 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001859 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
1861 int old_rows = 0;
1862 int new_rows = 0;
1863 int xtra_rows;
1864 linenr_T l;
1865
1866 /* Count the old number of window rows, using w_lines[], which
1867 * should still contain the sizes for the lines as they are
1868 * currently displayed. */
1869 for (i = idx; i < wp->w_lines_valid; ++i)
1870 {
1871 /* Only valid lines have a meaningful wl_lnum. Invalid
1872 * lines are part of the changed area. */
1873 if (wp->w_lines[i].wl_valid
1874 && wp->w_lines[i].wl_lnum == mod_bot)
1875 break;
1876 old_rows += wp->w_lines[i].wl_size;
1877#ifdef FEAT_FOLDING
1878 if (wp->w_lines[i].wl_valid
1879 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1880 {
1881 /* Must have found the last valid entry above mod_bot.
1882 * Add following invalid entries. */
1883 ++i;
1884 while (i < wp->w_lines_valid
1885 && !wp->w_lines[i].wl_valid)
1886 old_rows += wp->w_lines[i++].wl_size;
1887 break;
1888 }
1889#endif
1890 }
1891
1892 if (i >= wp->w_lines_valid)
1893 {
1894 /* We can't find a valid line below the changed lines,
1895 * need to redraw until the end of the window.
1896 * Inserting/deleting lines has no use. */
1897 bot_start = 0;
1898 }
1899 else
1900 {
1901 /* Able to count old number of rows: Count new window
1902 * rows, and may insert/delete lines */
1903 j = idx;
1904 for (l = lnum; l < mod_bot; ++l)
1905 {
1906#ifdef FEAT_FOLDING
1907 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1908 ++new_rows;
1909 else
1910#endif
1911#ifdef FEAT_DIFF
1912 if (l == wp->w_topline)
1913 new_rows += plines_win_nofill(wp, l, TRUE)
1914 + wp->w_topfill;
1915 else
1916#endif
1917 new_rows += plines_win(wp, l, TRUE);
1918 ++j;
1919 if (new_rows > wp->w_height - row - 2)
1920 {
1921 /* it's getting too much, must redraw the rest */
1922 new_rows = 9999;
1923 break;
1924 }
1925 }
1926 xtra_rows = new_rows - old_rows;
1927 if (xtra_rows < 0)
1928 {
1929 /* May scroll text up. If there is not enough
1930 * remaining text or scrolling fails, must redraw the
1931 * rest. If scrolling works, must redraw the text
1932 * below the scrolled text. */
1933 if (row - xtra_rows >= wp->w_height - 2)
1934 mod_bot = MAXLNUM;
1935 else
1936 {
1937 check_for_delay(FALSE);
1938 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001939 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 mod_bot = MAXLNUM;
1941 else
1942 bot_start = wp->w_height + xtra_rows;
1943 }
1944 }
1945 else if (xtra_rows > 0)
1946 {
1947 /* May scroll text down. If there is not enough
1948 * remaining text of scrolling fails, must redraw the
1949 * rest. */
1950 if (row + xtra_rows >= wp->w_height - 2)
1951 mod_bot = MAXLNUM;
1952 else
1953 {
1954 check_for_delay(FALSE);
1955 if (win_ins_lines(wp, row + old_rows,
1956 xtra_rows, FALSE, FALSE) == FAIL)
1957 mod_bot = MAXLNUM;
1958 else if (top_end > row + old_rows)
1959 /* Scrolled the part at the top that requires
1960 * updating down. */
1961 top_end += xtra_rows;
1962 }
1963 }
1964
1965 /* When not updating the rest, may need to move w_lines[]
1966 * entries. */
1967 if (mod_bot != MAXLNUM && i != j)
1968 {
1969 if (j < i)
1970 {
1971 int x = row + new_rows;
1972
1973 /* move entries in w_lines[] upwards */
1974 for (;;)
1975 {
1976 /* stop at last valid entry in w_lines[] */
1977 if (i >= wp->w_lines_valid)
1978 {
1979 wp->w_lines_valid = j;
1980 break;
1981 }
1982 wp->w_lines[j] = wp->w_lines[i];
1983 /* stop at a line that won't fit */
1984 if (x + (int)wp->w_lines[j].wl_size
1985 > wp->w_height)
1986 {
1987 wp->w_lines_valid = j + 1;
1988 break;
1989 }
1990 x += wp->w_lines[j++].wl_size;
1991 ++i;
1992 }
1993 if (bot_start > x)
1994 bot_start = x;
1995 }
1996 else /* j > i */
1997 {
1998 /* move entries in w_lines[] downwards */
1999 j -= i;
2000 wp->w_lines_valid += j;
2001 if (wp->w_lines_valid > wp->w_height)
2002 wp->w_lines_valid = wp->w_height;
2003 for (i = wp->w_lines_valid; i - j >= idx; --i)
2004 wp->w_lines[i] = wp->w_lines[i - j];
2005
2006 /* The w_lines[] entries for inserted lines are
2007 * now invalid, but wl_size may be used above.
2008 * Reset to zero. */
2009 while (i >= idx)
2010 {
2011 wp->w_lines[i].wl_size = 0;
2012 wp->w_lines[i--].wl_valid = FALSE;
2013 }
2014 }
2015 }
2016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 }
2018
2019#ifdef FEAT_FOLDING
2020 /*
2021 * When lines are folded, display one line for all of them.
2022 * Otherwise, display normally (can be several display lines when
2023 * 'wrap' is on).
2024 */
2025 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2026 if (fold_count != 0)
2027 {
2028 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2029 ++row;
2030 --fold_count;
2031 wp->w_lines[idx].wl_folded = TRUE;
2032 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2033# ifdef FEAT_SYN_HL
2034 did_update = DID_FOLD;
2035# endif
2036 }
2037 else
2038#endif
2039 if (idx < wp->w_lines_valid
2040 && wp->w_lines[idx].wl_valid
2041 && wp->w_lines[idx].wl_lnum == lnum
2042 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002043 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 && srow + wp->w_lines[idx].wl_size > wp->w_height
2045#ifdef FEAT_DIFF
2046 && diff_check_fill(wp, lnum) == 0
2047#endif
2048 )
2049 {
2050 /* This line is not going to fit. Don't draw anything here,
2051 * will draw "@ " lines below. */
2052 row = wp->w_height + 1;
2053 }
2054 else
2055 {
2056#ifdef FEAT_SEARCH_EXTRA
2057 prepare_search_hl(wp, lnum);
2058#endif
2059#ifdef FEAT_SYN_HL
2060 /* Let the syntax stuff know we skipped a few lines. */
2061 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002062 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 syntax_end_parsing(syntax_last_parsed + 1);
2064#endif
2065
2066 /*
2067 * Display one line.
2068 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002069 row = win_line(wp, lnum, srow, wp->w_height,
2070 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
2072#ifdef FEAT_FOLDING
2073 wp->w_lines[idx].wl_folded = FALSE;
2074 wp->w_lines[idx].wl_lastlnum = lnum;
2075#endif
2076#ifdef FEAT_SYN_HL
2077 did_update = DID_LINE;
2078 syntax_last_parsed = lnum;
2079#endif
2080 }
2081
2082 wp->w_lines[idx].wl_lnum = lnum;
2083 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002084
2085 /* Past end of the window or end of the screen. Note that after
2086 * resizing wp->w_height may be end up too big. That's a problem
2087 * elsewhere, but prevent a crash here. */
2088 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 {
2090 /* we may need the size of that too long line later on */
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 = plines_win(wp, lnum, TRUE);
2093 ++idx;
2094 break;
2095 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002096 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 wp->w_lines[idx].wl_size = row - srow;
2098 ++idx;
2099#ifdef FEAT_FOLDING
2100 lnum += fold_count + 1;
2101#else
2102 ++lnum;
2103#endif
2104 }
2105 else
2106 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002107 if (wp->w_p_rnu)
2108 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002109#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002110 // 'relativenumber' set: The text doesn't need to be drawn, but
2111 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002112 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2113 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002114 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002115 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002116#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002117 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002118 }
2119
2120 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 row += wp->w_lines[idx++].wl_size;
2122 if (row > wp->w_height) /* past end of screen */
2123 break;
2124#ifdef FEAT_FOLDING
2125 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2126#else
2127 ++lnum;
2128#endif
2129#ifdef FEAT_SYN_HL
2130 did_update = DID_NONE;
2131#endif
2132 }
2133
2134 if (lnum > buf->b_ml.ml_line_count)
2135 {
2136 eof = TRUE;
2137 break;
2138 }
2139 }
2140 /*
2141 * End of loop over all window lines.
2142 */
2143
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002144#ifdef FEAT_VTP
2145 /* Rewrite the character at the end of the screen line. */
2146 if (use_vtp())
2147 {
2148 int i;
2149
2150 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002151 if (enc_utf8)
2152 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2153 LineOffset[i] + screen_Columns) > 1)
2154 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2155 else
2156 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2157 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002158 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2159 }
2160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161
2162 if (idx > wp->w_lines_valid)
2163 wp->w_lines_valid = idx;
2164
2165#ifdef FEAT_SYN_HL
2166 /*
2167 * Let the syntax stuff know we stop parsing here.
2168 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002169 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 syntax_end_parsing(syntax_last_parsed + 1);
2171#endif
2172
2173 /*
2174 * If we didn't hit the end of the file, and we didn't finish the last
2175 * line we were working on, then the line didn't fit.
2176 */
2177 wp->w_empty_rows = 0;
2178#ifdef FEAT_DIFF
2179 wp->w_filler_rows = 0;
2180#endif
2181 if (!eof && !didline)
2182 {
2183 if (lnum == wp->w_topline)
2184 {
2185 /*
2186 * Single line that does not fit!
2187 * Don't overwrite it, it can be edited.
2188 */
2189 wp->w_botline = lnum + 1;
2190 }
2191#ifdef FEAT_DIFF
2192 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2193 {
2194 /* Window ends in filler lines. */
2195 wp->w_botline = lnum;
2196 wp->w_filler_rows = wp->w_height - srow;
2197 }
2198#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002199 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2200 {
2201 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2202
2203 /*
2204 * Last line isn't finished: Display "@@@" in the last screen line.
2205 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002206 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002207 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002208 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002209 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002210 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002211 set_empty_rows(wp, srow);
2212 wp->w_botline = lnum;
2213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2215 {
2216 /*
2217 * Last line isn't finished: Display "@@@" at the end.
2218 */
2219 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2220 W_WINROW(wp) + wp->w_height,
2221 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002222 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 set_empty_rows(wp, srow);
2224 wp->w_botline = lnum;
2225 }
2226 else
2227 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002228 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 wp->w_botline = lnum;
2230 }
2231 }
2232 else
2233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 if (eof) /* we hit the end of the file */
2236 {
2237 wp->w_botline = buf->b_ml.ml_line_count + 1;
2238#ifdef FEAT_DIFF
2239 j = diff_check_fill(wp, wp->w_botline);
2240 if (j > 0 && !wp->w_botfill)
2241 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002242 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 if (char2cells(fill_diff) > 1)
2244 i = '-';
2245 else
2246 i = fill_diff;
2247 if (row + j > wp->w_height)
2248 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002249 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 row += j;
2251 }
2252#endif
2253 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002254 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 wp->w_botline = lnum;
2256
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002257 // Make sure the rest of the screen is blank
2258 // put '~'s on rows that aren't part of the file.
2259 win_draw_end(wp, '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 }
2261
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002262#ifdef SYN_TIME_LIMIT
2263 syn_set_timeout(NULL);
2264#endif
2265
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 /* Reset the type of redrawing required, the window has been updated. */
2267 wp->w_redr_type = 0;
2268#ifdef FEAT_DIFF
2269 wp->w_old_topfill = wp->w_topfill;
2270 wp->w_old_botfill = wp->w_botfill;
2271#endif
2272
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002273 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
2275 /*
2276 * There is a trick with w_botline. If we invalidate it on each
2277 * change that might modify it, this will cause a lot of expensive
2278 * calls to plines() in update_topline() each time. Therefore the
2279 * value of w_botline is often approximated, and this value is used to
2280 * compute the value of w_topline. If the value of w_botline was
2281 * wrong, check that the value of w_topline is correct (cursor is on
2282 * the visible part of the text). If it's not, we need to redraw
2283 * again. Mostly this just means scrolling up a few lines, so it
2284 * doesn't look too bad. Only do this for the current window (where
2285 * changes are relevant).
2286 */
2287 wp->w_valid |= VALID_BOTLINE;
2288 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2289 {
2290 recursive = TRUE;
2291 curwin->w_valid &= ~VALID_TOPLINE;
2292 update_topline(); /* may invalidate w_botline again */
2293 if (must_redraw != 0)
2294 {
2295 /* Don't update for changes in buffer again. */
2296 i = curbuf->b_mod_set;
2297 curbuf->b_mod_set = FALSE;
2298 win_update(curwin);
2299 must_redraw = 0;
2300 curbuf->b_mod_set = i;
2301 }
2302 recursive = FALSE;
2303 }
2304 }
2305
2306#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2307 /* restore got_int, unless CTRL-C was hit while redrawing */
2308 if (!got_int)
2309 got_int = save_got_int;
2310#endif
2311}
2312
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002314 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2315 * Return the new offset.
2316 */
2317 static int
2318screen_fill_end(
2319 win_T *wp,
2320 int c1,
2321 int c2,
2322 int off,
2323 int width,
2324 int row,
2325 int endrow,
2326 int attr)
2327{
2328 int nn = off + width;
2329
2330 if (nn > wp->w_width)
2331 nn = wp->w_width;
2332#ifdef FEAT_RIGHTLEFT
2333 if (wp->w_p_rl)
2334 {
2335 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2336 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2337 c1, c2, attr);
2338 }
2339 else
2340#endif
2341 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2342 wp->w_wincol + off, (int)wp->w_wincol + nn,
2343 c1, c2, attr);
2344 return nn;
2345}
2346
2347/*
2348 * Clear lines near the end the window and mark the unused lines with "c1".
2349 * use "c2" as the filler character.
2350 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 */
2352 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002353win_draw_end(
2354 win_T *wp,
2355 int c1,
2356 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002357 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002358 int row,
2359 int endrow,
2360 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 int n = 0;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002363
2364 if (draw_margin)
2365 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002366#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002367 int fdc = compute_foldcolumn(wp, 0);
2368
2369 if (fdc > 0)
2370 // draw the fold column
2371 n = screen_fill_end(wp, ' ', ' ', n, fdc,
2372 row, endrow, HL_ATTR(HLF_FC));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002373#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002374#ifdef FEAT_SIGNS
2375 if (signcolumn_on(wp))
2376 // draw the sign column
2377 n = screen_fill_end(wp, ' ', ' ', n, 2,
2378 row, endrow, HL_ATTR(HLF_SC));
2379#endif
2380 if ((wp->w_p_nu || wp->w_p_rnu)
2381 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2382 // draw the number column
2383 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
2384 row, endrow, HL_ATTR(HLF_N));
2385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
2387#ifdef FEAT_RIGHTLEFT
2388 if (wp->w_p_rl)
2389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002391 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002392 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002394 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002395 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397 else
2398#endif
2399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002401 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002402 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002404
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 set_empty_rows(wp, row);
2406}
2407
Bram Moolenaar1a384422010-07-14 19:53:30 +02002408#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002409/*
2410 * Advance **color_cols and return TRUE when there are columns to draw.
2411 */
2412 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002413advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002414{
2415 while (**color_cols >= 0 && vcol > **color_cols)
2416 ++*color_cols;
2417 return (**color_cols >= 0);
2418}
2419#endif
2420
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002421#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2422/*
2423 * Copy "text" to ScreenLines using "attr".
2424 * Returns the next screen column.
2425 */
2426 static int
2427text_to_screenline(win_T *wp, char_u *text, int col)
2428{
2429 int off = (int)(current_ScreenLine - ScreenLines);
2430
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002431 if (has_mbyte)
2432 {
2433 int cells;
2434 int u8c, u8cc[MAX_MCO];
2435 int i;
2436 int idx;
2437 int c_len;
2438 char_u *p;
2439# ifdef FEAT_ARABIC
2440 int prev_c = 0; /* previous Arabic character */
2441 int prev_c1 = 0; /* first composing char for prev_c */
2442# endif
2443
2444# ifdef FEAT_RIGHTLEFT
2445 if (wp->w_p_rl)
2446 idx = off;
2447 else
2448# endif
2449 idx = off + col;
2450
2451 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2452 for (p = text; *p != NUL; )
2453 {
2454 cells = (*mb_ptr2cells)(p);
2455 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002456 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002457# ifdef FEAT_RIGHTLEFT
2458 - (wp->w_p_rl ? col : 0)
2459# endif
2460 )
2461 break;
2462 ScreenLines[idx] = *p;
2463 if (enc_utf8)
2464 {
2465 u8c = utfc_ptr2char(p, u8cc);
2466 if (*p < 0x80 && u8cc[0] == 0)
2467 {
2468 ScreenLinesUC[idx] = 0;
2469#ifdef FEAT_ARABIC
2470 prev_c = u8c;
2471#endif
2472 }
2473 else
2474 {
2475#ifdef FEAT_ARABIC
2476 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2477 {
2478 /* Do Arabic shaping. */
2479 int pc, pc1, nc;
2480 int pcc[MAX_MCO];
2481 int firstbyte = *p;
2482
2483 /* The idea of what is the previous and next
2484 * character depends on 'rightleft'. */
2485 if (wp->w_p_rl)
2486 {
2487 pc = prev_c;
2488 pc1 = prev_c1;
2489 nc = utf_ptr2char(p + c_len);
2490 prev_c1 = u8cc[0];
2491 }
2492 else
2493 {
2494 pc = utfc_ptr2char(p + c_len, pcc);
2495 nc = prev_c;
2496 pc1 = pcc[0];
2497 }
2498 prev_c = u8c;
2499
2500 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2501 pc, pc1, nc);
2502 ScreenLines[idx] = firstbyte;
2503 }
2504 else
2505 prev_c = u8c;
2506#endif
2507 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002508 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002509 for (i = 0; i < Screen_mco; ++i)
2510 {
2511 ScreenLinesC[i][idx] = u8cc[i];
2512 if (u8cc[i] == 0)
2513 break;
2514 }
2515 }
2516 if (cells > 1)
2517 ScreenLines[idx + 1] = 0;
2518 }
2519 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2520 /* double-byte single width character */
2521 ScreenLines2[idx] = p[1];
2522 else if (cells > 1)
2523 /* double-width character */
2524 ScreenLines[idx + 1] = p[1];
2525 col += cells;
2526 idx += cells;
2527 p += c_len;
2528 }
2529 }
2530 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002531 {
2532 int len = (int)STRLEN(text);
2533
Bram Moolenaar02631462017-09-22 15:20:32 +02002534 if (len > wp->w_width - col)
2535 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002536 if (len > 0)
2537 {
2538#ifdef FEAT_RIGHTLEFT
2539 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002540 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002541 else
2542#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002543 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002544 col += len;
2545 }
2546 }
2547 return col;
2548}
2549#endif
2550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551#ifdef FEAT_FOLDING
2552/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002553 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2554 * space is available for window "wp", minus "col".
2555 */
2556 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002557compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002558{
2559 int fdc = wp->w_p_fdc;
2560 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002561 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002562
2563 if (fdc > wwidth - (col + wmw))
2564 fdc = wwidth - (col + wmw);
2565 return fdc;
2566}
2567
2568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 * Display one folded line.
2570 */
2571 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002572fold_line(
2573 win_T *wp,
2574 long fold_count,
2575 foldinfo_T *foldinfo,
2576 linenr_T lnum,
2577 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002579 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 pos_T *top, *bot;
2581 linenr_T lnume = lnum + fold_count - 1;
2582 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002583 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 int col;
2586 int txtcol;
2587 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002588 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
2590 /* Build the fold line:
2591 * 1. Add the cmdwin_type for the command-line window
2592 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002593 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 * 4. Compose the text
2595 * 5. Add the text
2596 * 6. set highlighting for the Visual area an other text
2597 */
2598 col = 0;
2599
2600 /*
2601 * 1. Add the cmdwin_type for the command-line window
2602 * Ignores 'rightleft', this window is never right-left.
2603 */
2604#ifdef FEAT_CMDWIN
2605 if (cmdwin_type != 0 && wp == curwin)
2606 {
2607 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002608 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 if (enc_utf8)
2610 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 ++col;
2612 }
2613#endif
2614
2615 /*
2616 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002617 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002619 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 if (fdc > 0)
2621 {
2622 fill_foldcolumn(buf, wp, TRUE, lnum);
2623#ifdef FEAT_RIGHTLEFT
2624 if (wp->w_p_rl)
2625 {
2626 int i;
2627
Bram Moolenaar02631462017-09-22 15:20:32 +02002628 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002629 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 /* reverse the fold column */
2631 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002632 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 }
2634 else
2635#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002636 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 col += fdc;
2638 }
2639
2640#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002641# define RL_MEMSET(p, v, l) \
2642 do { \
2643 if (wp->w_p_rl) \
2644 for (ri = 0; ri < l; ++ri) \
2645 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2646 else \
2647 for (ri = 0; ri < l; ++ri) \
2648 ScreenAttrs[off + (p) + ri] = v; \
2649 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002651# define RL_MEMSET(p, v, l) \
2652 do { \
2653 for (ri = 0; ri < l; ++ri) \
2654 ScreenAttrs[off + (p) + ri] = v; \
2655 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656#endif
2657
Bram Moolenaar64486672010-05-16 15:46:46 +02002658 /* Set all attributes of the 'number' or 'relativenumber' column and the
2659 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002660 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
2662#ifdef FEAT_SIGNS
2663 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002664 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002666 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if (len > 0)
2668 {
2669 if (len > 2)
2670 len = 2;
2671# ifdef FEAT_RIGHTLEFT
2672 if (wp->w_p_rl)
2673 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002674 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002675 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 else
2677# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002678 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 col += len;
2680 }
2681 }
2682#endif
2683
2684 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002685 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002687 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002689 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 if (len > 0)
2691 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002692 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002693 long num;
2694 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002695
2696 if (len > w + 1)
2697 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002698
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002699 if (wp->w_p_nu && !wp->w_p_rnu)
2700 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002701 num = (long)lnum;
2702 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002703 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002704 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002705 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002706 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002707 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002708 /* 'number' + 'relativenumber': cursor line shows absolute
2709 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002710 num = lnum;
2711 fmt = "%-*ld ";
2712 }
2713 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002714
Bram Moolenaar700e7342013-01-30 12:31:36 +01002715 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716#ifdef FEAT_RIGHTLEFT
2717 if (wp->w_p_rl)
2718 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002719 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002720 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 else
2722#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002723 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 col += len;
2725 }
2726 }
2727
2728 /*
2729 * 4. Compose the folded-line string with 'foldtext', if set.
2730 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002731 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
2733 txtcol = col; /* remember where text starts */
2734
2735 /*
2736 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2737 * Right-left text is put in columns 0 - number-col, normal text is put
2738 * in columns number-col - window-width.
2739 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002740 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
2742 /* Fill the rest of the line with the fold filler */
2743#ifdef FEAT_RIGHTLEFT
2744 if (wp->w_p_rl)
2745 col -= txtcol;
2746#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002747 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748#ifdef FEAT_RIGHTLEFT
2749 - (wp->w_p_rl ? txtcol : 0)
2750#endif
2751 )
2752 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 if (enc_utf8)
2754 {
2755 if (fill_fold >= 0x80)
2756 {
2757 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002758 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002759 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
2761 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002762 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002764 ScreenLines[off + col] = fill_fold;
2765 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002766 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002768 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002769 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
2771
2772 if (text != buf)
2773 vim_free(text);
2774
2775 /*
2776 * 6. set highlighting for the Visual area an other text.
2777 * If all folded lines are in the Visual area, highlight the line.
2778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2780 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002781 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 {
2783 /* Visual is after curwin->w_cursor */
2784 top = &curwin->w_cursor;
2785 bot = &VIsual;
2786 }
2787 else
2788 {
2789 /* Visual is before curwin->w_cursor */
2790 top = &VIsual;
2791 bot = &curwin->w_cursor;
2792 }
2793 if (lnum >= top->lnum
2794 && lnume <= bot->lnum
2795 && (VIsual_mode != 'v'
2796 || ((lnum > top->lnum
2797 || (lnum == top->lnum
2798 && top->col == 0))
2799 && (lnume < bot->lnum
2800 || (lnume == bot->lnum
2801 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002802 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 {
2804 if (VIsual_mode == Ctrl_V)
2805 {
2806 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002807 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002809 if (wp->w_old_cursor_lcol != MAXCOL
2810 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002811 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 len = wp->w_old_cursor_lcol;
2813 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002814 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002815 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002816 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 }
2818 }
2819 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002820 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002822 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
2825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002827#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002828 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2829 if (wp->w_p_cc_cols)
2830 {
2831 int i = 0;
2832 int j = wp->w_p_cc_cols[i];
2833 int old_txtcol = txtcol;
2834
2835 while (j > -1)
2836 {
2837 txtcol += j;
2838 if (wp->w_p_wrap)
2839 txtcol -= wp->w_skipcol;
2840 else
2841 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002842 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002843 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002844 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002845 txtcol = old_txtcol;
2846 j = wp->w_p_cc_cols[++i];
2847 }
2848 }
2849
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002850 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002851 if (wp->w_p_cuc)
2852 {
2853 txtcol += wp->w_virtcol;
2854 if (wp->w_p_wrap)
2855 txtcol -= wp->w_skipcol;
2856 else
2857 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002858 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002859 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002860 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002861 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863
Bram Moolenaar02631462017-09-22 15:20:32 +02002864 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2865 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
2867 /*
2868 * Update w_cline_height and w_cline_folded if the cursor line was
2869 * updated (saves a call to plines() later).
2870 */
2871 if (wp == curwin
2872 && lnum <= curwin->w_cursor.lnum
2873 && lnume >= curwin->w_cursor.lnum)
2874 {
2875 curwin->w_cline_row = row;
2876 curwin->w_cline_height = 1;
2877 curwin->w_cline_folded = TRUE;
2878 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2879 }
2880}
2881
2882/*
2883 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2884 */
2885 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002886copy_text_attr(
2887 int off,
2888 char_u *buf,
2889 int len,
2890 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002892 int i;
2893
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 if (enc_utf8)
2896 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002897 for (i = 0; i < len; ++i)
2898 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899}
2900
2901/*
2902 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002903 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 */
2905 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002906fill_foldcolumn(
2907 char_u *p,
2908 win_T *wp,
2909 int closed, /* TRUE of FALSE */
2910 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911{
2912 int i = 0;
2913 int level;
2914 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002915 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002916 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917
2918 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002919 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920
2921 level = win_foldinfo.fi_level;
2922 if (level > 0)
2923 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002924 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002925 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002926
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 /* If the column is too narrow, we start at the lowest level that
2928 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002929 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 if (first_level < 1)
2931 first_level = 1;
2932
Bram Moolenaar1c934292015-01-27 16:39:29 +01002933 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 {
2935 if (win_foldinfo.fi_lnum == lnum
2936 && first_level + i >= win_foldinfo.fi_low_level)
2937 p[i] = '-';
2938 else if (first_level == 1)
2939 p[i] = '|';
2940 else if (first_level + i <= 9)
2941 p[i] = '0' + first_level + i;
2942 else
2943 p[i] = '>';
2944 if (first_level + i == level)
2945 break;
2946 }
2947 }
2948 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002949 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950}
2951#endif /* FEAT_FOLDING */
2952
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01002953#ifdef FEAT_TEXT_PROP
2954static textprop_T *current_text_props = NULL;
2955static buf_T *current_buf = NULL;
2956
2957 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01002958text_prop_compare(const void *s1, const void *s2)
2959{
2960 int idx1, idx2;
2961 proptype_T *pt1, *pt2;
2962 colnr_T col1, col2;
2963
2964 idx1 = *(int *)s1;
2965 idx2 = *(int *)s2;
2966 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
2967 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
2968 if (pt1 == pt2)
2969 return 0;
2970 if (pt1 == NULL)
2971 return -1;
2972 if (pt2 == NULL)
2973 return 1;
2974 if (pt1->pt_priority != pt2->pt_priority)
2975 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
2976 col1 = current_text_props[idx1].tp_col;
2977 col2 = current_text_props[idx2].tp_col;
2978 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
2979}
2980#endif
2981
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982/*
2983 * Display line "lnum" of window 'wp' on the screen.
2984 * Start at row "startrow", stop when "endrow" is reached.
2985 * wp->w_virtcol needs to be valid.
2986 *
2987 * Return the number of last row the line occupies.
2988 */
2989 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002990win_line(
2991 win_T *wp,
2992 linenr_T lnum,
2993 int startrow,
2994 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002995 int nochange UNUSED, // not updating for changed text
2996 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01002998 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3000 int c = 0; /* init for GCC */
3001 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003002#ifdef FEAT_LINEBREAK
3003 long vcol_sbr = -1; /* virtual column after showbreak */
3004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 long vcol_prev = -1; /* "vcol" of previous character */
3006 char_u *line; /* current line */
3007 char_u *ptr; /* current position in "line" */
3008 int row; /* row in the window, excl w_winrow */
3009 int screen_row; /* row on the screen, incl w_winrow */
3010
3011 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3012 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003013 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003014 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003016 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 int extra_attr = 0; /* attributes when n_extra != 0 */
3018 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3019 displaying lcs_eol at end-of-line */
3020 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3021 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3022
3023 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3024 int saved_n_extra = 0;
3025 char_u *saved_p_extra = NULL;
3026 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003027 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 int saved_char_attr = 0;
3029
3030 int n_attr = 0; /* chars with special attr */
3031 int saved_attr2 = 0; /* char_attr saved for n_attr */
3032 int n_attr3 = 0; /* chars with overruling special attr */
3033 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3034
3035 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3036
3037 int fromcol, tocol; /* start/end of inverting */
3038 int fromcol_prev = -2; /* start of inverting after cursor */
3039 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003041 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 pos_T pos;
3043 long v;
3044
3045 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003046 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3048 in this line */
3049 int attr = 0; /* attributes for area highlighting */
3050 int area_attr = 0; /* attributes desired by highlighting */
3051 int search_attr = 0; /* attributes desired by 'hlsearch' */
3052#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003053 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 int syntax_attr = 0; /* attributes desired by syntax */
3055 int has_syntax = FALSE; /* this buffer has syntax highl. */
3056 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003057 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003058 int draw_color_col = FALSE; /* highlight colorcolumn */
3059 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003060#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003061#ifdef FEAT_TEXT_PROP
3062 int text_prop_count;
3063 int text_prop_next = 0; // next text property to use
3064 textprop_T *text_props = NULL;
3065 int *text_prop_idxs = NULL;
3066 int text_props_active = 0;
3067 proptype_T *text_prop_type = NULL;
3068 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003069 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003070#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003071#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003072 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003073# define SPWORDLEN 150
3074 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003075 int nextlinecol = 0; /* column where nextline[] starts */
3076 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003077 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003078 int spell_attr = 0; /* attributes desired by spelling */
3079 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003080 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3081 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003082 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003083 static int cap_col = -1; /* column to check for Cap word */
3084 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003085 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003087 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 int multi_attr = 0; /* attributes desired by multibyte */
3089 int mb_l = 1; /* multi-byte byte length */
3090 int mb_c = 0; /* decoded multi-byte character */
3091 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003092 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093#ifdef FEAT_DIFF
3094 int filler_lines; /* nr of filler lines to be drawn */
3095 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003096 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 int change_start = MAXCOL; /* first col of changed area */
3098 int change_end = -1; /* last col of changed area */
3099#endif
3100 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3101#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003102 int need_showbreak = FALSE; /* overlong line, skipping first x
3103 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003105#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003106 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003108 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109#endif
3110#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003111 matchitem_T *cur; /* points to the match list */
3112 match_T *shl; /* points to search_hl or a match */
3113 int shl_flag; /* flag to indicate whether search_hl
3114 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003115 int pos_inprogress; /* marks that position match search is
3116 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003117 int prevcol_hl_flag; /* flag to indicate whether prevcol
3118 equals startcol of search_hl or one
3119 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120#endif
3121#ifdef FEAT_ARABIC
3122 int prev_c = 0; /* previous Arabic character */
3123 int prev_c1 = 0; /* first composing char for prev_c */
3124#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003125#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003126 int did_line_attr = 0;
3127#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003128#ifdef FEAT_TERMINAL
3129 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003130 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132
3133 /* draw_state: items that are drawn in sequence: */
3134#define WL_START 0 /* nothing done yet */
3135#ifdef FEAT_CMDWIN
3136# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3137#else
3138# define WL_CMDLINE WL_START
3139#endif
3140#ifdef FEAT_FOLDING
3141# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3142#else
3143# define WL_FOLD WL_CMDLINE
3144#endif
3145#ifdef FEAT_SIGNS
3146# define WL_SIGN WL_FOLD + 1 /* column for signs */
3147#else
3148# define WL_SIGN WL_FOLD /* column for signs */
3149#endif
3150#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003151#ifdef FEAT_LINEBREAK
3152# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003154# define WL_BRI WL_NR
3155#endif
3156#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3157# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3158#else
3159# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160#endif
3161#define WL_LINE WL_SBR + 1 /* text in the line */
3162 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003163#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 int feedback_col = 0;
3165 int feedback_old_attr = -1;
3166#endif
3167
Bram Moolenaar860cae12010-06-05 23:22:07 +02003168#ifdef FEAT_CONCEAL
3169 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003170 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003171 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003172 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003173 int is_concealing = FALSE;
3174 int boguscols = 0; /* nonexistent columns added to force
3175 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003176 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003177 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003178 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003179 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003180# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003181# define FIX_FOR_BOGUSCOLS \
3182 { \
3183 n_extra += vcol_off; \
3184 vcol -= vcol_off; \
3185 vcol_off = 0; \
3186 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003187 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003188 boguscols = 0; \
3189 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003190#else
3191# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
3194 if (startrow > endrow) /* past the end already! */
3195 return startrow;
3196
3197 row = startrow;
3198 screen_row = row + W_WINROW(wp);
3199
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003200 if (!number_only)
3201 {
3202 /*
3203 * To speed up the loop below, set extra_check when there is linebreak,
3204 * trailing white space and/or syntax processing to be done.
3205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003207 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208#endif
3209#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003210 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003211# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003212 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003213# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003214 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003216 /* Prepare for syntax highlighting in this line. When there is an
3217 * error, stop syntax highlighting. */
3218 save_did_emsg = did_emsg;
3219 did_emsg = FALSE;
3220 syntax_start(wp, lnum);
3221 if (did_emsg)
3222 wp->w_s->b_syn_error = TRUE;
3223 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003224 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003225 did_emsg = save_did_emsg;
3226#ifdef SYN_TIME_LIMIT
3227 if (!wp->w_s->b_syn_slow)
3228#endif
3229 {
3230 has_syntax = TRUE;
3231 extra_check = TRUE;
3232 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003235
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003236 /* Check for columns to display for 'colorcolumn'. */
3237 color_cols = wp->w_p_cc_cols;
3238 if (color_cols != NULL)
3239 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003240#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003241
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003242#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003243 if (term_show_buffer(wp->w_buffer))
3244 {
3245 extra_check = TRUE;
3246 get_term_attr = TRUE;
3247 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3248 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003249#endif
3250
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003251#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003252 if (wp->w_p_spell
3253 && *wp->w_s->b_p_spl != NUL
3254 && wp->w_s->b_langp.ga_len > 0
3255 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003256 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003257 /* Prepare for spell checking. */
3258 has_spell = TRUE;
3259 extra_check = TRUE;
3260
Bram Moolenaar7701f302018-10-02 21:20:32 +02003261 /* Get the start of the next line, so that words that wrap to the
3262 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003263 * Trick: skip a few chars for C/shell/Vim comments */
3264 nextline[SPWORDLEN] = NUL;
3265 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3266 {
3267 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3268 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3269 }
3270
Bram Moolenaar7701f302018-10-02 21:20:32 +02003271 /* When a word wrapped from the previous line the start of the
3272 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003273 if (lnum == checked_lnum)
3274 cur_checked_col = checked_col;
3275 checked_lnum = 0;
3276
3277 /* When there was a sentence end in the previous line may require a
3278 * word starting with capital in this line. In line 1 always check
3279 * the first word. */
3280 if (lnum != capcol_lnum)
3281 cap_col = -1;
3282 if (lnum == 1)
3283 cap_col = 0;
3284 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286#endif
3287
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003288 /*
3289 * handle visual active in this window
3290 */
3291 fromcol = -10;
3292 tocol = MAXCOL;
3293 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003295 /* Visual is after curwin->w_cursor */
3296 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003298 top = &curwin->w_cursor;
3299 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003301 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003303 top = &VIsual;
3304 bot = &curwin->w_cursor;
3305 }
3306 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3307 if (VIsual_mode == Ctrl_V) /* block mode */
3308 {
3309 if (lnum_in_visual_area)
3310 {
3311 fromcol = wp->w_old_cursor_fcol;
3312 tocol = wp->w_old_cursor_lcol;
3313 }
3314 }
3315 else /* non-block mode */
3316 {
3317 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003319 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003321 if (VIsual_mode == 'V') /* linewise */
3322 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 else
3324 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003325 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3326 if (gchar_pos(top) == NUL)
3327 tocol = fromcol + 1;
3328 }
3329 }
3330 if (VIsual_mode != 'V' && lnum == bot->lnum)
3331 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003332 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003333 {
3334 fromcol = -10;
3335 tocol = MAXCOL;
3336 }
3337 else if (bot->col == MAXCOL)
3338 tocol = MAXCOL;
3339 else
3340 {
3341 pos = *bot;
3342 if (*p_sel == 'e')
3343 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3344 else
3345 {
3346 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3347 ++tocol;
3348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 }
3350 }
3351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003353 /* Check if the character under the cursor should not be inverted */
3354 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003355#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003356 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003357#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003358 )
3359 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003361 /* if inverting in this line set area_highlighting */
3362 if (fromcol >= 0)
3363 {
3364 area_highlighting = TRUE;
3365 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003367 if ((clip_star.available && !clip_star.owned
3368 && clip_isautosel_star())
3369 || (clip_plus.available && !clip_plus.owned
3370 && clip_isautosel_plus()))
3371 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003376 /*
3377 * handle 'incsearch' and ":s///c" highlighting
3378 */
3379 else if (highlight_match
3380 && wp == curwin
3381 && lnum >= curwin->w_cursor.lnum
3382 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003384 if (lnum == curwin->w_cursor.lnum)
3385 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003386 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003387 else
3388 fromcol = 0;
3389 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3390 {
3391 pos.lnum = lnum;
3392 pos.col = search_match_endcol;
3393 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3394 }
3395 else
3396 tocol = MAXCOL;
3397 /* do at least one character; happens when past end of line */
3398 if (fromcol == tocol)
3399 tocol = fromcol + 1;
3400 area_highlighting = TRUE;
3401 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 }
3404
3405#ifdef FEAT_DIFF
3406 filler_lines = diff_check(wp, lnum);
3407 if (filler_lines < 0)
3408 {
3409 if (filler_lines == -1)
3410 {
3411 if (diff_find_change(wp, lnum, &change_start, &change_end))
3412 diff_hlf = HLF_ADD; /* added line */
3413 else if (change_start == 0)
3414 diff_hlf = HLF_TXD; /* changed text */
3415 else
3416 diff_hlf = HLF_CHD; /* changed line */
3417 }
3418 else
3419 diff_hlf = HLF_ADD; /* added line */
3420 filler_lines = 0;
3421 area_highlighting = TRUE;
3422 }
3423 if (lnum == wp->w_topline)
3424 filler_lines = wp->w_topfill;
3425 filler_todo = filler_lines;
3426#endif
3427
3428#ifdef LINE_ATTR
3429# ifdef FEAT_SIGNS
3430 /* If this line has a sign with line highlighting set line_attr. */
3431 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3432 if (v != 0)
3433 line_attr = sign_get_attr((int)v, TRUE);
3434# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003435# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003437 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003438 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439# endif
3440 if (line_attr != 0)
3441 area_highlighting = TRUE;
3442#endif
3443
3444 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3445 ptr = line;
3446
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003447#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003448 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003449 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003450 /* For checking first word with a capital skip white space. */
3451 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003452 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003453
Bram Moolenaar30abd282005-06-22 22:35:10 +00003454 /* To be able to spell-check over line boundaries copy the end of the
3455 * current line into nextline[]. Above the start of the next line was
3456 * copied to nextline[SPWORDLEN]. */
3457 if (nextline[SPWORDLEN] == NUL)
3458 {
3459 /* No next line or it is empty. */
3460 nextlinecol = MAXCOL;
3461 nextline_idx = 0;
3462 }
3463 else
3464 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003465 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003466 if (v < SPWORDLEN)
3467 {
3468 /* Short line, use it completely and append the start of the
3469 * next line. */
3470 nextlinecol = 0;
3471 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003472 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003473 nextline_idx = v + 1;
3474 }
3475 else
3476 {
3477 /* Long line, use only the last SPWORDLEN bytes. */
3478 nextlinecol = v - SPWORDLEN;
3479 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3480 nextline_idx = SPWORDLEN + 1;
3481 }
3482 }
3483 }
3484#endif
3485
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003486 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003488 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003489 extra_check = TRUE;
3490 /* find start of trailing whitespace */
3491 if (lcs_trail)
3492 {
3493 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003494 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003495 --trailcol;
3496 trailcol += (colnr_T) (ptr - line);
3497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
3499
3500 /*
3501 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3502 * first character to be displayed.
3503 */
3504 if (wp->w_p_wrap)
3505 v = wp->w_skipcol;
3506 else
3507 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003508 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003511
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 while (vcol < v && *ptr != NUL)
3513 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003514 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003517 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
3519
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003520 /* When:
3521 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003522 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003523 * - 'virtualedit' is set, or
3524 * - the visual mode is active,
3525 * the end of the line may be before the start of the displayed part.
3526 */
3527 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003528#ifdef FEAT_SYN_HL
3529 wp->w_p_cuc || draw_color_col ||
3530#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003531 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003532 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534
3535 /* Handle a character that's not completely on the screen: Put ptr at
3536 * that character but skip the first few screen characters. */
3537 if (vcol > v)
3538 {
3539 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003541 /* If the character fits on the screen, don't need to skip it.
3542 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003543 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003544 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
3546
3547 /*
3548 * Adjust for when the inverted text is before the screen,
3549 * and when the start of the inverted text is before the screen.
3550 */
3551 if (tocol <= vcol)
3552 fromcol = 0;
3553 else if (fromcol >= 0 && fromcol < vcol)
3554 fromcol = vcol;
3555
3556#ifdef FEAT_LINEBREAK
3557 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3558 if (wp->w_p_wrap)
3559 need_showbreak = TRUE;
3560#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003561#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003562 /* When spell checking a word we need to figure out the start of the
3563 * word and if it's badly spelled or not. */
3564 if (has_spell)
3565 {
3566 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003567 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003568 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003569
3570 pos = wp->w_cursor;
3571 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003572 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003573 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003574
3575 /* spell_move_to() may call ml_get() and make "line" invalid */
3576 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3577 ptr = line + linecol;
3578
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003579 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003580 {
3581 /* no bad word found at line start, don't check until end of a
3582 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003583 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003584 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003585 }
3586 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003587 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003588 /* bad word found, use attributes until end of word */
3589 word_end = wp->w_cursor.col + len + 1;
3590
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003591 /* Turn index into actual attributes. */
3592 if (spell_hlf != HLF_COUNT)
3593 spell_attr = highlight_attr[spell_hlf];
3594 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003595 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003596
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003597# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003598 /* Need to restart syntax highlighting for this line. */
3599 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003600 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003601# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003602 }
3603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 }
3605
3606 /*
3607 * Correct highlighting for cursor that can't be disabled.
3608 * Avoids having to check this for each character.
3609 */
3610 if (fromcol >= 0)
3611 {
3612 if (noinvcur)
3613 {
3614 if ((colnr_T)fromcol == wp->w_virtcol)
3615 {
3616 /* highlighting starts at cursor, let it start just after the
3617 * cursor */
3618 fromcol_prev = fromcol;
3619 fromcol = -1;
3620 }
3621 else if ((colnr_T)fromcol < wp->w_virtcol)
3622 /* restart highlighting after the cursor */
3623 fromcol_prev = wp->w_virtcol;
3624 }
3625 if (fromcol >= tocol)
3626 fromcol = -1;
3627 }
3628
3629#ifdef FEAT_SEARCH_EXTRA
3630 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003631 * Handle highlighting the last used search pattern and matches.
3632 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003634 cur = wp->w_match_head;
3635 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003636 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003638 if (shl_flag == FALSE)
3639 {
3640 shl = &search_hl;
3641 shl_flag = TRUE;
3642 }
3643 else
3644 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003645 shl->startcol = MAXCOL;
3646 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003648 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003649 v = (long)(ptr - line);
3650 if (cur != NULL)
3651 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003652 next_search_hl(wp, shl, lnum, (colnr_T)v,
3653 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003654
3655 /* Need to get the line again, a multi-line regexp may have made it
3656 * invalid. */
3657 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3658 ptr = line + v;
3659
3660 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003662 if (shl->lnum == lnum)
3663 shl->startcol = shl->rm.startpos[0].col;
3664 else
3665 shl->startcol = 0;
3666 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3667 - shl->rm.startpos[0].lnum)
3668 shl->endcol = shl->rm.endpos[0].col;
3669 else
3670 shl->endcol = MAXCOL;
3671 /* Highlight one character for an empty match. */
3672 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003674 if (has_mbyte && line[shl->endcol] != NUL)
3675 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3676 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003677 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003679 if ((long)shl->startcol < v) /* match at leftcol */
3680 {
3681 shl->attr_cur = shl->attr;
3682 search_attr = shl->attr;
3683 }
3684 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003686 if (shl != &search_hl && cur != NULL)
3687 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
3689#endif
3690
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003691#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003692 // Cursor line highlighting for 'cursorline' in the current window.
3693 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003694 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003695 // Do not show the cursor line when Visual mode is active, because it's
3696 // not clear what is selected then. Do update w_last_cursorline.
3697 if (!(wp == curwin && VIsual_active))
3698 {
3699 line_attr = HL_ATTR(HLF_CUL);
3700 area_highlighting = TRUE;
3701 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003702 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003703 }
3704#endif
3705
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003706#ifdef FEAT_TEXT_PROP
3707 {
3708 char_u *prop_start;
3709
3710 text_prop_count = get_text_props(wp->w_buffer, lnum,
3711 &prop_start, FALSE);
3712 if (text_prop_count > 0)
3713 {
3714 // Make a copy of the properties, so that they are properly
3715 // aligned.
3716 text_props = (textprop_T *)alloc(
3717 text_prop_count * sizeof(textprop_T));
3718 if (text_props != NULL)
3719 mch_memmove(text_props, prop_start,
3720 text_prop_count * sizeof(textprop_T));
3721
3722 // Allocate an array for the indexes.
3723 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3724 area_highlighting = TRUE;
3725 extra_check = TRUE;
3726 }
3727 }
3728#endif
3729
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003730 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 col = 0;
3732#ifdef FEAT_RIGHTLEFT
3733 if (wp->w_p_rl)
3734 {
3735 /* Rightleft window: process the text in the normal direction, but put
3736 * it in current_ScreenLine[] from right to left. Start at the
3737 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003738 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 off += col;
3740 }
3741#endif
3742
3743 /*
3744 * Repeat for the whole displayed line.
3745 */
3746 for (;;)
3747 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003748#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003749 int has_match_conc = 0; // match wants to conceal
3750 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 /* Skip this quickly when working on the text. */
3753 if (draw_state != WL_LINE)
3754 {
3755#ifdef FEAT_CMDWIN
3756 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3757 {
3758 draw_state = WL_CMDLINE;
3759 if (cmdwin_type != 0 && wp == curwin)
3760 {
3761 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003763 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003764 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003765 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 }
3767 }
3768#endif
3769
3770#ifdef FEAT_FOLDING
3771 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3772 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003773 int fdc = compute_foldcolumn(wp, 0);
3774
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003776 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003778 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003779 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003780 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003781 p_extra_free = alloc(12 + 1);
3782
3783 if (p_extra_free != NULL)
3784 {
3785 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3786 n_extra = fdc;
3787 p_extra_free[n_extra] = NUL;
3788 p_extra = p_extra_free;
3789 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003790 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003791 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
3794 }
3795#endif
3796
3797#ifdef FEAT_SIGNS
3798 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3799 {
3800 draw_state = WL_SIGN;
3801 /* Show the sign column when there are any signs in this
3802 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003803 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003805 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003807 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808# endif
3809
3810 /* Draw two cells with the sign value or blank. */
3811 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003812 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003813 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 n_extra = 2;
3815
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003816 if (row == startrow
3817#ifdef FEAT_DIFF
3818 + filler_lines && filler_todo <= 0
3819#endif
3820 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 {
3822 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3823 SIGN_TEXT);
3824# ifdef FEAT_SIGN_ICONS
3825 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3826 SIGN_ICON);
3827 if (gui.in_use && icon_sign != 0)
3828 {
3829 /* Use the image in this position. */
3830 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003831 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832# ifdef FEAT_NETBEANS_INTG
3833 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003836 c_final = NUL;
3837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838# endif
3839 char_attr = icon_sign;
3840 }
3841 else
3842# endif
3843 if (text_sign != 0)
3844 {
3845 p_extra = sign_get_text(text_sign);
3846 if (p_extra != NULL)
3847 {
3848 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003849 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003850 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
3852 char_attr = sign_get_attr(text_sign, FALSE);
3853 }
3854 }
3855 }
3856 }
3857#endif
3858
3859 if (draw_state == WL_NR - 1 && n_extra == 0)
3860 {
3861 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003862 /* Display the absolute or relative line number. After the
3863 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3864 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 && (row == startrow
3866#ifdef FEAT_DIFF
3867 + filler_lines
3868#endif
3869 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3870 {
3871 /* Draw the line number (empty space after wrapping). */
3872 if (row == startrow
3873#ifdef FEAT_DIFF
3874 + filler_lines
3875#endif
3876 )
3877 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003878 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003879 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003880
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003881 if (wp->w_p_nu && !wp->w_p_rnu)
3882 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003883 num = (long)lnum;
3884 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003885 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003886 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003887 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003888 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003889 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003890 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003891 num = lnum;
3892 fmt = "%-*ld ";
3893 }
3894 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003895
Bram Moolenaar700e7342013-01-30 12:31:36 +01003896 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003897 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 if (wp->w_skipcol > 0)
3899 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3900 *p_extra = '-';
3901#ifdef FEAT_RIGHTLEFT
3902 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01003903 {
3904 char_u *p1, *p2;
3905 int t;
3906
3907 // like rl_mirror(), but keep the space at the end
3908 p2 = skiptowhite(extra) - 1;
3909 for (p1 = extra; p1 < p2; ++p1, --p2)
3910 {
3911 t = *p1;
3912 *p1 = *p2;
3913 *p2 = t;
3914 }
3915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916#endif
3917 p_extra = extra;
3918 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003919 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01003922 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003924 c_final = NUL;
3925 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003926 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003927 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003928#ifdef FEAT_SYN_HL
3929 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003930 * the current line differently.
3931 * TODO: Can we use CursorLine instead of CursorLineNr
3932 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003933 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003934 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003935 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
3938 }
3939
Bram Moolenaar597a4222014-06-25 14:39:50 +02003940#ifdef FEAT_LINEBREAK
3941 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3942 && n_extra == 0 && *p_sbr != NUL)
3943 /* draw indent after showbreak value */
3944 draw_state = WL_BRI;
3945 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3946 /* After the showbreak, draw the breakindent */
3947 draw_state = WL_BRI - 1;
3948
3949 /* draw 'breakindent': indent wrapped text accordingly */
3950 if (draw_state == WL_BRI - 1 && n_extra == 0)
3951 {
3952 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003953 /* if need_showbreak is set, breakindent also applies */
3954 if (wp->w_p_bri && n_extra == 0
3955 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003956# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003957 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003958# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003959 )
3960 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003961 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003962# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003963 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003964 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003965 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003966# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003967 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3968 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003969 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003970# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003971 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003972# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003973 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003974 c_extra = ' ';
3975 n_extra = get_breakindent_win(wp,
3976 ml_get_buf(wp->w_buffer, lnum, FALSE));
3977 /* Correct end of highlighted area for 'breakindent',
3978 * required when 'linebreak' is also set. */
3979 if (tocol == vcol)
3980 tocol += n_extra;
3981 }
3982 }
3983#endif
3984
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3986 if (draw_state == WL_SBR - 1 && n_extra == 0)
3987 {
3988 draw_state = WL_SBR;
3989# ifdef FEAT_DIFF
3990 if (filler_todo > 0)
3991 {
3992 /* Draw "deleted" diff line(s). */
3993 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003994 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003996 c_final = NUL;
3997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01003999 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004001 c_final = NUL;
4002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003# ifdef FEAT_RIGHTLEFT
4004 if (wp->w_p_rl)
4005 n_extra = col + 1;
4006 else
4007# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004008 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004009 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 }
4011# endif
4012# ifdef FEAT_LINEBREAK
4013 if (*p_sbr != NUL && need_showbreak)
4014 {
4015 /* Draw 'showbreak' at the start of each broken line. */
4016 p_extra = p_sbr;
4017 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004018 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004020 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004022 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 /* Correct end of highlighted area for 'showbreak',
4024 * required when 'linebreak' is also set. */
4025 if (tocol == vcol)
4026 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004027#ifdef FEAT_SYN_HL
4028 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004029 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004030 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004031 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
4034# endif
4035 }
4036#endif
4037
4038 if (draw_state == WL_LINE - 1 && n_extra == 0)
4039 {
4040 draw_state = WL_LINE;
4041 if (saved_n_extra)
4042 {
4043 /* Continue item from end of wrapped line. */
4044 n_extra = saved_n_extra;
4045 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004046 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 p_extra = saved_p_extra;
4048 char_attr = saved_char_attr;
4049 }
4050 else
4051 char_attr = 0;
4052 }
4053 }
4054
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004055 // When still displaying '$' of change command, stop at cursor.
4056 // When only displaying the (relative) line number and that's done,
4057 // stop here.
4058 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004059 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060#ifdef FEAT_DIFF
4061 && filler_todo <= 0
4062#endif
4063 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004064 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004066 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004067 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004068 /* Pretend we have finished updating the window. Except when
4069 * 'cursorcolumn' is set. */
4070#ifdef FEAT_SYN_HL
4071 if (wp->w_p_cuc)
4072 row = wp->w_cline_row + wp->w_cline_height;
4073 else
4074#endif
4075 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 break;
4077 }
4078
Bram Moolenaar637532b2019-01-03 21:44:40 +01004079 if (draw_state == WL_LINE && (area_highlighting
4080#ifdef FEAT_SPELL
4081 || has_spell
4082#endif
4083 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 {
4085 /* handle Visual or match highlighting in this line */
4086 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4088 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004090 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 && vcol < tocol))
4092 area_attr = attr; /* start highlighting */
4093 else if (area_attr != 0
4094 && (vcol == tocol
4095 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
4098#ifdef FEAT_SEARCH_EXTRA
4099 if (!n_extra)
4100 {
4101 /*
4102 * Check for start/end of search pattern match.
4103 * After end, check for start/end of next match.
4104 * When another match, have to check for start again.
4105 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004106 * Do this for 'search_hl' and the match list (ordered by
4107 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004109 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004110 cur = wp->w_match_head;
4111 shl_flag = FALSE;
4112 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004114 if (shl_flag == FALSE
4115 && ((cur != NULL
4116 && cur->priority > SEARCH_HL_PRIORITY)
4117 || cur == NULL))
4118 {
4119 shl = &search_hl;
4120 shl_flag = TRUE;
4121 }
4122 else
4123 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004124 if (cur != NULL)
4125 cur->pos.cur = 0;
4126 pos_inprogress = TRUE;
4127 while (shl->rm.regprog != NULL
4128 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004130 if (shl->startcol != MAXCOL
4131 && v >= (long)shl->startcol
4132 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004134 int tmp_col = v + MB_PTR2LEN(ptr);
4135
4136 if (shl->endcol < tmp_col)
4137 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004139#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004140 // Match with the "Conceal" group results in hiding
4141 // the match.
4142 if (cur != NULL
4143 && shl != &search_hl
4144 && syn_name2id((char_u *)"Conceal")
4145 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004146 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004147 has_match_conc =
4148 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004149 match_conc = cur->conceal_char;
4150 }
4151 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004152 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004155 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
4157 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004158 next_search_hl(wp, shl, lnum, (colnr_T)v,
4159 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004160 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004161 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162
4163 /* Need to get the line again, a multi-line regexp
4164 * may have made it invalid. */
4165 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4166 ptr = line + v;
4167
4168 if (shl->lnum == lnum)
4169 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004170 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004172 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004174 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004176 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 {
4178 /* highlight empty match, try again after
4179 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004181 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004182 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004184 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 }
4186
4187 /* Loop to check if the match starts at the
4188 * current position */
4189 continue;
4190 }
4191 }
4192 break;
4193 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004194 if (shl != &search_hl && cur != NULL)
4195 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004197
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004198 /* Use attributes from match with highest priority among
4199 * 'search_hl' and the match list. */
4200 search_attr = search_hl.attr_cur;
4201 cur = wp->w_match_head;
4202 shl_flag = FALSE;
4203 while (cur != NULL || shl_flag == FALSE)
4204 {
4205 if (shl_flag == FALSE
4206 && ((cur != NULL
4207 && cur->priority > SEARCH_HL_PRIORITY)
4208 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004209 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004210 shl = &search_hl;
4211 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004212 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004213 else
4214 shl = &cur->hl;
4215 if (shl->attr_cur != 0)
4216 search_attr = shl->attr_cur;
4217 if (shl != &search_hl && cur != NULL)
4218 cur = cur->next;
4219 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004220 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004221 if (*ptr == NUL && (did_line_attr >= 1
4222 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004223 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
4225#endif
4226
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004228 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004230 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4231 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004233 if (diff_hlf == HLF_TXD && ptr - line > change_end
4234 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004236 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004237 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004238 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 }
4240#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004241
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004242#ifdef FEAT_TEXT_PROP
4243 if (text_props != NULL)
4244 {
4245 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004246 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004247
4248 // Check if any active property ends.
4249 for (pi = 0; pi < text_props_active; ++pi)
4250 {
4251 int tpi = text_prop_idxs[pi];
4252
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004253 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004254 + text_props[tpi].tp_len)
4255 {
4256 if (pi + 1 < text_props_active)
4257 mch_memmove(text_prop_idxs + pi,
4258 text_prop_idxs + pi + 1,
4259 sizeof(int)
4260 * (text_props_active - (pi + 1)));
4261 --text_props_active;
4262 --pi;
4263 }
4264 }
4265
4266 // Add any text property that starts in this column.
4267 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004268 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004269 text_prop_idxs[text_props_active++] = text_prop_next++;
4270
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004271 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004272 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004273 if (text_props_active > 0)
4274 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004275 // Sort the properties on priority and/or starting last.
4276 // Then combine the attributes, highest priority last.
4277 current_text_props = text_props;
4278 current_buf = wp->w_buffer;
4279 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4280 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004281
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004282 for (pi = 0; pi < text_props_active; ++pi)
4283 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004284 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004285 proptype_T *pt = text_prop_type_by_id(
4286 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004287
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004288 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004289 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004290 int pt_attr = syn_id2attr(pt->pt_hl_id);
4291
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004292 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004293 text_prop_attr =
4294 hl_combine_attr(text_prop_attr, pt_attr);
4295 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004296 }
4297 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004298 }
4299 }
4300#endif
4301
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004302 /* Decide which of the highlight attributes to use. */
4303 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004304#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004305 if (area_attr != 0)
4306 char_attr = hl_combine_attr(line_attr, area_attr);
4307 else if (search_attr != 0)
4308 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004309# ifdef FEAT_TEXT_PROP
4310 else if (text_prop_type != NULL)
4311 char_attr = hl_combine_attr(line_attr, text_prop_attr);
4312# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004313 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004314 || vcol < fromcol || vcol_prev < fromcol_prev
4315 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004316 // Use line_attr when not in the Visual or 'incsearch' area
4317 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004318 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004319#else
4320 if (area_attr != 0)
4321 char_attr = area_attr;
4322 else if (search_attr != 0)
4323 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004324#endif
4325 else
4326 {
4327 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004328#ifdef FEAT_TEXT_PROP
4329 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004330 {
4331 if (text_prop_combine)
4332 char_attr = hl_combine_attr(
4333 syntax_attr, text_prop_attr);
4334 else
4335 char_attr = text_prop_attr;
4336 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004337 else
4338#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004339#ifdef FEAT_SYN_HL
4340 if (has_syntax)
4341 char_attr = syntax_attr;
4342 else
4343#endif
4344 char_attr = 0;
4345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347
4348 /*
4349 * Get the next character to put on the screen.
4350 */
4351 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004352 * The "p_extra" points to the extra stuff that is inserted to
4353 * represent special characters (non-printable stuff) and other
4354 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004355 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004356 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4357 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4359 */
4360 if (n_extra > 0)
4361 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004362 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004364 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004366 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 {
4368 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004369 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004370 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 }
4372 else
4373 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 }
4375 else
4376 {
4377 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 if (has_mbyte)
4379 {
4380 mb_c = c;
4381 if (enc_utf8)
4382 {
4383 /* If the UTF-8 character is more than one byte:
4384 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004385 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 mb_utf8 = FALSE;
4387 if (mb_l > n_extra)
4388 mb_l = 1;
4389 else if (mb_l > 1)
4390 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004391 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004393 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 }
4395 }
4396 else
4397 {
4398 /* if this is a DBCS character, put it in "mb_c" */
4399 mb_l = MB_BYTE2LEN(c);
4400 if (mb_l >= n_extra)
4401 mb_l = 1;
4402 else if (mb_l > 1)
4403 mb_c = (c << 8) + p_extra[1];
4404 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004405 if (mb_l == 0) /* at the NUL at end-of-line */
4406 mb_l = 1;
4407
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 /* If a double-width char doesn't fit display a '>' in the
4409 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004410 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411# ifdef FEAT_RIGHTLEFT
4412 wp->w_p_rl ? (col <= 0) :
4413# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004414 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 && (*mb_char2cells)(mb_c) == 2)
4416 {
4417 c = '>';
4418 mb_c = c;
4419 mb_l = 1;
4420 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004421 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 /* put the pointer back to output the double-width
4423 * character at the start of the next line. */
4424 ++n_extra;
4425 --p_extra;
4426 }
4427 else
4428 {
4429 n_extra -= mb_l - 1;
4430 p_extra += mb_l - 1;
4431 }
4432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 ++p_extra;
4434 }
4435 --n_extra;
4436 }
4437 else
4438 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004439#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004440 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004441#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004442
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004443 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004444 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 /*
4446 * Get a character from the line itself.
4447 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004448 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004449#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004450 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 if (has_mbyte)
4453 {
4454 mb_c = c;
4455 if (enc_utf8)
4456 {
4457 /* If the UTF-8 character is more than one byte: Decode it
4458 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004459 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 mb_utf8 = FALSE;
4461 if (mb_l > 1)
4462 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004463 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 /* Overlong encoded ASCII or ASCII with composing char
4465 * is displayed normally, except a NUL. */
4466 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004467 {
4468 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004469#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004470 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004471#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004474
4475 /* At start of the line we can have a composing char.
4476 * Draw it as a space with a composing char. */
4477 if (utf_iscomposing(mb_c))
4478 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004479 int i;
4480
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004481 for (i = Screen_mco - 1; i > 0; --i)
4482 u8cc[i] = u8cc[i - 1];
4483 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004484 mb_c = ' ';
4485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487
4488 if ((mb_l == 1 && c >= 0x80)
4489 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004490 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 {
4492 /*
4493 * Illegal UTF-8 byte: display as <xx>.
4494 * Non-BMP character : display as ? or fullwidth ?.
4495 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004496 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004497# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004498 if (wp->w_p_rl) /* reverse */
4499 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004500# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 p_extra = extra;
4502 c = *p_extra;
4503 mb_c = mb_ptr2char_adv(&p_extra);
4504 mb_utf8 = (c >= 0x80);
4505 n_extra = (int)STRLEN(p_extra);
4506 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004507 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 if (area_attr == 0 && search_attr == 0)
4509 {
4510 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004511 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 saved_attr2 = char_attr; /* save current attr */
4513 }
4514 }
4515 else if (mb_l == 0) /* at the NUL at end-of-line */
4516 mb_l = 1;
4517#ifdef FEAT_ARABIC
4518 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4519 {
4520 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004521 int pc, pc1, nc;
4522 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523
4524 /* The idea of what is the previous and next
4525 * character depends on 'rightleft'. */
4526 if (wp->w_p_rl)
4527 {
4528 pc = prev_c;
4529 pc1 = prev_c1;
4530 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004531 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 }
4533 else
4534 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004535 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004537 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 }
4539 prev_c = mb_c;
4540
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004541 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 }
4543 else
4544 prev_c = mb_c;
4545#endif
4546 }
4547 else /* enc_dbcs */
4548 {
4549 mb_l = MB_BYTE2LEN(c);
4550 if (mb_l == 0) /* at the NUL at end-of-line */
4551 mb_l = 1;
4552 else if (mb_l > 1)
4553 {
4554 /* We assume a second byte below 32 is illegal.
4555 * Hopefully this is OK for all double-byte encodings!
4556 */
4557 if (ptr[1] >= 32)
4558 mb_c = (c << 8) + ptr[1];
4559 else
4560 {
4561 if (ptr[1] == NUL)
4562 {
4563 /* head byte at end of line */
4564 mb_l = 1;
4565 transchar_nonprint(extra, c);
4566 }
4567 else
4568 {
4569 /* illegal tail byte */
4570 mb_l = 2;
4571 STRCPY(extra, "XX");
4572 }
4573 p_extra = extra;
4574 n_extra = (int)STRLEN(extra) - 1;
4575 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004576 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 c = *p_extra++;
4578 if (area_attr == 0 && search_attr == 0)
4579 {
4580 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004581 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 saved_attr2 = char_attr; /* save current attr */
4583 }
4584 mb_c = c;
4585 }
4586 }
4587 }
4588 /* If a double-width char doesn't fit display a '>' in the
4589 * last column; the character is displayed at the start of the
4590 * next line. */
4591 if ((
4592# ifdef FEAT_RIGHTLEFT
4593 wp->w_p_rl ? (col <= 0) :
4594# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004595 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 && (*mb_char2cells)(mb_c) == 2)
4597 {
4598 c = '>';
4599 mb_c = c;
4600 mb_utf8 = FALSE;
4601 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004602 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004603 // Put pointer back so that the character will be
4604 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004606#ifdef FEAT_CONCEAL
4607 did_decrement_ptr = TRUE;
4608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 }
4610 else if (*ptr != NUL)
4611 ptr += mb_l - 1;
4612
4613 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004614 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004615 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004616 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004619 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004620 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 c = ' ';
4622 if (area_attr == 0 && search_attr == 0)
4623 {
4624 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004625 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 saved_attr2 = char_attr; /* save current attr */
4627 }
4628 mb_c = c;
4629 mb_utf8 = FALSE;
4630 mb_l = 1;
4631 }
4632
4633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 ++ptr;
4635
4636 if (extra_check)
4637 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004638#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004639 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004640#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004641
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004642#ifdef FEAT_TERMINAL
4643 if (get_term_attr)
4644 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004645 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004646
4647 if (!attr_pri)
4648 char_attr = syntax_attr;
4649 else
4650 char_attr = hl_combine_attr(syntax_attr, char_attr);
4651 }
4652#endif
4653
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004654#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004655 // Get syntax attribute, unless still at the start of the line
4656 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004657 v = (long)(ptr - line);
4658 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 {
4660 /* Get the syntax attribute for the character. If there
4661 * is an error, disable syntax highlighting. */
4662 save_did_emsg = did_emsg;
4663 did_emsg = FALSE;
4664
Bram Moolenaar217ad922005-03-20 22:37:15 +00004665 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004666# ifdef FEAT_SPELL
4667 has_spell ? &can_spell :
4668# endif
4669 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670
4671 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004672 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004673 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004674 has_syntax = FALSE;
4675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 else
4677 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004678#ifdef SYN_TIME_LIMIT
4679 if (wp->w_s->b_syn_slow)
4680 has_syntax = FALSE;
4681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
4683 /* Need to get the line again, a multi-line regexp may
4684 * have made it invalid. */
4685 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4686 ptr = line + v;
4687
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004688# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004689 // Text properties overrule syntax highlighting or combine.
4690 if (text_prop_attr == 0 || text_prop_combine)
4691# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004692 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004693 int comb_attr = syntax_attr;
4694# ifdef FEAT_TEXT_PROP
4695 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4696# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004697 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004698 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004699 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004700 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004701 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004702# ifdef FEAT_CONCEAL
4703 /* no concealing past the end of the line, it interferes
4704 * with line highlighting */
4705 if (c == NUL)
4706 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004707 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004708 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004709# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004711#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004712
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004713#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004714 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004715 * Only do this when there is no syntax highlighting, the
4716 * @Spell cluster is not used or the current syntax item
4717 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004718 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004719 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004720 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004721 if (c != 0 && (
4722# ifdef FEAT_SYN_HL
4723 !has_syntax ||
4724# endif
4725 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004726 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004727 char_u *prev_ptr, *p;
4728 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004729 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004730 if (has_mbyte)
4731 {
4732 prev_ptr = ptr - mb_l;
4733 v -= mb_l - 1;
4734 }
4735 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004736 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004737
4738 /* Use nextline[] if possible, it has the start of the
4739 * next line concatenated. */
4740 if ((prev_ptr - line) - nextlinecol >= 0)
4741 p = nextline + (prev_ptr - line) - nextlinecol;
4742 else
4743 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004744 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004745 len = spell_check(wp, p, &spell_hlf, &cap_col,
4746 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004747 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004748
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004749 /* In Insert mode only highlight a word that
4750 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004751 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004752 && (State & INSERT) != 0
4753 && wp->w_cursor.lnum == lnum
4754 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004755 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004756 && wp->w_cursor.col < (colnr_T)word_end)
4757 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004758 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004759 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004760 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004761
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004762 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004763 && (p - nextline) + len > nextline_idx)
4764 {
4765 /* Remember that the good word continues at the
4766 * start of the next line. */
4767 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004768 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004769 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004770
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004771 /* Turn index into actual attributes. */
4772 if (spell_hlf != HLF_COUNT)
4773 spell_attr = highlight_attr[spell_hlf];
4774
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004775 if (cap_col > 0)
4776 {
4777 if (p != prev_ptr
4778 && (p - nextline) + cap_col >= nextline_idx)
4779 {
4780 /* Remember that the word in the next line
4781 * must start with a capital. */
4782 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004783 cap_col = (int)((p - nextline) + cap_col
4784 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004785 }
4786 else
4787 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004788 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004789 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004790 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004791 }
4792 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004793 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004794 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004795 char_attr = hl_combine_attr(char_attr, spell_attr);
4796 else
4797 char_attr = hl_combine_attr(spell_attr, char_attr);
4798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799#endif
4800#ifdef FEAT_LINEBREAK
4801 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004802 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004804 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004805 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004807 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004808 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004809
Bram Moolenaar597a4222014-06-25 14:39:50 +02004810 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004811 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004812 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004813 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004814# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004815 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004816 wp->w_buffer->b_p_vts_array) - 1;
4817# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004818 n_extra = (int)wp->w_buffer->b_p_ts
4819 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004820# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004821
Bram Moolenaar4df70292015-03-21 14:20:16 +01004822 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004823 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004824 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004825 {
4826#ifdef FEAT_CONCEAL
4827 if (c == TAB)
4828 /* See "Tab alignment" below. */
4829 FIX_FOR_BOGUSCOLS;
4830#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004831 if (!wp->w_p_list)
4832 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 }
4835#endif
4836
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004837 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4838 // But not when the character is followed by a composing
4839 // character (use mb_l to check that).
4840 if (wp->w_p_list
4841 && ((((c == 160 && mb_l == 1)
4842 || (mb_utf8
4843 && ((mb_c == 160 && mb_l == 2)
4844 || (mb_c == 0x202f && mb_l == 3))))
4845 && lcs_nbsp)
4846 || (c == ' '
4847 && mb_l == 1
4848 && lcs_space
4849 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004850 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004851 c = (c == ' ') ? lcs_space : lcs_nbsp;
4852 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004853 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004854 n_attr = 1;
4855 extra_attr = HL_ATTR(HLF_8);
4856 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004857 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004858 mb_c = c;
4859 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004860 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004861 mb_utf8 = TRUE;
4862 u8cc[0] = 0;
4863 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004864 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004865 else
4866 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004867 }
4868
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4870 {
4871 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004872 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 {
4874 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004875 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 saved_attr2 = char_attr; /* save current attr */
4877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004879 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
4881 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004882 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004883 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 }
4885 else
4886 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
4888 }
4889
4890 /*
4891 * Handling of non-printable characters.
4892 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004893 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 {
4895 /*
4896 * when getting a character from the file, we may have to
4897 * turn it into something else on the way to putting it
4898 * into "ScreenLines".
4899 */
4900 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4901 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004902 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004903 long vcol_adjusted = vcol; /* removed showbreak length */
4904#ifdef FEAT_LINEBREAK
4905 /* only adjust the tab_len, when at the first column
4906 * after the showbreak value was drawn */
4907 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4908 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004911#ifdef FEAT_VARTABS
4912 tab_len = tabstop_padding(vcol_adjusted,
4913 wp->w_buffer->b_p_ts,
4914 wp->w_buffer->b_p_vts_array) - 1;
4915#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004916 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004917 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4918#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004919
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004920#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004921 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004922#endif
4923 /* tab amount depends on current column */
4924 n_extra = tab_len;
4925#ifdef FEAT_LINEBREAK
4926 else
4927 {
4928 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01004929 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004930 int i;
4931 int saved_nextra = n_extra;
4932
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004933#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004934 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004935 /* there are characters to conceal */
4936 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004937 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4938 */
4939 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4940 && n_extra > tab_len)
4941 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004942#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004943
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004944 /* if n_extra > 0, it gives the number of chars, to
4945 * use for a tab, else we need to calculate the width
4946 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004947 len = (tab_len * mb_char2len(lcs_tab2));
4948 if (n_extra > 0)
4949 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004950 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02004951 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004952 vim_memset(p, ' ', len);
4953 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004954 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004955 p_extra_free = p;
4956 for (i = 0; i < tab_len; i++)
4957 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004958 if (*p == NUL)
4959 {
4960 tab_len = i;
4961 break;
4962 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004963 mb_char2bytes(lcs_tab2, p);
4964 p += mb_char2len(lcs_tab2);
4965 n_extra += mb_char2len(lcs_tab2)
4966 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004967 }
4968 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004969#ifdef FEAT_CONCEAL
4970 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4971 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004972 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004973 n_extra -= vcol_off;
4974#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004975 }
4976#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004977#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004978 {
4979 int vc_saved = vcol_off;
4980
4981 /* Tab alignment should be identical regardless of
4982 * 'conceallevel' value. So tab compensates of all
4983 * previous concealed characters, and thus resets
4984 * vcol_off and boguscols accumulated so far in the
4985 * line. Note that the tab can be longer than
4986 * 'tabstop' when there are concealed characters. */
4987 FIX_FOR_BOGUSCOLS;
4988
4989 /* Make sure, the highlighting for the tab char will be
4990 * correctly set further below (effectively reverts the
4991 * FIX_FOR_BOGSUCOLS macro */
4992 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004993 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004994 tab_len += vc_saved;
4995 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 if (wp->w_p_list)
4999 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005000 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005001#ifdef FEAT_LINEBREAK
5002 if (wp->w_p_lbr)
5003 c_extra = NUL; /* using p_extra from above */
5004 else
5005#endif
5006 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005007 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005008 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005009 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005012 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
5014 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005015 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005016 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 else
5020 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005021 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 c_extra = ' ';
5023 c = ' ';
5024 }
5025 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005026 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005027 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005028 || ((fromcol >= 0 || fromcol_prev >= 0)
5029 && tocol > vcol
5030 && VIsual_mode != Ctrl_V
5031 && (
5032# ifdef FEAT_RIGHTLEFT
5033 wp->w_p_rl ? (col >= 0) :
5034# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005035 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005036 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005037 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005038 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005039 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005041 /* Display a '$' after the line or highlight an extra
5042 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5044 /* For a diff line the highlighting continues after the
5045 * "$". */
5046 if (
5047# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005048 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049# ifdef LINE_ATTR
5050 &&
5051# endif
5052# endif
5053# ifdef LINE_ATTR
5054 line_attr == 0
5055# endif
5056 )
5057#endif
5058 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 /* In virtualedit, visual selections may extend
5060 * beyond end of line. */
5061 if (area_highlighting && virtual_active()
5062 && tocol != MAXCOL && vcol < tocol)
5063 n_extra = 0;
5064 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
5066 p_extra = at_end_str;
5067 n_extra = 1;
5068 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005069 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
5071 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005072 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005073 c = lcs_eol;
5074 else
5075 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 lcs_eol_one = -1;
5077 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005078 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005080 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 n_attr = 1;
5082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005084 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
5086 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005087 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005088 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 }
5090 else
5091 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093 else if (c != NUL)
5094 {
5095 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005096 if (n_extra == 0)
5097 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098#ifdef FEAT_RIGHTLEFT
5099 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5100 rl_mirror(p_extra); /* reverse "<12>" */
5101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005103 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005104#ifdef FEAT_LINEBREAK
5105 if (wp->w_p_lbr)
5106 {
5107 char_u *p;
5108
5109 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005110 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005111 vim_memset(p, ' ', n_extra);
5112 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5113 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005114 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005115 p_extra_free = p_extra = p;
5116 }
5117 else
5118#endif
5119 {
5120 n_extra = byte2cells(c) - 1;
5121 c = *p_extra++;
5122 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005123 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
5125 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005126 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 saved_attr2 = char_attr; /* save current attr */
5128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 else if (VIsual_active
5132 && (VIsual_mode == Ctrl_V
5133 || VIsual_mode == 'v')
5134 && virtual_active()
5135 && tocol != MAXCOL
5136 && vcol < tocol
5137 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005138#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005140#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005141 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 {
5143 c = ' ';
5144 --ptr; /* put it back at the NUL */
5145 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005146#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 else if ((
5148# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005149 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005151# ifdef FEAT_TERMINAL
5152 term_attr != 0 ||
5153# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 ) && (
5156# ifdef FEAT_RIGHTLEFT
5157 wp->w_p_rl ? (col >= 0) :
5158# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005159 (col
5160# ifdef FEAT_CONCEAL
5161 - boguscols
5162# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005163 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 {
5165 /* Highlight until the right side of the window */
5166 c = ' ';
5167 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005168
5169 /* Remember we do the char for line highlighting. */
5170 ++did_line_attr;
5171
5172 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005173 if (line_attr != 0 && char_attr == search_attr
5174 && (did_line_attr > 1
5175 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005176 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177# ifdef FEAT_DIFF
5178 if (diff_hlf == HLF_TXD)
5179 {
5180 diff_hlf = HLF_CHD;
5181 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005182 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005183 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005184 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5185 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005186 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
5189# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005190# ifdef FEAT_TERMINAL
5191 if (term_attr != 0)
5192 {
5193 char_attr = term_attr;
5194 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5195 char_attr = hl_combine_attr(char_attr,
5196 HL_ATTR(HLF_CUL));
5197 }
5198# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
5200#endif
5201 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005202
5203#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005204 if ( wp->w_p_cole > 0
5205 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005206 conceal_cursor_line(wp))
5207 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005208 && !(lnum_in_visual_area
5209 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005210 {
5211 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005212 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005213 && (syn_get_sub_char() != NUL || match_conc
5214 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005215 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005216 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005217 /* First time at this concealed item: display one
5218 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005219 if (match_conc)
5220 c = match_conc;
5221 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005222 c = syn_get_sub_char();
5223 else if (lcs_conceal != NUL)
5224 c = lcs_conceal;
5225 else
5226 c = ' ';
5227
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005228 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005229
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005230 if (n_extra > 0)
5231 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005232 vcol += n_extra;
5233 if (wp->w_p_wrap && n_extra > 0)
5234 {
5235# ifdef FEAT_RIGHTLEFT
5236 if (wp->w_p_rl)
5237 {
5238 col -= n_extra;
5239 boguscols -= n_extra;
5240 }
5241 else
5242# endif
5243 {
5244 boguscols += n_extra;
5245 col += n_extra;
5246 }
5247 }
5248 n_extra = 0;
5249 n_attr = 0;
5250 }
5251 else if (n_skip == 0)
5252 {
5253 is_concealing = TRUE;
5254 n_skip = 1;
5255 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005256 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005257 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005258 {
5259 mb_utf8 = TRUE;
5260 u8cc[0] = 0;
5261 c = 0xc0;
5262 }
5263 else
5264 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005265 }
5266 else
5267 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005268 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005269 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005270 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005271
5272 if (n_skip > 0 && did_decrement_ptr)
5273 // not showing the '>', put pointer back to avoid getting stuck
5274 ++ptr;
5275
5276#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 }
5278
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005279#ifdef FEAT_CONCEAL
5280 /* In the cursor line and we may be concealing characters: correct
5281 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005282 if (!did_wcol && draw_state == WL_LINE
5283 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005284 && conceal_cursor_line(wp)
5285 && (int)wp->w_virtcol <= vcol + n_skip)
5286 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005287# ifdef FEAT_RIGHTLEFT
5288 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005289 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005290 else
5291# endif
5292 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005293 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005294 did_wcol = TRUE;
5295 }
5296#endif
5297
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 /* Don't override visual selection highlighting. */
5299 if (n_attr > 0
5300 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005301 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 char_attr = extra_attr;
5303
Bram Moolenaar81695252004-12-29 20:58:21 +00005304#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 /* XIM don't send preedit_start and preedit_end, but they send
5306 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5307 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005308 if (p_imst == IM_ON_THE_SPOT
5309 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005310 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 && (State & INSERT)
5312 && !p_imdisable
5313 && im_is_preediting()
5314 && draw_state == WL_LINE)
5315 {
5316 colnr_T tcol;
5317
5318 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005319 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 else
5321 tcol = preedit_end_col;
5322 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5323 {
5324 if (feedback_old_attr < 0)
5325 {
5326 feedback_col = 0;
5327 feedback_old_attr = char_attr;
5328 }
5329 char_attr = im_get_feedback_attr(feedback_col);
5330 if (char_attr < 0)
5331 char_attr = feedback_old_attr;
5332 feedback_col++;
5333 }
5334 else if (feedback_old_attr >= 0)
5335 {
5336 char_attr = feedback_old_attr;
5337 feedback_old_attr = -1;
5338 feedback_col = 0;
5339 }
5340 }
5341#endif
5342 /*
5343 * Handle the case where we are in column 0 but not on the first
5344 * character of the line and the user wants us to show us a
5345 * special character (via 'listchars' option "precedes:<char>".
5346 */
5347 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005348 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5350#ifdef FEAT_DIFF
5351 && filler_todo <= 0
5352#endif
5353 && draw_state > WL_NR
5354 && c != NUL)
5355 {
5356 c = lcs_prec;
5357 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005358 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5359 {
5360 /* Double-width character being overwritten by the "precedes"
5361 * character, need to fill up half the character. */
5362 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005363 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005364 n_extra = 1;
5365 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005366 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005369 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 {
5371 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005372 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005373 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
5375 else
5376 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005377 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 {
5379 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005380 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 n_attr3 = 1;
5382 }
5383 }
5384
5385 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005386 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005388 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005389#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005390 || did_line_attr == 1
5391#endif
5392 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005394#ifdef FEAT_SEARCH_EXTRA
5395 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005396
5397 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005398 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005399 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005400#endif
5401
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005402 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 * highlight match at end of line. If it's beyond the last
5404 * char on the screen, just overwrite that one (tricky!) Not
5405 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005406#ifdef FEAT_SEARCH_EXTRA
5407 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005408 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005409 prevcol_hl_flag = TRUE;
5410 else
5411 {
5412 cur = wp->w_match_head;
5413 while (cur != NULL)
5414 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005415 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005416 {
5417 prevcol_hl_flag = TRUE;
5418 break;
5419 }
5420 cur = cur->next;
5421 }
5422 }
5423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005425 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005426 && (VIsual_mode != Ctrl_V
5427 || lnum == VIsual.lnum
5428 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005429 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430#ifdef FEAT_SEARCH_EXTRA
5431 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005432 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005433# ifdef FEAT_SYN_HL
5434 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5435 && !(wp == curwin && VIsual_active))
5436# endif
5437# ifdef FEAT_DIFF
5438 && diff_hlf == (hlf_T)0
5439# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005440# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005441 && did_line_attr <= 1
5442# endif
5443 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444#endif
5445 ))
5446 {
5447 int n = 0;
5448
5449#ifdef FEAT_RIGHTLEFT
5450 if (wp->w_p_rl)
5451 {
5452 if (col < 0)
5453 n = 1;
5454 }
5455 else
5456#endif
5457 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005458 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 n = -1;
5460 }
5461 if (n != 0)
5462 {
5463 /* At the window boundary, highlight the last character
5464 * instead (better than nothing). */
5465 off += n;
5466 col += n;
5467 }
5468 else
5469 {
5470 /* Add a blank character to highlight. */
5471 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 if (enc_utf8)
5473 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 }
5475#ifdef FEAT_SEARCH_EXTRA
5476 if (area_attr == 0)
5477 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005478 /* Use attributes from match with highest priority among
5479 * 'search_hl' and the match list. */
5480 char_attr = search_hl.attr;
5481 cur = wp->w_match_head;
5482 shl_flag = FALSE;
5483 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005484 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005485 if (shl_flag == FALSE
5486 && ((cur != NULL
5487 && cur->priority > SEARCH_HL_PRIORITY)
5488 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005489 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005490 shl = &search_hl;
5491 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005492 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005493 else
5494 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005495 if ((ptr - line) - 1 == (long)shl->startcol
5496 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005497 char_attr = shl->attr;
5498 if (shl != &search_hl && cur != NULL)
5499 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 }
5502#endif
5503 ScreenAttrs[off] = char_attr;
5504#ifdef FEAT_RIGHTLEFT
5505 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005506 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005508 --off;
5509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 else
5511#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005512 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005514 ++off;
5515 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005516 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005517#ifdef FEAT_SYN_HL
5518 eol_hl_off = 1;
5519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522
Bram Moolenaar91170f82006-05-05 21:15:17 +00005523 /*
5524 * At end of the text line.
5525 */
5526 if (c == NUL)
5527 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005528#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005529 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005530 if (wp->w_p_wrap)
5531 v = wp->w_skipcol;
5532 else
5533 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005534
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005535 /* check if line ends before left margin */
5536 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005537 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005538#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005539 // Get rid of the boguscols now, we want to draw until the right
5540 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005541 col -= boguscols;
5542 boguscols = 0;
5543#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005544
5545 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005546 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005547
5548 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005549 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5550 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005551 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005552 && lnum != wp->w_cursor.lnum)
5553 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005554# ifdef FEAT_RIGHTLEFT
5555 && !wp->w_p_rl
5556# endif
5557 )
5558 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005559 int rightmost_vcol = 0;
5560 int i;
5561
5562 if (wp->w_p_cuc)
5563 rightmost_vcol = wp->w_virtcol;
5564 if (draw_color_col)
5565 /* determine rightmost colorcolumn to possibly draw */
5566 for (i = 0; color_cols[i] >= 0; ++i)
5567 if (rightmost_vcol < color_cols[i])
5568 rightmost_vcol = color_cols[i];
5569
Bram Moolenaar02631462017-09-22 15:20:32 +02005570 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005571 {
5572 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005573 if (enc_utf8)
5574 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005575 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005576 if (draw_color_col)
5577 draw_color_col = advance_color_col(VCOL_HLC,
5578 &color_cols);
5579
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005580 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005581 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005582 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005583 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005584 else
5585 ScreenAttrs[off++] = 0;
5586
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005587 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005588 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005589
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005590 ++vcol;
5591 }
5592 }
5593#endif
5594
Bram Moolenaar53f81742017-09-22 14:35:51 +02005595 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005596 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 row++;
5598
5599 /*
5600 * Update w_cline_height and w_cline_folded if the cursor line was
5601 * updated (saves a call to plines() later).
5602 */
5603 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5604 {
5605 curwin->w_cline_row = startrow;
5606 curwin->w_cline_height = row - startrow;
5607#ifdef FEAT_FOLDING
5608 curwin->w_cline_folded = FALSE;
5609#endif
5610 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5611 }
5612
5613 break;
5614 }
5615
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005616 // Show "extends" character from 'listchars' if beyond the line end and
5617 // 'list' is set.
5618 if (lcs_ext != NUL
5619 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 && !wp->w_p_wrap
5621#ifdef FEAT_DIFF
5622 && filler_todo <= 0
5623#endif
5624 && (
5625#ifdef FEAT_RIGHTLEFT
5626 wp->w_p_rl ? col == 0 :
5627#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005628 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005630 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5632 {
5633 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005634 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005636 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 {
5638 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005639 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005640 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 }
5642 else
5643 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 }
5645
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005646#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005647 /* advance to the next 'colorcolumn' */
5648 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005649 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005650
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005651 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005652 * highlight the cursor position itself.
5653 * Also highlight the 'colorcolumn' if it is different than
5654 * 'cursorcolumn' */
5655 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005656 if (draw_state == WL_LINE && !lnum_in_visual_area
5657 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005658 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005659 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005660 && lnum != wp->w_cursor.lnum)
5661 {
5662 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005663 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005664 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005665 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005666 {
5667 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005668 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005669 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005670 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005671#endif
5672
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 /*
5674 * Store character to be displayed.
5675 * Skip characters that are left of the screen for 'nowrap'.
5676 */
5677 vcol_prev = vcol;
5678 if (draw_state < WL_LINE || n_skip <= 0)
5679 {
5680 /*
5681 * Store the character.
5682 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005683#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5685 {
5686 /* A double-wide character is: put first halve in left cell. */
5687 --off;
5688 --col;
5689 }
5690#endif
5691 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005693 {
5694 if ((mb_c & 0xff00) == 0x8e00)
5695 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 else if (enc_utf8)
5699 {
5700 if (mb_utf8)
5701 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005702 int i;
5703
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005705 if ((c & 0xff) == 0)
5706 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005707 for (i = 0; i < Screen_mco; ++i)
5708 {
5709 ScreenLinesC[i][off] = u8cc[i];
5710 if (u8cc[i] == 0)
5711 break;
5712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 }
5714 else
5715 ScreenLinesUC[off] = 0;
5716 }
5717 if (multi_attr)
5718 {
5719 ScreenAttrs[off] = multi_attr;
5720 multi_attr = 0;
5721 }
5722 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 ScreenAttrs[off] = char_attr;
5724
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5726 {
5727 /* Need to fill two screen columns. */
5728 ++off;
5729 ++col;
5730 if (enc_utf8)
5731 /* UTF-8: Put a 0 in the second screen char. */
5732 ScreenLines[off] = 0;
5733 else
5734 /* DBCS: Put second byte in the second screen char. */
5735 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005736 if (draw_state > WL_NR
5737#ifdef FEAT_DIFF
5738 && filler_todo <= 0
5739#endif
5740 )
5741 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 /* When "tocol" is halfway a character, set it to the end of
5743 * the character, otherwise highlighting won't stop. */
5744 if (tocol == vcol)
5745 ++tocol;
5746#ifdef FEAT_RIGHTLEFT
5747 if (wp->w_p_rl)
5748 {
5749 /* now it's time to backup one cell */
5750 --off;
5751 --col;
5752 }
5753#endif
5754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755#ifdef FEAT_RIGHTLEFT
5756 if (wp->w_p_rl)
5757 {
5758 --off;
5759 --col;
5760 }
5761 else
5762#endif
5763 {
5764 ++off;
5765 ++col;
5766 }
5767 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005768#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005769 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005770 {
5771 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005772 ++vcol_off;
5773 if (n_extra > 0)
5774 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005775 if (wp->w_p_wrap)
5776 {
5777 /*
5778 * Special voodoo required if 'wrap' is on.
5779 *
5780 * Advance the column indicator to force the line
5781 * drawing to wrap early. This will make the line
5782 * take up the same screen space when parts are concealed,
5783 * so that cursor line computations aren't messed up.
5784 *
5785 * To avoid the fictitious advance of 'col' causing
5786 * trailing junk to be written out of the screen line
5787 * we are building, 'boguscols' keeps track of the number
5788 * of bad columns we have advanced.
5789 */
5790 if (n_extra > 0)
5791 {
5792 vcol += n_extra;
5793# ifdef FEAT_RIGHTLEFT
5794 if (wp->w_p_rl)
5795 {
5796 col -= n_extra;
5797 boguscols -= n_extra;
5798 }
5799 else
5800# endif
5801 {
5802 col += n_extra;
5803 boguscols += n_extra;
5804 }
5805 n_extra = 0;
5806 n_attr = 0;
5807 }
5808
5809
Bram Moolenaar860cae12010-06-05 23:22:07 +02005810 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5811 {
5812 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005813# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005814 if (wp->w_p_rl)
5815 {
5816 --boguscols;
5817 --col;
5818 }
5819 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005820# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005821 {
5822 ++boguscols;
5823 ++col;
5824 }
5825 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005826
5827# ifdef FEAT_RIGHTLEFT
5828 if (wp->w_p_rl)
5829 {
5830 --boguscols;
5831 --col;
5832 }
5833 else
5834# endif
5835 {
5836 ++boguscols;
5837 ++col;
5838 }
5839 }
5840 else
5841 {
5842 if (n_extra > 0)
5843 {
5844 vcol += n_extra;
5845 n_extra = 0;
5846 n_attr = 0;
5847 }
5848 }
5849
5850 }
5851#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 else
5853 --n_skip;
5854
Bram Moolenaar64486672010-05-16 15:46:46 +02005855 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5856 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005857 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858#ifdef FEAT_DIFF
5859 && filler_todo <= 0
5860#endif
5861 )
5862 ++vcol;
5863
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005864#ifdef FEAT_SYN_HL
5865 if (vcol_save_attr >= 0)
5866 char_attr = vcol_save_attr;
5867#endif
5868
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 /* restore attributes after "predeces" in 'listchars' */
5870 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5871 char_attr = saved_attr3;
5872
5873 /* restore attributes after last 'listchars' or 'number' char */
5874 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5875 char_attr = saved_attr2;
5876
5877 /*
5878 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005879 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 */
5881 if ((
5882#ifdef FEAT_RIGHTLEFT
5883 wp->w_p_rl ? (col < 0) :
5884#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005885 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 && (*ptr != NUL
5887#ifdef FEAT_DIFF
5888 || filler_todo > 0
5889#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005890 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5892 )
5893 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005894#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005895 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005896 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005897 boguscols = 0;
5898#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005899 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005900 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 ++row;
5903 ++screen_row;
5904
5905 /* When not wrapping and finished diff lines, or when displayed
5906 * '$' and highlighting until last column, break here. */
5907 if ((!wp->w_p_wrap
5908#ifdef FEAT_DIFF
5909 && filler_todo <= 0
5910#endif
5911 ) || lcs_eol_one == -1)
5912 break;
5913
5914 /* When the window is too narrow draw all "@" lines. */
5915 if (draw_state != WL_LINE
5916#ifdef FEAT_DIFF
5917 && filler_todo <= 0
5918#endif
5919 )
5920 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01005921 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 row = endrow;
5924 }
5925
5926 /* When line got too long for screen break here. */
5927 if (row == endrow)
5928 {
5929 ++row;
5930 break;
5931 }
5932
5933 if (screen_cur_row == screen_row - 1
5934#ifdef FEAT_DIFF
5935 && filler_todo <= 0
5936#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005937 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 {
5939 /* Remember that the line wraps, used for modeless copy. */
5940 LineWraps[screen_row - 1] = TRUE;
5941
5942 /*
5943 * Special trick to make copy/paste of wrapped lines work with
5944 * xterm/screen: write an extra character beyond the end of
5945 * the line. This will work with all terminal types
5946 * (regardless of the xn,am settings).
5947 * Only do this on a fast tty.
5948 * Only do this if the cursor is on the current line
5949 * (something has been written in it).
5950 * Don't do this for the GUI.
5951 * Don't do this for double-width characters.
5952 * Don't do this for a window not at the right screen border.
5953 */
5954 if (p_tf
5955#ifdef FEAT_GUI
5956 && !gui.in_use
5957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005959 && ((*mb_off2cells)(LineOffset[screen_row],
5960 LineOffset[screen_row] + screen_Columns)
5961 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005963 + (int)Columns - 2,
5964 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01005965 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 {
5967 /* First make sure we are at the end of the screen line,
5968 * then output the same character again to let the
5969 * terminal know about the wrap. If the terminal doesn't
5970 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005971 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 screen_char(LineOffset[screen_row - 1]
5973 + (unsigned)Columns - 1,
5974 screen_row - 1, (int)(Columns - 1));
5975
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 /* When there is a multi-byte character, just output a
5977 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005978 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5979 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 out_char(' ');
5981 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 out_char(ScreenLines[LineOffset[screen_row - 1]
5983 + (Columns - 1)]);
5984 /* force a redraw of the first char on the next line */
5985 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5986 screen_start(); /* don't know where cursor is now */
5987 }
5988 }
5989
5990 col = 0;
5991 off = (unsigned)(current_ScreenLine - ScreenLines);
5992#ifdef FEAT_RIGHTLEFT
5993 if (wp->w_p_rl)
5994 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005995 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 off += col;
5997 }
5998#endif
5999
6000 /* reset the drawing state for the start of a wrapped line */
6001 draw_state = WL_START;
6002 saved_n_extra = n_extra;
6003 saved_p_extra = p_extra;
6004 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006005 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 saved_char_attr = char_attr;
6007 n_extra = 0;
6008 lcs_prec_todo = lcs_prec;
6009#ifdef FEAT_LINEBREAK
6010# ifdef FEAT_DIFF
6011 if (filler_todo <= 0)
6012# endif
6013 need_showbreak = TRUE;
6014#endif
6015#ifdef FEAT_DIFF
6016 --filler_todo;
6017 /* When the filler lines are actually below the last line of the
6018 * file, don't draw the line itself, break here. */
6019 if (filler_todo == 0 && wp->w_botfill)
6020 break;
6021#endif
6022 }
6023
6024 } /* for every character in the line */
6025
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006026#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006027 /* After an empty line check first word for capital. */
6028 if (*skipwhite(line) == NUL)
6029 {
6030 capcol_lnum = lnum + 1;
6031 cap_col = 0;
6032 }
6033#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006034#ifdef FEAT_TEXT_PROP
6035 vim_free(text_props);
6036 vim_free(text_prop_idxs);
6037#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006038
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006039 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 return row;
6041}
6042
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006043/*
6044 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006045 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006046 */
6047 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006048comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006049{
6050 int i;
6051
6052 for (i = 0; i < Screen_mco; ++i)
6053 {
6054 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6055 return TRUE;
6056 if (ScreenLinesC[i][off_from] == 0)
6057 break;
6058 }
6059 return FALSE;
6060}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006061
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062/*
6063 * Check whether the given character needs redrawing:
6064 * - the (first byte of the) character is different
6065 * - the attributes are different
6066 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006067 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 */
6069 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006070char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071{
6072 if (cols > 0
6073 && ((ScreenLines[off_from] != ScreenLines[off_to]
6074 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 || (enc_dbcs != 0
6076 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6077 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6078 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6079 : (cols > 1 && ScreenLines[off_from + 1]
6080 != ScreenLines[off_to + 1])))
6081 || (enc_utf8
6082 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6083 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006084 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006085 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6086 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006087 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 return TRUE;
6089 return FALSE;
6090}
6091
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006092#if defined(FEAT_TERMINAL) || defined(PROTO)
6093/*
6094 * Return the index in ScreenLines[] for the current screen line.
6095 */
6096 int
6097screen_get_current_line_off()
6098{
6099 return (int)(current_ScreenLine - ScreenLines);
6100}
6101#endif
6102
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103/*
6104 * Move one "cooked" screen line to the screen, but only the characters that
6105 * have actually changed. Handle insert/delete character.
6106 * "coloff" gives the first column on the screen for this line.
6107 * "endcol" gives the columns where valid characters are.
6108 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6109 * needs to be cleared, negative otherwise.
6110 * "rlflag" is TRUE in a rightleft window:
6111 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6112 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6113 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006114 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006115screen_line(
6116 int row,
6117 int coloff,
6118 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006119 int clear_width,
6120 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
6122 unsigned off_from;
6123 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006124 unsigned max_off_from;
6125 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 int force = FALSE; /* force update rest of the line */
6129 int redraw_this /* bool: does character need redraw? */
6130#ifdef FEAT_GUI
6131 = TRUE /* For GUI when while-loop empty */
6132#endif
6133 ;
6134 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 int clear_next = FALSE;
6136 int char_cells; /* 1: normal char */
6137 /* 2: occupies two display cells */
6138# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006140 /* Check for illegal row and col, just in case. */
6141 if (row >= Rows)
6142 row = Rows - 1;
6143 if (endcol > Columns)
6144 endcol = Columns;
6145
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146# ifdef FEAT_CLIPBOARD
6147 clip_may_clear_selection(row, row);
6148# endif
6149
6150 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6151 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006152 max_off_from = off_from + screen_Columns;
6153 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154
6155#ifdef FEAT_RIGHTLEFT
6156 if (rlflag)
6157 {
6158 /* Clear rest first, because it's left of the text. */
6159 if (clear_width > 0)
6160 {
6161 while (col <= endcol && ScreenLines[off_to] == ' '
6162 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006163 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 {
6165 ++off_to;
6166 ++col;
6167 }
6168 if (col <= endcol)
6169 screen_fill(row, row + 1, col + coloff,
6170 endcol + coloff + 1, ' ', ' ', 0);
6171 }
6172 col = endcol + 1;
6173 off_to = LineOffset[row] + col + coloff;
6174 off_from += col;
6175 endcol = (clear_width > 0 ? clear_width : -clear_width);
6176 }
6177#endif /* FEAT_RIGHTLEFT */
6178
6179 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6180
6181 while (col < endcol)
6182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006184 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 else
6186 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187
6188 redraw_this = redraw_next;
6189 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6190 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6191
6192#ifdef FEAT_GUI
6193 /* If the next character was bold, then redraw the current character to
6194 * remove any pixels that might have spilt over into us. This only
6195 * happens in the GUI.
6196 */
6197 if (redraw_next && gui.in_use)
6198 {
6199 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006200 if (hl > HL_ALL)
6201 hl = syn_attr2attr(hl);
6202 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 redraw_this = TRUE;
6204 }
6205#endif
6206
6207 if (redraw_this)
6208 {
6209 /*
6210 * Special handling when 'xs' termcap flag set (hpterm):
6211 * Attributes for characters are stored at the position where the
6212 * cursor is when writing the highlighting code. The
6213 * start-highlighting code must be written with the cursor on the
6214 * first highlighted character. The stop-highlighting code must
6215 * be written with the cursor just after the last highlighted
6216 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006217 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 * to clear the rest of the line, and force redrawing it
6219 * completely.
6220 */
6221 if ( p_wiv
6222 && !force
6223#ifdef FEAT_GUI
6224 && !gui.in_use
6225#endif
6226 && ScreenAttrs[off_to] != 0
6227 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6228 {
6229 /*
6230 * Need to remove highlighting attributes here.
6231 */
6232 windgoto(row, col + coloff);
6233 out_str(T_CE); /* clear rest of this screen line */
6234 screen_start(); /* don't know where cursor is now */
6235 force = TRUE; /* force redraw of rest of the line */
6236 redraw_next = TRUE; /* or else next char would miss out */
6237
6238 /*
6239 * If the previous character was highlighted, need to stop
6240 * highlighting at this character.
6241 */
6242 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6243 {
6244 screen_attr = ScreenAttrs[off_to - 1];
6245 term_windgoto(row, col + coloff);
6246 screen_stop_highlight();
6247 }
6248 else
6249 screen_attr = 0; /* highlighting has stopped */
6250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 if (enc_dbcs != 0)
6252 {
6253 /* Check if overwriting a double-byte with a single-byte or
6254 * the other way around requires another character to be
6255 * redrawn. For UTF-8 this isn't needed, because comparing
6256 * ScreenLinesUC[] is sufficient. */
6257 if (char_cells == 1
6258 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006259 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 {
6261 /* Writing a single-cell character over a double-cell
6262 * character: need to redraw the next cell. */
6263 ScreenLines[off_to + 1] = 0;
6264 redraw_next = TRUE;
6265 }
6266 else if (char_cells == 2
6267 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006268 && (*mb_off2cells)(off_to, max_off_to) == 1
6269 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 {
6271 /* Writing the second half of a double-cell character over
6272 * a double-cell character: need to redraw the second
6273 * cell. */
6274 ScreenLines[off_to + 2] = 0;
6275 redraw_next = TRUE;
6276 }
6277
6278 if (enc_dbcs == DBCS_JPNU)
6279 ScreenLines2[off_to] = ScreenLines2[off_from];
6280 }
6281 /* When writing a single-width character over a double-width
6282 * character and at the end of the redrawn text, need to clear out
6283 * the right halve of the old character.
6284 * Also required when writing the right halve of a double-width
6285 * char over the left halve of an existing one. */
6286 if (has_mbyte && col + char_cells == endcol
6287 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006288 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006290 && (*mb_off2cells)(off_to, max_off_to) == 1
6291 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293
6294 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 if (enc_utf8)
6296 {
6297 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6298 if (ScreenLinesUC[off_from] != 0)
6299 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006300 int i;
6301
6302 for (i = 0; i < Screen_mco; ++i)
6303 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 }
6305 }
6306 if (char_cells == 2)
6307 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308
6309#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006310 /* The bold trick makes a single column of pixels appear in the
6311 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 * character should be redrawn too. This happens for our own GUI
6313 * and for some xterms. */
6314 if (
6315# ifdef FEAT_GUI
6316 gui.in_use
6317# endif
6318# if defined(FEAT_GUI) && defined(UNIX)
6319 ||
6320# endif
6321# ifdef UNIX
6322 term_is_xterm
6323# endif
6324 )
6325 {
6326 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006327 if (hl > HL_ALL)
6328 hl = syn_attr2attr(hl);
6329 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 redraw_next = TRUE;
6331 }
6332#endif
6333 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006334
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006335 /* For simplicity set the attributes of second half of a
6336 * double-wide character equal to the first half. */
6337 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006339
6340 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 screen_char(off_to, row, col + coloff);
6344 }
6345 else if ( p_wiv
6346#ifdef FEAT_GUI
6347 && !gui.in_use
6348#endif
6349 && col + coloff > 0)
6350 {
6351 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6352 {
6353 /*
6354 * Don't output stop-highlight when moving the cursor, it will
6355 * stop the highlighting when it should continue.
6356 */
6357 screen_attr = 0;
6358 }
6359 else if (screen_attr != 0)
6360 screen_stop_highlight();
6361 }
6362
6363 off_to += CHAR_CELLS;
6364 off_from += CHAR_CELLS;
6365 col += CHAR_CELLS;
6366 }
6367
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 if (clear_next)
6369 {
6370 /* Clear the second half of a double-wide character of which the left
6371 * half was overwritten with a single-wide character. */
6372 ScreenLines[off_to] = ' ';
6373 if (enc_utf8)
6374 ScreenLinesUC[off_to] = 0;
6375 screen_char(off_to, row, col + coloff);
6376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
6378 if (clear_width > 0
6379#ifdef FEAT_RIGHTLEFT
6380 && !rlflag
6381#endif
6382 )
6383 {
6384#ifdef FEAT_GUI
6385 int startCol = col;
6386#endif
6387
6388 /* blank out the rest of the line */
6389 while (col < clear_width && ScreenLines[off_to] == ' '
6390 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006391 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 {
6393 ++off_to;
6394 ++col;
6395 }
6396 if (col < clear_width)
6397 {
6398#ifdef FEAT_GUI
6399 /*
6400 * In the GUI, clearing the rest of the line may leave pixels
6401 * behind if the first character cleared was bold. Some bold
6402 * fonts spill over the left. In this case we redraw the previous
6403 * character too. If we didn't skip any blanks above, then we
6404 * only redraw if the character wasn't already redrawn anyway.
6405 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006406 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 {
6408 hl = ScreenAttrs[off_to];
6409 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006410 {
6411 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006412
Bram Moolenaar9c697322006-10-09 20:11:17 +00006413 if (enc_utf8)
6414 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6415 * that its width is 2. */
6416 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6417 else if (enc_dbcs != 0)
6418 {
6419 /* find previous character by counting from first
6420 * column and get its width. */
6421 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006422 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006423
6424 while (off < off_to)
6425 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006426 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006427 off += prev_cells;
6428 }
6429 }
6430
6431 if (enc_dbcs != 0 && prev_cells > 1)
6432 screen_char_2(off_to - prev_cells, row,
6433 col + coloff - prev_cells);
6434 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006435 screen_char(off_to - prev_cells, row,
6436 col + coloff - prev_cells);
6437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 }
6439#endif
6440 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6441 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 off_to += clear_width - col;
6443 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 }
6445 }
6446
6447 if (clear_width > 0)
6448 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 /* For a window that's left of another, draw the separator char. */
6450 if (col + coloff < Columns)
6451 {
6452 int c;
6453
6454 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006455 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006456 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6457 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 || ScreenAttrs[off_to] != hl)
6459 {
6460 ScreenLines[off_to] = c;
6461 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 if (enc_utf8)
6463 {
6464 if (c >= 0x80)
6465 {
6466 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006467 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 }
6469 else
6470 ScreenLinesUC[off_to] = 0;
6471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 screen_char(off_to, row, col + coloff);
6473 }
6474 }
6475 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 LineWraps[row] = FALSE;
6477 }
6478}
6479
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006480#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006482 * Mirror text "str" for right-left displaying.
6483 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006485 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006486rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
6488 char_u *p1, *p2;
6489 int t;
6490
6491 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6492 {
6493 t = *p1;
6494 *p1 = *p2;
6495 *p2 = t;
6496 }
6497}
6498#endif
6499
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500/*
6501 * mark all status lines for redraw; used after first :cd
6502 */
6503 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006504status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505{
6506 win_T *wp;
6507
Bram Moolenaar29323592016-07-24 22:04:11 +02006508 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 if (wp->w_status_height)
6510 {
6511 wp->w_redr_status = TRUE;
6512 redraw_later(VALID);
6513 }
6514}
6515
6516/*
6517 * mark all status lines of the current buffer for redraw
6518 */
6519 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006520status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521{
6522 win_T *wp;
6523
Bram Moolenaar29323592016-07-24 22:04:11 +02006524 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6526 {
6527 wp->w_redr_status = TRUE;
6528 redraw_later(VALID);
6529 }
6530}
6531
6532/*
6533 * Redraw all status lines that need to be redrawn.
6534 */
6535 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006536redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
6538 win_T *wp;
6539
Bram Moolenaar29323592016-07-24 22:04:11 +02006540 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006542 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006543 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006544 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546
Bram Moolenaar4033c552017-09-16 20:54:51 +02006547#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548/*
6549 * Redraw all status lines at the bottom of frame "frp".
6550 */
6551 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006552win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553{
6554 if (frp->fr_layout == FR_LEAF)
6555 frp->fr_win->w_redr_status = TRUE;
6556 else if (frp->fr_layout == FR_ROW)
6557 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006558 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 win_redraw_last_status(frp);
6560 }
6561 else /* frp->fr_layout == FR_COL */
6562 {
6563 frp = frp->fr_child;
6564 while (frp->fr_next != NULL)
6565 frp = frp->fr_next;
6566 win_redraw_last_status(frp);
6567 }
6568}
6569#endif
6570
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571/*
6572 * Draw the verticap separator right of window "wp" starting with line "row".
6573 */
6574 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006575draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576{
6577 int hl;
6578 int c;
6579
6580 if (wp->w_vsep_width)
6581 {
6582 /* draw the vertical separator right of this window */
6583 c = fillchar_vsep(&hl);
6584 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6585 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6586 c, ' ', hl);
6587 }
6588}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589
6590#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006591static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592
6593/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006594 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 */
6596 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006597status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598{
6599 int len = 0;
6600
6601#ifdef FEAT_MENU
6602 int emenu = (xp->xp_context == EXPAND_MENUS
6603 || xp->xp_context == EXPAND_MENUNAMES);
6604
6605 /* Check for menu separators - replace with '|'. */
6606 if (emenu && menu_is_separator(s))
6607 return 1;
6608#endif
6609
6610 while (*s != NUL)
6611 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006612 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006613 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006614 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 }
6616
6617 return len;
6618}
6619
6620/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006621 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006622 * These are backslashes used for escaping. Do show backslashes in help tags.
6623 */
6624 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006625skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006626{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006627 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006628#ifdef FEAT_MENU
6629 || ((xp->xp_context == EXPAND_MENUS
6630 || xp->xp_context == EXPAND_MENUNAMES)
6631 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6632#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006633 )
6634 {
6635#ifndef BACKSLASH_IN_FILENAME
6636 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6637 return 2;
6638#endif
6639 return 1;
6640 }
6641 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006642}
6643
6644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 * Show wildchar matches in the status line.
6646 * Show at least the "match" item.
6647 * We start at item 'first_match' in the list and show all matches that fit.
6648 *
6649 * If inversion is possible we use it. Else '=' characters are used.
6650 */
6651 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006652win_redr_status_matches(
6653 expand_T *xp,
6654 int num_matches,
6655 char_u **matches, /* list of matches */
6656 int match,
6657 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658{
6659#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6660 int row;
6661 char_u *buf;
6662 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006663 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 int fillchar;
6665 int attr;
6666 int i;
6667 int highlight = TRUE;
6668 char_u *selstart = NULL;
6669 int selstart_col = 0;
6670 char_u *selend = NULL;
6671 static int first_match = 0;
6672 int add_left = FALSE;
6673 char_u *s;
6674#ifdef FEAT_MENU
6675 int emenu;
6676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
6679 if (matches == NULL) /* interrupted completion? */
6680 return;
6681
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006682 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006683 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006684 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006685 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 if (buf == NULL)
6687 return;
6688
6689 if (match == -1) /* don't show match but original text */
6690 {
6691 match = 0;
6692 highlight = FALSE;
6693 }
6694 /* count 1 for the ending ">" */
6695 clen = status_match_len(xp, L_MATCH(match)) + 3;
6696 if (match == 0)
6697 first_match = 0;
6698 else if (match < first_match)
6699 {
6700 /* jumping left, as far as we can go */
6701 first_match = match;
6702 add_left = TRUE;
6703 }
6704 else
6705 {
6706 /* check if match fits on the screen */
6707 for (i = first_match; i < match; ++i)
6708 clen += status_match_len(xp, L_MATCH(i)) + 2;
6709 if (first_match > 0)
6710 clen += 2;
6711 /* jumping right, put match at the left */
6712 if ((long)clen > Columns)
6713 {
6714 first_match = match;
6715 /* if showing the last match, we can add some on the left */
6716 clen = 2;
6717 for (i = match; i < num_matches; ++i)
6718 {
6719 clen += status_match_len(xp, L_MATCH(i)) + 2;
6720 if ((long)clen >= Columns)
6721 break;
6722 }
6723 if (i == num_matches)
6724 add_left = TRUE;
6725 }
6726 }
6727 if (add_left)
6728 while (first_match > 0)
6729 {
6730 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6731 if ((long)clen >= Columns)
6732 break;
6733 --first_match;
6734 }
6735
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006736 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737
6738 if (first_match == 0)
6739 {
6740 *buf = NUL;
6741 len = 0;
6742 }
6743 else
6744 {
6745 STRCPY(buf, "< ");
6746 len = 2;
6747 }
6748 clen = len;
6749
6750 i = first_match;
6751 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6752 {
6753 if (i == match)
6754 {
6755 selstart = buf + len;
6756 selstart_col = clen;
6757 }
6758
6759 s = L_MATCH(i);
6760 /* Check for menu separators - replace with '|' */
6761#ifdef FEAT_MENU
6762 emenu = (xp->xp_context == EXPAND_MENUS
6763 || xp->xp_context == EXPAND_MENUNAMES);
6764 if (emenu && menu_is_separator(s))
6765 {
6766 STRCPY(buf + len, transchar('|'));
6767 l = (int)STRLEN(buf + len);
6768 len += l;
6769 clen += l;
6770 }
6771 else
6772#endif
6773 for ( ; *s != NUL; ++s)
6774 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006775 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006777 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 {
6779 STRNCPY(buf + len, s, l);
6780 s += l - 1;
6781 len += l;
6782 }
6783 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 {
6785 STRCPY(buf + len, transchar_byte(*s));
6786 len += (int)STRLEN(buf + len);
6787 }
6788 }
6789 if (i == match)
6790 selend = buf + len;
6791
6792 *(buf + len++) = ' ';
6793 *(buf + len++) = ' ';
6794 clen += 2;
6795 if (++i == num_matches)
6796 break;
6797 }
6798
6799 if (i != num_matches)
6800 {
6801 *(buf + len++) = '>';
6802 ++clen;
6803 }
6804
6805 buf[len] = NUL;
6806
6807 row = cmdline_row - 1;
6808 if (row >= 0)
6809 {
6810 if (wild_menu_showing == 0)
6811 {
6812 if (msg_scrolled > 0)
6813 {
6814 /* Put the wildmenu just above the command line. If there is
6815 * no room, scroll the screen one line up. */
6816 if (cmdline_row == Rows - 1)
6817 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006818 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 ++msg_scrolled;
6820 }
6821 else
6822 {
6823 ++cmdline_row;
6824 ++row;
6825 }
6826 wild_menu_showing = WM_SCROLLED;
6827 }
6828 else
6829 {
6830 /* Create status line if needed by setting 'laststatus' to 2.
6831 * Set 'winminheight' to zero to avoid that the window is
6832 * resized. */
6833 if (lastwin->w_status_height == 0)
6834 {
6835 save_p_ls = p_ls;
6836 save_p_wmh = p_wmh;
6837 p_ls = 2;
6838 p_wmh = 0;
6839 last_status(FALSE);
6840 }
6841 wild_menu_showing = WM_SHOWN;
6842 }
6843 }
6844
6845 screen_puts(buf, row, 0, attr);
6846 if (selstart != NULL && highlight)
6847 {
6848 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006849 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 }
6851
6852 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6853 }
6854
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 vim_free(buf);
6857}
6858#endif
6859
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860/*
6861 * Redraw the status line of window wp.
6862 *
6863 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006864 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6865 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006867 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006868win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869{
6870 int row;
6871 char_u *p;
6872 int len;
6873 int fillchar;
6874 int attr;
6875 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006876 static int busy = FALSE;
6877
6878 /* It's possible to get here recursively when 'statusline' (indirectly)
6879 * invokes ":redrawstatus". Simply ignore the call then. */
6880 if (busy)
6881 return;
6882 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883
6884 wp->w_redr_status = FALSE;
6885 if (wp->w_status_height == 0)
6886 {
6887 /* no status line, can only be last window */
6888 redraw_cmdline = TRUE;
6889 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006890 else if (!redrawing()
6891#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006892 // don't update status line when popup menu is visible and may be
6893 // drawn over it, unless it will be redrawn later
6894 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006895#endif
6896 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 {
6898 /* Don't redraw right now, do it later. */
6899 wp->w_redr_status = TRUE;
6900 }
6901#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006902 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 {
6904 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006905 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 }
6907#endif
6908 else
6909 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006910 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006912 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 p = NameBuff;
6914 len = (int)STRLEN(p);
6915
Bram Moolenaard85f2712017-07-28 21:51:57 +02006916 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917#ifdef FEAT_QUICKFIX
6918 || wp->w_p_pvw
6919#endif
6920 || bufIsChanged(wp->w_buffer)
6921 || wp->w_buffer->b_p_ro)
6922 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006923 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006925 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 len += (int)STRLEN(p + len);
6927 }
6928#ifdef FEAT_QUICKFIX
6929 if (wp->w_p_pvw)
6930 {
6931 STRCPY(p + len, _("[Preview]"));
6932 len += (int)STRLEN(p + len);
6933 }
6934#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006935 if (bufIsChanged(wp->w_buffer)
6936#ifdef FEAT_TERMINAL
6937 && !bt_terminal(wp->w_buffer)
6938#endif
6939 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 {
6941 STRCPY(p + len, "[+]");
6942 len += 3;
6943 }
6944 if (wp->w_buffer->b_p_ro)
6945 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006946 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006947 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 }
6949
Bram Moolenaar02631462017-09-22 15:20:32 +02006950 this_ru_col = ru_col - (Columns - wp->w_width);
6951 if (this_ru_col < (wp->w_width + 1) / 2)
6952 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 if (this_ru_col <= 1)
6954 {
6955 p = (char_u *)"<"; /* No room for file name! */
6956 len = 1;
6957 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01006958 else if (has_mbyte)
6959 {
6960 int clen = 0, i;
6961
6962 /* Count total number of display cells. */
6963 clen = mb_string2cells(p, -1);
6964
6965 /* Find first character that will fit.
6966 * Going from start to end is much faster for DBCS. */
6967 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
6968 i += (*mb_ptr2len)(p + i))
6969 clen -= (*mb_ptr2cells)(p + i);
6970 len = clen;
6971 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01006973 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01006975 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 }
6977
Bram Moolenaara12a1612019-01-24 16:39:02 +01006978 }
6979 else if (len > this_ru_col - 1)
6980 {
6981 p += len - (this_ru_col - 1);
6982 *p = '<';
6983 len = this_ru_col - 1;
6984 }
6985
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006987 screen_puts(p, row, wp->w_wincol, attr);
6988 screen_fill(row, row + 1, len + wp->w_wincol,
6989 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006991 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6993 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006994 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995
6996#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02006997 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998#endif
6999 }
7000
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 /*
7002 * May need to draw the character below the vertical separator.
7003 */
7004 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7005 {
7006 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007007 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 else
7009 fillchar = fillchar_vsep(&attr);
7010 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7011 attr);
7012 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007013 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014}
7015
Bram Moolenaar238a5642006-02-21 22:12:05 +00007016#ifdef FEAT_STL_OPT
7017/*
7018 * Redraw the status line according to 'statusline' and take care of any
7019 * errors encountered.
7020 */
7021 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007022redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007023{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007024 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007025 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007026
7027 /* When called recursively return. This can happen when the statusline
7028 * contains an expression that triggers a redraw. */
7029 if (entered)
7030 return;
7031 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007032
Bram Moolenaara742e082016-04-05 21:10:38 +02007033 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007034 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007035 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007036 {
7037 /* When there is an error disable the statusline, otherwise the
7038 * display is messed up with errors and a redraw triggers the problem
7039 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007040 set_string_option_direct((char_u *)"statusline", -1,
7041 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007042 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007043 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007044 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007045 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007046}
7047#endif
7048
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049/*
7050 * Return TRUE if the status line of window "wp" is connected to the status
7051 * line of the window right of it. If not, then it's a vertical separator.
7052 * Only call if (wp->w_vsep_width != 0).
7053 */
7054 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007055stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056{
7057 frame_T *fr;
7058
7059 fr = wp->w_frame;
7060 while (fr->fr_parent != NULL)
7061 {
7062 if (fr->fr_parent->fr_layout == FR_COL)
7063 {
7064 if (fr->fr_next != NULL)
7065 break;
7066 }
7067 else
7068 {
7069 if (fr->fr_next != NULL)
7070 return TRUE;
7071 }
7072 fr = fr->fr_parent;
7073 }
7074 return FALSE;
7075}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078/*
7079 * Get the value to show for the language mappings, active 'keymap'.
7080 */
7081 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007082get_keymap_str(
7083 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007084 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007085 char_u *buf, /* buffer for the result */
7086 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087{
7088 char_u *p;
7089
7090 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7091 return FALSE;
7092
7093 {
7094#ifdef FEAT_EVAL
7095 buf_T *old_curbuf = curbuf;
7096 win_T *old_curwin = curwin;
7097 char_u *s;
7098
7099 curbuf = wp->w_buffer;
7100 curwin = wp;
7101 STRCPY(buf, "b:keymap_name"); /* must be writable */
7102 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007103 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 --emsg_skip;
7105 curbuf = old_curbuf;
7106 curwin = old_curwin;
7107 if (p == NULL || *p == NUL)
7108#endif
7109 {
7110#ifdef FEAT_KEYMAP
7111 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7112 p = wp->w_buffer->b_p_keymap;
7113 else
7114#endif
7115 p = (char_u *)"lang";
7116 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007117 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 buf[0] = NUL;
7119#ifdef FEAT_EVAL
7120 vim_free(s);
7121#endif
7122 }
7123 return buf[0] != NUL;
7124}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
7126#if defined(FEAT_STL_OPT) || defined(PROTO)
7127/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007128 * Redraw the status line or ruler of window "wp".
7129 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 */
7131 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007132win_redr_custom(
7133 win_T *wp,
7134 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007136 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 int attr;
7138 int curattr;
7139 int row;
7140 int col = 0;
7141 int maxwidth;
7142 int width;
7143 int n;
7144 int len;
7145 int fillchar;
7146 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007147 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007149 struct stl_hlrec hltab[STL_MAX_ITEM];
7150 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007151 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007152 win_T *ewp;
7153 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154
Bram Moolenaar1d633412013-12-11 15:52:01 +01007155 /* There is a tiny chance that this gets called recursively: When
7156 * redrawing a status line triggers redrawing the ruler or tabline.
7157 * Avoid trouble by not allowing recursion. */
7158 if (entered)
7159 return;
7160 entered = TRUE;
7161
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007163 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007165 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007166 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007167 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007168 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007169 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007170 maxwidth = Columns;
7171# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007172 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007173# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007175 else
7176 {
7177 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007178 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007179 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007180
7181 if (draw_ruler)
7182 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007183 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007184 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007185 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007186 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007187 if (*++stl == '-')
7188 stl++;
7189 if (atoi((char *)stl))
7190 while (VIM_ISDIGIT(*stl))
7191 stl++;
7192 if (*stl++ != '(')
7193 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007194 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007195 col = ru_col - (Columns - wp->w_width);
7196 if (col < (wp->w_width + 1) / 2)
7197 col = (wp->w_width + 1) / 2;
7198 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007199 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007200 {
7201 row = Rows - 1;
7202 --maxwidth; /* writing in last column may cause scrolling */
7203 fillchar = ' ';
7204 attr = 0;
7205 }
7206
7207# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007208 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007209# endif
7210 }
7211 else
7212 {
7213 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007214 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007215 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007216 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007217# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007218 use_sandbox = was_set_insecurely((char_u *)"statusline",
7219 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007220# endif
7221 }
7222
Bram Moolenaar53f81742017-09-22 14:35:51 +02007223 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007224 }
7225
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007227 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228
Bram Moolenaar61452852011-02-01 18:01:11 +01007229 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7230 * the cursor away and back. */
7231 ewp = wp == NULL ? curwin : wp;
7232 p_crb_save = ewp->w_p_crb;
7233 ewp->w_p_crb = FALSE;
7234
Bram Moolenaar362f3562009-11-03 16:20:34 +00007235 /* Make a copy, because the statusline may include a function call that
7236 * might change the option value and free the memory. */
7237 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007238 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007239 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007240 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007241 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007242 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007244 /* Make all characters printable. */
7245 p = transstr(buf);
7246 if (p != NULL)
7247 {
7248 vim_strncpy(buf, p, sizeof(buf) - 1);
7249 vim_free(p);
7250 }
7251
7252 /* fill up with "fillchar" */
7253 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007254 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 ++width;
7258 }
7259 buf[len] = NUL;
7260
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007261 /*
7262 * Draw each snippet with the specified highlighting.
7263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 curattr = attr;
7265 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007266 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007268 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 screen_puts_len(p, len, row, col, curattr);
7270 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007271 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007273 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007275 else if (hltab[n].userhl < 0)
7276 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007277#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007278 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7279 && wp->w_status_height != 0)
7280 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007281 else if (wp != NULL && bt_terminal(wp->w_buffer)
7282 && wp->w_status_height != 0)
7283 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007284#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007285 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007286 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007288 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 }
7290 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007291
7292 if (wp == NULL)
7293 {
7294 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7295 col = 0;
7296 len = 0;
7297 p = buf;
7298 fillchar = 0;
7299 for (n = 0; tabtab[n].start != NULL; n++)
7300 {
7301 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7302 while (col < len)
7303 TabPageIdxs[col++] = fillchar;
7304 p = tabtab[n].start;
7305 fillchar = tabtab[n].userhl;
7306 }
7307 while (col < Columns)
7308 TabPageIdxs[col++] = fillchar;
7309 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007310
7311theend:
7312 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313}
7314
7315#endif /* FEAT_STL_OPT */
7316
7317/*
7318 * Output a single character directly to the screen and update ScreenLines.
7319 */
7320 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007321screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 char_u buf[MB_MAXBYTES + 1];
7324
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007325 if (has_mbyte)
7326 buf[(*mb_char2bytes)(c, buf)] = NUL;
7327 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007328 {
7329 buf[0] = c;
7330 buf[1] = NUL;
7331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 screen_puts(buf, row, col, attr);
7333}
7334
7335/*
7336 * Get a single character directly from ScreenLines into "bytes[]".
7337 * Also return its attribute in *attrp;
7338 */
7339 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007340screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341{
7342 unsigned off;
7343
7344 /* safety check */
7345 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7346 {
7347 off = LineOffset[row] + col;
7348 *attrp = ScreenAttrs[off];
7349 bytes[0] = ScreenLines[off];
7350 bytes[1] = NUL;
7351
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 if (enc_utf8 && ScreenLinesUC[off] != 0)
7353 bytes[utfc_char2bytes(off, bytes)] = NUL;
7354 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7355 {
7356 bytes[0] = ScreenLines[off];
7357 bytes[1] = ScreenLines2[off];
7358 bytes[2] = NUL;
7359 }
7360 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7361 {
7362 bytes[1] = ScreenLines[off + 1];
7363 bytes[2] = NUL;
7364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 }
7366}
7367
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007368/*
7369 * Return TRUE if composing characters for screen posn "off" differs from
7370 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007371 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007372 */
7373 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007374screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007375{
7376 int i;
7377
7378 for (i = 0; i < Screen_mco; ++i)
7379 {
7380 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7381 return TRUE;
7382 if (u8cc[i] == 0)
7383 break;
7384 }
7385 return FALSE;
7386}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007387
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388/*
7389 * Put string '*text' on the screen at position 'row' and 'col', with
7390 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7391 * Note: only outputs within one row, message is truncated at screen boundary!
7392 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7393 */
7394 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007395screen_puts(
7396 char_u *text,
7397 int row,
7398 int col,
7399 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400{
7401 screen_puts_len(text, -1, row, col, attr);
7402}
7403
7404/*
7405 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7406 * a NUL.
7407 */
7408 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007409screen_puts_len(
7410 char_u *text,
7411 int textlen,
7412 int row,
7413 int col,
7414 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
7416 unsigned off;
7417 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007418 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007420 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 int mbyte_blen = 1;
7422 int mbyte_cells = 1;
7423 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007424 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007426#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007428 int pc, nc, nc1;
7429 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007431 int force_redraw_this;
7432 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007433 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434
7435 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7436 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007437 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438
Bram Moolenaarc236c162008-07-13 17:41:49 +00007439 /* When drawing over the right halve of a double-wide char clear out the
7440 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007441 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007442#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007443 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007444#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007445 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007446 {
7447 ScreenLines[off - 1] = ' ';
7448 ScreenAttrs[off - 1] = 0;
7449 if (enc_utf8)
7450 {
7451 ScreenLinesUC[off - 1] = 0;
7452 ScreenLinesC[0][off - 1] = 0;
7453 }
7454 /* redraw the previous cell, make it empty */
7455 screen_char(off - 1, row, col - 1);
7456 /* force the cell at "col" to be redrawn */
7457 force_redraw_next = TRUE;
7458 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007459
Bram Moolenaar367329b2007-08-30 11:53:22 +00007460 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007461 while (col < screen_Columns
7462 && (len < 0 || (int)(ptr - text) < len)
7463 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 {
7465 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 /* check if this is the first byte of a multibyte */
7467 if (has_mbyte)
7468 {
7469 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007470 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007472 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7474 mbyte_cells = 1;
7475 else if (enc_dbcs != 0)
7476 mbyte_cells = mbyte_blen;
7477 else /* enc_utf8 */
7478 {
7479 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007480 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 (int)((text + len) - ptr));
7482 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007483 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007485#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7487 {
7488 /* Do Arabic shaping. */
7489 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7490 {
7491 /* Past end of string to be displayed. */
7492 nc = NUL;
7493 nc1 = NUL;
7494 }
7495 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007496 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007497 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7498 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007499 nc1 = pcc[0];
7500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 pc = prev_c;
7502 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007503 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 }
7505 else
7506 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007507#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007508 if (col + mbyte_cells > screen_Columns)
7509 {
7510 /* Only 1 cell left, but character requires 2 cells:
7511 * display a '>' in the last column to avoid wrapping. */
7512 c = '>';
7513 mbyte_cells = 1;
7514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 }
7516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007518 force_redraw_this = force_redraw_next;
7519 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007520
7521 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 || (mbyte_cells == 2
7523 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7524 || (enc_dbcs == DBCS_JPNU
7525 && c == 0x8e
7526 && ScreenLines2[off] != ptr[1])
7527 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007528 && (ScreenLinesUC[off] !=
7529 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7530 || (ScreenLinesUC[off] != 0
7531 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007533 || exmode_active;
7534
Bram Moolenaara12a1612019-01-24 16:39:02 +01007535 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 {
7537#if defined(FEAT_GUI) || defined(UNIX)
7538 /* The bold trick makes a single row of pixels appear in the next
7539 * character. When a bold character is removed, the next
7540 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007541 * and for some xterms. */
7542 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543# ifdef FEAT_GUI
7544 gui.in_use
7545# endif
7546# if defined(FEAT_GUI) && defined(UNIX)
7547 ||
7548# endif
7549# ifdef UNIX
7550 term_is_xterm
7551# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007552 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007554 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007556 if (n > HL_ALL)
7557 n = syn_attr2attr(n);
7558 if (n & HL_BOLD)
7559 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 }
7561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 /* When at the end of the text and overwriting a two-cell
7563 * character with a one-cell character, need to clear the next
7564 * cell. Also when overwriting the left halve of a two-cell char
7565 * with the right halve of a two-cell char. Do this only once
7566 * (mb_off2cells() may return 2 on the right halve). */
7567 if (clear_next_cell)
7568 clear_next_cell = FALSE;
7569 else if (has_mbyte
7570 && (len < 0 ? ptr[mbyte_blen] == NUL
7571 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007572 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007574 && (*mb_off2cells)(off, max_off) == 1
7575 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 clear_next_cell = TRUE;
7577
7578 /* Make sure we never leave a second byte of a double-byte behind,
7579 * it confuses mb_off2cells(). */
7580 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007581 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007583 && (*mb_off2cells)(off, max_off) == 1
7584 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 ScreenLines[off] = c;
7587 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 if (enc_utf8)
7589 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007590 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 ScreenLinesUC[off] = 0;
7592 else
7593 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007594 int i;
7595
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007597 for (i = 0; i < Screen_mco; ++i)
7598 {
7599 ScreenLinesC[i][off] = u8cc[i];
7600 if (u8cc[i] == 0)
7601 break;
7602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 }
7604 if (mbyte_cells == 2)
7605 {
7606 ScreenLines[off + 1] = 0;
7607 ScreenAttrs[off + 1] = attr;
7608 }
7609 screen_char(off, row, col);
7610 }
7611 else if (mbyte_cells == 2)
7612 {
7613 ScreenLines[off + 1] = ptr[1];
7614 ScreenAttrs[off + 1] = attr;
7615 screen_char_2(off, row, col);
7616 }
7617 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7618 {
7619 ScreenLines2[off] = ptr[1];
7620 screen_char(off, row, col);
7621 }
7622 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 screen_char(off, row, col);
7624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 if (has_mbyte)
7626 {
7627 off += mbyte_cells;
7628 col += mbyte_cells;
7629 ptr += mbyte_blen;
7630 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007631 {
7632 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007634 len = -1;
7635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 }
7637 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 {
7639 ++off;
7640 ++col;
7641 ++ptr;
7642 }
7643 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007644
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007645 /* If we detected the next character needs to be redrawn, but the text
7646 * doesn't extend up to there, update the character here. */
7647 if (force_redraw_next && col < screen_Columns)
7648 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007649 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7650 screen_char_2(off, row, col);
7651 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007652 screen_char(off, row, col);
7653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654}
7655
7656#ifdef FEAT_SEARCH_EXTRA
7657/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007658 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 */
7660 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007661start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662{
7663 if (p_hls && !no_hlsearch)
7664 {
7665 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007666 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007667# ifdef FEAT_RELTIME
7668 /* Set the time limit to 'redrawtime'. */
7669 profile_setlimit(p_rdt, &search_hl.tm);
7670# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 }
7672}
7673
7674/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007675 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 */
7677 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007678end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679{
7680 if (search_hl.rm.regprog != NULL)
7681 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007682 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 search_hl.rm.regprog = NULL;
7684 }
7685}
7686
7687/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007688 * Init for calling prepare_search_hl().
7689 */
7690 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007691init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007692{
7693 matchitem_T *cur;
7694
7695 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7696 * match */
7697 cur = wp->w_match_head;
7698 while (cur != NULL)
7699 {
7700 cur->hl.rm = cur->match;
7701 if (cur->hlg_id == 0)
7702 cur->hl.attr = 0;
7703 else
7704 cur->hl.attr = syn_id2attr(cur->hlg_id);
7705 cur->hl.buf = wp->w_buffer;
7706 cur->hl.lnum = 0;
7707 cur->hl.first_lnum = 0;
7708# ifdef FEAT_RELTIME
7709 /* Set the time limit to 'redrawtime'. */
7710 profile_setlimit(p_rdt, &(cur->hl.tm));
7711# endif
7712 cur = cur->next;
7713 }
7714 search_hl.buf = wp->w_buffer;
7715 search_hl.lnum = 0;
7716 search_hl.first_lnum = 0;
7717 /* time limit is set at the toplevel, for all windows */
7718}
7719
7720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 * Advance to the match in window "wp" line "lnum" or past it.
7722 */
7723 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007724prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007726 matchitem_T *cur; /* points to the match list */
7727 match_T *shl; /* points to search_hl or a match */
7728 int shl_flag; /* flag to indicate whether search_hl
7729 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007730 int pos_inprogress; /* marks that position match search is
7731 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 int n;
7733
7734 /*
7735 * When using a multi-line pattern, start searching at the top
7736 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007737 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007739 cur = wp->w_match_head;
7740 shl_flag = FALSE;
7741 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007743 if (shl_flag == FALSE)
7744 {
7745 shl = &search_hl;
7746 shl_flag = TRUE;
7747 }
7748 else
7749 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 if (shl->rm.regprog != NULL
7751 && shl->lnum == 0
7752 && re_multiline(shl->rm.regprog))
7753 {
7754 if (shl->first_lnum == 0)
7755 {
7756# ifdef FEAT_FOLDING
7757 for (shl->first_lnum = lnum;
7758 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7759 if (hasFoldingWin(wp, shl->first_lnum - 1,
7760 NULL, NULL, TRUE, NULL))
7761 break;
7762# else
7763 shl->first_lnum = wp->w_topline;
7764# endif
7765 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007766 if (cur != NULL)
7767 cur->pos.cur = 0;
7768 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007770 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7771 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007773 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7774 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007775 pos_inprogress = cur == NULL || cur->pos.cur == 0
7776 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 if (shl->lnum != 0)
7778 {
7779 shl->first_lnum = shl->lnum
7780 + shl->rm.endpos[0].lnum
7781 - shl->rm.startpos[0].lnum;
7782 n = shl->rm.endpos[0].col;
7783 }
7784 else
7785 {
7786 ++shl->first_lnum;
7787 n = 0;
7788 }
7789 }
7790 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007791 if (shl != &search_hl && cur != NULL)
7792 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 }
7794}
7795
7796/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007797 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 * Uses shl->buf.
7799 * Sets shl->lnum and shl->rm contents.
7800 * Note: Assumes a previous match is always before "lnum", unless
7801 * shl->lnum is zero.
7802 * Careful: Any pointers for buffer lines will become invalid.
7803 */
7804 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007805next_search_hl(
7806 win_T *win,
7807 match_T *shl, /* points to search_hl or a match */
7808 linenr_T lnum,
7809 colnr_T mincol, /* minimal column for a match */
7810 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811{
7812 linenr_T l;
7813 colnr_T matchcol;
7814 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007815 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007817 // for :{range}s/pat only highlight inside the range
7818 if (lnum < search_first_line || lnum > search_last_line)
7819 {
7820 shl->lnum = 0;
7821 return;
7822 }
7823
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 if (shl->lnum != 0)
7825 {
7826 /* Check for three situations:
7827 * 1. If the "lnum" is below a previous match, start a new search.
7828 * 2. If the previous match includes "mincol", use it.
7829 * 3. Continue after the previous match.
7830 */
7831 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7832 if (lnum > l)
7833 shl->lnum = 0;
7834 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7835 return;
7836 }
7837
7838 /*
7839 * Repeat searching for a match until one is found that includes "mincol"
7840 * or none is found in this line.
7841 */
7842 called_emsg = FALSE;
7843 for (;;)
7844 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007845#ifdef FEAT_RELTIME
7846 /* Stop searching after passing the time limit. */
7847 if (profile_passed_limit(&(shl->tm)))
7848 {
7849 shl->lnum = 0; /* no match found in time */
7850 break;
7851 }
7852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 /* Three situations:
7854 * 1. No useful previous match: search from start of line.
7855 * 2. Not Vi compatible or empty match: continue at next character.
7856 * Break the loop if this is beyond the end of the line.
7857 * 3. Vi compatible searching: continue at end of previous match.
7858 */
7859 if (shl->lnum == 0)
7860 matchcol = 0;
7861 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7862 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007863 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007865 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007866
7867 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007868 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007869 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007871 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 shl->lnum = 0;
7873 break;
7874 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007875 if (has_mbyte)
7876 matchcol += mb_ptr2len(ml);
7877 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007878 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 }
7880 else
7881 matchcol = shl->rm.endpos[0].col;
7882
7883 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007884 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007886 /* Remember whether shl->rm is using a copy of the regprog in
7887 * cur->match. */
7888 int regprog_is_copy = (shl != &search_hl && cur != NULL
7889 && shl == &cur->hl
7890 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007891 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007892
Bram Moolenaarb3414592014-06-17 17:48:32 +02007893 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7894 matchcol,
7895#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007896 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007897#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007898 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007899#endif
7900 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007901 /* Copy the regprog, in case it got freed and recompiled. */
7902 if (regprog_is_copy)
7903 cur->match.regprog = cur->hl.rm.regprog;
7904
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007905 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007906 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007907 /* Error while handling regexp: stop using this regexp. */
7908 if (shl == &search_hl)
7909 {
7910 /* don't free regprog in the match list, it's a copy */
7911 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02007912 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007913 }
7914 shl->rm.regprog = NULL;
7915 shl->lnum = 0;
7916 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7917 message */
7918 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007919 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007920 }
7921 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007922 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007923 else
7924 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 if (nmatched == 0)
7926 {
7927 shl->lnum = 0; /* no match found */
7928 break;
7929 }
7930 if (shl->rm.startpos[0].lnum > 0
7931 || shl->rm.startpos[0].col >= mincol
7932 || nmatched > 1
7933 || shl->rm.endpos[0].col > mincol)
7934 {
7935 shl->lnum += shl->rm.startpos[0].lnum;
7936 break; /* useful match found */
7937 }
7938 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007939
7940 // Restore called_emsg for assert_fails().
7941 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943
Bram Moolenaar85077472016-10-16 14:35:48 +02007944/*
7945 * If there is a match fill "shl" and return one.
7946 * Return zero otherwise.
7947 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007948 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007949next_search_hl_pos(
7950 match_T *shl, /* points to a match */
7951 linenr_T lnum,
7952 posmatch_T *posmatch, /* match positions */
7953 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007954{
7955 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007956 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007957
Bram Moolenaarb3414592014-06-17 17:48:32 +02007958 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7959 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007960 llpos_T *pos = &posmatch->pos[i];
7961
7962 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007963 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007964 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007965 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007966 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007967 {
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 /* if this match comes before the one at "found" then swap
7971 * them */
7972 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007973 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007974 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007975
Bram Moolenaar85077472016-10-16 14:35:48 +02007976 *pos = posmatch->pos[found];
7977 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007978 }
7979 }
7980 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007981 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007982 }
7983 }
7984 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007985 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007986 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007987 colnr_T start = posmatch->pos[found].col == 0
7988 ? 0 : posmatch->pos[found].col - 1;
7989 colnr_T end = posmatch->pos[found].col == 0
7990 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007991
Bram Moolenaar85077472016-10-16 14:35:48 +02007992 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007993 shl->rm.startpos[0].lnum = 0;
7994 shl->rm.startpos[0].col = start;
7995 shl->rm.endpos[0].lnum = 0;
7996 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007997 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007998 posmatch->cur = found + 1;
7999 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008000 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008001 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008002}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008003#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008004
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008006screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007{
8008 attrentry_T *aep = NULL;
8009
8010 screen_attr = attr;
8011 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008012#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 && termcap_active
8014#endif
8015 )
8016 {
8017#ifdef FEAT_GUI
8018 if (gui.in_use)
8019 {
8020 char buf[20];
8021
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008022 /* The GUI handles this internally. */
8023 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 OUT_STR(buf);
8025 }
8026 else
8027#endif
8028 {
8029 if (attr > HL_ALL) /* special HL attr. */
8030 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008031 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 aep = syn_cterm_attr2entry(attr);
8033 else
8034 aep = syn_term_attr2entry(attr);
8035 if (aep == NULL) /* did ":syntax clear" */
8036 attr = 0;
8037 else
8038 attr = aep->ae_attr;
8039 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008040 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008042 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008043#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008044 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8045 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8046 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008047#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008048 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008049 /* If the Normal FG color has BOLD attribute and the new HL
8050 * has a FG color defined, clear BOLD. */
8051 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008052 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008054 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008055 out_str(T_UCS);
8056 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008057 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8058 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008060 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008062 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008064 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008065 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066
8067 /*
8068 * Output the color or start string after bold etc., in case the
8069 * bold etc. override the color setting.
8070 */
8071 if (aep != NULL)
8072 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008073#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008074 /* When 'termguicolors' is set but fg or bg is unset,
8075 * fall back to the cterm colors. This helps for SpellBad,
8076 * where the GUI uses a red undercurl. */
8077 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008079 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008080 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008081 }
8082 else
8083#endif
8084 if (t_colors > 1)
8085 {
8086 if (aep->ae_u.cterm.fg_color)
8087 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8088 }
8089#ifdef FEAT_TERMGUICOLORS
8090 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8091 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008092 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008093 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 }
8095 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008096#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008097 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008099 if (aep->ae_u.cterm.bg_color)
8100 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8101 }
8102
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008103 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008104 {
8105 if (aep->ae_u.term.start != NULL)
8106 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 }
8108 }
8109 }
8110 }
8111}
8112
8113 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008114screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115{
8116 int do_ME = FALSE; /* output T_ME code */
8117
8118 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008119#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 && termcap_active
8121#endif
8122 )
8123 {
8124#ifdef FEAT_GUI
8125 if (gui.in_use)
8126 {
8127 char buf[20];
8128
8129 /* use internal GUI code */
8130 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8131 OUT_STR(buf);
8132 }
8133 else
8134#endif
8135 {
8136 if (screen_attr > HL_ALL) /* special HL attr. */
8137 {
8138 attrentry_T *aep;
8139
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008140 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {
8142 /*
8143 * Assume that t_me restores the original colors!
8144 */
8145 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008146 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008147#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008148 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8149 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8150 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008151#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008152 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008153#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008154 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8155 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8156 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008157#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008158 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 do_ME = TRUE;
8160 }
8161 else
8162 {
8163 aep = syn_term_attr2entry(screen_attr);
8164 if (aep != NULL && aep->ae_u.term.stop != NULL)
8165 {
8166 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8167 do_ME = TRUE;
8168 else
8169 out_str(aep->ae_u.term.stop);
8170 }
8171 }
8172 if (aep == NULL) /* did ":syntax clear" */
8173 screen_attr = 0;
8174 else
8175 screen_attr = aep->ae_attr;
8176 }
8177
8178 /*
8179 * Often all ending-codes are equal to T_ME. Avoid outputting the
8180 * same sequence several times.
8181 */
8182 if (screen_attr & HL_STANDOUT)
8183 {
8184 if (STRCMP(T_SE, T_ME) == 0)
8185 do_ME = TRUE;
8186 else
8187 out_str(T_SE);
8188 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008189 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008190 {
8191 if (STRCMP(T_UCE, T_ME) == 0)
8192 do_ME = TRUE;
8193 else
8194 out_str(T_UCE);
8195 }
8196 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008197 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {
8199 if (STRCMP(T_UE, T_ME) == 0)
8200 do_ME = TRUE;
8201 else
8202 out_str(T_UE);
8203 }
8204 if (screen_attr & HL_ITALIC)
8205 {
8206 if (STRCMP(T_CZR, T_ME) == 0)
8207 do_ME = TRUE;
8208 else
8209 out_str(T_CZR);
8210 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008211 if (screen_attr & HL_STRIKETHROUGH)
8212 {
8213 if (STRCMP(T_STE, T_ME) == 0)
8214 do_ME = TRUE;
8215 else
8216 out_str(T_STE);
8217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8219 out_str(T_ME);
8220
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008221#ifdef FEAT_TERMGUICOLORS
8222 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008224 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008225 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008226 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008227 term_bg_rgb_color(cterm_normal_bg_gui_color);
8228 }
8229 else
8230#endif
8231 {
8232 if (t_colors > 1)
8233 {
8234 /* set Normal cterm colors */
8235 if (cterm_normal_fg_color != 0)
8236 term_fg_color(cterm_normal_fg_color - 1);
8237 if (cterm_normal_bg_color != 0)
8238 term_bg_color(cterm_normal_bg_color - 1);
8239 if (cterm_normal_fg_bold)
8240 out_str(T_MD);
8241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 }
8243 }
8244 }
8245 screen_attr = 0;
8246}
8247
8248/*
8249 * Reset the colors for a cterm. Used when leaving Vim.
8250 * The machine specific code may override this again.
8251 */
8252 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008253reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008255 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {
8257 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008258#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008259 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8260 || cterm_normal_bg_gui_color != INVALCOLOR)
8261 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008262#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 {
8266 out_str(T_OP);
8267 screen_attr = -1;
8268 }
8269 if (cterm_normal_fg_bold)
8270 {
8271 out_str(T_ME);
8272 screen_attr = -1;
8273 }
8274 }
8275}
8276
8277/*
8278 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8279 * using the attributes from ScreenAttrs["off"].
8280 */
8281 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008282screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283{
8284 int attr;
8285
8286 /* Check for illegal values, just in case (could happen just after
8287 * resizing). */
8288 if (row >= screen_Rows || col >= screen_Columns)
8289 return;
8290
Bram Moolenaarae654382019-01-17 21:09:05 +01008291#ifdef FEAT_INS_EXPAND
8292 if (pum_under_menu(row, col))
8293 return;
8294#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008295 /* Outputting a character in the last cell on the screen may scroll the
8296 * screen up. Only do it when the "xn" termcap property is set, otherwise
8297 * mark the character invalid (update it when scrolled up). */
8298 if (*T_XN == NUL
8299 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300#ifdef FEAT_RIGHTLEFT
8301 /* account for first command-line character in rightleft mode */
8302 && !cmdmsg_rl
8303#endif
8304 )
8305 {
8306 ScreenAttrs[off] = (sattr_T)-1;
8307 return;
8308 }
8309
8310 /*
8311 * Stop highlighting first, so it's easier to move the cursor.
8312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 if (screen_char_attr != 0)
8314 attr = screen_char_attr;
8315 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 attr = ScreenAttrs[off];
8317 if (screen_attr != attr)
8318 screen_stop_highlight();
8319
8320 windgoto(row, col);
8321
8322 if (screen_attr != attr)
8323 screen_start_highlight(attr);
8324
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 if (enc_utf8 && ScreenLinesUC[off] != 0)
8326 {
8327 char_u buf[MB_MAXBYTES + 1];
8328
Bram Moolenaarcb070082016-04-02 22:14:51 +02008329 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008330 {
8331 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008332#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008333 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008334#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008335 )
8336 {
8337 /* Clear the two screen cells. If the character is actually
8338 * single width it won't change the second cell. */
8339 out_str((char_u *)" ");
8340 term_windgoto(row, col);
8341 }
8342 /* not sure where the cursor is after drawing the ambiguous width
8343 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008344 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008345 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008346 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008348
8349 /* Convert the UTF-8 character to bytes and write it. */
8350 buf[utfc_char2bytes(off, buf)] = NUL;
8351 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 }
8353 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 /* double-byte character in single-width cell */
8358 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8359 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 }
8361
8362 screen_cur_col++;
8363}
8364
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365/*
8366 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8367 * on the screen at position 'row' and 'col'.
8368 * The attributes of the first byte is used for all. This is required to
8369 * output the two bytes of a double-byte character with nothing in between.
8370 */
8371 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008372screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373{
8374 /* Check for illegal values (could be wrong when screen was resized). */
8375 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8376 return;
8377
8378 /* Outputting the last character on the screen may scrollup the screen.
8379 * Don't to it! Mark the character invalid (update it when scrolled up) */
8380 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8381 {
8382 ScreenAttrs[off] = (sattr_T)-1;
8383 return;
8384 }
8385
8386 /* Output the first byte normally (positions the cursor), then write the
8387 * second byte directly. */
8388 screen_char(off, row, col);
8389 out_char(ScreenLines[off + 1]);
8390 ++screen_cur_col;
8391}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393/*
8394 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8395 * This uses the contents of ScreenLines[] and doesn't change it.
8396 */
8397 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008398screen_draw_rectangle(
8399 int row,
8400 int col,
8401 int height,
8402 int width,
8403 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
8405 int r, c;
8406 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008407 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008409 /* Can't use ScreenLines unless initialized */
8410 if (ScreenLines == NULL)
8411 return;
8412
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 if (invert)
8414 screen_char_attr = HL_INVERSE;
8415 for (r = row; r < row + height; ++r)
8416 {
8417 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008418 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 for (c = col; c < col + width; ++c)
8420 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008421 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 {
8423 screen_char_2(off + c, r, c);
8424 ++c;
8425 }
8426 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 {
8428 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008429 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 }
8432 }
8433 }
8434 screen_char_attr = 0;
8435}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437/*
8438 * Redraw the characters for a vertically split window.
8439 */
8440 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008441redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442{
8443 int col;
8444 int width;
8445
8446# ifdef FEAT_CLIPBOARD
8447 clip_may_clear_selection(row, end - 1);
8448# endif
8449
8450 if (wp == NULL)
8451 {
8452 col = 0;
8453 width = Columns;
8454 }
8455 else
8456 {
8457 col = wp->w_wincol;
8458 width = wp->w_width;
8459 }
8460 screen_draw_rectangle(row, col, end - row, width, FALSE);
8461}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008463 static void
8464space_to_screenline(int off, int attr)
8465{
8466 ScreenLines[off] = ' ';
8467 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008468 if (enc_utf8)
8469 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008470}
8471
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472/*
8473 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8474 * with character 'c1' in first column followed by 'c2' in the other columns.
8475 * Use attributes 'attr'.
8476 */
8477 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008478screen_fill(
8479 int start_row,
8480 int end_row,
8481 int start_col,
8482 int end_col,
8483 int c1,
8484 int c2,
8485 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486{
8487 int row;
8488 int col;
8489 int off;
8490 int end_off;
8491 int did_delete;
8492 int c;
8493 int norm_term;
8494#if defined(FEAT_GUI) || defined(UNIX)
8495 int force_next = FALSE;
8496#endif
8497
8498 if (end_row > screen_Rows) /* safety check */
8499 end_row = screen_Rows;
8500 if (end_col > screen_Columns) /* safety check */
8501 end_col = screen_Columns;
8502 if (ScreenLines == NULL
8503 || start_row >= end_row
8504 || start_col >= end_col) /* nothing to do */
8505 return;
8506
8507 /* it's a "normal" terminal when not in a GUI or cterm */
8508 norm_term = (
8509#ifdef FEAT_GUI
8510 !gui.in_use &&
8511#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008512 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 for (row = start_row; row < end_row; ++row)
8514 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008515 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008516#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008517 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008518#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008519 )
8520 {
8521 /* When drawing over the right halve of a double-wide char clear
8522 * out the left halve. When drawing over the left halve of a
8523 * double wide-char clear out the right halve. Only needed in a
8524 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008525 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008526 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008527 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008528 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 /*
8531 * Try to use delete-line termcap code, when no attributes or in a
8532 * "normal" terminal, where a bold/italic space is just a
8533 * space.
8534 */
8535 did_delete = FALSE;
8536 if (c2 == ' '
8537 && end_col == Columns
8538 && can_clear(T_CE)
8539 && (attr == 0
8540 || (norm_term
8541 && attr <= HL_ALL
8542 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8543 {
8544 /*
8545 * check if we really need to clear something
8546 */
8547 col = start_col;
8548 if (c1 != ' ') /* don't clear first char */
8549 ++col;
8550
8551 off = LineOffset[row] + col;
8552 end_off = LineOffset[row] + end_col;
8553
8554 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 if (enc_utf8)
8556 while (off < end_off && ScreenLines[off] == ' '
8557 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8558 ++off;
8559 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 while (off < end_off && ScreenLines[off] == ' '
8561 && ScreenAttrs[off] == 0)
8562 ++off;
8563 if (off < end_off) /* something to be cleared */
8564 {
8565 col = off - LineOffset[row];
8566 screen_stop_highlight();
8567 term_windgoto(row, col);/* clear rest of this screen line */
8568 out_str(T_CE);
8569 screen_start(); /* don't know where cursor is now */
8570 col = end_col - col;
8571 while (col--) /* clear chars in ScreenLines */
8572 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008573 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 ++off;
8575 }
8576 }
8577 did_delete = TRUE; /* the chars are cleared now */
8578 }
8579
8580 off = LineOffset[row] + start_col;
8581 c = c1;
8582 for (col = start_col; col < end_col; ++col)
8583 {
8584 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008585 || (enc_utf8 && (int)ScreenLinesUC[off]
8586 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 || ScreenAttrs[off] != attr
8588#if defined(FEAT_GUI) || defined(UNIX)
8589 || force_next
8590#endif
8591 )
8592 {
8593#if defined(FEAT_GUI) || defined(UNIX)
8594 /* The bold trick may make a single row of pixels appear in
8595 * the next character. When a bold character is removed, the
8596 * next character should be redrawn too. This happens for our
8597 * own GUI and for some xterms. */
8598 if (
8599# ifdef FEAT_GUI
8600 gui.in_use
8601# endif
8602# if defined(FEAT_GUI) && defined(UNIX)
8603 ||
8604# endif
8605# ifdef UNIX
8606 term_is_xterm
8607# endif
8608 )
8609 {
8610 if (ScreenLines[off] != ' '
8611 && (ScreenAttrs[off] > HL_ALL
8612 || ScreenAttrs[off] & HL_BOLD))
8613 force_next = TRUE;
8614 else
8615 force_next = FALSE;
8616 }
8617#endif
8618 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 if (enc_utf8)
8620 {
8621 if (c >= 0x80)
8622 {
8623 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008624 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 }
8626 else
8627 ScreenLinesUC[off] = 0;
8628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 ScreenAttrs[off] = attr;
8630 if (!did_delete || c != ' ')
8631 screen_char(off, row, col);
8632 }
8633 ++off;
8634 if (col == start_col)
8635 {
8636 if (did_delete)
8637 break;
8638 c = c2;
8639 }
8640 }
8641 if (end_col == Columns)
8642 LineWraps[row] = FALSE;
8643 if (row == Rows - 1) /* overwritten the command line */
8644 {
8645 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008646 if (start_col == 0 && end_col == Columns
8647 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008649 if (start_col == 0)
8650 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 }
8652 }
8653}
8654
8655/*
8656 * Check if there should be a delay. Used before clearing or redrawing the
8657 * screen or the command line.
8658 */
8659 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008660check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661{
8662 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8663 && !did_wait_return
8664 && emsg_silent == 0)
8665 {
8666 out_flush();
8667 ui_delay(1000L, TRUE);
8668 emsg_on_display = FALSE;
8669 if (check_msg_scroll)
8670 msg_scroll = FALSE;
8671 }
8672}
8673
8674/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008675 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8676 */
8677 static void
8678clear_TabPageIdxs(void)
8679{
8680 int scol;
8681
8682 for (scol = 0; scol < Columns; ++scol)
8683 TabPageIdxs[scol] = 0;
8684}
8685
8686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008688 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 * Returns TRUE if there is a valid screen to write to.
8690 * Returns FALSE when starting up and screen not initialized yet.
8691 */
8692 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008693screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008695 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 return (ScreenLines != NULL);
8697}
8698
8699/*
8700 * Resize the shell to Rows and Columns.
8701 * Allocate ScreenLines[] and associated items.
8702 *
8703 * There may be some time between setting Rows and Columns and (re)allocating
8704 * ScreenLines[]. This happens when starting up and when (manually) changing
8705 * the shell size. Always use screen_Rows and screen_Columns to access items
8706 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8707 * final size of the shell is needed.
8708 */
8709 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008710screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711{
8712 int new_row, old_row;
8713#ifdef FEAT_GUI
8714 int old_Rows;
8715#endif
8716 win_T *wp;
8717 int outofmem = FALSE;
8718 int len;
8719 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008721 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008723 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 sattr_T *new_ScreenAttrs;
8725 unsigned *new_LineOffset;
8726 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008727 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008728 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008730 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008731 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008733retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 /*
8735 * Allocation of the screen buffers is done only when the size changes and
8736 * when Rows and Columns have been set and we have started doing full
8737 * screen stuff.
8738 */
8739 if ((ScreenLines != NULL
8740 && Rows == screen_Rows
8741 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 && enc_utf8 == (ScreenLinesUC != NULL)
8743 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008744 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 || Rows == 0
8746 || Columns == 0
8747 || (!full_screen && ScreenLines == NULL))
8748 return;
8749
8750 /*
8751 * It's possible that we produce an out-of-memory message below, which
8752 * will cause this function to be called again. To break the loop, just
8753 * return here.
8754 */
8755 if (entered)
8756 return;
8757 entered = TRUE;
8758
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008759 /*
8760 * Note that the window sizes are updated before reallocating the arrays,
8761 * thus we must not redraw here!
8762 */
8763 ++RedrawingDisabled;
8764
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 win_new_shellsize(); /* fit the windows in the new sized shell */
8766
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 comp_col(); /* recompute columns for shown command and ruler */
8768
8769 /*
8770 * We're changing the size of the screen.
8771 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8772 * - Move lines from the old arrays into the new arrays, clear extra
8773 * lines (unless the screen is going to be cleared).
8774 * - Free the old arrays.
8775 *
8776 * If anything fails, make ScreenLines NULL, so we don't do anything!
8777 * Continuing with the old ScreenLines may result in a crash, because the
8778 * size is wrong.
8779 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008780 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008782 if (aucmd_win != NULL)
8783 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784
8785 new_ScreenLines = (schar_T *)lalloc((long_u)(
8786 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008787 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 if (enc_utf8)
8789 {
8790 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8791 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008792 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008793 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8795 }
8796 if (enc_dbcs == DBCS_JPNU)
8797 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8798 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8800 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8801 new_LineOffset = (unsigned *)lalloc((long_u)(
8802 Rows * sizeof(unsigned)), FALSE);
8803 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008804 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008806 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 {
8808 if (win_alloc_lines(wp) == FAIL)
8809 {
8810 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008811 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 }
8813 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008814 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8815 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008816 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008817give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008819 for (i = 0; i < p_mco; ++i)
8820 if (new_ScreenLinesC[i] == NULL)
8821 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008823 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 || new_ScreenAttrs == NULL
8826 || new_LineOffset == NULL
8827 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008828 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 || outofmem)
8830 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008831 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008832 {
8833 /* guess the size */
8834 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8835
8836 /* Remember we did this to avoid getting outofmem messages over
8837 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008838 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008839 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008840 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008841 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008842 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008843 VIM_CLEAR(new_ScreenLinesC[i]);
8844 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008845 VIM_CLEAR(new_ScreenAttrs);
8846 VIM_CLEAR(new_LineOffset);
8847 VIM_CLEAR(new_LineWraps);
8848 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 }
8850 else
8851 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008852 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008853
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 for (new_row = 0; new_row < Rows; ++new_row)
8855 {
8856 new_LineOffset[new_row] = new_row * Columns;
8857 new_LineWraps[new_row] = FALSE;
8858
8859 /*
8860 * If the screen is not going to be cleared, copy as much as
8861 * possible from the old screen to the new one and clear the rest
8862 * (used when resizing the window at the "--more--" prompt or when
8863 * executing an external command, for the GUI).
8864 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008865 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 {
8867 (void)vim_memset(new_ScreenLines + new_row * Columns,
8868 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 if (enc_utf8)
8870 {
8871 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8872 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008873 for (i = 0; i < p_mco; ++i)
8874 (void)vim_memset(new_ScreenLinesC[i]
8875 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 0, (size_t)Columns * sizeof(u8char_T));
8877 }
8878 if (enc_dbcs == DBCS_JPNU)
8879 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8880 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8882 0, (size_t)Columns * sizeof(sattr_T));
8883 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008884 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 {
8886 if (screen_Columns < Columns)
8887 len = screen_Columns;
8888 else
8889 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008890 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008891 * may be invalid now. Also when p_mco changes. */
8892 if (!(enc_utf8 && ScreenLinesUC == NULL)
8893 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008894 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8895 ScreenLines + LineOffset[old_row],
8896 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008897 if (enc_utf8 && ScreenLinesUC != NULL
8898 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 {
8900 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8901 ScreenLinesUC + LineOffset[old_row],
8902 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008903 for (i = 0; i < p_mco; ++i)
8904 mch_memmove(new_ScreenLinesC[i]
8905 + new_LineOffset[new_row],
8906 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 (size_t)len * sizeof(u8char_T));
8908 }
8909 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8910 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8911 ScreenLines2 + LineOffset[old_row],
8912 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8914 ScreenAttrs + LineOffset[old_row],
8915 (size_t)len * sizeof(sattr_T));
8916 }
8917 }
8918 }
8919 /* Use the last line of the screen for the current line. */
8920 current_ScreenLine = new_ScreenLines + Rows * Columns;
8921 }
8922
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008923 free_screenlines();
8924
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008927 for (i = 0; i < p_mco; ++i)
8928 ScreenLinesC[i] = new_ScreenLinesC[i];
8929 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 ScreenAttrs = new_ScreenAttrs;
8932 LineOffset = new_LineOffset;
8933 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008934 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935
8936 /* It's important that screen_Rows and screen_Columns reflect the actual
8937 * size of ScreenLines[]. Set them before calling anything. */
8938#ifdef FEAT_GUI
8939 old_Rows = screen_Rows;
8940#endif
8941 screen_Rows = Rows;
8942 screen_Columns = Columns;
8943
8944 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008945 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947#ifdef FEAT_GUI
8948 else if (gui.in_use
8949 && !gui.starting
8950 && ScreenLines != NULL
8951 && old_Rows != Rows)
8952 {
8953 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8954 /*
8955 * Adjust the position of the cursor, for when executing an external
8956 * command.
8957 */
8958 if (msg_row >= Rows) /* Rows got smaller */
8959 msg_row = Rows - 1; /* put cursor at last row */
8960 else if (Rows > old_Rows) /* Rows got bigger */
8961 msg_row += Rows - old_Rows; /* put cursor in same place */
8962 if (msg_col >= Columns) /* Columns got smaller */
8963 msg_col = Columns - 1; /* put cursor at last column */
8964 }
8965#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008966 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008969 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008970
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008971 /*
8972 * Do not apply autocommands more than 3 times to avoid an endless loop
8973 * in case applying autocommands always changes Rows or Columns.
8974 */
8975 if (starting == 0 && ++retry_count <= 3)
8976 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008977 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008978 /* In rare cases, autocommands may have altered Rows or Columns,
8979 * jump back to check if we need to allocate the screen again. */
8980 goto retry;
8981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982}
8983
8984 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008985free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008986{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008987 int i;
8988
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008989 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008990 for (i = 0; i < Screen_mco; ++i)
8991 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008992 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008993 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008994 vim_free(ScreenAttrs);
8995 vim_free(LineOffset);
8996 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008997 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008998}
8999
9000 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009001screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002{
9003 check_for_delay(FALSE);
9004 screenalloc(FALSE); /* allocate screen buffers if size changed */
9005 screenclear2(); /* clear the screen */
9006}
9007
9008 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009009screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010{
9011 int i;
9012
9013 if (starting == NO_SCREEN || ScreenLines == NULL
9014#ifdef FEAT_GUI
9015 || (gui.in_use && gui.starting)
9016#endif
9017 )
9018 return;
9019
9020#ifdef FEAT_GUI
9021 if (!gui.in_use)
9022#endif
9023 screen_attr = -1; /* force setting the Normal colors */
9024 screen_stop_highlight(); /* don't want highlighting here */
9025
9026#ifdef FEAT_CLIPBOARD
9027 /* disable selection without redrawing it */
9028 clip_scroll_selection(9999);
9029#endif
9030
9031 /* blank out ScreenLines */
9032 for (i = 0; i < Rows; ++i)
9033 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009034 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 LineWraps[i] = FALSE;
9036 }
9037
9038 if (can_clear(T_CL))
9039 {
9040 out_str(T_CL); /* clear the display */
9041 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009042 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 }
9044 else
9045 {
9046 /* can't clear the screen, mark all chars with invalid attributes */
9047 for (i = 0; i < Rows; ++i)
9048 lineinvalid(LineOffset[i], (int)Columns);
9049 clear_cmdline = TRUE;
9050 }
9051
9052 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9053
9054 win_rest_invalid(firstwin);
9055 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009056 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 if (must_redraw == CLEAR) /* no need to clear again */
9058 must_redraw = NOT_VALID;
9059 compute_cmdrow();
9060 msg_row = cmdline_row; /* put cursor on last line for messages */
9061 msg_col = 0;
9062 screen_start(); /* don't know where cursor is now */
9063 msg_scrolled = 0; /* can't scroll back */
9064 msg_didany = FALSE;
9065 msg_didout = FALSE;
9066}
9067
9068/*
9069 * Clear one line in ScreenLines.
9070 */
9071 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009072lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073{
9074 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 if (enc_utf8)
9076 (void)vim_memset(ScreenLinesUC + off, 0,
9077 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009078 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079}
9080
9081/*
9082 * Mark one line in ScreenLines invalid by setting the attributes to an
9083 * invalid value.
9084 */
9085 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009086lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087{
9088 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9089}
9090
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091/*
9092 * Copy part of a Screenline for vertically split window "wp".
9093 */
9094 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009095linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096{
9097 unsigned off_to = LineOffset[to] + wp->w_wincol;
9098 unsigned off_from = LineOffset[from] + wp->w_wincol;
9099
9100 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9101 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 if (enc_utf8)
9103 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009104 int i;
9105
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9107 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009108 for (i = 0; i < p_mco; ++i)
9109 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9110 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 }
9112 if (enc_dbcs == DBCS_JPNU)
9113 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9114 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9116 wp->w_width * sizeof(sattr_T));
9117}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118
9119/*
9120 * Return TRUE if clearing with term string "p" would work.
9121 * It can't work when the string is empty or it won't set the right background.
9122 */
9123 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009124can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125{
9126 return (*p != NUL && (t_colors <= 1
9127#ifdef FEAT_GUI
9128 || gui.in_use
9129#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009130#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009131 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009132 || (!p_tgc && cterm_normal_bg_color == 0)
9133#else
9134 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009135#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009136 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137}
9138
9139/*
9140 * Reset cursor position. Use whenever cursor was moved because of outputting
9141 * something directly to the screen (shell commands) or a terminal control
9142 * code.
9143 */
9144 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009145screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146{
9147 screen_cur_row = screen_cur_col = 9999;
9148}
9149
9150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 * Move the cursor to position "row","col" in the screen.
9152 * This tries to find the most efficient way to move, minimizing the number of
9153 * characters sent to the terminal.
9154 */
9155 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009156windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009158 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 int i;
9160 int plan;
9161 int cost;
9162 int wouldbe_col;
9163 int noinvcurs;
9164 char_u *bs;
9165 int goto_cost;
9166 int attr;
9167
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009168#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9170
9171#define PLAN_LE 1
9172#define PLAN_CR 2
9173#define PLAN_NL 3
9174#define PLAN_WRITE 4
9175 /* Can't use ScreenLines unless initialized */
9176 if (ScreenLines == NULL)
9177 return;
9178
9179 if (col != screen_cur_col || row != screen_cur_row)
9180 {
9181 /* Check for valid position. */
9182 if (row < 0) /* window without text lines? */
9183 row = 0;
9184 if (row >= screen_Rows)
9185 row = screen_Rows - 1;
9186 if (col >= screen_Columns)
9187 col = screen_Columns - 1;
9188
9189 /* check if no cursor movement is allowed in highlight mode */
9190 if (screen_attr && *T_MS == NUL)
9191 noinvcurs = HIGHL_COST;
9192 else
9193 noinvcurs = 0;
9194 goto_cost = GOTO_COST + noinvcurs;
9195
9196 /*
9197 * Plan how to do the positioning:
9198 * 1. Use CR to move it to column 0, same row.
9199 * 2. Use T_LE to move it a few columns to the left.
9200 * 3. Use NL to move a few lines down, column 0.
9201 * 4. Move a few columns to the right with T_ND or by writing chars.
9202 *
9203 * Don't do this if the cursor went beyond the last column, the cursor
9204 * position is unknown then (some terminals wrap, some don't )
9205 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009206 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 * characters to move the cursor to the right.
9208 */
9209 if (row >= screen_cur_row && screen_cur_col < Columns)
9210 {
9211 /*
9212 * If the cursor is in the same row, bigger col, we can use CR
9213 * or T_LE.
9214 */
9215 bs = NULL; /* init for GCC */
9216 attr = screen_attr;
9217 if (row == screen_cur_row && col < screen_cur_col)
9218 {
9219 /* "le" is preferred over "bc", because "bc" is obsolete */
9220 if (*T_LE)
9221 bs = T_LE; /* "cursor left" */
9222 else
9223 bs = T_BC; /* "backspace character (old) */
9224 if (*bs)
9225 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9226 else
9227 cost = 999;
9228 if (col + 1 < cost) /* using CR is less characters */
9229 {
9230 plan = PLAN_CR;
9231 wouldbe_col = 0;
9232 cost = 1; /* CR is just one character */
9233 }
9234 else
9235 {
9236 plan = PLAN_LE;
9237 wouldbe_col = col;
9238 }
9239 if (noinvcurs) /* will stop highlighting */
9240 {
9241 cost += noinvcurs;
9242 attr = 0;
9243 }
9244 }
9245
9246 /*
9247 * If the cursor is above where we want to be, we can use CR LF.
9248 */
9249 else if (row > screen_cur_row)
9250 {
9251 plan = PLAN_NL;
9252 wouldbe_col = 0;
9253 cost = (row - screen_cur_row) * 2; /* CR LF */
9254 if (noinvcurs) /* will stop highlighting */
9255 {
9256 cost += noinvcurs;
9257 attr = 0;
9258 }
9259 }
9260
9261 /*
9262 * If the cursor is in the same row, smaller col, just use write.
9263 */
9264 else
9265 {
9266 plan = PLAN_WRITE;
9267 wouldbe_col = screen_cur_col;
9268 cost = 0;
9269 }
9270
9271 /*
9272 * Check if any characters that need to be written have the
9273 * correct attributes. Also avoid UTF-8 characters.
9274 */
9275 i = col - wouldbe_col;
9276 if (i > 0)
9277 cost += i;
9278 if (cost < goto_cost && i > 0)
9279 {
9280 /*
9281 * Check if the attributes are correct without additionally
9282 * stopping highlighting.
9283 */
9284 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9285 while (i && *p++ == attr)
9286 --i;
9287 if (i != 0)
9288 {
9289 /*
9290 * Try if it works when highlighting is stopped here.
9291 */
9292 if (*--p == 0)
9293 {
9294 cost += noinvcurs;
9295 while (i && *p++ == 0)
9296 --i;
9297 }
9298 if (i != 0)
9299 cost = 999; /* different attributes, don't do it */
9300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 if (enc_utf8)
9302 {
9303 /* Don't use an UTF-8 char for positioning, it's slow. */
9304 for (i = wouldbe_col; i < col; ++i)
9305 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9306 {
9307 cost = 999;
9308 break;
9309 }
9310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 }
9312
9313 /*
9314 * We can do it without term_windgoto()!
9315 */
9316 if (cost < goto_cost)
9317 {
9318 if (plan == PLAN_LE)
9319 {
9320 if (noinvcurs)
9321 screen_stop_highlight();
9322 while (screen_cur_col > col)
9323 {
9324 out_str(bs);
9325 --screen_cur_col;
9326 }
9327 }
9328 else if (plan == PLAN_CR)
9329 {
9330 if (noinvcurs)
9331 screen_stop_highlight();
9332 out_char('\r');
9333 screen_cur_col = 0;
9334 }
9335 else if (plan == PLAN_NL)
9336 {
9337 if (noinvcurs)
9338 screen_stop_highlight();
9339 while (screen_cur_row < row)
9340 {
9341 out_char('\n');
9342 ++screen_cur_row;
9343 }
9344 screen_cur_col = 0;
9345 }
9346
9347 i = col - screen_cur_col;
9348 if (i > 0)
9349 {
9350 /*
9351 * Use cursor-right if it's one character only. Avoids
9352 * removing a line of pixels from the last bold char, when
9353 * using the bold trick in the GUI.
9354 */
9355 if (T_ND[0] != NUL && T_ND[1] == NUL)
9356 {
9357 while (i-- > 0)
9358 out_char(*T_ND);
9359 }
9360 else
9361 {
9362 int off;
9363
9364 off = LineOffset[row] + screen_cur_col;
9365 while (i-- > 0)
9366 {
9367 if (ScreenAttrs[off] != screen_attr)
9368 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 if (enc_dbcs == DBCS_JPNU
9372 && ScreenLines[off] == 0x8e)
9373 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 ++off;
9375 }
9376 }
9377 }
9378 }
9379 }
9380 else
9381 cost = 999;
9382
9383 if (cost >= goto_cost)
9384 {
9385 if (noinvcurs)
9386 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009387 if (row == screen_cur_row && (col > screen_cur_col)
9388 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 term_cursor_right(col - screen_cur_col);
9390 else
9391 term_windgoto(row, col);
9392 }
9393 screen_cur_row = row;
9394 screen_cur_col = col;
9395 }
9396}
9397
9398/*
9399 * Set cursor to its position in the current window.
9400 */
9401 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009402setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009404 setcursor_mayforce(FALSE);
9405}
9406
9407/*
9408 * Set cursor to its position in the current window.
9409 * When "force" is TRUE also when not redrawing.
9410 */
9411 void
9412setcursor_mayforce(int force)
9413{
9414 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 {
9416 validate_cursor();
9417 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009418 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009420 /* With 'rightleft' set and the cursor on a double-wide
9421 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009422 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9423 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009424 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009425 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426#endif
9427 curwin->w_wcol));
9428 }
9429}
9430
9431
9432/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009433 * Insert 'line_count' lines at 'row' in window 'wp'.
9434 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9435 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 * scrolling.
9437 * Returns FAIL if the lines are not inserted, OK for success.
9438 */
9439 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009440win_ins_lines(
9441 win_T *wp,
9442 int row,
9443 int line_count,
9444 int invalid,
9445 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446{
9447 int did_delete;
9448 int nextrow;
9449 int lastrow;
9450 int retval;
9451
9452 if (invalid)
9453 wp->w_lines_valid = 0;
9454
9455 if (wp->w_height < 5)
9456 return FAIL;
9457
9458 if (line_count > wp->w_height - row)
9459 line_count = wp->w_height - row;
9460
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009461 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 if (retval != MAYBE)
9463 return retval;
9464
9465 /*
9466 * If there is a next window or a status line, we first try to delete the
9467 * lines at the bottom to avoid messing what is after the window.
9468 * If this fails and there are following windows, don't do anything to avoid
9469 * messing up those windows, better just redraw.
9470 */
9471 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 if (wp->w_next != NULL || wp->w_status_height)
9473 {
9474 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009475 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 did_delete = TRUE;
9477 else if (wp->w_next)
9478 return FAIL;
9479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 /*
9481 * if no lines deleted, blank the lines that will end up below the window
9482 */
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 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009487 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 lastrow = nextrow + line_count;
9489 if (lastrow > Rows)
9490 lastrow = Rows;
9491 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009492 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 ' ', ' ', 0);
9494 }
9495
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009496 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 == FAIL)
9498 {
9499 /* deletion will have messed up other windows */
9500 if (did_delete)
9501 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 win_rest_invalid(W_NEXT(wp));
9504 }
9505 return FAIL;
9506 }
9507
9508 return OK;
9509}
9510
9511/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009512 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9514 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9515 * scrolling
9516 * Return OK for success, FAIL if the lines are not deleted.
9517 */
9518 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009519win_del_lines(
9520 win_T *wp,
9521 int row,
9522 int line_count,
9523 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009524 int mayclear,
9525 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526{
9527 int retval;
9528
9529 if (invalid)
9530 wp->w_lines_valid = 0;
9531
9532 if (line_count > wp->w_height - row)
9533 line_count = wp->w_height - row;
9534
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009535 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 if (retval != MAYBE)
9537 return retval;
9538
9539 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009540 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 return FAIL;
9542
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 /*
9544 * If there are windows or status lines below, try to put them at the
9545 * correct place. If we can't do that, they have to be redrawn.
9546 */
9547 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9548 {
9549 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009550 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 {
9552 wp->w_redr_status = TRUE;
9553 win_rest_invalid(wp->w_next);
9554 }
9555 }
9556 /*
9557 * If this is the last window and there is no status line, redraw the
9558 * command line later.
9559 */
9560 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 redraw_cmdline = TRUE;
9562 return OK;
9563}
9564
9565/*
9566 * Common code for win_ins_lines() and win_del_lines().
9567 * Returns OK or FAIL when the work has been done.
9568 * Returns MAYBE when not finished yet.
9569 */
9570 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009571win_do_lines(
9572 win_T *wp,
9573 int row,
9574 int line_count,
9575 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009576 int del,
9577 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578{
9579 int retval;
9580
9581 if (!redrawing() || line_count <= 0)
9582 return FAIL;
9583
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009584 /* When inserting lines would result in loss of command output, just redraw
9585 * the lines. */
9586 if (no_win_do_lines_ins && !del)
9587 return FAIL;
9588
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009590 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009592 if (!no_win_do_lines_ins)
9593 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 return FAIL;
9595 }
9596
9597 /*
9598 * Delete all remaining lines
9599 */
9600 if (row + line_count >= wp->w_height)
9601 {
9602 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009603 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 ' ', ' ', 0);
9605 return OK;
9606 }
9607
9608 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009609 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009611 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009613 if (!no_win_do_lines_ins)
9614 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615
9616 /*
9617 * If the terminal can set a scroll region, use that.
9618 * Always do this in a vertically split window. This will redraw from
9619 * ScreenLines[] when t_CV isn't defined. That's faster than using
9620 * win_line().
9621 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009622 * a character in the lower right corner of the scroll region may cause a
9623 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009625 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 scroll_region_set(wp, row);
9629 if (del)
9630 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009631 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 else
9633 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009634 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 scroll_region_reset();
9637 return retval;
9638 }
9639
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9641 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642
9643 return MAYBE;
9644}
9645
9646/*
9647 * window 'wp' and everything after it is messed up, mark it for redraw
9648 */
9649 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009650win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 {
9654 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 wp->w_redr_status = TRUE;
9656 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 }
9658 redraw_cmdline = TRUE;
9659}
9660
9661/*
9662 * The rest of the routines in this file perform screen manipulations. The
9663 * given operation is performed physically on the screen. The corresponding
9664 * change is also made to the internal screen image. In this way, the editor
9665 * anticipates the effect of editing changes on the appearance of the screen.
9666 * That way, when we call screenupdate a complete redraw isn't usually
9667 * necessary. Another advantage is that we can keep adding code to anticipate
9668 * screen changes, and in the meantime, everything still works.
9669 */
9670
9671/*
9672 * types for inserting or deleting lines
9673 */
9674#define USE_T_CAL 1
9675#define USE_T_CDL 2
9676#define USE_T_AL 3
9677#define USE_T_CE 4
9678#define USE_T_DL 5
9679#define USE_T_SR 6
9680#define USE_NL 7
9681#define USE_T_CD 8
9682#define USE_REDRAW 9
9683
9684/*
9685 * insert lines on the screen and update ScreenLines[]
9686 * 'end' is the line after the scrolled part. Normally it is Rows.
9687 * When scrolling region used 'off' is the offset from the top for the region.
9688 * 'row' and 'end' are relative to the start of the region.
9689 *
9690 * return FAIL for failure, OK for success.
9691 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009692 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009693screen_ins_lines(
9694 int off,
9695 int row,
9696 int line_count,
9697 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009698 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009699 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700{
9701 int i;
9702 int j;
9703 unsigned temp;
9704 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009705 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 int type;
9707 int result_empty;
9708 int can_ce = can_clear(T_CE);
9709
9710 /*
9711 * FAIL if
9712 * - there is no valid screen
9713 * - the screen has to be redrawn completely
9714 * - the line count is less than one
9715 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009716 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009718 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9719#ifdef FEAT_CLIPBOARD
9720 || (clip_star.state != SELECT_CLEARED
9721 && redrawing_for_callback > 0)
9722#endif
9723 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 return FAIL;
9725
9726 /*
9727 * There are seven ways to insert lines:
9728 * 0. When in a vertically split window and t_CV isn't set, redraw the
9729 * characters from ScreenLines[].
9730 * 1. Use T_CD (clear to end of display) if it exists and the result of
9731 * the insert is just empty lines
9732 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9733 * present or line_count > 1. It looks better if we do all the inserts
9734 * at once.
9735 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9736 * insert is just empty lines and T_CE is not present or line_count >
9737 * 1.
9738 * 4. Use T_AL (insert line) if it exists.
9739 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9740 * just empty lines.
9741 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9742 * just empty lines.
9743 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9744 * the 'da' flag is not set or we have clear line capability.
9745 * 8. redraw the characters from ScreenLines[].
9746 *
9747 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9748 * the scrollbar for the window. It does have insert line, use that if it
9749 * exists.
9750 */
9751 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9753 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009754 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 type = USE_T_CD;
9756 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9757 type = USE_T_CAL;
9758 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9759 type = USE_T_CDL;
9760 else if (*T_AL != NUL)
9761 type = USE_T_AL;
9762 else if (can_ce && result_empty)
9763 type = USE_T_CE;
9764 else if (*T_DL != NUL && result_empty)
9765 type = USE_T_DL;
9766 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9767 type = USE_T_SR;
9768 else
9769 return FAIL;
9770
9771 /*
9772 * For clearing the lines screen_del_lines() is used. This will also take
9773 * care of t_db if necessary.
9774 */
9775 if (type == USE_T_CD || type == USE_T_CDL ||
9776 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009777 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778
9779 /*
9780 * If text is retained below the screen, first clear or delete as many
9781 * lines at the bottom of the window as are about to be inserted so that
9782 * the deleted lines won't later surface during a screen_del_lines.
9783 */
9784 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009785 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786
9787#ifdef FEAT_CLIPBOARD
9788 /* Remove a modeless selection when inserting lines halfway the screen
9789 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009790 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009791 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 else
9793 clip_scroll_selection(-line_count);
9794#endif
9795
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796#ifdef FEAT_GUI
9797 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9798 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009799 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800#endif
9801
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009802 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9803 cursor_col = wp->w_wincol;
9804
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 if (*T_CCS != NUL) /* cursor relative to region */
9806 cursor_row = row;
9807 else
9808 cursor_row = row + off;
9809
9810 /*
9811 * Shift LineOffset[] line_count down to reflect the inserted lines.
9812 * Clear the inserted lines in ScreenLines[].
9813 */
9814 row += off;
9815 end += off;
9816 for (i = 0; i < line_count; ++i)
9817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 if (wp != NULL && wp->w_width != Columns)
9819 {
9820 /* need to copy part of a line */
9821 j = end - 1 - i;
9822 while ((j -= line_count) >= row)
9823 linecopy(j + line_count, j, wp);
9824 j += line_count;
9825 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009826 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9827 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 else
9829 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9830 LineWraps[j] = FALSE;
9831 }
9832 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 {
9834 j = end - 1 - i;
9835 temp = LineOffset[j];
9836 while ((j -= line_count) >= row)
9837 {
9838 LineOffset[j + line_count] = LineOffset[j];
9839 LineWraps[j + line_count] = LineWraps[j];
9840 }
9841 LineOffset[j + line_count] = temp;
9842 LineWraps[j + line_count] = FALSE;
9843 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009844 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 else
9846 lineinvalid(temp, (int)Columns);
9847 }
9848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849
9850 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009851 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009852 if (clear_attr != 0)
9853 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 /* redraw the characters */
9856 if (type == USE_REDRAW)
9857 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009858 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 {
9860 term_append_lines(line_count);
9861 screen_start(); /* don't know where cursor is now */
9862 }
9863 else
9864 {
9865 for (i = 0; i < line_count; i++)
9866 {
9867 if (type == USE_T_AL)
9868 {
9869 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009870 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 out_str(T_AL);
9872 }
9873 else /* type == USE_T_SR */
9874 out_str(T_SR);
9875 screen_start(); /* don't know where cursor is now */
9876 }
9877 }
9878
9879 /*
9880 * With scroll-reverse and 'da' flag set we need to clear the lines that
9881 * have been scrolled down into the region.
9882 */
9883 if (type == USE_T_SR && *T_DA)
9884 {
9885 for (i = 0; i < line_count; ++i)
9886 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009887 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 out_str(T_CE);
9889 screen_start(); /* don't know where cursor is now */
9890 }
9891 }
9892
9893#ifdef FEAT_GUI
9894 gui_can_update_cursor();
9895 if (gui.in_use)
9896 out_flush(); /* always flush after a scroll */
9897#endif
9898 return OK;
9899}
9900
9901/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009902 * Delete lines on the screen and update ScreenLines[].
9903 * "end" is the line after the scrolled part. Normally it is Rows.
9904 * When scrolling region used "off" is the offset from the top for the region.
9905 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 *
9907 * Return OK for success, FAIL if the lines are not deleted.
9908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009910screen_del_lines(
9911 int off,
9912 int row,
9913 int line_count,
9914 int end,
9915 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009916 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009917 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918{
9919 int j;
9920 int i;
9921 unsigned temp;
9922 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009923 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 int cursor_end;
9925 int result_empty; /* result is empty until end of region */
9926 int can_delete; /* deleting line codes can be used */
9927 int type;
9928
9929 /*
9930 * FAIL if
9931 * - there is no valid screen
9932 * - the screen has to be redrawn completely
9933 * - the line count is less than one
9934 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009935 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009937 if (!screen_valid(TRUE) || line_count <= 0
9938 || (!force && line_count > p_ttyscroll)
9939#ifdef FEAT_CLIPBOARD
9940 || (clip_star.state != SELECT_CLEARED
9941 && redrawing_for_callback > 0)
9942#endif
9943 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 return FAIL;
9945
9946 /*
9947 * Check if the rest of the current region will become empty.
9948 */
9949 result_empty = row + line_count >= end;
9950
9951 /*
9952 * We can delete lines only when 'db' flag not set or when 'ce' option
9953 * available.
9954 */
9955 can_delete = (*T_DB == NUL || can_clear(T_CE));
9956
9957 /*
9958 * There are six ways to delete lines:
9959 * 0. When in a vertically split window and t_CV isn't set, redraw the
9960 * characters from ScreenLines[].
9961 * 1. Use T_CD if it exists and the result is empty.
9962 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9963 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9964 * none of the other ways work.
9965 * 4. Use T_CE (erase line) if the result is empty.
9966 * 5. Use T_DL (delete line) if it exists.
9967 * 6. redraw the characters from ScreenLines[].
9968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9970 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009971 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 type = USE_T_CD;
9973#if defined(__BEOS__) && defined(BEOS_DR8)
9974 /*
9975 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9976 * its internal termcap... this works okay for tests which test *T_DB !=
9977 * NUL. It has the disadvantage that the user cannot use any :set t_*
9978 * command to get T_DB (back) to empty_option, only :set term=... will do
9979 * the trick...
9980 * Anyway, this hack will hopefully go away with the next OS release.
9981 * (Olaf Seibert)
9982 */
9983 else if (row == 0 && T_DB == empty_option
9984 && (line_count == 1 || *T_CDL == NUL))
9985#else
9986 else if (row == 0 && (
9987#ifndef AMIGA
9988 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9989 * up, so use delete-line command */
9990 line_count == 1 ||
9991#endif
9992 *T_CDL == NUL))
9993#endif
9994 type = USE_NL;
9995 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9996 type = USE_T_CDL;
9997 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02009998 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 type = USE_T_CE;
10000 else if (*T_DL != NUL && can_delete)
10001 type = USE_T_DL;
10002 else if (*T_CDL != NUL && can_delete)
10003 type = USE_T_CDL;
10004 else
10005 return FAIL;
10006
10007#ifdef FEAT_CLIPBOARD
10008 /* Remove a modeless selection when deleting lines halfway the screen or
10009 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010010 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010011 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 else
10013 clip_scroll_selection(line_count);
10014#endif
10015
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016#ifdef FEAT_GUI
10017 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10018 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010019 gui_dont_update_cursor(gui.cursor_row >= row + off
10020 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021#endif
10022
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010023 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10024 cursor_col = wp->w_wincol;
10025
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 if (*T_CCS != NUL) /* cursor relative to region */
10027 {
10028 cursor_row = row;
10029 cursor_end = end;
10030 }
10031 else
10032 {
10033 cursor_row = row + off;
10034 cursor_end = end + off;
10035 }
10036
10037 /*
10038 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10039 * Clear the inserted lines in ScreenLines[].
10040 */
10041 row += off;
10042 end += off;
10043 for (i = 0; i < line_count; ++i)
10044 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 if (wp != NULL && wp->w_width != Columns)
10046 {
10047 /* need to copy part of a line */
10048 j = row + i;
10049 while ((j += line_count) <= end - 1)
10050 linecopy(j - line_count, j, wp);
10051 j -= line_count;
10052 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010053 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10054 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 else
10056 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10057 LineWraps[j] = FALSE;
10058 }
10059 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 {
10061 /* whole width, moving the line pointers is faster */
10062 j = row + i;
10063 temp = LineOffset[j];
10064 while ((j += line_count) <= end - 1)
10065 {
10066 LineOffset[j - line_count] = LineOffset[j];
10067 LineWraps[j - line_count] = LineWraps[j];
10068 }
10069 LineOffset[j - line_count] = temp;
10070 LineWraps[j - line_count] = FALSE;
10071 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010072 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 else
10074 lineinvalid(temp, (int)Columns);
10075 }
10076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010078 if (screen_attr != clear_attr)
10079 screen_stop_highlight();
10080 if (clear_attr != 0)
10081 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 /* redraw the characters */
10084 if (type == USE_REDRAW)
10085 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010086 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010088 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 out_str(T_CD);
10090 screen_start(); /* don't know where cursor is now */
10091 }
10092 else if (type == USE_T_CDL)
10093 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010094 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 term_delete_lines(line_count);
10096 screen_start(); /* don't know where cursor is now */
10097 }
10098 /*
10099 * Deleting lines at top of the screen or scroll region: Just scroll
10100 * the whole screen (scroll region) up by outputting newlines on the
10101 * last line.
10102 */
10103 else if (type == USE_NL)
10104 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010105 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 for (i = line_count; --i >= 0; )
10107 out_char('\n'); /* cursor will remain on same line */
10108 }
10109 else
10110 {
10111 for (i = line_count; --i >= 0; )
10112 {
10113 if (type == USE_T_DL)
10114 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010115 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 out_str(T_DL); /* delete a line */
10117 }
10118 else /* type == USE_T_CE */
10119 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010120 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 out_str(T_CE); /* erase a line */
10122 }
10123 screen_start(); /* don't know where cursor is now */
10124 }
10125 }
10126
10127 /*
10128 * If the 'db' flag is set, we need to clear the lines that have been
10129 * scrolled up at the bottom of the region.
10130 */
10131 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10132 {
10133 for (i = line_count; i > 0; --i)
10134 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010135 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 out_str(T_CE); /* erase a line */
10137 screen_start(); /* don't know where cursor is now */
10138 }
10139 }
10140
10141#ifdef FEAT_GUI
10142 gui_can_update_cursor();
10143 if (gui.in_use)
10144 out_flush(); /* always flush after a scroll */
10145#endif
10146
10147 return OK;
10148}
10149
10150/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010151 * Return TRUE when postponing displaying the mode message: when not redrawing
10152 * or inside a mapping.
10153 */
10154 int
10155skip_showmode()
10156{
10157 // Call char_avail() only when we are going to show something, because it
10158 // takes a bit of time. redrawing() may also call char_avail_avail().
10159 if (global_busy
10160 || msg_silent != 0
10161 || !redrawing()
10162 || (char_avail() && !KeyTyped))
10163 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010164 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010165 return TRUE;
10166 }
10167 return FALSE;
10168}
10169
10170/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010171 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 *
10173 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10174 * If clear_cmdline is FALSE there may be a message there that needs to be
10175 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010176 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 * Return the length of the message (0 if no message).
10178 */
10179 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010180showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181{
10182 int need_clear;
10183 int length = 0;
10184 int do_mode;
10185 int attr;
10186 int nwr_save;
10187#ifdef FEAT_INS_EXPAND
10188 int sub_attr;
10189#endif
10190
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010191 do_mode = ((p_smd && msg_silent == 0)
10192 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010193 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010194 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010195 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010197 if (skip_showmode())
10198 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199
10200 nwr_save = need_wait_return;
10201
10202 /* wait a bit before overwriting an important message */
10203 check_for_delay(FALSE);
10204
10205 /* if the cmdline is more than one line high, erase top lines */
10206 need_clear = clear_cmdline;
10207 if (clear_cmdline && cmdline_row < Rows - 1)
10208 msg_clr_cmdline(); /* will reset clear_cmdline */
10209
10210 /* Position on the last line in the window, column 0 */
10211 msg_pos_mode();
10212 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010213 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 if (do_mode)
10215 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010216 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010218 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010219# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010220 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010221# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010222 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010223# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010224 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010225# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010226 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010228 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229# endif
10230#endif
10231#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10232 if (gui.in_use)
10233 {
10234 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010235 {
10236 /* HANGUL */
10237 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010238 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010239 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010240 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 }
10243#endif
10244#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010245 /* CTRL-X in Insert mode */
10246 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 {
10248 /* These messages can get long, avoid a wrap in a narrow
10249 * window. Prefer showing edit_submode_extra. */
10250 length = (Rows - msg_row) * Columns - 3;
10251 if (edit_submode_extra != NULL)
10252 length -= vim_strsize(edit_submode_extra);
10253 if (length > 0)
10254 {
10255 if (edit_submode_pre != NULL)
10256 length -= vim_strsize(edit_submode_pre);
10257 if (length - vim_strsize(edit_submode) > 0)
10258 {
10259 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010260 msg_puts_attr((char *)edit_submode_pre, attr);
10261 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 }
10263 if (edit_submode_extra != NULL)
10264 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010265 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010267 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 else
10269 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010270 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 }
10272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 }
10274 else
10275#endif
10276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010278 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010279 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010280 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 else if (State & INSERT)
10282 {
10283#ifdef FEAT_RIGHTLEFT
10284 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010285 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010287 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010289 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010290 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010292 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010294 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295#ifdef FEAT_RIGHTLEFT
10296 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010297 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298#endif
10299#ifdef FEAT_KEYMAP
10300 if (State & LANGMAP)
10301 {
10302# ifdef FEAT_ARABIC
10303 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010304 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 else
10306# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010307 if (get_keymap_str(curwin, (char_u *)" (%s)",
10308 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010309 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 }
10311#endif
10312 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010313 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 if (VIsual_active)
10316 {
10317 char *p;
10318
10319 /* Don't concatenate separate words to avoid translation
10320 * problems. */
10321 switch ((VIsual_select ? 4 : 0)
10322 + (VIsual_mode == Ctrl_V) * 2
10323 + (VIsual_mode == 'V'))
10324 {
10325 case 0: p = N_(" VISUAL"); break;
10326 case 1: p = N_(" VISUAL LINE"); break;
10327 case 2: p = N_(" VISUAL BLOCK"); break;
10328 case 4: p = N_(" SELECT"); break;
10329 case 5: p = N_(" SELECT LINE"); break;
10330 default: p = N_(" SELECT BLOCK"); break;
10331 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010332 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010334 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010336
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 need_clear = TRUE;
10338 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010339 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340#ifdef FEAT_INS_EXPAND
10341 && edit_submode == NULL /* otherwise it gets too long */
10342#endif
10343 )
10344 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010345 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 need_clear = TRUE;
10347 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010348
10349 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010350 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 msg_clr_eos();
10352 msg_didout = FALSE; /* overwrite this message */
10353 length = msg_col;
10354 msg_col = 0;
10355 need_wait_return = nwr_save; /* never ask for hit-return for this */
10356 }
10357 else if (clear_cmdline && msg_silent == 0)
10358 /* Clear the whole command line. Will reset "clear_cmdline". */
10359 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010360 else if (redraw_mode)
10361 {
10362 msg_pos_mode();
10363 msg_clr_eos();
10364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365
10366#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 /* In Visual mode the size of the selected area must be redrawn. */
10368 if (VIsual_active)
10369 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370
10371 /* If the last window has no status line, the ruler is after the mode
10372 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010373 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010374 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375#endif
10376 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010377 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 clear_cmdline = FALSE;
10379
10380 return length;
10381}
10382
10383/*
10384 * Position for a mode message.
10385 */
10386 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010387msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388{
10389 msg_col = 0;
10390 msg_row = Rows - 1;
10391}
10392
10393/*
10394 * Delete mode message. Used when ESC is typed which is expected to end
10395 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010396 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 */
10398 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010399unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400{
10401 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010402 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 */
10404 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10405 redraw_cmdline = TRUE; /* delete mode later */
10406 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010407 clearmode();
10408}
10409
10410/*
10411 * Clear the mode message.
10412 */
10413 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010414clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010415{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010416 int save_msg_row = msg_row;
10417 int save_msg_col = msg_col;
10418
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010419 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010420 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010421 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010422 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010423
10424 msg_col = save_msg_col;
10425 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426}
10427
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010428 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010429recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010430{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010431 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010432 if (!shortmess(SHM_RECORDING))
10433 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010434 char s[4];
10435
10436 sprintf(s, " @%c", reg_recording);
10437 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010438 }
10439}
10440
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010441/*
10442 * Draw the tab pages line at the top of the Vim window.
10443 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010444 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010445draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010446{
10447 int tabcount = 0;
10448 tabpage_T *tp;
10449 int tabwidth;
10450 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010451 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010452 int attr;
10453 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010454 win_T *cwp;
10455 int wincount;
10456 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010457 int c;
10458 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010459 int attr_sel = HL_ATTR(HLF_TPS);
10460 int attr_nosel = HL_ATTR(HLF_TP);
10461 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010462 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010463 int room;
10464 int use_sep_chars = (t_colors < 8
10465#ifdef FEAT_GUI
10466 && !gui.in_use
10467#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010468#ifdef FEAT_TERMGUICOLORS
10469 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010470#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010471 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010472
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010473 if (ScreenLines == NULL)
10474 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010475 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010476
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010477#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010478 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010479 if (gui_use_tabline())
10480 {
10481 gui_update_tabline();
10482 return;
10483 }
10484#endif
10485
10486 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010487 return;
10488
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010489#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010490 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010491
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010492 /* Use the 'tabline' option if it's set. */
10493 if (*p_tal != NUL)
10494 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010495 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010496
10497 /* Check for an error. If there is one we would loop in redrawing the
10498 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010499 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010500 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010501 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010502 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010503 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010504 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010505 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010506 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010507#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010508 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010509 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010510 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010511
Bram Moolenaar238a5642006-02-21 22:12:05 +000010512 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10513 if (tabwidth < 6)
10514 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010515
Bram Moolenaar238a5642006-02-21 22:12:05 +000010516 attr = attr_nosel;
10517 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010518 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10519 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010520 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010521 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010522
Bram Moolenaar238a5642006-02-21 22:12:05 +000010523 if (tp->tp_topframe == topframe)
10524 attr = attr_sel;
10525 if (use_sep_chars && col > 0)
10526 screen_putchar('|', 0, col++, attr);
10527
10528 if (tp->tp_topframe != topframe)
10529 attr = attr_nosel;
10530
10531 screen_putchar(' ', 0, col++, attr);
10532
10533 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010534 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010535 cwp = curwin;
10536 wp = firstwin;
10537 }
10538 else
10539 {
10540 cwp = tp->tp_curwin;
10541 wp = tp->tp_firstwin;
10542 }
10543
10544 modified = FALSE;
10545 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10546 if (bufIsChanged(wp->w_buffer))
10547 modified = TRUE;
10548 if (modified || wincount > 1)
10549 {
10550 if (wincount > 1)
10551 {
10552 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010553 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010554 if (col + len >= Columns - 3)
10555 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010556 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010557#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010558 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010559#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010560 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010561#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010562 );
10563 col += len;
10564 }
10565 if (modified)
10566 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10567 screen_putchar(' ', 0, col++, attr);
10568 }
10569
10570 room = scol - col + tabwidth - 1;
10571 if (room > 0)
10572 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010573 /* Get buffer name in NameBuff[] */
10574 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010575 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010576 len = vim_strsize(NameBuff);
10577 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010578 if (has_mbyte)
10579 while (len > room)
10580 {
10581 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010582 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010583 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010584 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010585 {
10586 p += len - room;
10587 len = room;
10588 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010589 if (len > Columns - col - 1)
10590 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010591
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010592 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010593 col += len;
10594 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010595 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010596
10597 /* Store the tab page number in TabPageIdxs[], so that
10598 * jump_to_mouse() knows where each one is. */
10599 ++tabcount;
10600 while (scol < col)
10601 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010602 }
10603
Bram Moolenaar238a5642006-02-21 22:12:05 +000010604 if (use_sep_chars)
10605 c = '_';
10606 else
10607 c = ' ';
10608 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010609
10610 /* Put an "X" for closing the current tab if there are several. */
10611 if (first_tabpage->tp_next != NULL)
10612 {
10613 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10614 TabPageIdxs[Columns - 1] = -999;
10615 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010616 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010617
10618 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10619 * set. */
10620 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010621}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010622
10623/*
10624 * Get buffer name for "buf" into NameBuff[].
10625 * Takes care of special buffer names and translates special characters.
10626 */
10627 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010628get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010629{
10630 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010631 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010632 else
10633 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10634 trans_characters(NameBuff, MAXPATHL);
10635}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010636
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637/*
10638 * Get the character to use in a status line. Get its attributes in "*attr".
10639 */
10640 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010641fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642{
10643 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010644
10645#ifdef FEAT_TERMINAL
10646 if (bt_terminal(wp->w_buffer))
10647 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010648 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010649 {
10650 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010651 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010652 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010653 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010654 {
10655 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010656 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010657 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010658 }
10659 else
10660#endif
10661 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010663 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 fill = fill_stl;
10665 }
10666 else
10667 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010668 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 fill = fill_stlnc;
10670 }
10671 /* Use fill when there is highlighting, and highlighting of current
10672 * window differs, or the fillchars differ, or this is not the
10673 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010674 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010675 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676 || (fill_stl != fill_stlnc)))
10677 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010678 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679 return '^';
10680 return '=';
10681}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683/*
10684 * Get the character to use in a separator between vertically split windows.
10685 * Get its attributes in "*attr".
10686 */
10687 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010688fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010690 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 if (*attr == 0 && fill_vert == ' ')
10692 return '|';
10693 else
10694 return fill_vert;
10695}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696
10697/*
10698 * Return TRUE if redrawing should currently be done.
10699 */
10700 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010701redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010703#ifdef FEAT_EVAL
10704 if (disable_redraw_for_testing)
10705 return 0;
10706 else
10707#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010708 return ((!RedrawingDisabled
10709#ifdef FEAT_EVAL
10710 || ignore_redraw_flag_for_testing
10711#endif
10712 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713}
10714
10715/*
10716 * Return TRUE if printing messages should currently be done.
10717 */
10718 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010719messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720{
10721 return (!(p_lz && char_avail() && !KeyTyped));
10722}
10723
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010724#ifdef FEAT_MENU
10725/*
10726 * Draw the window toolbar.
10727 */
10728 static void
10729redraw_win_toolbar(win_T *wp)
10730{
10731 vimmenu_T *menu;
10732 int item_idx = 0;
10733 int item_count = 0;
10734 int col = 0;
10735 int next_col;
10736 int off = (int)(current_ScreenLine - ScreenLines);
10737 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10738 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10739
10740 vim_free(wp->w_winbar_items);
10741 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10742 ++item_count;
10743 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10744 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10745
10746 /* TODO: use fewer spaces if there is not enough room */
10747 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010748 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010749 {
10750 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010751 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010752 break;
10753 if (col > 1)
10754 {
10755 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010756 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010757 break;
10758 }
10759
10760 wp->w_winbar_items[item_idx].wb_startcol = col;
10761 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010762 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010763 break;
10764
10765 next_col = text_to_screenline(wp, menu->name, col);
10766 while (col < next_col)
10767 {
10768 ScreenAttrs[off + col] = button_attr;
10769 ++col;
10770 }
10771 wp->w_winbar_items[item_idx].wb_endcol = col;
10772 wp->w_winbar_items[item_idx].wb_menu = menu;
10773 ++item_idx;
10774
Bram Moolenaar02631462017-09-22 15:20:32 +020010775 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010776 break;
10777 space_to_screenline(off + col, button_attr);
10778 ++col;
10779 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010780 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010781 {
10782 space_to_screenline(off + col, fill_attr);
10783 ++col;
10784 }
10785 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10786
Bram Moolenaar02631462017-09-22 15:20:32 +020010787 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10788 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010789}
10790#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010791
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792/*
10793 * Show current status info in ruler and various other places
10794 * If always is FALSE, only show ruler if position has changed.
10795 */
10796 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010797showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798{
10799 if (!always && !redrawing())
10800 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010801#ifdef FEAT_INS_EXPAND
10802 if (pum_visible())
10803 {
10804 /* Don't redraw right now, do it later. */
10805 curwin->w_redr_status = TRUE;
10806 return;
10807 }
10808#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010809#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010810 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010811 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 else
10813#endif
10814#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010815 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816#endif
10817
10818#ifdef FEAT_TITLE
10819 if (need_maketitle
10820# ifdef FEAT_STL_OPT
10821 || (p_icon && (stl_syntax & STL_IN_ICON))
10822 || (p_title && (stl_syntax & STL_IN_TITLE))
10823# endif
10824 )
10825 maketitle();
10826#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010827 /* Redraw the tab pages line if needed. */
10828 if (redraw_tabline)
10829 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830}
10831
10832#ifdef FEAT_CMDL_INFO
10833 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010834win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010836#define RULER_BUF_LEN 70
10837 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 int row;
10839 int fillchar;
10840 int attr;
10841 int empty_line = FALSE;
10842 colnr_T virtcol;
10843 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010844 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 int this_ru_col;
10847 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010848 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849
10850 /* If 'ruler' off or redrawing disabled, don't do anything */
10851 if (!p_ru)
10852 return;
10853
10854 /*
10855 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10856 * after deleting lines, before cursor.lnum is corrected.
10857 */
10858 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10859 return;
10860
10861#ifdef FEAT_INS_EXPAND
10862 /* Don't draw the ruler while doing insert-completion, it might overwrite
10863 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 if (edit_submode != NULL)
10866 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010867 // Don't draw the ruler when the popup menu is visible, it may overlap.
10868 // Except when the popup menu will be redrawn anyway.
10869 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010870 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871#endif
10872
10873#ifdef FEAT_STL_OPT
10874 if (*p_ruf)
10875 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010876 int save_called_emsg = called_emsg;
10877
10878 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010880 if (called_emsg)
10881 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010882 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010883 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 return;
10885 }
10886#endif
10887
10888 /*
10889 * Check if not in Insert mode and the line is empty (will show "0-1").
10890 */
10891 if (!(State & INSERT)
10892 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10893 empty_line = TRUE;
10894
10895 /*
10896 * Only draw the ruler when something changed.
10897 */
10898 validate_virtcol_win(wp);
10899 if ( redraw_cmdline
10900 || always
10901 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10902 || wp->w_cursor.col != wp->w_ru_cursor.col
10903 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 || wp->w_topline != wp->w_ru_topline
10906 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10907#ifdef FEAT_DIFF
10908 || wp->w_topfill != wp->w_ru_topfill
10909#endif
10910 || empty_line != wp->w_ru_empty)
10911 {
10912 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 if (wp->w_status_height)
10914 {
10915 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010916 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010917 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010918 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 }
10920 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 {
10922 row = Rows - 1;
10923 fillchar = ' ';
10924 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 width = Columns;
10926 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 }
10928
10929 /* In list mode virtcol needs to be recomputed */
10930 virtcol = wp->w_virtcol;
10931 if (wp->w_p_list && lcs_tab1 == NUL)
10932 {
10933 wp->w_p_list = FALSE;
10934 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10935 wp->w_p_list = TRUE;
10936 }
10937
10938 /*
10939 * Some sprintfs return the length, some return a pointer.
10940 * To avoid portability problems we use strlen() here.
10941 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010942 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10944 ? 0L
10945 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010946 len = STRLEN(buffer);
10947 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10949 (int)virtcol + 1);
10950
10951 /*
10952 * Add a "50%" if there is room for it.
10953 * On the last line, don't print in the last column (scrolls the
10954 * screen up on some terminals).
10955 */
10956 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010957 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 this_ru_col = ru_col - (Columns - width);
10962 if (this_ru_col < 0)
10963 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 /* Never use more than half the window/screen width, leave the other
10965 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010966 if (this_ru_col < (width + 1) / 2)
10967 this_ru_col = (width + 1) / 2;
10968 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010970 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010971 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 if (has_mbyte)
10974 i += (*mb_char2bytes)(fillchar, buffer + i);
10975 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 buffer[i++] = fillchar;
10977 ++o;
10978 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010979 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 }
10981 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 if (has_mbyte)
10983 {
10984 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010985 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 {
10987 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010988 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 {
10990 buffer[i] = NUL;
10991 break;
10992 }
10993 }
10994 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010995 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020010996 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997
Bram Moolenaar4033c552017-09-16 20:54:51 +020010998 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 i = redraw_cmdline;
11000 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011001 this_ru_col + off + (int)STRLEN(buffer),
11002 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 fillchar, fillchar, attr);
11004 /* don't redraw the cmdline because of showing the ruler */
11005 redraw_cmdline = i;
11006 wp->w_ru_cursor = wp->w_cursor;
11007 wp->w_ru_virtcol = wp->w_virtcol;
11008 wp->w_ru_empty = empty_line;
11009 wp->w_ru_topline = wp->w_topline;
11010 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11011#ifdef FEAT_DIFF
11012 wp->w_ru_topfill = wp->w_topfill;
11013#endif
11014 }
11015}
11016#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011017
11018#if defined(FEAT_LINEBREAK) || defined(PROTO)
11019/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011020 * Return the width of the 'number' and 'relativenumber' column.
11021 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011022 * Otherwise it depends on 'numberwidth' and the line count.
11023 */
11024 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011025number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011026{
11027 int n;
11028 linenr_T lnum;
11029
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011030 if (wp->w_p_rnu && !wp->w_p_nu)
11031 /* cursor line shows "0" */
11032 lnum = wp->w_height;
11033 else
11034 /* cursor line shows absolute line number */
11035 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011036
Bram Moolenaar6b314672015-03-20 15:42:10 +010011037 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011038 return wp->w_nrwidth_width;
11039 wp->w_nrwidth_line_count = lnum;
11040
11041 n = 0;
11042 do
11043 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011044 lnum /= 10;
11045 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011046 } while (lnum > 0);
11047
11048 /* 'numberwidth' gives the minimal width plus one */
11049 if (n < wp->w_p_nuw - 1)
11050 n = wp->w_p_nuw - 1;
11051
11052 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011053 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011054 return n;
11055}
11056#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011057
Bram Moolenaar113e1072019-01-20 15:30:40 +010011058#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011059/*
11060 * Return the current cursor column. This is the actual position on the
11061 * screen. First column is 0.
11062 */
11063 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011064screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011065{
11066 return screen_cur_col;
11067}
11068
11069/*
11070 * Return the current cursor row. This is the actual position on the screen.
11071 * First row is 0.
11072 */
11073 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011074screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011075{
11076 return screen_cur_row;
11077}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011078#endif