blob: e56d2bc018b58468d5cdd549d78fd6fd1840acb2 [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 Moolenaar071d4272004-06-13 20:20:40 +0000567 if (must_redraw)
568 {
569 if (type < must_redraw) /* use maximal type */
570 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000571
572 /* must_redraw is reset here, so that when we run into some weird
573 * reason to redraw while busy redrawing (e.g., asynchronous
574 * scrolling), or update_topline() in win_update() will cause a
575 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 must_redraw = 0;
577 }
578
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200579 /* May need to update w_lines[]. */
580 if (curwin->w_lines_valid == 0 && type < NOT_VALID
581#ifdef FEAT_TERMINAL
582 && !term_do_update_window(curwin)
583#endif
584 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 type = NOT_VALID;
586
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000587 /* Postpone the redrawing when it's not needed and when being called
588 * recursively. */
589 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 {
591 redraw_later(type); /* remember type for next time */
592 must_redraw = type;
593 if (type > INVERTED_ALL)
594 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200595 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 }
597
598 updating_screen = TRUE;
599#ifdef FEAT_SYN_HL
600 ++display_tick; /* let syntax code know we're in a next round of
601 * display updating */
602#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200603 if (no_update)
604 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
606 /*
607 * if the screen was scrolled up when displaying a message, scroll it down
608 */
609 if (msg_scrolled)
610 {
611 clear_cmdline = TRUE;
612 if (msg_scrolled > Rows - 5) /* clearing is faster */
613 type = CLEAR;
614 else if (type != CLEAR)
615 {
616 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200617 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
618 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 type = CLEAR;
620 FOR_ALL_WINDOWS(wp)
621 {
622 if (W_WINROW(wp) < msg_scrolled)
623 {
624 if (W_WINROW(wp) + wp->w_height > msg_scrolled
625 && wp->w_redr_type < REDRAW_TOP
626 && wp->w_lines_valid > 0
627 && wp->w_topline == wp->w_lines[0].wl_lnum)
628 {
629 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
630 wp->w_redr_type = REDRAW_TOP;
631 }
632 else
633 {
634 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200635 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
636 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 }
639 }
640 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200641 if (!no_update)
642 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000643 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 }
645 msg_scrolled = 0;
646 need_wait_return = FALSE;
647 }
648
649 /* reset cmdline_row now (may have been changed temporarily) */
650 compute_cmdrow();
651
652 /* Check for changed highlighting */
653 if (need_highlight_changed)
654 highlight_changed();
655
656 if (type == CLEAR) /* first clear screen */
657 {
658 screenclear(); /* will reset clear_cmdline */
659 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200660 /* must_redraw may be set indirectly, avoid another redraw later */
661 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 }
663
664 if (clear_cmdline) /* going to clear cmdline (done below) */
665 check_for_delay(FALSE);
666
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000667#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200668 /* Force redraw when width of 'number' or 'relativenumber' column
669 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000670 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200671 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
672 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000673 curwin->w_redr_type = NOT_VALID;
674#endif
675
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 /*
677 * Only start redrawing if there is really something to do.
678 */
679 if (type == INVERTED)
680 update_curswant();
681 if (curwin->w_redr_type < type
682 && !((type == VALID
683 && curwin->w_lines[0].wl_valid
684#ifdef FEAT_DIFF
685 && curwin->w_topfill == curwin->w_old_topfill
686 && curwin->w_botfill == curwin->w_old_botfill
687#endif
688 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000690 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
692 && curwin->w_old_visual_mode == VIsual_mode
693 && (curwin->w_valid & VALID_VIRTCOL)
694 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 ))
696 curwin->w_redr_type = type;
697
Bram Moolenaar5a305422006-04-28 22:38:25 +0000698 /* Redraw the tab pages line if needed. */
699 if (redraw_tabline || type >= NOT_VALID)
700 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702#ifdef FEAT_SYN_HL
703 /*
704 * Correct stored syntax highlighting info for changes in each displayed
705 * buffer. Each buffer must only be done once.
706 */
707 FOR_ALL_WINDOWS(wp)
708 {
709 if (wp->w_buffer->b_mod_set)
710 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 win_T *wwp;
712
713 /* Check if we already did this buffer. */
714 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
715 if (wwp->w_buffer == wp->w_buffer)
716 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200717 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 syn_stack_apply_changes(wp->w_buffer);
719 }
720 }
721#endif
722
723 /*
724 * Go from top to bottom through the windows, redrawing the ones that need
725 * it.
726 */
727#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
728 did_one = FALSE;
729#endif
730#ifdef FEAT_SEARCH_EXTRA
731 search_hl.rm.regprog = NULL;
732#endif
733 FOR_ALL_WINDOWS(wp)
734 {
735 if (wp->w_redr_type != 0)
736 {
737 cursor_off();
738#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
739 if (!did_one)
740 {
741 did_one = TRUE;
742# ifdef FEAT_SEARCH_EXTRA
743 start_search_hl();
744# endif
745# ifdef FEAT_CLIPBOARD
746 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200747 if (clip_star.available && clip_isautosel_star())
748 clip_update_selection(&clip_star);
749 if (clip_plus.available && clip_isautosel_plus())
750 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751# endif
752#ifdef FEAT_GUI
753 /* Remove the cursor before starting to do anything, because
754 * scrolling may make it difficult to redraw the text under
755 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200756 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200757 {
758 gui_cursor_col = gui.cursor_col;
759 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200761 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763#endif
764 }
765#endif
766 win_update(wp);
767 }
768
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 /* redraw status line after the window to minimize cursor movement */
770 if (wp->w_redr_status)
771 {
772 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200773 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 }
776#if defined(FEAT_SEARCH_EXTRA)
777 end_search_hl();
778#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100779#ifdef FEAT_INS_EXPAND
780 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200781 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 /* Reset b_mod_set flags. Going through all windows is probably faster
785 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200786 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200789 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
791 /* Clear or redraw the command line. Done last, because scrolling may
792 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200793 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 showmode();
795
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200796 if (no_update)
797 --no_win_do_lines_ins;
798
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200800 if (!did_intro)
801 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 did_intro = TRUE;
803
804#ifdef FEAT_GUI
805 /* Redraw the cursor and update the scrollbars when all screen updating is
806 * done. */
807 if (gui.in_use)
808 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200809 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200810 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100811 mch_disable_flush();
812 out_flush(); /* required before updating the cursor */
813 mch_enable_flush();
814
Bram Moolenaar144445d2016-07-08 21:41:54 +0200815 /* Put the GUI position where the cursor was, gui_update_cursor()
816 * uses that. */
817 gui.col = gui_cursor_col;
818 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200819 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100821 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200822 screen_cur_col = gui.col;
823 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200824 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100825 else
826 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 gui_update_scrollbars(FALSE);
828 }
829#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200830 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831}
832
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100833#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100834/*
835 * Prepare for updating one or more windows.
836 * Caller must check for "updating_screen" already set to avoid recursiveness.
837 */
838 static void
839update_prepare(void)
840{
841 cursor_off();
842 updating_screen = TRUE;
843#ifdef FEAT_GUI
844 /* Remove the cursor before starting to do anything, because scrolling may
845 * make it difficult to redraw the text under it. */
846 if (gui.in_use)
847 gui_undraw_cursor();
848#endif
849#ifdef FEAT_SEARCH_EXTRA
850 start_search_hl();
851#endif
852}
853
854/*
855 * Finish updating one or more windows.
856 */
857 static void
858update_finish(void)
859{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200860 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100861 showmode();
862
863# ifdef FEAT_SEARCH_EXTRA
864 end_search_hl();
865# endif
866
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200867 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100868
869# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100870 /* Redraw the cursor and update the scrollbars when all screen updating is
871 * done. */
872 if (gui.in_use)
873 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100874 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100875 gui_update_scrollbars(FALSE);
876 }
877# endif
878}
879#endif
880
Bram Moolenaar860cae12010-06-05 23:22:07 +0200881#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200882/*
883 * Return TRUE if the cursor line in window "wp" may be concealed, according
884 * to the 'concealcursor' option.
885 */
886 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100887conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200888{
889 int c;
890
891 if (*wp->w_p_cocu == NUL)
892 return FALSE;
893 if (get_real_state() & VISUAL)
894 c = 'v';
895 else if (State & INSERT)
896 c = 'i';
897 else if (State & NORMAL)
898 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200899 else if (State & CMDLINE)
900 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200901 else
902 return FALSE;
903 return vim_strchr(wp->w_p_cocu, c) != NULL;
904}
905
906/*
907 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
908 */
909 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200910conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200911{
912 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
913 {
914 need_cursor_line_redraw = TRUE;
915 /* Need to recompute cursor column, e.g., when starting Visual mode
916 * without concealing. */
917 curs_columns(TRUE);
918 }
919}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920#endif
921
Bram Moolenaar113e1072019-01-20 15:30:40 +0100922#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100924update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925{
926 win_T *wp;
927 int doit = FALSE;
928
929# ifdef FEAT_FOLDING
930 win_foldinfo.fi_level = 0;
931# endif
932
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100933 // update/delete a specific sign
934 redraw_buf_line_later(buf, lnum);
935
936 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 if (wp->w_redr_type != 0)
939 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100941 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200942 * happening (recursive call), messages on the screen or still starting up.
943 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100944 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200945 || State == ASKMORE || State == HITRETURN
946 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100947#ifdef FEAT_GUI
948 || gui.starting
949#endif
950 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 return;
952
953 /* update all windows that need updating */
954 update_prepare();
955
Bram Moolenaar29323592016-07-24 22:04:11 +0200956 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 {
958 if (wp->w_redr_type != 0)
959 win_update(wp);
960 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200961 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963
964 update_finish();
965}
966#endif
967
968
969#if defined(FEAT_GUI) || defined(PROTO)
970/*
971 * Update a single window, its status line and maybe the command line msg.
972 * Used for the GUI scrollbar.
973 */
974 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100975updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000977 /* return if already busy updating */
978 if (updating_screen)
979 return;
980
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 update_prepare();
982
983#ifdef FEAT_CLIPBOARD
984 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200985 if (clip_star.available && clip_isautosel_star())
986 clip_update_selection(&clip_star);
987 if (clip_plus.available && clip_isautosel_plus())
988 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000992
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000993 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000994 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000995 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000996
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 if (wp->w_redr_status
998# ifdef FEAT_CMDL_INFO
999 || p_ru
1000# endif
1001# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001002 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003# endif
1004 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001005 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007 update_finish();
1008}
1009#endif
1010
1011/*
1012 * Update a single window.
1013 *
1014 * This may cause the windows below it also to be redrawn (when clearing the
1015 * screen or scrolling lines).
1016 *
1017 * How the window is redrawn depends on wp->w_redr_type. Each type also
1018 * implies the one below it.
1019 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001020 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1022 * INVERTED redraw the changed part of the Visual area
1023 * INVERTED_ALL redraw the whole Visual area
1024 * VALID 1. scroll up/down to adjust for a changed w_topline
1025 * 2. update lines at the top when scrolled down
1026 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001027 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 * b_mod_top and b_mod_bot.
1029 * - if wp->w_redraw_top non-zero, redraw lines between
1030 * wp->w_redraw_top and wp->w_redr_bot.
1031 * - continue redrawing when syntax status is invalid.
1032 * 4. if scrolled up, update lines at the bottom.
1033 * This results in three areas that may need updating:
1034 * top: from first row to top_end (when scrolled down)
1035 * mid: from mid_start to mid_end (update inversion or changed text)
1036 * bot: from bot_start to last row (when scrolled up)
1037 */
1038 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001039win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
1041 buf_T *buf = wp->w_buffer;
1042 int type;
1043 int top_end = 0; /* Below last row of the top area that needs
1044 updating. 0 when no top area updating. */
1045 int mid_start = 999;/* first row of the mid area that needs
1046 updating. 999 when no mid area updating. */
1047 int mid_end = 0; /* Below last row of the mid area that needs
1048 updating. 0 when no mid area updating. */
1049 int bot_start = 999;/* first row of the bot area that needs
1050 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 int scrolled_down = FALSE; /* TRUE when scrolled down when
1052 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001054 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 int top_to_mod = FALSE; /* redraw above mod_top */
1056#endif
1057
1058 int row; /* current window row to display */
1059 linenr_T lnum; /* current buffer lnum to display */
1060 int idx; /* current index in w_lines[] */
1061 int srow; /* starting row of the current line */
1062
1063 int eof = FALSE; /* if TRUE, we hit the end of the file */
1064 int didline = FALSE; /* if TRUE, we finished the last line */
1065 int i;
1066 long j;
1067 static int recursive = FALSE; /* being called recursively */
1068 int old_botline = wp->w_botline;
1069#ifdef FEAT_FOLDING
1070 long fold_count;
1071#endif
1072#ifdef FEAT_SYN_HL
1073 /* remember what happened to the previous line, to know if
1074 * check_visual_highlight() can be used */
1075#define DID_NONE 1 /* didn't update a line */
1076#define DID_LINE 2 /* updated a normal line */
1077#define DID_FOLD 3 /* updated a folded line */
1078 int did_update = DID_NONE;
1079 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1080#endif
1081 linenr_T mod_top = 0;
1082 linenr_T mod_bot = 0;
1083#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1084 int save_got_int;
1085#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001086#ifdef SYN_TIME_LIMIT
1087 proftime_T syntax_tm;
1088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089
1090 type = wp->w_redr_type;
1091
1092 if (type == NOT_VALID)
1093 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 wp->w_lines_valid = 0;
1096 }
1097
1098 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001099 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 {
1101 wp->w_redr_type = 0;
1102 return;
1103 }
1104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 /* Window is zero-width: Only need to draw the separator. */
1106 if (wp->w_width == 0)
1107 {
1108 /* draw the vertical separator right of this window */
1109 draw_vsep_win(wp, 0);
1110 wp->w_redr_type = 0;
1111 return;
1112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001114#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001115 // If this window contains a terminal, redraw works completely differently.
1116 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001117 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001118 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001119# ifdef FEAT_MENU
1120 /* Draw the window toolbar, if there is one. */
1121 if (winbar_height(wp) > 0)
1122 redraw_win_toolbar(wp);
1123# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001124 wp->w_redr_type = 0;
1125 return;
1126 }
1127#endif
1128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001130 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131#endif
1132
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001133#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001134 /* Force redraw when width of 'number' or 'relativenumber' column
1135 * changes. */
1136 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001137 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001138 {
1139 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001140 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001141 }
1142 else
1143#endif
1144
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1146 {
1147 /*
1148 * When there are both inserted/deleted lines and specific lines to be
1149 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1150 * everything (only happens when redrawing is off for while).
1151 */
1152 type = NOT_VALID;
1153 }
1154 else
1155 {
1156 /*
1157 * Set mod_top to the first line that needs displaying because of
1158 * changes. Set mod_bot to the first line after the changes.
1159 */
1160 mod_top = wp->w_redraw_top;
1161 if (wp->w_redraw_bot != 0)
1162 mod_bot = wp->w_redraw_bot + 1;
1163 else
1164 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 if (buf->b_mod_set)
1166 {
1167 if (mod_top == 0 || mod_top > buf->b_mod_top)
1168 {
1169 mod_top = buf->b_mod_top;
1170#ifdef FEAT_SYN_HL
1171 /* Need to redraw lines above the change that may be included
1172 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001173 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001175 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 if (mod_top < 1)
1177 mod_top = 1;
1178 }
1179#endif
1180 }
1181 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1182 mod_bot = buf->b_mod_bot;
1183
1184#ifdef FEAT_SEARCH_EXTRA
1185 /* When 'hlsearch' is on and using a multi-line search pattern, a
1186 * change in one line may make the Search highlighting in a
1187 * previous line invalid. Simple solution: redraw all visible
1188 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001189 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001191 if (search_hl.rm.regprog != NULL
1192 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001194 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001195 {
1196 cur = wp->w_match_head;
1197 while (cur != NULL)
1198 {
1199 if (cur->match.regprog != NULL
1200 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001201 {
1202 top_to_mod = TRUE;
1203 break;
1204 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001205 cur = cur->next;
1206 }
1207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208#endif
1209 }
1210#ifdef FEAT_FOLDING
1211 if (mod_top != 0 && hasAnyFolding(wp))
1212 {
1213 linenr_T lnumt, lnumb;
1214
1215 /*
1216 * A change in a line can cause lines above it to become folded or
1217 * unfolded. Find the top most buffer line that may be affected.
1218 * If the line was previously folded and displayed, get the first
1219 * line of that fold. If the line is folded now, get the first
1220 * folded line. Use the minimum of these two.
1221 */
1222
1223 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1224 * the line below it. If there is no valid entry, use w_topline.
1225 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1226 * to this line. If there is no valid entry, use MAXLNUM. */
1227 lnumt = wp->w_topline;
1228 lnumb = MAXLNUM;
1229 for (i = 0; i < wp->w_lines_valid; ++i)
1230 if (wp->w_lines[i].wl_valid)
1231 {
1232 if (wp->w_lines[i].wl_lastlnum < mod_top)
1233 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1234 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1235 {
1236 lnumb = wp->w_lines[i].wl_lnum;
1237 /* When there is a fold column it might need updating
1238 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001239 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 ++lnumb;
1241 }
1242 }
1243
1244 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1245 if (mod_top > lnumt)
1246 mod_top = lnumt;
1247
1248 /* Now do the same for the bottom line (one above mod_bot). */
1249 --mod_bot;
1250 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1251 ++mod_bot;
1252 if (mod_bot < lnumb)
1253 mod_bot = lnumb;
1254 }
1255#endif
1256
1257 /* When a change starts above w_topline and the end is below
1258 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001259 * If the end of the change is above w_topline: do like no change was
1260 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 if (mod_top != 0 && mod_top < wp->w_topline)
1262 {
1263 if (mod_bot > wp->w_topline)
1264 mod_top = wp->w_topline;
1265#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001266 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 top_end = 1;
1268#endif
1269 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001270
1271 /* When line numbers are displayed need to redraw all lines below
1272 * inserted/deleted lines. */
1273 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1274 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001276 wp->w_redraw_top = 0; // reset for next time
1277 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278
1279 /*
1280 * When only displaying the lines at the top, set top_end. Used when
1281 * window has scrolled down for msg_scrolled.
1282 */
1283 if (type == REDRAW_TOP)
1284 {
1285 j = 0;
1286 for (i = 0; i < wp->w_lines_valid; ++i)
1287 {
1288 j += wp->w_lines[i].wl_size;
1289 if (j >= wp->w_upd_rows)
1290 {
1291 top_end = j;
1292 break;
1293 }
1294 }
1295 if (top_end == 0)
1296 /* not found (cannot happen?): redraw everything */
1297 type = NOT_VALID;
1298 else
1299 /* top area defined, the rest is VALID */
1300 type = VALID;
1301 }
1302
Bram Moolenaar367329b2007-08-30 11:53:22 +00001303 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001304 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1305 * non-zero and thus not FALSE) will indicate that screenclear() was not
1306 * called. */
1307 if (screen_cleared)
1308 screen_cleared = MAYBE;
1309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 /*
1311 * If there are no changes on the screen that require a complete redraw,
1312 * handle three cases:
1313 * 1: we are off the top of the screen by a few lines: scroll down
1314 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1315 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1316 * w_lines[] that needs updating.
1317 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001318 if ((type == VALID || type == SOME_VALID
1319 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320#ifdef FEAT_DIFF
1321 && !wp->w_botfill && !wp->w_old_botfill
1322#endif
1323 )
1324 {
1325 if (mod_top != 0 && wp->w_topline == mod_top)
1326 {
1327 /*
1328 * w_topline is the first changed line, the scrolling will be done
1329 * further down.
1330 */
1331 }
1332 else if (wp->w_lines[0].wl_valid
1333 && (wp->w_topline < wp->w_lines[0].wl_lnum
1334#ifdef FEAT_DIFF
1335 || (wp->w_topline == wp->w_lines[0].wl_lnum
1336 && wp->w_topfill > wp->w_old_topfill)
1337#endif
1338 ))
1339 {
1340 /*
1341 * New topline is above old topline: May scroll down.
1342 */
1343#ifdef FEAT_FOLDING
1344 if (hasAnyFolding(wp))
1345 {
1346 linenr_T ln;
1347
1348 /* count the number of lines we are off, counting a sequence
1349 * of folded lines as one */
1350 j = 0;
1351 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1352 {
1353 ++j;
1354 if (j >= wp->w_height - 2)
1355 break;
1356 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1357 }
1358 }
1359 else
1360#endif
1361 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1362 if (j < wp->w_height - 2) /* not too far off */
1363 {
1364 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1365#ifdef FEAT_DIFF
1366 /* insert extra lines for previously invisible filler lines */
1367 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1368 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1369 - wp->w_old_topfill;
1370#endif
1371 if (i < wp->w_height - 2) /* less than a screen off */
1372 {
1373 /*
1374 * Try to insert the correct number of lines.
1375 * If not the last window, delete the lines at the bottom.
1376 * win_ins_lines may fail when the terminal can't do it.
1377 */
1378 if (i > 0)
1379 check_for_delay(FALSE);
1380 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1381 {
1382 if (wp->w_lines_valid != 0)
1383 {
1384 /* Need to update rows that are new, stop at the
1385 * first one that scrolled down. */
1386 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388
1389 /* Move the entries that were scrolled, disable
1390 * the entries for the lines to be redrawn. */
1391 if ((wp->w_lines_valid += j) > wp->w_height)
1392 wp->w_lines_valid = wp->w_height;
1393 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1394 wp->w_lines[idx] = wp->w_lines[idx - j];
1395 while (idx >= 0)
1396 wp->w_lines[idx--].wl_valid = FALSE;
1397 }
1398 }
1399 else
1400 mid_start = 0; /* redraw all lines */
1401 }
1402 else
1403 mid_start = 0; /* redraw all lines */
1404 }
1405 else
1406 mid_start = 0; /* redraw all lines */
1407 }
1408 else
1409 {
1410 /*
1411 * New topline is at or below old topline: May scroll up.
1412 * When topline didn't change, find first entry in w_lines[] that
1413 * needs updating.
1414 */
1415
1416 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1417 j = -1;
1418 row = 0;
1419 for (i = 0; i < wp->w_lines_valid; i++)
1420 {
1421 if (wp->w_lines[i].wl_valid
1422 && wp->w_lines[i].wl_lnum == wp->w_topline)
1423 {
1424 j = i;
1425 break;
1426 }
1427 row += wp->w_lines[i].wl_size;
1428 }
1429 if (j == -1)
1430 {
1431 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1432 * lines */
1433 mid_start = 0;
1434 }
1435 else
1436 {
1437 /*
1438 * Try to delete the correct number of lines.
1439 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1440 */
1441#ifdef FEAT_DIFF
1442 /* If the topline didn't change, delete old filler lines,
1443 * otherwise delete filler lines of the new topline... */
1444 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1445 row += wp->w_old_topfill;
1446 else
1447 row += diff_check_fill(wp, wp->w_topline);
1448 /* ... but don't delete new filler lines. */
1449 row -= wp->w_topfill;
1450#endif
1451 if (row > 0)
1452 {
1453 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001454 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1455 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 bot_start = wp->w_height - row;
1457 else
1458 mid_start = 0; /* redraw all lines */
1459 }
1460 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1461 {
1462 /*
1463 * Skip the lines (below the deleted lines) that are still
1464 * valid and don't need redrawing. Copy their info
1465 * upwards, to compensate for the deleted lines. Set
1466 * bot_start to the first row that needs redrawing.
1467 */
1468 bot_start = 0;
1469 idx = 0;
1470 for (;;)
1471 {
1472 wp->w_lines[idx] = wp->w_lines[j];
1473 /* stop at line that didn't fit, unless it is still
1474 * valid (no lines deleted) */
1475 if (row > 0 && bot_start + row
1476 + (int)wp->w_lines[j].wl_size > wp->w_height)
1477 {
1478 wp->w_lines_valid = idx + 1;
1479 break;
1480 }
1481 bot_start += wp->w_lines[idx++].wl_size;
1482
1483 /* stop at the last valid entry in w_lines[].wl_size */
1484 if (++j >= wp->w_lines_valid)
1485 {
1486 wp->w_lines_valid = idx;
1487 break;
1488 }
1489 }
1490#ifdef FEAT_DIFF
1491 /* Correct the first entry for filler lines at the top
1492 * when it won't get updated below. */
1493 if (wp->w_p_diff && bot_start > 0)
1494 wp->w_lines[0].wl_size =
1495 plines_win_nofill(wp, wp->w_topline, TRUE)
1496 + wp->w_topfill;
1497#endif
1498 }
1499 }
1500 }
1501
1502 /* When starting redraw in the first line, redraw all lines. When
1503 * there is only one window it's probably faster to clear the screen
1504 * first. */
1505 if (mid_start == 0)
1506 {
1507 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001508 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001509 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001510 /* Clear the screen when it was not done by win_del_lines() or
1511 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1512 * then. */
1513 if (screen_cleared != TRUE)
1514 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001515 /* The screen was cleared, redraw the tab pages line. */
1516 if (redraw_tabline)
1517 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001520
1521 /* When win_del_lines() or win_ins_lines() caused the screen to be
1522 * cleared (only happens for the first window) or when screenclear()
1523 * was called directly above, "must_redraw" will have been set to
1524 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1525 if (screen_cleared == TRUE)
1526 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 }
1528 else
1529 {
1530 /* Not VALID or INVERTED: redraw all lines. */
1531 mid_start = 0;
1532 mid_end = wp->w_height;
1533 }
1534
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001535 if (type == SOME_VALID)
1536 {
1537 /* SOME_VALID: redraw all lines. */
1538 mid_start = 0;
1539 mid_end = wp->w_height;
1540 type = NOT_VALID;
1541 }
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 /* check if we are updating or removing the inverted part */
1544 if ((VIsual_active && buf == curwin->w_buffer)
1545 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1546 {
1547 linenr_T from, to;
1548
1549 if (VIsual_active)
1550 {
1551 if (VIsual_active
1552 && (VIsual_mode != wp->w_old_visual_mode
1553 || type == INVERTED_ALL))
1554 {
1555 /*
1556 * If the type of Visual selection changed, redraw the whole
1557 * selection. Also when the ownership of the X selection is
1558 * gained or lost.
1559 */
1560 if (curwin->w_cursor.lnum < VIsual.lnum)
1561 {
1562 from = curwin->w_cursor.lnum;
1563 to = VIsual.lnum;
1564 }
1565 else
1566 {
1567 from = VIsual.lnum;
1568 to = curwin->w_cursor.lnum;
1569 }
1570 /* redraw more when the cursor moved as well */
1571 if (wp->w_old_cursor_lnum < from)
1572 from = wp->w_old_cursor_lnum;
1573 if (wp->w_old_cursor_lnum > to)
1574 to = wp->w_old_cursor_lnum;
1575 if (wp->w_old_visual_lnum < from)
1576 from = wp->w_old_visual_lnum;
1577 if (wp->w_old_visual_lnum > to)
1578 to = wp->w_old_visual_lnum;
1579 }
1580 else
1581 {
1582 /*
1583 * Find the line numbers that need to be updated: The lines
1584 * between the old cursor position and the current cursor
1585 * position. Also check if the Visual position changed.
1586 */
1587 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1588 {
1589 from = curwin->w_cursor.lnum;
1590 to = wp->w_old_cursor_lnum;
1591 }
1592 else
1593 {
1594 from = wp->w_old_cursor_lnum;
1595 to = curwin->w_cursor.lnum;
1596 if (from == 0) /* Visual mode just started */
1597 from = to;
1598 }
1599
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001600 if (VIsual.lnum != wp->w_old_visual_lnum
1601 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 {
1603 if (wp->w_old_visual_lnum < from
1604 && wp->w_old_visual_lnum != 0)
1605 from = wp->w_old_visual_lnum;
1606 if (wp->w_old_visual_lnum > to)
1607 to = wp->w_old_visual_lnum;
1608 if (VIsual.lnum < from)
1609 from = VIsual.lnum;
1610 if (VIsual.lnum > to)
1611 to = VIsual.lnum;
1612 }
1613 }
1614
1615 /*
1616 * If in block mode and changed column or curwin->w_curswant:
1617 * update all lines.
1618 * First compute the actual start and end column.
1619 */
1620 if (VIsual_mode == Ctrl_V)
1621 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001622 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001623#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001624 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
Bram Moolenaar404406a2014-10-09 13:24:43 +02001626 if (curwin->w_p_lbr)
1627 ve_flags = VE_ALL;
1628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001630#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001631 ve_flags = save_ve_flags;
1632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 ++toc;
1634 if (curwin->w_curswant == MAXCOL)
1635 toc = MAXCOL;
1636
1637 if (fromc != wp->w_old_cursor_fcol
1638 || toc != wp->w_old_cursor_lcol)
1639 {
1640 if (from > VIsual.lnum)
1641 from = VIsual.lnum;
1642 if (to < VIsual.lnum)
1643 to = VIsual.lnum;
1644 }
1645 wp->w_old_cursor_fcol = fromc;
1646 wp->w_old_cursor_lcol = toc;
1647 }
1648 }
1649 else
1650 {
1651 /* Use the line numbers of the old Visual area. */
1652 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1653 {
1654 from = wp->w_old_cursor_lnum;
1655 to = wp->w_old_visual_lnum;
1656 }
1657 else
1658 {
1659 from = wp->w_old_visual_lnum;
1660 to = wp->w_old_cursor_lnum;
1661 }
1662 }
1663
1664 /*
1665 * There is no need to update lines above the top of the window.
1666 */
1667 if (from < wp->w_topline)
1668 from = wp->w_topline;
1669
1670 /*
1671 * If we know the value of w_botline, use it to restrict the update to
1672 * the lines that are visible in the window.
1673 */
1674 if (wp->w_valid & VALID_BOTLINE)
1675 {
1676 if (from >= wp->w_botline)
1677 from = wp->w_botline - 1;
1678 if (to >= wp->w_botline)
1679 to = wp->w_botline - 1;
1680 }
1681
1682 /*
1683 * Find the minimal part to be updated.
1684 * Watch out for scrolling that made entries in w_lines[] invalid.
1685 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1686 * top_end; need to redraw from top_end to the "to" line.
1687 * A middle mouse click with a Visual selection may change the text
1688 * above the Visual area and reset wl_valid, do count these for
1689 * mid_end (in srow).
1690 */
1691 if (mid_start > 0)
1692 {
1693 lnum = wp->w_topline;
1694 idx = 0;
1695 srow = 0;
1696 if (scrolled_down)
1697 mid_start = top_end;
1698 else
1699 mid_start = 0;
1700 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1701 {
1702 if (wp->w_lines[idx].wl_valid)
1703 mid_start += wp->w_lines[idx].wl_size;
1704 else if (!scrolled_down)
1705 srow += wp->w_lines[idx].wl_size;
1706 ++idx;
1707# ifdef FEAT_FOLDING
1708 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1709 lnum = wp->w_lines[idx].wl_lnum;
1710 else
1711# endif
1712 ++lnum;
1713 }
1714 srow += mid_start;
1715 mid_end = wp->w_height;
1716 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1717 {
1718 if (wp->w_lines[idx].wl_valid
1719 && wp->w_lines[idx].wl_lnum >= to + 1)
1720 {
1721 /* Only update until first row of this line */
1722 mid_end = srow;
1723 break;
1724 }
1725 srow += wp->w_lines[idx].wl_size;
1726 }
1727 }
1728 }
1729
1730 if (VIsual_active && buf == curwin->w_buffer)
1731 {
1732 wp->w_old_visual_mode = VIsual_mode;
1733 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1734 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001735 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 wp->w_old_curswant = curwin->w_curswant;
1737 }
1738 else
1739 {
1740 wp->w_old_visual_mode = 0;
1741 wp->w_old_cursor_lnum = 0;
1742 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001743 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
1746#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1747 /* reset got_int, otherwise regexp won't work */
1748 save_got_int = got_int;
1749 got_int = 0;
1750#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001751#ifdef SYN_TIME_LIMIT
1752 /* Set the time limit to 'redrawtime'. */
1753 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001754 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756#ifdef FEAT_FOLDING
1757 win_foldinfo.fi_level = 0;
1758#endif
1759
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001760#ifdef FEAT_MENU
1761 /*
1762 * Draw the window toolbar, if there is one.
1763 * TODO: only when needed.
1764 */
1765 if (winbar_height(wp) > 0)
1766 redraw_win_toolbar(wp);
1767#endif
1768
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 /*
1770 * Update all the window rows.
1771 */
1772 idx = 0; /* first entry in w_lines[].wl_size */
1773 row = 0;
1774 srow = 0;
1775 lnum = wp->w_topline; /* first line shown in window */
1776 for (;;)
1777 {
1778 /* stop updating when reached the end of the window (check for _past_
1779 * the end of the window is at the end of the loop) */
1780 if (row == wp->w_height)
1781 {
1782 didline = TRUE;
1783 break;
1784 }
1785
1786 /* stop updating when hit the end of the file */
1787 if (lnum > buf->b_ml.ml_line_count)
1788 {
1789 eof = TRUE;
1790 break;
1791 }
1792
1793 /* Remember the starting row of the line that is going to be dealt
1794 * with. It is used further down when the line doesn't fit. */
1795 srow = row;
1796
1797 /*
1798 * Update a line when it is in an area that needs updating, when it
1799 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001800 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 * win_del_lines(), check if the current line includes it.
1802 * When syntax folding is being used, the saved syntax states will
1803 * already have been updated, we can't see where the syntax state is
1804 * the same again, just update until the end of the window.
1805 */
1806 if (row < top_end
1807 || (row >= mid_start && row < mid_end)
1808#ifdef FEAT_SEARCH_EXTRA
1809 || top_to_mod
1810#endif
1811 || idx >= wp->w_lines_valid
1812 || (row + wp->w_lines[idx].wl_size > bot_start)
1813 || (mod_top != 0
1814 && (lnum == mod_top
1815 || (lnum >= mod_top
1816 && (lnum < mod_bot
1817#ifdef FEAT_SYN_HL
1818 || did_update == DID_FOLD
1819 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001820 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 && (
1822# ifdef FEAT_FOLDING
1823 (foldmethodIsSyntax(wp)
1824 && hasAnyFolding(wp)) ||
1825# endif
1826 syntax_check_changed(lnum)))
1827#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001828#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001829 /* match in fixed position might need redraw
1830 * if lines were inserted or deleted */
1831 || (wp->w_match_head != NULL
1832 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 )))))
1835 {
1836#ifdef FEAT_SEARCH_EXTRA
1837 if (lnum == mod_top)
1838 top_to_mod = FALSE;
1839#endif
1840
1841 /*
1842 * When at start of changed lines: May scroll following lines
1843 * up or down to minimize redrawing.
1844 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001845 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 */
1847 if (lnum == mod_top
1848 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001849 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 {
1851 int old_rows = 0;
1852 int new_rows = 0;
1853 int xtra_rows;
1854 linenr_T l;
1855
1856 /* Count the old number of window rows, using w_lines[], which
1857 * should still contain the sizes for the lines as they are
1858 * currently displayed. */
1859 for (i = idx; i < wp->w_lines_valid; ++i)
1860 {
1861 /* Only valid lines have a meaningful wl_lnum. Invalid
1862 * lines are part of the changed area. */
1863 if (wp->w_lines[i].wl_valid
1864 && wp->w_lines[i].wl_lnum == mod_bot)
1865 break;
1866 old_rows += wp->w_lines[i].wl_size;
1867#ifdef FEAT_FOLDING
1868 if (wp->w_lines[i].wl_valid
1869 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1870 {
1871 /* Must have found the last valid entry above mod_bot.
1872 * Add following invalid entries. */
1873 ++i;
1874 while (i < wp->w_lines_valid
1875 && !wp->w_lines[i].wl_valid)
1876 old_rows += wp->w_lines[i++].wl_size;
1877 break;
1878 }
1879#endif
1880 }
1881
1882 if (i >= wp->w_lines_valid)
1883 {
1884 /* We can't find a valid line below the changed lines,
1885 * need to redraw until the end of the window.
1886 * Inserting/deleting lines has no use. */
1887 bot_start = 0;
1888 }
1889 else
1890 {
1891 /* Able to count old number of rows: Count new window
1892 * rows, and may insert/delete lines */
1893 j = idx;
1894 for (l = lnum; l < mod_bot; ++l)
1895 {
1896#ifdef FEAT_FOLDING
1897 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1898 ++new_rows;
1899 else
1900#endif
1901#ifdef FEAT_DIFF
1902 if (l == wp->w_topline)
1903 new_rows += plines_win_nofill(wp, l, TRUE)
1904 + wp->w_topfill;
1905 else
1906#endif
1907 new_rows += plines_win(wp, l, TRUE);
1908 ++j;
1909 if (new_rows > wp->w_height - row - 2)
1910 {
1911 /* it's getting too much, must redraw the rest */
1912 new_rows = 9999;
1913 break;
1914 }
1915 }
1916 xtra_rows = new_rows - old_rows;
1917 if (xtra_rows < 0)
1918 {
1919 /* May scroll text up. If there is not enough
1920 * remaining text or scrolling fails, must redraw the
1921 * rest. If scrolling works, must redraw the text
1922 * below the scrolled text. */
1923 if (row - xtra_rows >= wp->w_height - 2)
1924 mod_bot = MAXLNUM;
1925 else
1926 {
1927 check_for_delay(FALSE);
1928 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001929 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 mod_bot = MAXLNUM;
1931 else
1932 bot_start = wp->w_height + xtra_rows;
1933 }
1934 }
1935 else if (xtra_rows > 0)
1936 {
1937 /* May scroll text down. If there is not enough
1938 * remaining text of scrolling fails, must redraw the
1939 * rest. */
1940 if (row + xtra_rows >= wp->w_height - 2)
1941 mod_bot = MAXLNUM;
1942 else
1943 {
1944 check_for_delay(FALSE);
1945 if (win_ins_lines(wp, row + old_rows,
1946 xtra_rows, FALSE, FALSE) == FAIL)
1947 mod_bot = MAXLNUM;
1948 else if (top_end > row + old_rows)
1949 /* Scrolled the part at the top that requires
1950 * updating down. */
1951 top_end += xtra_rows;
1952 }
1953 }
1954
1955 /* When not updating the rest, may need to move w_lines[]
1956 * entries. */
1957 if (mod_bot != MAXLNUM && i != j)
1958 {
1959 if (j < i)
1960 {
1961 int x = row + new_rows;
1962
1963 /* move entries in w_lines[] upwards */
1964 for (;;)
1965 {
1966 /* stop at last valid entry in w_lines[] */
1967 if (i >= wp->w_lines_valid)
1968 {
1969 wp->w_lines_valid = j;
1970 break;
1971 }
1972 wp->w_lines[j] = wp->w_lines[i];
1973 /* stop at a line that won't fit */
1974 if (x + (int)wp->w_lines[j].wl_size
1975 > wp->w_height)
1976 {
1977 wp->w_lines_valid = j + 1;
1978 break;
1979 }
1980 x += wp->w_lines[j++].wl_size;
1981 ++i;
1982 }
1983 if (bot_start > x)
1984 bot_start = x;
1985 }
1986 else /* j > i */
1987 {
1988 /* move entries in w_lines[] downwards */
1989 j -= i;
1990 wp->w_lines_valid += j;
1991 if (wp->w_lines_valid > wp->w_height)
1992 wp->w_lines_valid = wp->w_height;
1993 for (i = wp->w_lines_valid; i - j >= idx; --i)
1994 wp->w_lines[i] = wp->w_lines[i - j];
1995
1996 /* The w_lines[] entries for inserted lines are
1997 * now invalid, but wl_size may be used above.
1998 * Reset to zero. */
1999 while (i >= idx)
2000 {
2001 wp->w_lines[i].wl_size = 0;
2002 wp->w_lines[i--].wl_valid = FALSE;
2003 }
2004 }
2005 }
2006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 }
2008
2009#ifdef FEAT_FOLDING
2010 /*
2011 * When lines are folded, display one line for all of them.
2012 * Otherwise, display normally (can be several display lines when
2013 * 'wrap' is on).
2014 */
2015 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2016 if (fold_count != 0)
2017 {
2018 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2019 ++row;
2020 --fold_count;
2021 wp->w_lines[idx].wl_folded = TRUE;
2022 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2023# ifdef FEAT_SYN_HL
2024 did_update = DID_FOLD;
2025# endif
2026 }
2027 else
2028#endif
2029 if (idx < wp->w_lines_valid
2030 && wp->w_lines[idx].wl_valid
2031 && wp->w_lines[idx].wl_lnum == lnum
2032 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002033 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 && srow + wp->w_lines[idx].wl_size > wp->w_height
2035#ifdef FEAT_DIFF
2036 && diff_check_fill(wp, lnum) == 0
2037#endif
2038 )
2039 {
2040 /* This line is not going to fit. Don't draw anything here,
2041 * will draw "@ " lines below. */
2042 row = wp->w_height + 1;
2043 }
2044 else
2045 {
2046#ifdef FEAT_SEARCH_EXTRA
2047 prepare_search_hl(wp, lnum);
2048#endif
2049#ifdef FEAT_SYN_HL
2050 /* Let the syntax stuff know we skipped a few lines. */
2051 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002052 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 syntax_end_parsing(syntax_last_parsed + 1);
2054#endif
2055
2056 /*
2057 * Display one line.
2058 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002059 row = win_line(wp, lnum, srow, wp->w_height,
2060 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
2062#ifdef FEAT_FOLDING
2063 wp->w_lines[idx].wl_folded = FALSE;
2064 wp->w_lines[idx].wl_lastlnum = lnum;
2065#endif
2066#ifdef FEAT_SYN_HL
2067 did_update = DID_LINE;
2068 syntax_last_parsed = lnum;
2069#endif
2070 }
2071
2072 wp->w_lines[idx].wl_lnum = lnum;
2073 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002074
2075 /* Past end of the window or end of the screen. Note that after
2076 * resizing wp->w_height may be end up too big. That's a problem
2077 * elsewhere, but prevent a crash here. */
2078 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 {
2080 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002081 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2083 ++idx;
2084 break;
2085 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002086 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 wp->w_lines[idx].wl_size = row - srow;
2088 ++idx;
2089#ifdef FEAT_FOLDING
2090 lnum += fold_count + 1;
2091#else
2092 ++lnum;
2093#endif
2094 }
2095 else
2096 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002097 if (wp->w_p_rnu)
2098 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002099#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002100 // 'relativenumber' set: The text doesn't need to be drawn, but
2101 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002102 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2103 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002104 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002105 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002106#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002107 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002108 }
2109
2110 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 row += wp->w_lines[idx++].wl_size;
2112 if (row > wp->w_height) /* past end of screen */
2113 break;
2114#ifdef FEAT_FOLDING
2115 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2116#else
2117 ++lnum;
2118#endif
2119#ifdef FEAT_SYN_HL
2120 did_update = DID_NONE;
2121#endif
2122 }
2123
2124 if (lnum > buf->b_ml.ml_line_count)
2125 {
2126 eof = TRUE;
2127 break;
2128 }
2129 }
2130 /*
2131 * End of loop over all window lines.
2132 */
2133
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002134#ifdef FEAT_VTP
2135 /* Rewrite the character at the end of the screen line. */
2136 if (use_vtp())
2137 {
2138 int i;
2139
2140 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002141 if (enc_utf8)
2142 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2143 LineOffset[i] + screen_Columns) > 1)
2144 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2145 else
2146 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2147 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002148 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2149 }
2150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
2152 if (idx > wp->w_lines_valid)
2153 wp->w_lines_valid = idx;
2154
2155#ifdef FEAT_SYN_HL
2156 /*
2157 * Let the syntax stuff know we stop parsing here.
2158 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002159 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 syntax_end_parsing(syntax_last_parsed + 1);
2161#endif
2162
2163 /*
2164 * If we didn't hit the end of the file, and we didn't finish the last
2165 * line we were working on, then the line didn't fit.
2166 */
2167 wp->w_empty_rows = 0;
2168#ifdef FEAT_DIFF
2169 wp->w_filler_rows = 0;
2170#endif
2171 if (!eof && !didline)
2172 {
2173 if (lnum == wp->w_topline)
2174 {
2175 /*
2176 * Single line that does not fit!
2177 * Don't overwrite it, it can be edited.
2178 */
2179 wp->w_botline = lnum + 1;
2180 }
2181#ifdef FEAT_DIFF
2182 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2183 {
2184 /* Window ends in filler lines. */
2185 wp->w_botline = lnum;
2186 wp->w_filler_rows = wp->w_height - srow;
2187 }
2188#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002189 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2190 {
2191 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2192
2193 /*
2194 * Last line isn't finished: Display "@@@" in the last screen line.
2195 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002196 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002197 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002198 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002199 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002200 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002201 set_empty_rows(wp, srow);
2202 wp->w_botline = lnum;
2203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2205 {
2206 /*
2207 * Last line isn't finished: Display "@@@" at the end.
2208 */
2209 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2210 W_WINROW(wp) + wp->w_height,
2211 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002212 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 set_empty_rows(wp, srow);
2214 wp->w_botline = lnum;
2215 }
2216 else
2217 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002218 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 wp->w_botline = lnum;
2220 }
2221 }
2222 else
2223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 if (eof) /* we hit the end of the file */
2226 {
2227 wp->w_botline = buf->b_ml.ml_line_count + 1;
2228#ifdef FEAT_DIFF
2229 j = diff_check_fill(wp, wp->w_botline);
2230 if (j > 0 && !wp->w_botfill)
2231 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002232 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 if (char2cells(fill_diff) > 1)
2234 i = '-';
2235 else
2236 i = fill_diff;
2237 if (row + j > wp->w_height)
2238 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002239 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 row += j;
2241 }
2242#endif
2243 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002244 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 wp->w_botline = lnum;
2246
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002247 // Make sure the rest of the screen is blank
2248 // put '~'s on rows that aren't part of the file.
2249 win_draw_end(wp, '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 }
2251
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002252#ifdef SYN_TIME_LIMIT
2253 syn_set_timeout(NULL);
2254#endif
2255
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 /* Reset the type of redrawing required, the window has been updated. */
2257 wp->w_redr_type = 0;
2258#ifdef FEAT_DIFF
2259 wp->w_old_topfill = wp->w_topfill;
2260 wp->w_old_botfill = wp->w_botfill;
2261#endif
2262
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002263 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
2265 /*
2266 * There is a trick with w_botline. If we invalidate it on each
2267 * change that might modify it, this will cause a lot of expensive
2268 * calls to plines() in update_topline() each time. Therefore the
2269 * value of w_botline is often approximated, and this value is used to
2270 * compute the value of w_topline. If the value of w_botline was
2271 * wrong, check that the value of w_topline is correct (cursor is on
2272 * the visible part of the text). If it's not, we need to redraw
2273 * again. Mostly this just means scrolling up a few lines, so it
2274 * doesn't look too bad. Only do this for the current window (where
2275 * changes are relevant).
2276 */
2277 wp->w_valid |= VALID_BOTLINE;
2278 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2279 {
2280 recursive = TRUE;
2281 curwin->w_valid &= ~VALID_TOPLINE;
2282 update_topline(); /* may invalidate w_botline again */
2283 if (must_redraw != 0)
2284 {
2285 /* Don't update for changes in buffer again. */
2286 i = curbuf->b_mod_set;
2287 curbuf->b_mod_set = FALSE;
2288 win_update(curwin);
2289 must_redraw = 0;
2290 curbuf->b_mod_set = i;
2291 }
2292 recursive = FALSE;
2293 }
2294 }
2295
2296#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2297 /* restore got_int, unless CTRL-C was hit while redrawing */
2298 if (!got_int)
2299 got_int = save_got_int;
2300#endif
2301}
2302
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002304 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2305 * Return the new offset.
2306 */
2307 static int
2308screen_fill_end(
2309 win_T *wp,
2310 int c1,
2311 int c2,
2312 int off,
2313 int width,
2314 int row,
2315 int endrow,
2316 int attr)
2317{
2318 int nn = off + width;
2319
2320 if (nn > wp->w_width)
2321 nn = wp->w_width;
2322#ifdef FEAT_RIGHTLEFT
2323 if (wp->w_p_rl)
2324 {
2325 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2326 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2327 c1, c2, attr);
2328 }
2329 else
2330#endif
2331 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2332 wp->w_wincol + off, (int)wp->w_wincol + nn,
2333 c1, c2, attr);
2334 return nn;
2335}
2336
2337/*
2338 * Clear lines near the end the window and mark the unused lines with "c1".
2339 * use "c2" as the filler character.
2340 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 */
2342 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002343win_draw_end(
2344 win_T *wp,
2345 int c1,
2346 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002347 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002348 int row,
2349 int endrow,
2350 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 int n = 0;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002353
2354 if (draw_margin)
2355 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002356#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002357 int fdc = compute_foldcolumn(wp, 0);
2358
2359 if (fdc > 0)
2360 // draw the fold column
2361 n = screen_fill_end(wp, ' ', ' ', n, fdc,
2362 row, endrow, HL_ATTR(HLF_FC));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002363#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002364#ifdef FEAT_SIGNS
2365 if (signcolumn_on(wp))
2366 // draw the sign column
2367 n = screen_fill_end(wp, ' ', ' ', n, 2,
2368 row, endrow, HL_ATTR(HLF_SC));
2369#endif
2370 if ((wp->w_p_nu || wp->w_p_rnu)
2371 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2372 // draw the number column
2373 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
2374 row, endrow, HL_ATTR(HLF_N));
2375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376
2377#ifdef FEAT_RIGHTLEFT
2378 if (wp->w_p_rl)
2379 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002381 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002382 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002384 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002385 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 }
2387 else
2388#endif
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 + n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002392 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002394
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 set_empty_rows(wp, row);
2396}
2397
Bram Moolenaar1a384422010-07-14 19:53:30 +02002398#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002399/*
2400 * Advance **color_cols and return TRUE when there are columns to draw.
2401 */
2402 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002403advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002404{
2405 while (**color_cols >= 0 && vcol > **color_cols)
2406 ++*color_cols;
2407 return (**color_cols >= 0);
2408}
2409#endif
2410
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002411#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2412/*
2413 * Copy "text" to ScreenLines using "attr".
2414 * Returns the next screen column.
2415 */
2416 static int
2417text_to_screenline(win_T *wp, char_u *text, int col)
2418{
2419 int off = (int)(current_ScreenLine - ScreenLines);
2420
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002421 if (has_mbyte)
2422 {
2423 int cells;
2424 int u8c, u8cc[MAX_MCO];
2425 int i;
2426 int idx;
2427 int c_len;
2428 char_u *p;
2429# ifdef FEAT_ARABIC
2430 int prev_c = 0; /* previous Arabic character */
2431 int prev_c1 = 0; /* first composing char for prev_c */
2432# endif
2433
2434# ifdef FEAT_RIGHTLEFT
2435 if (wp->w_p_rl)
2436 idx = off;
2437 else
2438# endif
2439 idx = off + col;
2440
2441 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2442 for (p = text; *p != NUL; )
2443 {
2444 cells = (*mb_ptr2cells)(p);
2445 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002446 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002447# ifdef FEAT_RIGHTLEFT
2448 - (wp->w_p_rl ? col : 0)
2449# endif
2450 )
2451 break;
2452 ScreenLines[idx] = *p;
2453 if (enc_utf8)
2454 {
2455 u8c = utfc_ptr2char(p, u8cc);
2456 if (*p < 0x80 && u8cc[0] == 0)
2457 {
2458 ScreenLinesUC[idx] = 0;
2459#ifdef FEAT_ARABIC
2460 prev_c = u8c;
2461#endif
2462 }
2463 else
2464 {
2465#ifdef FEAT_ARABIC
2466 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2467 {
2468 /* Do Arabic shaping. */
2469 int pc, pc1, nc;
2470 int pcc[MAX_MCO];
2471 int firstbyte = *p;
2472
2473 /* The idea of what is the previous and next
2474 * character depends on 'rightleft'. */
2475 if (wp->w_p_rl)
2476 {
2477 pc = prev_c;
2478 pc1 = prev_c1;
2479 nc = utf_ptr2char(p + c_len);
2480 prev_c1 = u8cc[0];
2481 }
2482 else
2483 {
2484 pc = utfc_ptr2char(p + c_len, pcc);
2485 nc = prev_c;
2486 pc1 = pcc[0];
2487 }
2488 prev_c = u8c;
2489
2490 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2491 pc, pc1, nc);
2492 ScreenLines[idx] = firstbyte;
2493 }
2494 else
2495 prev_c = u8c;
2496#endif
2497 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002498 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002499 for (i = 0; i < Screen_mco; ++i)
2500 {
2501 ScreenLinesC[i][idx] = u8cc[i];
2502 if (u8cc[i] == 0)
2503 break;
2504 }
2505 }
2506 if (cells > 1)
2507 ScreenLines[idx + 1] = 0;
2508 }
2509 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2510 /* double-byte single width character */
2511 ScreenLines2[idx] = p[1];
2512 else if (cells > 1)
2513 /* double-width character */
2514 ScreenLines[idx + 1] = p[1];
2515 col += cells;
2516 idx += cells;
2517 p += c_len;
2518 }
2519 }
2520 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002521 {
2522 int len = (int)STRLEN(text);
2523
Bram Moolenaar02631462017-09-22 15:20:32 +02002524 if (len > wp->w_width - col)
2525 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002526 if (len > 0)
2527 {
2528#ifdef FEAT_RIGHTLEFT
2529 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002530 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002531 else
2532#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002533 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002534 col += len;
2535 }
2536 }
2537 return col;
2538}
2539#endif
2540
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541#ifdef FEAT_FOLDING
2542/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002543 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2544 * space is available for window "wp", minus "col".
2545 */
2546 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002547compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002548{
2549 int fdc = wp->w_p_fdc;
2550 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002551 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002552
2553 if (fdc > wwidth - (col + wmw))
2554 fdc = wwidth - (col + wmw);
2555 return fdc;
2556}
2557
2558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 * Display one folded line.
2560 */
2561 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002562fold_line(
2563 win_T *wp,
2564 long fold_count,
2565 foldinfo_T *foldinfo,
2566 linenr_T lnum,
2567 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002569 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 pos_T *top, *bot;
2571 linenr_T lnume = lnum + fold_count - 1;
2572 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002573 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 int col;
2576 int txtcol;
2577 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002578 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579
2580 /* Build the fold line:
2581 * 1. Add the cmdwin_type for the command-line window
2582 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002583 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 * 4. Compose the text
2585 * 5. Add the text
2586 * 6. set highlighting for the Visual area an other text
2587 */
2588 col = 0;
2589
2590 /*
2591 * 1. Add the cmdwin_type for the command-line window
2592 * Ignores 'rightleft', this window is never right-left.
2593 */
2594#ifdef FEAT_CMDWIN
2595 if (cmdwin_type != 0 && wp == curwin)
2596 {
2597 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002598 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 if (enc_utf8)
2600 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 ++col;
2602 }
2603#endif
2604
2605 /*
2606 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002607 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002609 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 if (fdc > 0)
2611 {
2612 fill_foldcolumn(buf, wp, TRUE, lnum);
2613#ifdef FEAT_RIGHTLEFT
2614 if (wp->w_p_rl)
2615 {
2616 int i;
2617
Bram Moolenaar02631462017-09-22 15:20:32 +02002618 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002619 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 /* reverse the fold column */
2621 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002622 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
2624 else
2625#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002626 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 col += fdc;
2628 }
2629
2630#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002631# define RL_MEMSET(p, v, l) \
2632 do { \
2633 if (wp->w_p_rl) \
2634 for (ri = 0; ri < l; ++ri) \
2635 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2636 else \
2637 for (ri = 0; ri < l; ++ri) \
2638 ScreenAttrs[off + (p) + ri] = v; \
2639 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002641# define RL_MEMSET(p, v, l) \
2642 do { \
2643 for (ri = 0; ri < l; ++ri) \
2644 ScreenAttrs[off + (p) + ri] = v; \
2645 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646#endif
2647
Bram Moolenaar64486672010-05-16 15:46:46 +02002648 /* Set all attributes of the 'number' or 'relativenumber' column and the
2649 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002650 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
2652#ifdef FEAT_SIGNS
2653 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002654 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002656 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 if (len > 0)
2658 {
2659 if (len > 2)
2660 len = 2;
2661# ifdef FEAT_RIGHTLEFT
2662 if (wp->w_p_rl)
2663 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002664 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002665 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 else
2667# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002668 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 col += len;
2670 }
2671 }
2672#endif
2673
2674 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002675 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002677 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002679 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 if (len > 0)
2681 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002682 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002683 long num;
2684 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002685
2686 if (len > w + 1)
2687 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002688
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002689 if (wp->w_p_nu && !wp->w_p_rnu)
2690 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002691 num = (long)lnum;
2692 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002693 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002694 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002695 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002696 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002697 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002698 /* 'number' + 'relativenumber': cursor line shows absolute
2699 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002700 num = lnum;
2701 fmt = "%-*ld ";
2702 }
2703 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002704
Bram Moolenaar700e7342013-01-30 12:31:36 +01002705 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706#ifdef FEAT_RIGHTLEFT
2707 if (wp->w_p_rl)
2708 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002709 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002710 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 else
2712#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002713 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 col += len;
2715 }
2716 }
2717
2718 /*
2719 * 4. Compose the folded-line string with 'foldtext', if set.
2720 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002721 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
2723 txtcol = col; /* remember where text starts */
2724
2725 /*
2726 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2727 * Right-left text is put in columns 0 - number-col, normal text is put
2728 * in columns number-col - window-width.
2729 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002730 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 /* Fill the rest of the line with the fold filler */
2733#ifdef FEAT_RIGHTLEFT
2734 if (wp->w_p_rl)
2735 col -= txtcol;
2736#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002737 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738#ifdef FEAT_RIGHTLEFT
2739 - (wp->w_p_rl ? txtcol : 0)
2740#endif
2741 )
2742 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 if (enc_utf8)
2744 {
2745 if (fill_fold >= 0x80)
2746 {
2747 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002748 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002749 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 }
2751 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002752 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002754 ScreenLines[off + col] = fill_fold;
2755 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002756 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002758 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002759 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
2761
2762 if (text != buf)
2763 vim_free(text);
2764
2765 /*
2766 * 6. set highlighting for the Visual area an other text.
2767 * If all folded lines are in the Visual area, highlight the line.
2768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2770 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002771 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 {
2773 /* Visual is after curwin->w_cursor */
2774 top = &curwin->w_cursor;
2775 bot = &VIsual;
2776 }
2777 else
2778 {
2779 /* Visual is before curwin->w_cursor */
2780 top = &VIsual;
2781 bot = &curwin->w_cursor;
2782 }
2783 if (lnum >= top->lnum
2784 && lnume <= bot->lnum
2785 && (VIsual_mode != 'v'
2786 || ((lnum > top->lnum
2787 || (lnum == top->lnum
2788 && top->col == 0))
2789 && (lnume < bot->lnum
2790 || (lnume == bot->lnum
2791 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002792 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 {
2794 if (VIsual_mode == Ctrl_V)
2795 {
2796 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002797 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002799 if (wp->w_old_cursor_lcol != MAXCOL
2800 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002801 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 len = wp->w_old_cursor_lcol;
2803 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002804 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002805 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002806 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 }
2808 }
2809 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002812 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 }
2815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002817#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002818 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2819 if (wp->w_p_cc_cols)
2820 {
2821 int i = 0;
2822 int j = wp->w_p_cc_cols[i];
2823 int old_txtcol = txtcol;
2824
2825 while (j > -1)
2826 {
2827 txtcol += j;
2828 if (wp->w_p_wrap)
2829 txtcol -= wp->w_skipcol;
2830 else
2831 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002832 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002833 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002834 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002835 txtcol = old_txtcol;
2836 j = wp->w_p_cc_cols[++i];
2837 }
2838 }
2839
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002840 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002841 if (wp->w_p_cuc)
2842 {
2843 txtcol += wp->w_virtcol;
2844 if (wp->w_p_wrap)
2845 txtcol -= wp->w_skipcol;
2846 else
2847 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002848 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002849 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002850 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002851 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853
Bram Moolenaar02631462017-09-22 15:20:32 +02002854 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2855 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
2857 /*
2858 * Update w_cline_height and w_cline_folded if the cursor line was
2859 * updated (saves a call to plines() later).
2860 */
2861 if (wp == curwin
2862 && lnum <= curwin->w_cursor.lnum
2863 && lnume >= curwin->w_cursor.lnum)
2864 {
2865 curwin->w_cline_row = row;
2866 curwin->w_cline_height = 1;
2867 curwin->w_cline_folded = TRUE;
2868 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2869 }
2870}
2871
2872/*
2873 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2874 */
2875 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002876copy_text_attr(
2877 int off,
2878 char_u *buf,
2879 int len,
2880 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002882 int i;
2883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 if (enc_utf8)
2886 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002887 for (i = 0; i < len; ++i)
2888 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889}
2890
2891/*
2892 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002893 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 */
2895 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002896fill_foldcolumn(
2897 char_u *p,
2898 win_T *wp,
2899 int closed, /* TRUE of FALSE */
2900 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901{
2902 int i = 0;
2903 int level;
2904 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002905 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002906 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907
2908 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002909 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910
2911 level = win_foldinfo.fi_level;
2912 if (level > 0)
2913 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002914 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002915 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002916
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 /* If the column is too narrow, we start at the lowest level that
2918 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002919 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 if (first_level < 1)
2921 first_level = 1;
2922
Bram Moolenaar1c934292015-01-27 16:39:29 +01002923 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 {
2925 if (win_foldinfo.fi_lnum == lnum
2926 && first_level + i >= win_foldinfo.fi_low_level)
2927 p[i] = '-';
2928 else if (first_level == 1)
2929 p[i] = '|';
2930 else if (first_level + i <= 9)
2931 p[i] = '0' + first_level + i;
2932 else
2933 p[i] = '>';
2934 if (first_level + i == level)
2935 break;
2936 }
2937 }
2938 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002939 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940}
2941#endif /* FEAT_FOLDING */
2942
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01002943#ifdef FEAT_TEXT_PROP
2944static textprop_T *current_text_props = NULL;
2945static buf_T *current_buf = NULL;
2946
2947 static int
2948#ifdef __BORLANDC__
2949_RTLENTRYF
2950#endif
2951text_prop_compare(const void *s1, const void *s2)
2952{
2953 int idx1, idx2;
2954 proptype_T *pt1, *pt2;
2955 colnr_T col1, col2;
2956
2957 idx1 = *(int *)s1;
2958 idx2 = *(int *)s2;
2959 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
2960 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
2961 if (pt1 == pt2)
2962 return 0;
2963 if (pt1 == NULL)
2964 return -1;
2965 if (pt2 == NULL)
2966 return 1;
2967 if (pt1->pt_priority != pt2->pt_priority)
2968 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
2969 col1 = current_text_props[idx1].tp_col;
2970 col2 = current_text_props[idx2].tp_col;
2971 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
2972}
2973#endif
2974
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975/*
2976 * Display line "lnum" of window 'wp' on the screen.
2977 * Start at row "startrow", stop when "endrow" is reached.
2978 * wp->w_virtcol needs to be valid.
2979 *
2980 * Return the number of last row the line occupies.
2981 */
2982 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002983win_line(
2984 win_T *wp,
2985 linenr_T lnum,
2986 int startrow,
2987 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002988 int nochange UNUSED, // not updating for changed text
2989 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01002991 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2993 int c = 0; /* init for GCC */
2994 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002995#ifdef FEAT_LINEBREAK
2996 long vcol_sbr = -1; /* virtual column after showbreak */
2997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 long vcol_prev = -1; /* "vcol" of previous character */
2999 char_u *line; /* current line */
3000 char_u *ptr; /* current position in "line" */
3001 int row; /* row in the window, excl w_winrow */
3002 int screen_row; /* row on the screen, incl w_winrow */
3003
3004 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3005 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003006 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003007 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003009 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 int extra_attr = 0; /* attributes when n_extra != 0 */
3011 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3012 displaying lcs_eol at end-of-line */
3013 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3014 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3015
3016 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3017 int saved_n_extra = 0;
3018 char_u *saved_p_extra = NULL;
3019 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003020 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 int saved_char_attr = 0;
3022
3023 int n_attr = 0; /* chars with special attr */
3024 int saved_attr2 = 0; /* char_attr saved for n_attr */
3025 int n_attr3 = 0; /* chars with overruling special attr */
3026 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3027
3028 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3029
3030 int fromcol, tocol; /* start/end of inverting */
3031 int fromcol_prev = -2; /* start of inverting after cursor */
3032 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003034 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 pos_T pos;
3036 long v;
3037
3038 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003039 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3041 in this line */
3042 int attr = 0; /* attributes for area highlighting */
3043 int area_attr = 0; /* attributes desired by highlighting */
3044 int search_attr = 0; /* attributes desired by 'hlsearch' */
3045#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003046 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 int syntax_attr = 0; /* attributes desired by syntax */
3048 int has_syntax = FALSE; /* this buffer has syntax highl. */
3049 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003050 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003051 int draw_color_col = FALSE; /* highlight colorcolumn */
3052 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003053#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003054#ifdef FEAT_TEXT_PROP
3055 int text_prop_count;
3056 int text_prop_next = 0; // next text property to use
3057 textprop_T *text_props = NULL;
3058 int *text_prop_idxs = NULL;
3059 int text_props_active = 0;
3060 proptype_T *text_prop_type = NULL;
3061 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003062 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003063#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003064#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003065 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003066# define SPWORDLEN 150
3067 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003068 int nextlinecol = 0; /* column where nextline[] starts */
3069 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003070 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003071 int spell_attr = 0; /* attributes desired by spelling */
3072 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003073 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3074 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003075 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003076 static int cap_col = -1; /* column to check for Cap word */
3077 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003078 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003080 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 int multi_attr = 0; /* attributes desired by multibyte */
3082 int mb_l = 1; /* multi-byte byte length */
3083 int mb_c = 0; /* decoded multi-byte character */
3084 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003085 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086#ifdef FEAT_DIFF
3087 int filler_lines; /* nr of filler lines to be drawn */
3088 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003089 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 int change_start = MAXCOL; /* first col of changed area */
3091 int change_end = -1; /* last col of changed area */
3092#endif
3093 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3094#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003095 int need_showbreak = FALSE; /* overlong line, skipping first x
3096 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003098#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003099 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003101 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102#endif
3103#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003104 matchitem_T *cur; /* points to the match list */
3105 match_T *shl; /* points to search_hl or a match */
3106 int shl_flag; /* flag to indicate whether search_hl
3107 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003108 int pos_inprogress; /* marks that position match search is
3109 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003110 int prevcol_hl_flag; /* flag to indicate whether prevcol
3111 equals startcol of search_hl or one
3112 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113#endif
3114#ifdef FEAT_ARABIC
3115 int prev_c = 0; /* previous Arabic character */
3116 int prev_c1 = 0; /* first composing char for prev_c */
3117#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003118#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003119 int did_line_attr = 0;
3120#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003121#ifdef FEAT_TERMINAL
3122 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003123 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125
3126 /* draw_state: items that are drawn in sequence: */
3127#define WL_START 0 /* nothing done yet */
3128#ifdef FEAT_CMDWIN
3129# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3130#else
3131# define WL_CMDLINE WL_START
3132#endif
3133#ifdef FEAT_FOLDING
3134# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3135#else
3136# define WL_FOLD WL_CMDLINE
3137#endif
3138#ifdef FEAT_SIGNS
3139# define WL_SIGN WL_FOLD + 1 /* column for signs */
3140#else
3141# define WL_SIGN WL_FOLD /* column for signs */
3142#endif
3143#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003144#ifdef FEAT_LINEBREAK
3145# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003147# define WL_BRI WL_NR
3148#endif
3149#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3150# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3151#else
3152# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153#endif
3154#define WL_LINE WL_SBR + 1 /* text in the line */
3155 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003156#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 int feedback_col = 0;
3158 int feedback_old_attr = -1;
3159#endif
3160
Bram Moolenaar860cae12010-06-05 23:22:07 +02003161#ifdef FEAT_CONCEAL
3162 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003163 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003164 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003165 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003166 int is_concealing = FALSE;
3167 int boguscols = 0; /* nonexistent columns added to force
3168 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003169 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003170 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003171 int match_conc = 0; /* cchar for match functions */
3172 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003173 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003174# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003175# define FIX_FOR_BOGUSCOLS \
3176 { \
3177 n_extra += vcol_off; \
3178 vcol -= vcol_off; \
3179 vcol_off = 0; \
3180 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003181 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003182 boguscols = 0; \
3183 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003184#else
3185# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
3188 if (startrow > endrow) /* past the end already! */
3189 return startrow;
3190
3191 row = startrow;
3192 screen_row = row + W_WINROW(wp);
3193
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003194 if (!number_only)
3195 {
3196 /*
3197 * To speed up the loop below, set extra_check when there is linebreak,
3198 * trailing white space and/or syntax processing to be done.
3199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003201 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202#endif
3203#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003204 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003205# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003206 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003207# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003208 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003210 /* Prepare for syntax highlighting in this line. When there is an
3211 * error, stop syntax highlighting. */
3212 save_did_emsg = did_emsg;
3213 did_emsg = FALSE;
3214 syntax_start(wp, lnum);
3215 if (did_emsg)
3216 wp->w_s->b_syn_error = TRUE;
3217 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003218 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003219 did_emsg = save_did_emsg;
3220#ifdef SYN_TIME_LIMIT
3221 if (!wp->w_s->b_syn_slow)
3222#endif
3223 {
3224 has_syntax = TRUE;
3225 extra_check = TRUE;
3226 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003229
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003230 /* Check for columns to display for 'colorcolumn'. */
3231 color_cols = wp->w_p_cc_cols;
3232 if (color_cols != NULL)
3233 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003234#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003235
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003236#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003237 if (term_show_buffer(wp->w_buffer))
3238 {
3239 extra_check = TRUE;
3240 get_term_attr = TRUE;
3241 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3242 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003243#endif
3244
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003245#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003246 if (wp->w_p_spell
3247 && *wp->w_s->b_p_spl != NUL
3248 && wp->w_s->b_langp.ga_len > 0
3249 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003250 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003251 /* Prepare for spell checking. */
3252 has_spell = TRUE;
3253 extra_check = TRUE;
3254
Bram Moolenaar7701f302018-10-02 21:20:32 +02003255 /* Get the start of the next line, so that words that wrap to the
3256 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003257 * Trick: skip a few chars for C/shell/Vim comments */
3258 nextline[SPWORDLEN] = NUL;
3259 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3260 {
3261 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3262 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3263 }
3264
Bram Moolenaar7701f302018-10-02 21:20:32 +02003265 /* When a word wrapped from the previous line the start of the
3266 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003267 if (lnum == checked_lnum)
3268 cur_checked_col = checked_col;
3269 checked_lnum = 0;
3270
3271 /* When there was a sentence end in the previous line may require a
3272 * word starting with capital in this line. In line 1 always check
3273 * the first word. */
3274 if (lnum != capcol_lnum)
3275 cap_col = -1;
3276 if (lnum == 1)
3277 cap_col = 0;
3278 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280#endif
3281
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003282 /*
3283 * handle visual active in this window
3284 */
3285 fromcol = -10;
3286 tocol = MAXCOL;
3287 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003289 /* Visual is after curwin->w_cursor */
3290 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003292 top = &curwin->w_cursor;
3293 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003295 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003297 top = &VIsual;
3298 bot = &curwin->w_cursor;
3299 }
3300 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3301 if (VIsual_mode == Ctrl_V) /* block mode */
3302 {
3303 if (lnum_in_visual_area)
3304 {
3305 fromcol = wp->w_old_cursor_fcol;
3306 tocol = wp->w_old_cursor_lcol;
3307 }
3308 }
3309 else /* non-block mode */
3310 {
3311 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003313 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003315 if (VIsual_mode == 'V') /* linewise */
3316 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 else
3318 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003319 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3320 if (gchar_pos(top) == NUL)
3321 tocol = fromcol + 1;
3322 }
3323 }
3324 if (VIsual_mode != 'V' && lnum == bot->lnum)
3325 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003326 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003327 {
3328 fromcol = -10;
3329 tocol = MAXCOL;
3330 }
3331 else if (bot->col == MAXCOL)
3332 tocol = MAXCOL;
3333 else
3334 {
3335 pos = *bot;
3336 if (*p_sel == 'e')
3337 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3338 else
3339 {
3340 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3341 ++tocol;
3342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
3344 }
3345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003347 /* Check if the character under the cursor should not be inverted */
3348 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003349#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003350 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003351#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003352 )
3353 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003355 /* if inverting in this line set area_highlighting */
3356 if (fromcol >= 0)
3357 {
3358 area_highlighting = TRUE;
3359 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003361 if ((clip_star.available && !clip_star.owned
3362 && clip_isautosel_star())
3363 || (clip_plus.available && !clip_plus.owned
3364 && clip_isautosel_plus()))
3365 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003370 /*
3371 * handle 'incsearch' and ":s///c" highlighting
3372 */
3373 else if (highlight_match
3374 && wp == curwin
3375 && lnum >= curwin->w_cursor.lnum
3376 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003378 if (lnum == curwin->w_cursor.lnum)
3379 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003380 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003381 else
3382 fromcol = 0;
3383 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3384 {
3385 pos.lnum = lnum;
3386 pos.col = search_match_endcol;
3387 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3388 }
3389 else
3390 tocol = MAXCOL;
3391 /* do at least one character; happens when past end of line */
3392 if (fromcol == tocol)
3393 tocol = fromcol + 1;
3394 area_highlighting = TRUE;
3395 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
3398
3399#ifdef FEAT_DIFF
3400 filler_lines = diff_check(wp, lnum);
3401 if (filler_lines < 0)
3402 {
3403 if (filler_lines == -1)
3404 {
3405 if (diff_find_change(wp, lnum, &change_start, &change_end))
3406 diff_hlf = HLF_ADD; /* added line */
3407 else if (change_start == 0)
3408 diff_hlf = HLF_TXD; /* changed text */
3409 else
3410 diff_hlf = HLF_CHD; /* changed line */
3411 }
3412 else
3413 diff_hlf = HLF_ADD; /* added line */
3414 filler_lines = 0;
3415 area_highlighting = TRUE;
3416 }
3417 if (lnum == wp->w_topline)
3418 filler_lines = wp->w_topfill;
3419 filler_todo = filler_lines;
3420#endif
3421
3422#ifdef LINE_ATTR
3423# ifdef FEAT_SIGNS
3424 /* If this line has a sign with line highlighting set line_attr. */
3425 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3426 if (v != 0)
3427 line_attr = sign_get_attr((int)v, TRUE);
3428# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003429# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003431 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003432 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433# endif
3434 if (line_attr != 0)
3435 area_highlighting = TRUE;
3436#endif
3437
3438 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3439 ptr = line;
3440
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003441#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003442 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003443 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003444 /* For checking first word with a capital skip white space. */
3445 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003446 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003447
Bram Moolenaar30abd282005-06-22 22:35:10 +00003448 /* To be able to spell-check over line boundaries copy the end of the
3449 * current line into nextline[]. Above the start of the next line was
3450 * copied to nextline[SPWORDLEN]. */
3451 if (nextline[SPWORDLEN] == NUL)
3452 {
3453 /* No next line or it is empty. */
3454 nextlinecol = MAXCOL;
3455 nextline_idx = 0;
3456 }
3457 else
3458 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003459 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003460 if (v < SPWORDLEN)
3461 {
3462 /* Short line, use it completely and append the start of the
3463 * next line. */
3464 nextlinecol = 0;
3465 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003466 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003467 nextline_idx = v + 1;
3468 }
3469 else
3470 {
3471 /* Long line, use only the last SPWORDLEN bytes. */
3472 nextlinecol = v - SPWORDLEN;
3473 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3474 nextline_idx = SPWORDLEN + 1;
3475 }
3476 }
3477 }
3478#endif
3479
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003480 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003482 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003483 extra_check = TRUE;
3484 /* find start of trailing whitespace */
3485 if (lcs_trail)
3486 {
3487 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003488 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003489 --trailcol;
3490 trailcol += (colnr_T) (ptr - line);
3491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493
3494 /*
3495 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3496 * first character to be displayed.
3497 */
3498 if (wp->w_p_wrap)
3499 v = wp->w_skipcol;
3500 else
3501 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003502 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 while (vcol < v && *ptr != NUL)
3507 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003508 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003511 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 }
3513
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003514 /* When:
3515 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003516 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003517 * - 'virtualedit' is set, or
3518 * - the visual mode is active,
3519 * the end of the line may be before the start of the displayed part.
3520 */
3521 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003522#ifdef FEAT_SYN_HL
3523 wp->w_p_cuc || draw_color_col ||
3524#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003525 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003526 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
3529 /* Handle a character that's not completely on the screen: Put ptr at
3530 * that character but skip the first few screen characters. */
3531 if (vcol > v)
3532 {
3533 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003535 /* If the character fits on the screen, don't need to skip it.
3536 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003537 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003538 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540
3541 /*
3542 * Adjust for when the inverted text is before the screen,
3543 * and when the start of the inverted text is before the screen.
3544 */
3545 if (tocol <= vcol)
3546 fromcol = 0;
3547 else if (fromcol >= 0 && fromcol < vcol)
3548 fromcol = vcol;
3549
3550#ifdef FEAT_LINEBREAK
3551 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3552 if (wp->w_p_wrap)
3553 need_showbreak = TRUE;
3554#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003555#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003556 /* When spell checking a word we need to figure out the start of the
3557 * word and if it's badly spelled or not. */
3558 if (has_spell)
3559 {
3560 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003561 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003562 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003563
3564 pos = wp->w_cursor;
3565 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003566 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003567 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003568
3569 /* spell_move_to() may call ml_get() and make "line" invalid */
3570 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3571 ptr = line + linecol;
3572
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003573 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003574 {
3575 /* no bad word found at line start, don't check until end of a
3576 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003577 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003578 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003579 }
3580 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003581 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003582 /* bad word found, use attributes until end of word */
3583 word_end = wp->w_cursor.col + len + 1;
3584
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003585 /* Turn index into actual attributes. */
3586 if (spell_hlf != HLF_COUNT)
3587 spell_attr = highlight_attr[spell_hlf];
3588 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003589 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003590
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003591# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003592 /* Need to restart syntax highlighting for this line. */
3593 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003594 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003595# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003596 }
3597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
3599
3600 /*
3601 * Correct highlighting for cursor that can't be disabled.
3602 * Avoids having to check this for each character.
3603 */
3604 if (fromcol >= 0)
3605 {
3606 if (noinvcur)
3607 {
3608 if ((colnr_T)fromcol == wp->w_virtcol)
3609 {
3610 /* highlighting starts at cursor, let it start just after the
3611 * cursor */
3612 fromcol_prev = fromcol;
3613 fromcol = -1;
3614 }
3615 else if ((colnr_T)fromcol < wp->w_virtcol)
3616 /* restart highlighting after the cursor */
3617 fromcol_prev = wp->w_virtcol;
3618 }
3619 if (fromcol >= tocol)
3620 fromcol = -1;
3621 }
3622
3623#ifdef FEAT_SEARCH_EXTRA
3624 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003625 * Handle highlighting the last used search pattern and matches.
3626 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003628 cur = wp->w_match_head;
3629 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003630 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003632 if (shl_flag == FALSE)
3633 {
3634 shl = &search_hl;
3635 shl_flag = TRUE;
3636 }
3637 else
3638 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003639 shl->startcol = MAXCOL;
3640 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003642 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003643 v = (long)(ptr - line);
3644 if (cur != NULL)
3645 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003646 next_search_hl(wp, shl, lnum, (colnr_T)v,
3647 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003648
3649 /* Need to get the line again, a multi-line regexp may have made it
3650 * invalid. */
3651 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3652 ptr = line + v;
3653
3654 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003656 if (shl->lnum == lnum)
3657 shl->startcol = shl->rm.startpos[0].col;
3658 else
3659 shl->startcol = 0;
3660 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3661 - shl->rm.startpos[0].lnum)
3662 shl->endcol = shl->rm.endpos[0].col;
3663 else
3664 shl->endcol = MAXCOL;
3665 /* Highlight one character for an empty match. */
3666 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003668 if (has_mbyte && line[shl->endcol] != NUL)
3669 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3670 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003671 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003673 if ((long)shl->startcol < v) /* match at leftcol */
3674 {
3675 shl->attr_cur = shl->attr;
3676 search_attr = shl->attr;
3677 }
3678 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003680 if (shl != &search_hl && cur != NULL)
3681 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 }
3683#endif
3684
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003685#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003686 // Cursor line highlighting for 'cursorline' in the current window.
3687 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003688 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003689 // Do not show the cursor line when Visual mode is active, because it's
3690 // not clear what is selected then. Do update w_last_cursorline.
3691 if (!(wp == curwin && VIsual_active))
3692 {
3693 line_attr = HL_ATTR(HLF_CUL);
3694 area_highlighting = TRUE;
3695 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003696 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003697 }
3698#endif
3699
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003700#ifdef FEAT_TEXT_PROP
3701 {
3702 char_u *prop_start;
3703
3704 text_prop_count = get_text_props(wp->w_buffer, lnum,
3705 &prop_start, FALSE);
3706 if (text_prop_count > 0)
3707 {
3708 // Make a copy of the properties, so that they are properly
3709 // aligned.
3710 text_props = (textprop_T *)alloc(
3711 text_prop_count * sizeof(textprop_T));
3712 if (text_props != NULL)
3713 mch_memmove(text_props, prop_start,
3714 text_prop_count * sizeof(textprop_T));
3715
3716 // Allocate an array for the indexes.
3717 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3718 area_highlighting = TRUE;
3719 extra_check = TRUE;
3720 }
3721 }
3722#endif
3723
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003724 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 col = 0;
3726#ifdef FEAT_RIGHTLEFT
3727 if (wp->w_p_rl)
3728 {
3729 /* Rightleft window: process the text in the normal direction, but put
3730 * it in current_ScreenLine[] from right to left. Start at the
3731 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003732 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 off += col;
3734 }
3735#endif
3736
3737 /*
3738 * Repeat for the whole displayed line.
3739 */
3740 for (;;)
3741 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003742#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003743 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 /* Skip this quickly when working on the text. */
3746 if (draw_state != WL_LINE)
3747 {
3748#ifdef FEAT_CMDWIN
3749 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3750 {
3751 draw_state = WL_CMDLINE;
3752 if (cmdwin_type != 0 && wp == curwin)
3753 {
3754 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003756 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003757 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003758 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
3760 }
3761#endif
3762
3763#ifdef FEAT_FOLDING
3764 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3765 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003766 int fdc = compute_foldcolumn(wp, 0);
3767
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003769 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003771 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003772 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003773 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003774 p_extra_free = alloc(12 + 1);
3775
3776 if (p_extra_free != NULL)
3777 {
3778 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3779 n_extra = fdc;
3780 p_extra_free[n_extra] = NUL;
3781 p_extra = p_extra_free;
3782 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003783 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003784 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 }
3787 }
3788#endif
3789
3790#ifdef FEAT_SIGNS
3791 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3792 {
3793 draw_state = WL_SIGN;
3794 /* Show the sign column when there are any signs in this
3795 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003796 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003798 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003800 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801# endif
3802
3803 /* Draw two cells with the sign value or blank. */
3804 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003805 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003806 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 n_extra = 2;
3808
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003809 if (row == startrow
3810#ifdef FEAT_DIFF
3811 + filler_lines && filler_todo <= 0
3812#endif
3813 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
3815 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3816 SIGN_TEXT);
3817# ifdef FEAT_SIGN_ICONS
3818 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3819 SIGN_ICON);
3820 if (gui.in_use && icon_sign != 0)
3821 {
3822 /* Use the image in this position. */
3823 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003824 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825# ifdef FEAT_NETBEANS_INTG
3826 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003827 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003829 c_final = NUL;
3830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831# endif
3832 char_attr = icon_sign;
3833 }
3834 else
3835# endif
3836 if (text_sign != 0)
3837 {
3838 p_extra = sign_get_text(text_sign);
3839 if (p_extra != NULL)
3840 {
3841 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003842 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003843 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
3845 char_attr = sign_get_attr(text_sign, FALSE);
3846 }
3847 }
3848 }
3849 }
3850#endif
3851
3852 if (draw_state == WL_NR - 1 && n_extra == 0)
3853 {
3854 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003855 /* Display the absolute or relative line number. After the
3856 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3857 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 && (row == startrow
3859#ifdef FEAT_DIFF
3860 + filler_lines
3861#endif
3862 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3863 {
3864 /* Draw the line number (empty space after wrapping). */
3865 if (row == startrow
3866#ifdef FEAT_DIFF
3867 + filler_lines
3868#endif
3869 )
3870 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003871 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003872 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003873
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003874 if (wp->w_p_nu && !wp->w_p_rnu)
3875 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003876 num = (long)lnum;
3877 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003878 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003879 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003880 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003881 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003882 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003883 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003884 num = lnum;
3885 fmt = "%-*ld ";
3886 }
3887 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003888
Bram Moolenaar700e7342013-01-30 12:31:36 +01003889 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003890 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 if (wp->w_skipcol > 0)
3892 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3893 *p_extra = '-';
3894#ifdef FEAT_RIGHTLEFT
3895 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01003896 {
3897 char_u *p1, *p2;
3898 int t;
3899
3900 // like rl_mirror(), but keep the space at the end
3901 p2 = skiptowhite(extra) - 1;
3902 for (p1 = extra; p1 < p2; ++p1, --p2)
3903 {
3904 t = *p1;
3905 *p1 = *p2;
3906 *p2 = t;
3907 }
3908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909#endif
3910 p_extra = extra;
3911 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003912 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 }
3914 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01003915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003917 c_final = NUL;
3918 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003919 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003920 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003921#ifdef FEAT_SYN_HL
3922 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003923 * the current line differently.
3924 * TODO: Can we use CursorLine instead of CursorLineNr
3925 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003926 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003927 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003928 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931 }
3932
Bram Moolenaar597a4222014-06-25 14:39:50 +02003933#ifdef FEAT_LINEBREAK
3934 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3935 && n_extra == 0 && *p_sbr != NUL)
3936 /* draw indent after showbreak value */
3937 draw_state = WL_BRI;
3938 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3939 /* After the showbreak, draw the breakindent */
3940 draw_state = WL_BRI - 1;
3941
3942 /* draw 'breakindent': indent wrapped text accordingly */
3943 if (draw_state == WL_BRI - 1 && n_extra == 0)
3944 {
3945 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003946 /* if need_showbreak is set, breakindent also applies */
3947 if (wp->w_p_bri && n_extra == 0
3948 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003949# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003950 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003951# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003952 )
3953 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003954 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003955# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003956 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003957 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003958 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003959# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003960 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3961 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003962 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003963# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003964 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003965# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003966 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003967 c_extra = ' ';
3968 n_extra = get_breakindent_win(wp,
3969 ml_get_buf(wp->w_buffer, lnum, FALSE));
3970 /* Correct end of highlighted area for 'breakindent',
3971 * required when 'linebreak' is also set. */
3972 if (tocol == vcol)
3973 tocol += n_extra;
3974 }
3975 }
3976#endif
3977
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3979 if (draw_state == WL_SBR - 1 && n_extra == 0)
3980 {
3981 draw_state = WL_SBR;
3982# ifdef FEAT_DIFF
3983 if (filler_todo > 0)
3984 {
3985 /* Draw "deleted" diff line(s). */
3986 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003987 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003989 c_final = NUL;
3990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01003992 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003994 c_final = NUL;
3995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996# ifdef FEAT_RIGHTLEFT
3997 if (wp->w_p_rl)
3998 n_extra = col + 1;
3999 else
4000# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004001 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004002 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 }
4004# endif
4005# ifdef FEAT_LINEBREAK
4006 if (*p_sbr != NUL && need_showbreak)
4007 {
4008 /* Draw 'showbreak' at the start of each broken line. */
4009 p_extra = p_sbr;
4010 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004011 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004013 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004015 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 /* Correct end of highlighted area for 'showbreak',
4017 * required when 'linebreak' is also set. */
4018 if (tocol == vcol)
4019 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004020#ifdef FEAT_SYN_HL
4021 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004022 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004023 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004024 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
4027# endif
4028 }
4029#endif
4030
4031 if (draw_state == WL_LINE - 1 && n_extra == 0)
4032 {
4033 draw_state = WL_LINE;
4034 if (saved_n_extra)
4035 {
4036 /* Continue item from end of wrapped line. */
4037 n_extra = saved_n_extra;
4038 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004039 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 p_extra = saved_p_extra;
4041 char_attr = saved_char_attr;
4042 }
4043 else
4044 char_attr = 0;
4045 }
4046 }
4047
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004048 // When still displaying '$' of change command, stop at cursor.
4049 // When only displaying the (relative) line number and that's done,
4050 // stop here.
4051 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004052 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053#ifdef FEAT_DIFF
4054 && filler_todo <= 0
4055#endif
4056 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004057 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004059 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004060 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004061 /* Pretend we have finished updating the window. Except when
4062 * 'cursorcolumn' is set. */
4063#ifdef FEAT_SYN_HL
4064 if (wp->w_p_cuc)
4065 row = wp->w_cline_row + wp->w_cline_height;
4066 else
4067#endif
4068 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 break;
4070 }
4071
Bram Moolenaar637532b2019-01-03 21:44:40 +01004072 if (draw_state == WL_LINE && (area_highlighting
4073#ifdef FEAT_SPELL
4074 || has_spell
4075#endif
4076 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
4078 /* handle Visual or match highlighting in this line */
4079 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4081 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004083 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 && vcol < tocol))
4085 area_attr = attr; /* start highlighting */
4086 else if (area_attr != 0
4087 && (vcol == tocol
4088 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090
4091#ifdef FEAT_SEARCH_EXTRA
4092 if (!n_extra)
4093 {
4094 /*
4095 * Check for start/end of search pattern match.
4096 * After end, check for start/end of next match.
4097 * When another match, have to check for start again.
4098 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004099 * Do this for 'search_hl' and the match list (ordered by
4100 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004102 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004103 cur = wp->w_match_head;
4104 shl_flag = FALSE;
4105 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004107 if (shl_flag == FALSE
4108 && ((cur != NULL
4109 && cur->priority > SEARCH_HL_PRIORITY)
4110 || cur == NULL))
4111 {
4112 shl = &search_hl;
4113 shl_flag = TRUE;
4114 }
4115 else
4116 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004117 if (cur != NULL)
4118 cur->pos.cur = 0;
4119 pos_inprogress = TRUE;
4120 while (shl->rm.regprog != NULL
4121 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004123 if (shl->startcol != MAXCOL
4124 && v >= (long)shl->startcol
4125 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004127 int tmp_col = v + MB_PTR2LEN(ptr);
4128
4129 if (shl->endcol < tmp_col)
4130 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004132#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004133 // Match with the "Conceal" group results in hiding
4134 // the match.
4135 if (cur != NULL
4136 && shl != &search_hl
4137 && syn_name2id((char_u *)"Conceal")
4138 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004139 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004140 has_match_conc =
4141 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004142 match_conc = cur->conceal_char;
4143 }
4144 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004145 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004148 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 {
4150 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004151 next_search_hl(wp, shl, lnum, (colnr_T)v,
4152 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004153 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004154 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156 /* Need to get the line again, a multi-line regexp
4157 * may have made it invalid. */
4158 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4159 ptr = line + v;
4160
4161 if (shl->lnum == lnum)
4162 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004163 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004165 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004167 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004169 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 {
4171 /* highlight empty match, try again after
4172 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004174 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004175 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004177 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179
4180 /* Loop to check if the match starts at the
4181 * current position */
4182 continue;
4183 }
4184 }
4185 break;
4186 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004187 if (shl != &search_hl && cur != NULL)
4188 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004190
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004191 /* Use attributes from match with highest priority among
4192 * 'search_hl' and the match list. */
4193 search_attr = search_hl.attr_cur;
4194 cur = wp->w_match_head;
4195 shl_flag = FALSE;
4196 while (cur != NULL || shl_flag == FALSE)
4197 {
4198 if (shl_flag == FALSE
4199 && ((cur != NULL
4200 && cur->priority > SEARCH_HL_PRIORITY)
4201 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004202 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004203 shl = &search_hl;
4204 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004205 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004206 else
4207 shl = &cur->hl;
4208 if (shl->attr_cur != 0)
4209 search_attr = shl->attr_cur;
4210 if (shl != &search_hl && cur != NULL)
4211 cur = cur->next;
4212 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004213 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004214 if (*ptr == NUL && (did_line_attr >= 1
4215 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004216 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218#endif
4219
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004221 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004223 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4224 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004226 if (diff_hlf == HLF_TXD && ptr - line > change_end
4227 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004229 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004230 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004231 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004234
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004235#ifdef FEAT_TEXT_PROP
4236 if (text_props != NULL)
4237 {
4238 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004239 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004240
4241 // Check if any active property ends.
4242 for (pi = 0; pi < text_props_active; ++pi)
4243 {
4244 int tpi = text_prop_idxs[pi];
4245
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004246 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004247 + text_props[tpi].tp_len)
4248 {
4249 if (pi + 1 < text_props_active)
4250 mch_memmove(text_prop_idxs + pi,
4251 text_prop_idxs + pi + 1,
4252 sizeof(int)
4253 * (text_props_active - (pi + 1)));
4254 --text_props_active;
4255 --pi;
4256 }
4257 }
4258
4259 // Add any text property that starts in this column.
4260 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004261 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004262 text_prop_idxs[text_props_active++] = text_prop_next++;
4263
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004264 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004265 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004266 if (text_props_active > 0)
4267 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004268 // Sort the properties on priority and/or starting last.
4269 // Then combine the attributes, highest priority last.
4270 current_text_props = text_props;
4271 current_buf = wp->w_buffer;
4272 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4273 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004274
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004275 for (pi = 0; pi < text_props_active; ++pi)
4276 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004277 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004278 proptype_T *pt = text_prop_type_by_id(
4279 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004280
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004281 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004282 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004283 int pt_attr = syn_id2attr(pt->pt_hl_id);
4284
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004285 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004286 text_prop_attr =
4287 hl_combine_attr(text_prop_attr, pt_attr);
4288 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004289 }
4290 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004291 }
4292 }
4293#endif
4294
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004295 /* Decide which of the highlight attributes to use. */
4296 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004297#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004298 if (area_attr != 0)
4299 char_attr = hl_combine_attr(line_attr, area_attr);
4300 else if (search_attr != 0)
4301 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004302 /* Use line_attr when not in the Visual or 'incsearch' area
4303 * (area_attr may be 0 when "noinvcur" is set). */
4304 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004305 || vcol < fromcol || vcol_prev < fromcol_prev
4306 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004307 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004308#else
4309 if (area_attr != 0)
4310 char_attr = area_attr;
4311 else if (search_attr != 0)
4312 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004313#endif
4314 else
4315 {
4316 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004317#ifdef FEAT_TEXT_PROP
4318 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004319 {
4320 if (text_prop_combine)
4321 char_attr = hl_combine_attr(
4322 syntax_attr, text_prop_attr);
4323 else
4324 char_attr = text_prop_attr;
4325 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004326 else
4327#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004328#ifdef FEAT_SYN_HL
4329 if (has_syntax)
4330 char_attr = syntax_attr;
4331 else
4332#endif
4333 char_attr = 0;
4334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 }
4336
4337 /*
4338 * Get the next character to put on the screen.
4339 */
4340 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004341 * The "p_extra" points to the extra stuff that is inserted to
4342 * represent special characters (non-printable stuff) and other
4343 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004344 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004345 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4346 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4348 */
4349 if (n_extra > 0)
4350 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004351 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004353 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004355 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 {
4357 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004358 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004359 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361 else
4362 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
4364 else
4365 {
4366 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 if (has_mbyte)
4368 {
4369 mb_c = c;
4370 if (enc_utf8)
4371 {
4372 /* If the UTF-8 character is more than one byte:
4373 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004374 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 mb_utf8 = FALSE;
4376 if (mb_l > n_extra)
4377 mb_l = 1;
4378 else if (mb_l > 1)
4379 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004380 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004382 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
4384 }
4385 else
4386 {
4387 /* if this is a DBCS character, put it in "mb_c" */
4388 mb_l = MB_BYTE2LEN(c);
4389 if (mb_l >= n_extra)
4390 mb_l = 1;
4391 else if (mb_l > 1)
4392 mb_c = (c << 8) + p_extra[1];
4393 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004394 if (mb_l == 0) /* at the NUL at end-of-line */
4395 mb_l = 1;
4396
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 /* If a double-width char doesn't fit display a '>' in the
4398 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004399 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400# ifdef FEAT_RIGHTLEFT
4401 wp->w_p_rl ? (col <= 0) :
4402# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004403 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 && (*mb_char2cells)(mb_c) == 2)
4405 {
4406 c = '>';
4407 mb_c = c;
4408 mb_l = 1;
4409 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004410 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 /* put the pointer back to output the double-width
4412 * character at the start of the next line. */
4413 ++n_extra;
4414 --p_extra;
4415 }
4416 else
4417 {
4418 n_extra -= mb_l - 1;
4419 p_extra += mb_l - 1;
4420 }
4421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 ++p_extra;
4423 }
4424 --n_extra;
4425 }
4426 else
4427 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004428#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004429 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004430#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004431
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004432 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004433 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 /*
4435 * Get a character from the line itself.
4436 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004437 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004438#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004439 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 if (has_mbyte)
4442 {
4443 mb_c = c;
4444 if (enc_utf8)
4445 {
4446 /* If the UTF-8 character is more than one byte: Decode it
4447 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004448 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 mb_utf8 = FALSE;
4450 if (mb_l > 1)
4451 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004452 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 /* Overlong encoded ASCII or ASCII with composing char
4454 * is displayed normally, except a NUL. */
4455 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004456 {
4457 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004458#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004459 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004460#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004463
4464 /* At start of the line we can have a composing char.
4465 * Draw it as a space with a composing char. */
4466 if (utf_iscomposing(mb_c))
4467 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004468 int i;
4469
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004470 for (i = Screen_mco - 1; i > 0; --i)
4471 u8cc[i] = u8cc[i - 1];
4472 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004473 mb_c = ' ';
4474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 }
4476
4477 if ((mb_l == 1 && c >= 0x80)
4478 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004479 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 {
4481 /*
4482 * Illegal UTF-8 byte: display as <xx>.
4483 * Non-BMP character : display as ? or fullwidth ?.
4484 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004485 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004486# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004487 if (wp->w_p_rl) /* reverse */
4488 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004489# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 p_extra = extra;
4491 c = *p_extra;
4492 mb_c = mb_ptr2char_adv(&p_extra);
4493 mb_utf8 = (c >= 0x80);
4494 n_extra = (int)STRLEN(p_extra);
4495 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004496 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 if (area_attr == 0 && search_attr == 0)
4498 {
4499 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004500 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 saved_attr2 = char_attr; /* save current attr */
4502 }
4503 }
4504 else if (mb_l == 0) /* at the NUL at end-of-line */
4505 mb_l = 1;
4506#ifdef FEAT_ARABIC
4507 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4508 {
4509 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004510 int pc, pc1, nc;
4511 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512
4513 /* The idea of what is the previous and next
4514 * character depends on 'rightleft'. */
4515 if (wp->w_p_rl)
4516 {
4517 pc = prev_c;
4518 pc1 = prev_c1;
4519 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004520 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 }
4522 else
4523 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004524 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004526 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528 prev_c = mb_c;
4529
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004530 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 }
4532 else
4533 prev_c = mb_c;
4534#endif
4535 }
4536 else /* enc_dbcs */
4537 {
4538 mb_l = MB_BYTE2LEN(c);
4539 if (mb_l == 0) /* at the NUL at end-of-line */
4540 mb_l = 1;
4541 else if (mb_l > 1)
4542 {
4543 /* We assume a second byte below 32 is illegal.
4544 * Hopefully this is OK for all double-byte encodings!
4545 */
4546 if (ptr[1] >= 32)
4547 mb_c = (c << 8) + ptr[1];
4548 else
4549 {
4550 if (ptr[1] == NUL)
4551 {
4552 /* head byte at end of line */
4553 mb_l = 1;
4554 transchar_nonprint(extra, c);
4555 }
4556 else
4557 {
4558 /* illegal tail byte */
4559 mb_l = 2;
4560 STRCPY(extra, "XX");
4561 }
4562 p_extra = extra;
4563 n_extra = (int)STRLEN(extra) - 1;
4564 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004565 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 c = *p_extra++;
4567 if (area_attr == 0 && search_attr == 0)
4568 {
4569 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004570 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 saved_attr2 = char_attr; /* save current attr */
4572 }
4573 mb_c = c;
4574 }
4575 }
4576 }
4577 /* If a double-width char doesn't fit display a '>' in the
4578 * last column; the character is displayed at the start of the
4579 * next line. */
4580 if ((
4581# ifdef FEAT_RIGHTLEFT
4582 wp->w_p_rl ? (col <= 0) :
4583# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004584 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 && (*mb_char2cells)(mb_c) == 2)
4586 {
4587 c = '>';
4588 mb_c = c;
4589 mb_utf8 = FALSE;
4590 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004591 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 /* Put pointer back so that the character will be
4593 * displayed at the start of the next line. */
4594 --ptr;
4595 }
4596 else if (*ptr != NUL)
4597 ptr += mb_l - 1;
4598
4599 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004600 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004601 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004602 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004605 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004606 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 c = ' ';
4608 if (area_attr == 0 && search_attr == 0)
4609 {
4610 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004611 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 saved_attr2 = char_attr; /* save current attr */
4613 }
4614 mb_c = c;
4615 mb_utf8 = FALSE;
4616 mb_l = 1;
4617 }
4618
4619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 ++ptr;
4621
4622 if (extra_check)
4623 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004624#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004625 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004626#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004627
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004628#ifdef FEAT_TERMINAL
4629 if (get_term_attr)
4630 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004631 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004632
4633 if (!attr_pri)
4634 char_attr = syntax_attr;
4635 else
4636 char_attr = hl_combine_attr(syntax_attr, char_attr);
4637 }
4638#endif
4639
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004640#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004641 // Get syntax attribute, unless still at the start of the line
4642 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004643 v = (long)(ptr - line);
4644 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 {
4646 /* Get the syntax attribute for the character. If there
4647 * is an error, disable syntax highlighting. */
4648 save_did_emsg = did_emsg;
4649 did_emsg = FALSE;
4650
Bram Moolenaar217ad922005-03-20 22:37:15 +00004651 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004652# ifdef FEAT_SPELL
4653 has_spell ? &can_spell :
4654# endif
4655 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656
4657 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004658 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004659 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004660 has_syntax = FALSE;
4661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 else
4663 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004664#ifdef SYN_TIME_LIMIT
4665 if (wp->w_s->b_syn_slow)
4666 has_syntax = FALSE;
4667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668
4669 /* Need to get the line again, a multi-line regexp may
4670 * have made it invalid. */
4671 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4672 ptr = line + v;
4673
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004674# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004675 // Text properties overrule syntax highlighting or combine.
4676 if (text_prop_attr == 0 || text_prop_combine)
4677# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004678 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004679 int comb_attr = syntax_attr;
4680# ifdef FEAT_TEXT_PROP
4681 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4682# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004683 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004684 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004685 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004686 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004687 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004688# ifdef FEAT_CONCEAL
4689 /* no concealing past the end of the line, it interferes
4690 * with line highlighting */
4691 if (c == NUL)
4692 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004693 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004694 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004695# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004697#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004698
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004699#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004700 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004701 * Only do this when there is no syntax highlighting, the
4702 * @Spell cluster is not used or the current syntax item
4703 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004704 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004705 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004706 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004707 if (c != 0 && (
4708# ifdef FEAT_SYN_HL
4709 !has_syntax ||
4710# endif
4711 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004712 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004713 char_u *prev_ptr, *p;
4714 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004715 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004716 if (has_mbyte)
4717 {
4718 prev_ptr = ptr - mb_l;
4719 v -= mb_l - 1;
4720 }
4721 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004722 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004723
4724 /* Use nextline[] if possible, it has the start of the
4725 * next line concatenated. */
4726 if ((prev_ptr - line) - nextlinecol >= 0)
4727 p = nextline + (prev_ptr - line) - nextlinecol;
4728 else
4729 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004730 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004731 len = spell_check(wp, p, &spell_hlf, &cap_col,
4732 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004733 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004734
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004735 /* In Insert mode only highlight a word that
4736 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004737 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004738 && (State & INSERT) != 0
4739 && wp->w_cursor.lnum == lnum
4740 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004741 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004742 && wp->w_cursor.col < (colnr_T)word_end)
4743 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004744 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004745 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004746 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004747
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004748 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004749 && (p - nextline) + len > nextline_idx)
4750 {
4751 /* Remember that the good word continues at the
4752 * start of the next line. */
4753 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004754 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004755 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004756
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004757 /* Turn index into actual attributes. */
4758 if (spell_hlf != HLF_COUNT)
4759 spell_attr = highlight_attr[spell_hlf];
4760
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004761 if (cap_col > 0)
4762 {
4763 if (p != prev_ptr
4764 && (p - nextline) + cap_col >= nextline_idx)
4765 {
4766 /* Remember that the word in the next line
4767 * must start with a capital. */
4768 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004769 cap_col = (int)((p - nextline) + cap_col
4770 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004771 }
4772 else
4773 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004774 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004775 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004776 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004777 }
4778 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004779 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004780 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004781 char_attr = hl_combine_attr(char_attr, spell_attr);
4782 else
4783 char_attr = hl_combine_attr(spell_attr, char_attr);
4784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785#endif
4786#ifdef FEAT_LINEBREAK
4787 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004788 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004790 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004791 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004793 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004794 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004795
Bram Moolenaar597a4222014-06-25 14:39:50 +02004796 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004797 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004798 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004799 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004800# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004801 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004802 wp->w_buffer->b_p_vts_array) - 1;
4803# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004804 n_extra = (int)wp->w_buffer->b_p_ts
4805 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004806# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004807
Bram Moolenaar4df70292015-03-21 14:20:16 +01004808 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004809 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004810 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004811 {
4812#ifdef FEAT_CONCEAL
4813 if (c == TAB)
4814 /* See "Tab alignment" below. */
4815 FIX_FOR_BOGUSCOLS;
4816#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004817 if (!wp->w_p_list)
4818 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 }
4821#endif
4822
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004823 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4824 // But not when the character is followed by a composing
4825 // character (use mb_l to check that).
4826 if (wp->w_p_list
4827 && ((((c == 160 && mb_l == 1)
4828 || (mb_utf8
4829 && ((mb_c == 160 && mb_l == 2)
4830 || (mb_c == 0x202f && mb_l == 3))))
4831 && lcs_nbsp)
4832 || (c == ' '
4833 && mb_l == 1
4834 && lcs_space
4835 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004836 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004837 c = (c == ' ') ? lcs_space : lcs_nbsp;
4838 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004839 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004840 n_attr = 1;
4841 extra_attr = HL_ATTR(HLF_8);
4842 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004843 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004844 mb_c = c;
4845 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004846 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004847 mb_utf8 = TRUE;
4848 u8cc[0] = 0;
4849 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004850 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004851 else
4852 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004853 }
4854
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4856 {
4857 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004858 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 {
4860 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004861 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 saved_attr2 = char_attr; /* save current attr */
4863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004865 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 {
4867 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004868 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004869 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
4871 else
4872 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
4874 }
4875
4876 /*
4877 * Handling of non-printable characters.
4878 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004879 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
4881 /*
4882 * when getting a character from the file, we may have to
4883 * turn it into something else on the way to putting it
4884 * into "ScreenLines".
4885 */
4886 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4887 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004888 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004889 long vcol_adjusted = vcol; /* removed showbreak length */
4890#ifdef FEAT_LINEBREAK
4891 /* only adjust the tab_len, when at the first column
4892 * after the showbreak value was drawn */
4893 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4894 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004897#ifdef FEAT_VARTABS
4898 tab_len = tabstop_padding(vcol_adjusted,
4899 wp->w_buffer->b_p_ts,
4900 wp->w_buffer->b_p_vts_array) - 1;
4901#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004902 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004903 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4904#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004905
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004906#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004907 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004908#endif
4909 /* tab amount depends on current column */
4910 n_extra = tab_len;
4911#ifdef FEAT_LINEBREAK
4912 else
4913 {
4914 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01004915 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004916 int i;
4917 int saved_nextra = n_extra;
4918
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004919#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004920 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004921 /* there are characters to conceal */
4922 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004923 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4924 */
4925 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4926 && n_extra > tab_len)
4927 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004928#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004929
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004930 /* if n_extra > 0, it gives the number of chars, to
4931 * use for a tab, else we need to calculate the width
4932 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004933 len = (tab_len * mb_char2len(lcs_tab2));
4934 if (n_extra > 0)
4935 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004936 c = lcs_tab1;
4937 p = alloc((unsigned)(len + 1));
4938 vim_memset(p, ' ', len);
4939 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004940 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004941 p_extra_free = p;
4942 for (i = 0; i < tab_len; i++)
4943 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004944 if (*p == NUL)
4945 {
4946 tab_len = i;
4947 break;
4948 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004949 mb_char2bytes(lcs_tab2, p);
4950 p += mb_char2len(lcs_tab2);
4951 n_extra += mb_char2len(lcs_tab2)
4952 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004953 }
4954 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004955#ifdef FEAT_CONCEAL
4956 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4957 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004958 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004959 n_extra -= vcol_off;
4960#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004961 }
4962#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004963#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004964 {
4965 int vc_saved = vcol_off;
4966
4967 /* Tab alignment should be identical regardless of
4968 * 'conceallevel' value. So tab compensates of all
4969 * previous concealed characters, and thus resets
4970 * vcol_off and boguscols accumulated so far in the
4971 * line. Note that the tab can be longer than
4972 * 'tabstop' when there are concealed characters. */
4973 FIX_FOR_BOGUSCOLS;
4974
4975 /* Make sure, the highlighting for the tab char will be
4976 * correctly set further below (effectively reverts the
4977 * FIX_FOR_BOGSUCOLS macro */
4978 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004979 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004980 tab_len += vc_saved;
4981 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 if (wp->w_p_list)
4985 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004986 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004987#ifdef FEAT_LINEBREAK
4988 if (wp->w_p_lbr)
4989 c_extra = NUL; /* using p_extra from above */
4990 else
4991#endif
4992 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004993 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004994 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004995 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004998 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
5000 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005001 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005002 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
5005 else
5006 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005007 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 c_extra = ' ';
5009 c = ' ';
5010 }
5011 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005012 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005013 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005014 || ((fromcol >= 0 || fromcol_prev >= 0)
5015 && tocol > vcol
5016 && VIsual_mode != Ctrl_V
5017 && (
5018# ifdef FEAT_RIGHTLEFT
5019 wp->w_p_rl ? (col >= 0) :
5020# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005021 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005022 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005023 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005024 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005025 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005027 /* Display a '$' after the line or highlight an extra
5028 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5030 /* For a diff line the highlighting continues after the
5031 * "$". */
5032 if (
5033# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005034 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035# ifdef LINE_ATTR
5036 &&
5037# endif
5038# endif
5039# ifdef LINE_ATTR
5040 line_attr == 0
5041# endif
5042 )
5043#endif
5044 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 /* In virtualedit, visual selections may extend
5046 * beyond end of line. */
5047 if (area_highlighting && virtual_active()
5048 && tocol != MAXCOL && vcol < tocol)
5049 n_extra = 0;
5050 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 {
5052 p_extra = at_end_str;
5053 n_extra = 1;
5054 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005055 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005058 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005059 c = lcs_eol;
5060 else
5061 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 lcs_eol_one = -1;
5063 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005064 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005066 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 n_attr = 1;
5068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005070 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 {
5072 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005073 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005074 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 }
5076 else
5077 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
5079 else if (c != NUL)
5080 {
5081 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005082 if (n_extra == 0)
5083 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084#ifdef FEAT_RIGHTLEFT
5085 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5086 rl_mirror(p_extra); /* reverse "<12>" */
5087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005089 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005090#ifdef FEAT_LINEBREAK
5091 if (wp->w_p_lbr)
5092 {
5093 char_u *p;
5094
5095 c = *p_extra;
5096 p = alloc((unsigned)n_extra + 1);
5097 vim_memset(p, ' ', n_extra);
5098 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5099 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005100 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005101 p_extra_free = p_extra = p;
5102 }
5103 else
5104#endif
5105 {
5106 n_extra = byte2cells(c) - 1;
5107 c = *p_extra++;
5108 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005109 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005112 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 saved_attr2 = char_attr; /* save current attr */
5114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 else if (VIsual_active
5118 && (VIsual_mode == Ctrl_V
5119 || VIsual_mode == 'v')
5120 && virtual_active()
5121 && tocol != MAXCOL
5122 && vcol < tocol
5123 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005124#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005126#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005127 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
5129 c = ' ';
5130 --ptr; /* put it back at the NUL */
5131 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005132#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 else if ((
5134# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005135 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005137# ifdef FEAT_TERMINAL
5138 term_attr != 0 ||
5139# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 ) && (
5142# ifdef FEAT_RIGHTLEFT
5143 wp->w_p_rl ? (col >= 0) :
5144# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005145 (col
5146# ifdef FEAT_CONCEAL
5147 - boguscols
5148# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005149 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {
5151 /* Highlight until the right side of the window */
5152 c = ' ';
5153 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005154
5155 /* Remember we do the char for line highlighting. */
5156 ++did_line_attr;
5157
5158 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005159 if (line_attr != 0 && char_attr == search_attr
5160 && (did_line_attr > 1
5161 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005162 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163# ifdef FEAT_DIFF
5164 if (diff_hlf == HLF_TXD)
5165 {
5166 diff_hlf = HLF_CHD;
5167 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005168 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005169 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005170 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5171 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005172 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
5175# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005176# ifdef FEAT_TERMINAL
5177 if (term_attr != 0)
5178 {
5179 char_attr = term_attr;
5180 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5181 char_attr = hl_combine_attr(char_attr,
5182 HL_ATTR(HLF_CUL));
5183 }
5184# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186#endif
5187 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005188
5189#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005190 if ( wp->w_p_cole > 0
5191 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005192 conceal_cursor_line(wp))
5193 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005194 && !(lnum_in_visual_area
5195 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005196 {
5197 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005198 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005199 && (syn_get_sub_char() != NUL || match_conc
5200 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005201 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005202 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005203 /* First time at this concealed item: display one
5204 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005205 if (match_conc)
5206 c = match_conc;
5207 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005208 c = syn_get_sub_char();
5209 else if (lcs_conceal != NUL)
5210 c = lcs_conceal;
5211 else
5212 c = ' ';
5213
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005214 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005215
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005216 if (n_extra > 0)
5217 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005218 vcol += n_extra;
5219 if (wp->w_p_wrap && n_extra > 0)
5220 {
5221# ifdef FEAT_RIGHTLEFT
5222 if (wp->w_p_rl)
5223 {
5224 col -= n_extra;
5225 boguscols -= n_extra;
5226 }
5227 else
5228# endif
5229 {
5230 boguscols += n_extra;
5231 col += n_extra;
5232 }
5233 }
5234 n_extra = 0;
5235 n_attr = 0;
5236 }
5237 else if (n_skip == 0)
5238 {
5239 is_concealing = TRUE;
5240 n_skip = 1;
5241 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005242 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005243 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005244 {
5245 mb_utf8 = TRUE;
5246 u8cc[0] = 0;
5247 c = 0xc0;
5248 }
5249 else
5250 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005251 }
5252 else
5253 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005254 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005255 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005256 }
5257#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 }
5259
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005260#ifdef FEAT_CONCEAL
5261 /* In the cursor line and we may be concealing characters: correct
5262 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005263 if (!did_wcol && draw_state == WL_LINE
5264 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005265 && conceal_cursor_line(wp)
5266 && (int)wp->w_virtcol <= vcol + n_skip)
5267 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005268# ifdef FEAT_RIGHTLEFT
5269 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005270 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005271 else
5272# endif
5273 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005274 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005275 did_wcol = TRUE;
5276 }
5277#endif
5278
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 /* Don't override visual selection highlighting. */
5280 if (n_attr > 0
5281 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005282 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 char_attr = extra_attr;
5284
Bram Moolenaar81695252004-12-29 20:58:21 +00005285#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 /* XIM don't send preedit_start and preedit_end, but they send
5287 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5288 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005289 if (p_imst == IM_ON_THE_SPOT
5290 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005291 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 && (State & INSERT)
5293 && !p_imdisable
5294 && im_is_preediting()
5295 && draw_state == WL_LINE)
5296 {
5297 colnr_T tcol;
5298
5299 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005300 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 else
5302 tcol = preedit_end_col;
5303 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5304 {
5305 if (feedback_old_attr < 0)
5306 {
5307 feedback_col = 0;
5308 feedback_old_attr = char_attr;
5309 }
5310 char_attr = im_get_feedback_attr(feedback_col);
5311 if (char_attr < 0)
5312 char_attr = feedback_old_attr;
5313 feedback_col++;
5314 }
5315 else if (feedback_old_attr >= 0)
5316 {
5317 char_attr = feedback_old_attr;
5318 feedback_old_attr = -1;
5319 feedback_col = 0;
5320 }
5321 }
5322#endif
5323 /*
5324 * Handle the case where we are in column 0 but not on the first
5325 * character of the line and the user wants us to show us a
5326 * special character (via 'listchars' option "precedes:<char>".
5327 */
5328 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005329 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5331#ifdef FEAT_DIFF
5332 && filler_todo <= 0
5333#endif
5334 && draw_state > WL_NR
5335 && c != NUL)
5336 {
5337 c = lcs_prec;
5338 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005339 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5340 {
5341 /* Double-width character being overwritten by the "precedes"
5342 * character, need to fill up half the character. */
5343 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005344 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005345 n_extra = 1;
5346 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005347 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005350 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 {
5352 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005353 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005354 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 }
5356 else
5357 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005358 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 {
5360 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005361 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 n_attr3 = 1;
5363 }
5364 }
5365
5366 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005367 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005369 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005370#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005371 || did_line_attr == 1
5372#endif
5373 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005375#ifdef FEAT_SEARCH_EXTRA
5376 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005377
5378 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005379 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005380 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005381#endif
5382
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005383 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 * highlight match at end of line. If it's beyond the last
5385 * char on the screen, just overwrite that one (tricky!) Not
5386 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005387#ifdef FEAT_SEARCH_EXTRA
5388 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005389 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005390 prevcol_hl_flag = TRUE;
5391 else
5392 {
5393 cur = wp->w_match_head;
5394 while (cur != NULL)
5395 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005396 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005397 {
5398 prevcol_hl_flag = TRUE;
5399 break;
5400 }
5401 cur = cur->next;
5402 }
5403 }
5404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005406 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005407 && (VIsual_mode != Ctrl_V
5408 || lnum == VIsual.lnum
5409 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005410 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411#ifdef FEAT_SEARCH_EXTRA
5412 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005413 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005414# ifdef FEAT_SYN_HL
5415 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5416 && !(wp == curwin && VIsual_active))
5417# endif
5418# ifdef FEAT_DIFF
5419 && diff_hlf == (hlf_T)0
5420# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005421# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005422 && did_line_attr <= 1
5423# endif
5424 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425#endif
5426 ))
5427 {
5428 int n = 0;
5429
5430#ifdef FEAT_RIGHTLEFT
5431 if (wp->w_p_rl)
5432 {
5433 if (col < 0)
5434 n = 1;
5435 }
5436 else
5437#endif
5438 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005439 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 n = -1;
5441 }
5442 if (n != 0)
5443 {
5444 /* At the window boundary, highlight the last character
5445 * instead (better than nothing). */
5446 off += n;
5447 col += n;
5448 }
5449 else
5450 {
5451 /* Add a blank character to highlight. */
5452 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 if (enc_utf8)
5454 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 }
5456#ifdef FEAT_SEARCH_EXTRA
5457 if (area_attr == 0)
5458 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005459 /* Use attributes from match with highest priority among
5460 * 'search_hl' and the match list. */
5461 char_attr = search_hl.attr;
5462 cur = wp->w_match_head;
5463 shl_flag = FALSE;
5464 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005465 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005466 if (shl_flag == FALSE
5467 && ((cur != NULL
5468 && cur->priority > SEARCH_HL_PRIORITY)
5469 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005470 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005471 shl = &search_hl;
5472 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005473 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005474 else
5475 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005476 if ((ptr - line) - 1 == (long)shl->startcol
5477 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005478 char_attr = shl->attr;
5479 if (shl != &search_hl && cur != NULL)
5480 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
5483#endif
5484 ScreenAttrs[off] = char_attr;
5485#ifdef FEAT_RIGHTLEFT
5486 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005487 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005489 --off;
5490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 else
5492#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005495 ++off;
5496 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005497 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005498#ifdef FEAT_SYN_HL
5499 eol_hl_off = 1;
5500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503
Bram Moolenaar91170f82006-05-05 21:15:17 +00005504 /*
5505 * At end of the text line.
5506 */
5507 if (c == NUL)
5508 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005509#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005510 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005511 if (wp->w_p_wrap)
5512 v = wp->w_skipcol;
5513 else
5514 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005515
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005516 /* check if line ends before left margin */
5517 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005518 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005519#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005520 // Get rid of the boguscols now, we want to draw until the right
5521 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005522 col -= boguscols;
5523 boguscols = 0;
5524#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005525
5526 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005527 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005528
5529 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005530 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5531 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005532 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005533 && lnum != wp->w_cursor.lnum)
5534 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005535# ifdef FEAT_RIGHTLEFT
5536 && !wp->w_p_rl
5537# endif
5538 )
5539 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005540 int rightmost_vcol = 0;
5541 int i;
5542
5543 if (wp->w_p_cuc)
5544 rightmost_vcol = wp->w_virtcol;
5545 if (draw_color_col)
5546 /* determine rightmost colorcolumn to possibly draw */
5547 for (i = 0; color_cols[i] >= 0; ++i)
5548 if (rightmost_vcol < color_cols[i])
5549 rightmost_vcol = color_cols[i];
5550
Bram Moolenaar02631462017-09-22 15:20:32 +02005551 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005552 {
5553 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005554 if (enc_utf8)
5555 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005556 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005557 if (draw_color_col)
5558 draw_color_col = advance_color_col(VCOL_HLC,
5559 &color_cols);
5560
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005561 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005562 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005563 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005564 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005565 else
5566 ScreenAttrs[off++] = 0;
5567
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005568 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005569 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005570
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005571 ++vcol;
5572 }
5573 }
5574#endif
5575
Bram Moolenaar53f81742017-09-22 14:35:51 +02005576 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005577 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 row++;
5579
5580 /*
5581 * Update w_cline_height and w_cline_folded if the cursor line was
5582 * updated (saves a call to plines() later).
5583 */
5584 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5585 {
5586 curwin->w_cline_row = startrow;
5587 curwin->w_cline_height = row - startrow;
5588#ifdef FEAT_FOLDING
5589 curwin->w_cline_folded = FALSE;
5590#endif
5591 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5592 }
5593
5594 break;
5595 }
5596
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005597 // Show "extends" character from 'listchars' if beyond the line end and
5598 // 'list' is set.
5599 if (lcs_ext != NUL
5600 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 && !wp->w_p_wrap
5602#ifdef FEAT_DIFF
5603 && filler_todo <= 0
5604#endif
5605 && (
5606#ifdef FEAT_RIGHTLEFT
5607 wp->w_p_rl ? col == 0 :
5608#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005609 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005611 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5613 {
5614 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005615 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005617 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 {
5619 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005620 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005621 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 }
5623 else
5624 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 }
5626
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005627#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005628 /* advance to the next 'colorcolumn' */
5629 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005630 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005631
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005632 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005633 * highlight the cursor position itself.
5634 * Also highlight the 'colorcolumn' if it is different than
5635 * 'cursorcolumn' */
5636 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005637 if (draw_state == WL_LINE && !lnum_in_visual_area
5638 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005639 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005640 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005641 && lnum != wp->w_cursor.lnum)
5642 {
5643 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005644 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005645 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005646 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005647 {
5648 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005649 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005650 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005651 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005652#endif
5653
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 /*
5655 * Store character to be displayed.
5656 * Skip characters that are left of the screen for 'nowrap'.
5657 */
5658 vcol_prev = vcol;
5659 if (draw_state < WL_LINE || n_skip <= 0)
5660 {
5661 /*
5662 * Store the character.
5663 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005664#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5666 {
5667 /* A double-wide character is: put first halve in left cell. */
5668 --off;
5669 --col;
5670 }
5671#endif
5672 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005674 {
5675 if ((mb_c & 0xff00) == 0x8e00)
5676 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 else if (enc_utf8)
5680 {
5681 if (mb_utf8)
5682 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005683 int i;
5684
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005686 if ((c & 0xff) == 0)
5687 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005688 for (i = 0; i < Screen_mco; ++i)
5689 {
5690 ScreenLinesC[i][off] = u8cc[i];
5691 if (u8cc[i] == 0)
5692 break;
5693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
5695 else
5696 ScreenLinesUC[off] = 0;
5697 }
5698 if (multi_attr)
5699 {
5700 ScreenAttrs[off] = multi_attr;
5701 multi_attr = 0;
5702 }
5703 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 ScreenAttrs[off] = char_attr;
5705
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5707 {
5708 /* Need to fill two screen columns. */
5709 ++off;
5710 ++col;
5711 if (enc_utf8)
5712 /* UTF-8: Put a 0 in the second screen char. */
5713 ScreenLines[off] = 0;
5714 else
5715 /* DBCS: Put second byte in the second screen char. */
5716 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005717 if (draw_state > WL_NR
5718#ifdef FEAT_DIFF
5719 && filler_todo <= 0
5720#endif
5721 )
5722 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 /* When "tocol" is halfway a character, set it to the end of
5724 * the character, otherwise highlighting won't stop. */
5725 if (tocol == vcol)
5726 ++tocol;
5727#ifdef FEAT_RIGHTLEFT
5728 if (wp->w_p_rl)
5729 {
5730 /* now it's time to backup one cell */
5731 --off;
5732 --col;
5733 }
5734#endif
5735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736#ifdef FEAT_RIGHTLEFT
5737 if (wp->w_p_rl)
5738 {
5739 --off;
5740 --col;
5741 }
5742 else
5743#endif
5744 {
5745 ++off;
5746 ++col;
5747 }
5748 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005749#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005750 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005751 {
5752 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005753 ++vcol_off;
5754 if (n_extra > 0)
5755 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005756 if (wp->w_p_wrap)
5757 {
5758 /*
5759 * Special voodoo required if 'wrap' is on.
5760 *
5761 * Advance the column indicator to force the line
5762 * drawing to wrap early. This will make the line
5763 * take up the same screen space when parts are concealed,
5764 * so that cursor line computations aren't messed up.
5765 *
5766 * To avoid the fictitious advance of 'col' causing
5767 * trailing junk to be written out of the screen line
5768 * we are building, 'boguscols' keeps track of the number
5769 * of bad columns we have advanced.
5770 */
5771 if (n_extra > 0)
5772 {
5773 vcol += n_extra;
5774# ifdef FEAT_RIGHTLEFT
5775 if (wp->w_p_rl)
5776 {
5777 col -= n_extra;
5778 boguscols -= n_extra;
5779 }
5780 else
5781# endif
5782 {
5783 col += n_extra;
5784 boguscols += n_extra;
5785 }
5786 n_extra = 0;
5787 n_attr = 0;
5788 }
5789
5790
Bram Moolenaar860cae12010-06-05 23:22:07 +02005791 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5792 {
5793 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005794# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005795 if (wp->w_p_rl)
5796 {
5797 --boguscols;
5798 --col;
5799 }
5800 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005801# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005802 {
5803 ++boguscols;
5804 ++col;
5805 }
5806 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005807
5808# ifdef FEAT_RIGHTLEFT
5809 if (wp->w_p_rl)
5810 {
5811 --boguscols;
5812 --col;
5813 }
5814 else
5815# endif
5816 {
5817 ++boguscols;
5818 ++col;
5819 }
5820 }
5821 else
5822 {
5823 if (n_extra > 0)
5824 {
5825 vcol += n_extra;
5826 n_extra = 0;
5827 n_attr = 0;
5828 }
5829 }
5830
5831 }
5832#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 else
5834 --n_skip;
5835
Bram Moolenaar64486672010-05-16 15:46:46 +02005836 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5837 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005838 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839#ifdef FEAT_DIFF
5840 && filler_todo <= 0
5841#endif
5842 )
5843 ++vcol;
5844
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005845#ifdef FEAT_SYN_HL
5846 if (vcol_save_attr >= 0)
5847 char_attr = vcol_save_attr;
5848#endif
5849
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 /* restore attributes after "predeces" in 'listchars' */
5851 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5852 char_attr = saved_attr3;
5853
5854 /* restore attributes after last 'listchars' or 'number' char */
5855 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5856 char_attr = saved_attr2;
5857
5858 /*
5859 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005860 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 */
5862 if ((
5863#ifdef FEAT_RIGHTLEFT
5864 wp->w_p_rl ? (col < 0) :
5865#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005866 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 && (*ptr != NUL
5868#ifdef FEAT_DIFF
5869 || filler_todo > 0
5870#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005871 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5873 )
5874 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005875#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005876 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005877 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005878 boguscols = 0;
5879#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005880 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005881 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 ++row;
5884 ++screen_row;
5885
5886 /* When not wrapping and finished diff lines, or when displayed
5887 * '$' and highlighting until last column, break here. */
5888 if ((!wp->w_p_wrap
5889#ifdef FEAT_DIFF
5890 && filler_todo <= 0
5891#endif
5892 ) || lcs_eol_one == -1)
5893 break;
5894
5895 /* When the window is too narrow draw all "@" lines. */
5896 if (draw_state != WL_LINE
5897#ifdef FEAT_DIFF
5898 && filler_todo <= 0
5899#endif
5900 )
5901 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01005902 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 row = endrow;
5905 }
5906
5907 /* When line got too long for screen break here. */
5908 if (row == endrow)
5909 {
5910 ++row;
5911 break;
5912 }
5913
5914 if (screen_cur_row == screen_row - 1
5915#ifdef FEAT_DIFF
5916 && filler_todo <= 0
5917#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005918 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 {
5920 /* Remember that the line wraps, used for modeless copy. */
5921 LineWraps[screen_row - 1] = TRUE;
5922
5923 /*
5924 * Special trick to make copy/paste of wrapped lines work with
5925 * xterm/screen: write an extra character beyond the end of
5926 * the line. This will work with all terminal types
5927 * (regardless of the xn,am settings).
5928 * Only do this on a fast tty.
5929 * Only do this if the cursor is on the current line
5930 * (something has been written in it).
5931 * Don't do this for the GUI.
5932 * Don't do this for double-width characters.
5933 * Don't do this for a window not at the right screen border.
5934 */
5935 if (p_tf
5936#ifdef FEAT_GUI
5937 && !gui.in_use
5938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005940 && ((*mb_off2cells)(LineOffset[screen_row],
5941 LineOffset[screen_row] + screen_Columns)
5942 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005944 + (int)Columns - 2,
5945 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01005946 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 {
5948 /* First make sure we are at the end of the screen line,
5949 * then output the same character again to let the
5950 * terminal know about the wrap. If the terminal doesn't
5951 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005952 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 screen_char(LineOffset[screen_row - 1]
5954 + (unsigned)Columns - 1,
5955 screen_row - 1, (int)(Columns - 1));
5956
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 /* When there is a multi-byte character, just output a
5958 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005959 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5960 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 out_char(' ');
5962 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 out_char(ScreenLines[LineOffset[screen_row - 1]
5964 + (Columns - 1)]);
5965 /* force a redraw of the first char on the next line */
5966 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5967 screen_start(); /* don't know where cursor is now */
5968 }
5969 }
5970
5971 col = 0;
5972 off = (unsigned)(current_ScreenLine - ScreenLines);
5973#ifdef FEAT_RIGHTLEFT
5974 if (wp->w_p_rl)
5975 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005976 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 off += col;
5978 }
5979#endif
5980
5981 /* reset the drawing state for the start of a wrapped line */
5982 draw_state = WL_START;
5983 saved_n_extra = n_extra;
5984 saved_p_extra = p_extra;
5985 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005986 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 saved_char_attr = char_attr;
5988 n_extra = 0;
5989 lcs_prec_todo = lcs_prec;
5990#ifdef FEAT_LINEBREAK
5991# ifdef FEAT_DIFF
5992 if (filler_todo <= 0)
5993# endif
5994 need_showbreak = TRUE;
5995#endif
5996#ifdef FEAT_DIFF
5997 --filler_todo;
5998 /* When the filler lines are actually below the last line of the
5999 * file, don't draw the line itself, break here. */
6000 if (filler_todo == 0 && wp->w_botfill)
6001 break;
6002#endif
6003 }
6004
6005 } /* for every character in the line */
6006
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006007#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006008 /* After an empty line check first word for capital. */
6009 if (*skipwhite(line) == NUL)
6010 {
6011 capcol_lnum = lnum + 1;
6012 cap_col = 0;
6013 }
6014#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006015#ifdef FEAT_TEXT_PROP
6016 vim_free(text_props);
6017 vim_free(text_prop_idxs);
6018#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006019
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006020 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 return row;
6022}
6023
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006024/*
6025 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006026 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006027 */
6028 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006029comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006030{
6031 int i;
6032
6033 for (i = 0; i < Screen_mco; ++i)
6034 {
6035 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6036 return TRUE;
6037 if (ScreenLinesC[i][off_from] == 0)
6038 break;
6039 }
6040 return FALSE;
6041}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006042
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043/*
6044 * Check whether the given character needs redrawing:
6045 * - the (first byte of the) character is different
6046 * - the attributes are different
6047 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006048 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 */
6050 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006051char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052{
6053 if (cols > 0
6054 && ((ScreenLines[off_from] != ScreenLines[off_to]
6055 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 || (enc_dbcs != 0
6057 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6058 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6059 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6060 : (cols > 1 && ScreenLines[off_from + 1]
6061 != ScreenLines[off_to + 1])))
6062 || (enc_utf8
6063 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6064 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006065 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006066 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6067 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006068 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 return TRUE;
6070 return FALSE;
6071}
6072
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006073#if defined(FEAT_TERMINAL) || defined(PROTO)
6074/*
6075 * Return the index in ScreenLines[] for the current screen line.
6076 */
6077 int
6078screen_get_current_line_off()
6079{
6080 return (int)(current_ScreenLine - ScreenLines);
6081}
6082#endif
6083
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084/*
6085 * Move one "cooked" screen line to the screen, but only the characters that
6086 * have actually changed. Handle insert/delete character.
6087 * "coloff" gives the first column on the screen for this line.
6088 * "endcol" gives the columns where valid characters are.
6089 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6090 * needs to be cleared, negative otherwise.
6091 * "rlflag" is TRUE in a rightleft window:
6092 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6093 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6094 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006095 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006096screen_line(
6097 int row,
6098 int coloff,
6099 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006100 int clear_width,
6101 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102{
6103 unsigned off_from;
6104 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006105 unsigned max_off_from;
6106 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 int force = FALSE; /* force update rest of the line */
6110 int redraw_this /* bool: does character need redraw? */
6111#ifdef FEAT_GUI
6112 = TRUE /* For GUI when while-loop empty */
6113#endif
6114 ;
6115 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 int clear_next = FALSE;
6117 int char_cells; /* 1: normal char */
6118 /* 2: occupies two display cells */
6119# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006121 /* Check for illegal row and col, just in case. */
6122 if (row >= Rows)
6123 row = Rows - 1;
6124 if (endcol > Columns)
6125 endcol = Columns;
6126
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127# ifdef FEAT_CLIPBOARD
6128 clip_may_clear_selection(row, row);
6129# endif
6130
6131 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6132 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006133 max_off_from = off_from + screen_Columns;
6134 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135
6136#ifdef FEAT_RIGHTLEFT
6137 if (rlflag)
6138 {
6139 /* Clear rest first, because it's left of the text. */
6140 if (clear_width > 0)
6141 {
6142 while (col <= endcol && ScreenLines[off_to] == ' '
6143 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006144 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 {
6146 ++off_to;
6147 ++col;
6148 }
6149 if (col <= endcol)
6150 screen_fill(row, row + 1, col + coloff,
6151 endcol + coloff + 1, ' ', ' ', 0);
6152 }
6153 col = endcol + 1;
6154 off_to = LineOffset[row] + col + coloff;
6155 off_from += col;
6156 endcol = (clear_width > 0 ? clear_width : -clear_width);
6157 }
6158#endif /* FEAT_RIGHTLEFT */
6159
6160 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6161
6162 while (col < endcol)
6163 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006165 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 else
6167 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168
6169 redraw_this = redraw_next;
6170 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6171 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6172
6173#ifdef FEAT_GUI
6174 /* If the next character was bold, then redraw the current character to
6175 * remove any pixels that might have spilt over into us. This only
6176 * happens in the GUI.
6177 */
6178 if (redraw_next && gui.in_use)
6179 {
6180 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006181 if (hl > HL_ALL)
6182 hl = syn_attr2attr(hl);
6183 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 redraw_this = TRUE;
6185 }
6186#endif
6187
6188 if (redraw_this)
6189 {
6190 /*
6191 * Special handling when 'xs' termcap flag set (hpterm):
6192 * Attributes for characters are stored at the position where the
6193 * cursor is when writing the highlighting code. The
6194 * start-highlighting code must be written with the cursor on the
6195 * first highlighted character. The stop-highlighting code must
6196 * be written with the cursor just after the last highlighted
6197 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006198 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 * to clear the rest of the line, and force redrawing it
6200 * completely.
6201 */
6202 if ( p_wiv
6203 && !force
6204#ifdef FEAT_GUI
6205 && !gui.in_use
6206#endif
6207 && ScreenAttrs[off_to] != 0
6208 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6209 {
6210 /*
6211 * Need to remove highlighting attributes here.
6212 */
6213 windgoto(row, col + coloff);
6214 out_str(T_CE); /* clear rest of this screen line */
6215 screen_start(); /* don't know where cursor is now */
6216 force = TRUE; /* force redraw of rest of the line */
6217 redraw_next = TRUE; /* or else next char would miss out */
6218
6219 /*
6220 * If the previous character was highlighted, need to stop
6221 * highlighting at this character.
6222 */
6223 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6224 {
6225 screen_attr = ScreenAttrs[off_to - 1];
6226 term_windgoto(row, col + coloff);
6227 screen_stop_highlight();
6228 }
6229 else
6230 screen_attr = 0; /* highlighting has stopped */
6231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 if (enc_dbcs != 0)
6233 {
6234 /* Check if overwriting a double-byte with a single-byte or
6235 * the other way around requires another character to be
6236 * redrawn. For UTF-8 this isn't needed, because comparing
6237 * ScreenLinesUC[] is sufficient. */
6238 if (char_cells == 1
6239 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006240 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 {
6242 /* Writing a single-cell character over a double-cell
6243 * character: need to redraw the next cell. */
6244 ScreenLines[off_to + 1] = 0;
6245 redraw_next = TRUE;
6246 }
6247 else if (char_cells == 2
6248 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006249 && (*mb_off2cells)(off_to, max_off_to) == 1
6250 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 {
6252 /* Writing the second half of a double-cell character over
6253 * a double-cell character: need to redraw the second
6254 * cell. */
6255 ScreenLines[off_to + 2] = 0;
6256 redraw_next = TRUE;
6257 }
6258
6259 if (enc_dbcs == DBCS_JPNU)
6260 ScreenLines2[off_to] = ScreenLines2[off_from];
6261 }
6262 /* When writing a single-width character over a double-width
6263 * character and at the end of the redrawn text, need to clear out
6264 * the right halve of the old character.
6265 * Also required when writing the right halve of a double-width
6266 * char over the left halve of an existing one. */
6267 if (has_mbyte && col + char_cells == endcol
6268 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006269 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006271 && (*mb_off2cells)(off_to, max_off_to) == 1
6272 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274
6275 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 if (enc_utf8)
6277 {
6278 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6279 if (ScreenLinesUC[off_from] != 0)
6280 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006281 int i;
6282
6283 for (i = 0; i < Screen_mco; ++i)
6284 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 }
6286 }
6287 if (char_cells == 2)
6288 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289
6290#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006291 /* The bold trick makes a single column of pixels appear in the
6292 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 * character should be redrawn too. This happens for our own GUI
6294 * and for some xterms. */
6295 if (
6296# ifdef FEAT_GUI
6297 gui.in_use
6298# endif
6299# if defined(FEAT_GUI) && defined(UNIX)
6300 ||
6301# endif
6302# ifdef UNIX
6303 term_is_xterm
6304# endif
6305 )
6306 {
6307 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006308 if (hl > HL_ALL)
6309 hl = syn_attr2attr(hl);
6310 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 redraw_next = TRUE;
6312 }
6313#endif
6314 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006315
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006316 /* For simplicity set the attributes of second half of a
6317 * double-wide character equal to the first half. */
6318 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006320
6321 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 screen_char(off_to, row, col + coloff);
6325 }
6326 else if ( p_wiv
6327#ifdef FEAT_GUI
6328 && !gui.in_use
6329#endif
6330 && col + coloff > 0)
6331 {
6332 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6333 {
6334 /*
6335 * Don't output stop-highlight when moving the cursor, it will
6336 * stop the highlighting when it should continue.
6337 */
6338 screen_attr = 0;
6339 }
6340 else if (screen_attr != 0)
6341 screen_stop_highlight();
6342 }
6343
6344 off_to += CHAR_CELLS;
6345 off_from += CHAR_CELLS;
6346 col += CHAR_CELLS;
6347 }
6348
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 if (clear_next)
6350 {
6351 /* Clear the second half of a double-wide character of which the left
6352 * half was overwritten with a single-wide character. */
6353 ScreenLines[off_to] = ' ';
6354 if (enc_utf8)
6355 ScreenLinesUC[off_to] = 0;
6356 screen_char(off_to, row, col + coloff);
6357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358
6359 if (clear_width > 0
6360#ifdef FEAT_RIGHTLEFT
6361 && !rlflag
6362#endif
6363 )
6364 {
6365#ifdef FEAT_GUI
6366 int startCol = col;
6367#endif
6368
6369 /* blank out the rest of the line */
6370 while (col < clear_width && ScreenLines[off_to] == ' '
6371 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006372 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 {
6374 ++off_to;
6375 ++col;
6376 }
6377 if (col < clear_width)
6378 {
6379#ifdef FEAT_GUI
6380 /*
6381 * In the GUI, clearing the rest of the line may leave pixels
6382 * behind if the first character cleared was bold. Some bold
6383 * fonts spill over the left. In this case we redraw the previous
6384 * character too. If we didn't skip any blanks above, then we
6385 * only redraw if the character wasn't already redrawn anyway.
6386 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006387 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 {
6389 hl = ScreenAttrs[off_to];
6390 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006391 {
6392 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006393
Bram Moolenaar9c697322006-10-09 20:11:17 +00006394 if (enc_utf8)
6395 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6396 * that its width is 2. */
6397 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6398 else if (enc_dbcs != 0)
6399 {
6400 /* find previous character by counting from first
6401 * column and get its width. */
6402 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006403 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006404
6405 while (off < off_to)
6406 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006407 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006408 off += prev_cells;
6409 }
6410 }
6411
6412 if (enc_dbcs != 0 && prev_cells > 1)
6413 screen_char_2(off_to - prev_cells, row,
6414 col + coloff - prev_cells);
6415 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006416 screen_char(off_to - prev_cells, row,
6417 col + coloff - prev_cells);
6418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 }
6420#endif
6421 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6422 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 off_to += clear_width - col;
6424 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 }
6426 }
6427
6428 if (clear_width > 0)
6429 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 /* For a window that's left of another, draw the separator char. */
6431 if (col + coloff < Columns)
6432 {
6433 int c;
6434
6435 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006436 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006437 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6438 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 || ScreenAttrs[off_to] != hl)
6440 {
6441 ScreenLines[off_to] = c;
6442 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 if (enc_utf8)
6444 {
6445 if (c >= 0x80)
6446 {
6447 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006448 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 }
6450 else
6451 ScreenLinesUC[off_to] = 0;
6452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 screen_char(off_to, row, col + coloff);
6454 }
6455 }
6456 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 LineWraps[row] = FALSE;
6458 }
6459}
6460
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006461#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006463 * Mirror text "str" for right-left displaying.
6464 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006466 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006467rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
6469 char_u *p1, *p2;
6470 int t;
6471
6472 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6473 {
6474 t = *p1;
6475 *p1 = *p2;
6476 *p2 = t;
6477 }
6478}
6479#endif
6480
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481/*
6482 * mark all status lines for redraw; used after first :cd
6483 */
6484 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006485status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486{
6487 win_T *wp;
6488
Bram Moolenaar29323592016-07-24 22:04:11 +02006489 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 if (wp->w_status_height)
6491 {
6492 wp->w_redr_status = TRUE;
6493 redraw_later(VALID);
6494 }
6495}
6496
6497/*
6498 * mark all status lines of the current buffer for redraw
6499 */
6500 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006501status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
6503 win_T *wp;
6504
Bram Moolenaar29323592016-07-24 22:04:11 +02006505 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6507 {
6508 wp->w_redr_status = TRUE;
6509 redraw_later(VALID);
6510 }
6511}
6512
6513/*
6514 * Redraw all status lines that need to be redrawn.
6515 */
6516 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006517redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518{
6519 win_T *wp;
6520
Bram Moolenaar29323592016-07-24 22:04:11 +02006521 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006523 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006524 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006525 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527
Bram Moolenaar4033c552017-09-16 20:54:51 +02006528#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529/*
6530 * Redraw all status lines at the bottom of frame "frp".
6531 */
6532 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006533win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534{
6535 if (frp->fr_layout == FR_LEAF)
6536 frp->fr_win->w_redr_status = TRUE;
6537 else if (frp->fr_layout == FR_ROW)
6538 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006539 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 win_redraw_last_status(frp);
6541 }
6542 else /* frp->fr_layout == FR_COL */
6543 {
6544 frp = frp->fr_child;
6545 while (frp->fr_next != NULL)
6546 frp = frp->fr_next;
6547 win_redraw_last_status(frp);
6548 }
6549}
6550#endif
6551
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552/*
6553 * Draw the verticap separator right of window "wp" starting with line "row".
6554 */
6555 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006556draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557{
6558 int hl;
6559 int c;
6560
6561 if (wp->w_vsep_width)
6562 {
6563 /* draw the vertical separator right of this window */
6564 c = fillchar_vsep(&hl);
6565 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6566 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6567 c, ' ', hl);
6568 }
6569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570
6571#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006572static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573
6574/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006575 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 */
6577 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006578status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579{
6580 int len = 0;
6581
6582#ifdef FEAT_MENU
6583 int emenu = (xp->xp_context == EXPAND_MENUS
6584 || xp->xp_context == EXPAND_MENUNAMES);
6585
6586 /* Check for menu separators - replace with '|'. */
6587 if (emenu && menu_is_separator(s))
6588 return 1;
6589#endif
6590
6591 while (*s != NUL)
6592 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006593 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006594 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006595 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 }
6597
6598 return len;
6599}
6600
6601/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006602 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006603 * These are backslashes used for escaping. Do show backslashes in help tags.
6604 */
6605 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006606skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006607{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006608 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006609#ifdef FEAT_MENU
6610 || ((xp->xp_context == EXPAND_MENUS
6611 || xp->xp_context == EXPAND_MENUNAMES)
6612 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6613#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006614 )
6615 {
6616#ifndef BACKSLASH_IN_FILENAME
6617 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6618 return 2;
6619#endif
6620 return 1;
6621 }
6622 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006623}
6624
6625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 * Show wildchar matches in the status line.
6627 * Show at least the "match" item.
6628 * We start at item 'first_match' in the list and show all matches that fit.
6629 *
6630 * If inversion is possible we use it. Else '=' characters are used.
6631 */
6632 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006633win_redr_status_matches(
6634 expand_T *xp,
6635 int num_matches,
6636 char_u **matches, /* list of matches */
6637 int match,
6638 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639{
6640#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6641 int row;
6642 char_u *buf;
6643 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006644 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 int fillchar;
6646 int attr;
6647 int i;
6648 int highlight = TRUE;
6649 char_u *selstart = NULL;
6650 int selstart_col = 0;
6651 char_u *selend = NULL;
6652 static int first_match = 0;
6653 int add_left = FALSE;
6654 char_u *s;
6655#ifdef FEAT_MENU
6656 int emenu;
6657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659
6660 if (matches == NULL) /* interrupted completion? */
6661 return;
6662
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006663 if (has_mbyte)
6664 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6665 else
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006666 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 if (buf == NULL)
6668 return;
6669
6670 if (match == -1) /* don't show match but original text */
6671 {
6672 match = 0;
6673 highlight = FALSE;
6674 }
6675 /* count 1 for the ending ">" */
6676 clen = status_match_len(xp, L_MATCH(match)) + 3;
6677 if (match == 0)
6678 first_match = 0;
6679 else if (match < first_match)
6680 {
6681 /* jumping left, as far as we can go */
6682 first_match = match;
6683 add_left = TRUE;
6684 }
6685 else
6686 {
6687 /* check if match fits on the screen */
6688 for (i = first_match; i < match; ++i)
6689 clen += status_match_len(xp, L_MATCH(i)) + 2;
6690 if (first_match > 0)
6691 clen += 2;
6692 /* jumping right, put match at the left */
6693 if ((long)clen > Columns)
6694 {
6695 first_match = match;
6696 /* if showing the last match, we can add some on the left */
6697 clen = 2;
6698 for (i = match; i < num_matches; ++i)
6699 {
6700 clen += status_match_len(xp, L_MATCH(i)) + 2;
6701 if ((long)clen >= Columns)
6702 break;
6703 }
6704 if (i == num_matches)
6705 add_left = TRUE;
6706 }
6707 }
6708 if (add_left)
6709 while (first_match > 0)
6710 {
6711 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6712 if ((long)clen >= Columns)
6713 break;
6714 --first_match;
6715 }
6716
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006717 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718
6719 if (first_match == 0)
6720 {
6721 *buf = NUL;
6722 len = 0;
6723 }
6724 else
6725 {
6726 STRCPY(buf, "< ");
6727 len = 2;
6728 }
6729 clen = len;
6730
6731 i = first_match;
6732 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6733 {
6734 if (i == match)
6735 {
6736 selstart = buf + len;
6737 selstart_col = clen;
6738 }
6739
6740 s = L_MATCH(i);
6741 /* Check for menu separators - replace with '|' */
6742#ifdef FEAT_MENU
6743 emenu = (xp->xp_context == EXPAND_MENUS
6744 || xp->xp_context == EXPAND_MENUNAMES);
6745 if (emenu && menu_is_separator(s))
6746 {
6747 STRCPY(buf + len, transchar('|'));
6748 l = (int)STRLEN(buf + len);
6749 len += l;
6750 clen += l;
6751 }
6752 else
6753#endif
6754 for ( ; *s != NUL; ++s)
6755 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006756 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006758 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 {
6760 STRNCPY(buf + len, s, l);
6761 s += l - 1;
6762 len += l;
6763 }
6764 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 {
6766 STRCPY(buf + len, transchar_byte(*s));
6767 len += (int)STRLEN(buf + len);
6768 }
6769 }
6770 if (i == match)
6771 selend = buf + len;
6772
6773 *(buf + len++) = ' ';
6774 *(buf + len++) = ' ';
6775 clen += 2;
6776 if (++i == num_matches)
6777 break;
6778 }
6779
6780 if (i != num_matches)
6781 {
6782 *(buf + len++) = '>';
6783 ++clen;
6784 }
6785
6786 buf[len] = NUL;
6787
6788 row = cmdline_row - 1;
6789 if (row >= 0)
6790 {
6791 if (wild_menu_showing == 0)
6792 {
6793 if (msg_scrolled > 0)
6794 {
6795 /* Put the wildmenu just above the command line. If there is
6796 * no room, scroll the screen one line up. */
6797 if (cmdline_row == Rows - 1)
6798 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006799 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 ++msg_scrolled;
6801 }
6802 else
6803 {
6804 ++cmdline_row;
6805 ++row;
6806 }
6807 wild_menu_showing = WM_SCROLLED;
6808 }
6809 else
6810 {
6811 /* Create status line if needed by setting 'laststatus' to 2.
6812 * Set 'winminheight' to zero to avoid that the window is
6813 * resized. */
6814 if (lastwin->w_status_height == 0)
6815 {
6816 save_p_ls = p_ls;
6817 save_p_wmh = p_wmh;
6818 p_ls = 2;
6819 p_wmh = 0;
6820 last_status(FALSE);
6821 }
6822 wild_menu_showing = WM_SHOWN;
6823 }
6824 }
6825
6826 screen_puts(buf, row, 0, attr);
6827 if (selstart != NULL && highlight)
6828 {
6829 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006830 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 }
6832
6833 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6834 }
6835
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 vim_free(buf);
6838}
6839#endif
6840
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841/*
6842 * Redraw the status line of window wp.
6843 *
6844 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006845 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6846 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006848 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006849win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850{
6851 int row;
6852 char_u *p;
6853 int len;
6854 int fillchar;
6855 int attr;
6856 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006857 static int busy = FALSE;
6858
6859 /* It's possible to get here recursively when 'statusline' (indirectly)
6860 * invokes ":redrawstatus". Simply ignore the call then. */
6861 if (busy)
6862 return;
6863 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864
6865 wp->w_redr_status = FALSE;
6866 if (wp->w_status_height == 0)
6867 {
6868 /* no status line, can only be last window */
6869 redraw_cmdline = TRUE;
6870 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006871 else if (!redrawing()
6872#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006873 // don't update status line when popup menu is visible and may be
6874 // drawn over it, unless it will be redrawn later
6875 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006876#endif
6877 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {
6879 /* Don't redraw right now, do it later. */
6880 wp->w_redr_status = TRUE;
6881 }
6882#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006883 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 {
6885 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006886 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 }
6888#endif
6889 else
6890 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006891 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006893 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 p = NameBuff;
6895 len = (int)STRLEN(p);
6896
Bram Moolenaard85f2712017-07-28 21:51:57 +02006897 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898#ifdef FEAT_QUICKFIX
6899 || wp->w_p_pvw
6900#endif
6901 || bufIsChanged(wp->w_buffer)
6902 || wp->w_buffer->b_p_ro)
6903 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006904 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006906 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 len += (int)STRLEN(p + len);
6908 }
6909#ifdef FEAT_QUICKFIX
6910 if (wp->w_p_pvw)
6911 {
6912 STRCPY(p + len, _("[Preview]"));
6913 len += (int)STRLEN(p + len);
6914 }
6915#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006916 if (bufIsChanged(wp->w_buffer)
6917#ifdef FEAT_TERMINAL
6918 && !bt_terminal(wp->w_buffer)
6919#endif
6920 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 {
6922 STRCPY(p + len, "[+]");
6923 len += 3;
6924 }
6925 if (wp->w_buffer->b_p_ro)
6926 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006927 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006928 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 }
6930
Bram Moolenaar02631462017-09-22 15:20:32 +02006931 this_ru_col = ru_col - (Columns - wp->w_width);
6932 if (this_ru_col < (wp->w_width + 1) / 2)
6933 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 if (this_ru_col <= 1)
6935 {
6936 p = (char_u *)"<"; /* No room for file name! */
6937 len = 1;
6938 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01006939 else if (has_mbyte)
6940 {
6941 int clen = 0, i;
6942
6943 /* Count total number of display cells. */
6944 clen = mb_string2cells(p, -1);
6945
6946 /* Find first character that will fit.
6947 * Going from start to end is much faster for DBCS. */
6948 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
6949 i += (*mb_ptr2len)(p + i))
6950 clen -= (*mb_ptr2cells)(p + i);
6951 len = clen;
6952 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01006954 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01006956 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 }
6958
Bram Moolenaara12a1612019-01-24 16:39:02 +01006959 }
6960 else if (len > this_ru_col - 1)
6961 {
6962 p += len - (this_ru_col - 1);
6963 *p = '<';
6964 len = this_ru_col - 1;
6965 }
6966
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006968 screen_puts(p, row, wp->w_wincol, attr);
6969 screen_fill(row, row + 1, len + wp->w_wincol,
6970 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006972 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6974 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006975 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976
6977#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02006978 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979#endif
6980 }
6981
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 /*
6983 * May need to draw the character below the vertical separator.
6984 */
6985 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6986 {
6987 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006988 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 else
6990 fillchar = fillchar_vsep(&attr);
6991 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6992 attr);
6993 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006994 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995}
6996
Bram Moolenaar238a5642006-02-21 22:12:05 +00006997#ifdef FEAT_STL_OPT
6998/*
6999 * Redraw the status line according to 'statusline' and take care of any
7000 * errors encountered.
7001 */
7002 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007003redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007004{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007005 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007006 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007007
7008 /* When called recursively return. This can happen when the statusline
7009 * contains an expression that triggers a redraw. */
7010 if (entered)
7011 return;
7012 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007013
Bram Moolenaara742e082016-04-05 21:10:38 +02007014 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007015 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007016 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007017 {
7018 /* When there is an error disable the statusline, otherwise the
7019 * display is messed up with errors and a redraw triggers the problem
7020 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007021 set_string_option_direct((char_u *)"statusline", -1,
7022 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007023 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007024 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007025 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007026 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007027}
7028#endif
7029
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030/*
7031 * Return TRUE if the status line of window "wp" is connected to the status
7032 * line of the window right of it. If not, then it's a vertical separator.
7033 * Only call if (wp->w_vsep_width != 0).
7034 */
7035 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007036stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037{
7038 frame_T *fr;
7039
7040 fr = wp->w_frame;
7041 while (fr->fr_parent != NULL)
7042 {
7043 if (fr->fr_parent->fr_layout == FR_COL)
7044 {
7045 if (fr->fr_next != NULL)
7046 break;
7047 }
7048 else
7049 {
7050 if (fr->fr_next != NULL)
7051 return TRUE;
7052 }
7053 fr = fr->fr_parent;
7054 }
7055 return FALSE;
7056}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059/*
7060 * Get the value to show for the language mappings, active 'keymap'.
7061 */
7062 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007063get_keymap_str(
7064 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007065 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007066 char_u *buf, /* buffer for the result */
7067 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068{
7069 char_u *p;
7070
7071 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7072 return FALSE;
7073
7074 {
7075#ifdef FEAT_EVAL
7076 buf_T *old_curbuf = curbuf;
7077 win_T *old_curwin = curwin;
7078 char_u *s;
7079
7080 curbuf = wp->w_buffer;
7081 curwin = wp;
7082 STRCPY(buf, "b:keymap_name"); /* must be writable */
7083 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007084 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 --emsg_skip;
7086 curbuf = old_curbuf;
7087 curwin = old_curwin;
7088 if (p == NULL || *p == NUL)
7089#endif
7090 {
7091#ifdef FEAT_KEYMAP
7092 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7093 p = wp->w_buffer->b_p_keymap;
7094 else
7095#endif
7096 p = (char_u *)"lang";
7097 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007098 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 buf[0] = NUL;
7100#ifdef FEAT_EVAL
7101 vim_free(s);
7102#endif
7103 }
7104 return buf[0] != NUL;
7105}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
7107#if defined(FEAT_STL_OPT) || defined(PROTO)
7108/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007109 * Redraw the status line or ruler of window "wp".
7110 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 */
7112 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007113win_redr_custom(
7114 win_T *wp,
7115 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007117 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 int attr;
7119 int curattr;
7120 int row;
7121 int col = 0;
7122 int maxwidth;
7123 int width;
7124 int n;
7125 int len;
7126 int fillchar;
7127 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007128 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007130 struct stl_hlrec hltab[STL_MAX_ITEM];
7131 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007132 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007133 win_T *ewp;
7134 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135
Bram Moolenaar1d633412013-12-11 15:52:01 +01007136 /* There is a tiny chance that this gets called recursively: When
7137 * redrawing a status line triggers redrawing the ruler or tabline.
7138 * Avoid trouble by not allowing recursion. */
7139 if (entered)
7140 return;
7141 entered = TRUE;
7142
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007144 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007146 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007147 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007148 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007149 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007150 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007151 maxwidth = Columns;
7152# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007153 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007154# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007156 else
7157 {
7158 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007159 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007160 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007161
7162 if (draw_ruler)
7163 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007164 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007165 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007166 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007167 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007168 if (*++stl == '-')
7169 stl++;
7170 if (atoi((char *)stl))
7171 while (VIM_ISDIGIT(*stl))
7172 stl++;
7173 if (*stl++ != '(')
7174 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007175 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007176 col = ru_col - (Columns - wp->w_width);
7177 if (col < (wp->w_width + 1) / 2)
7178 col = (wp->w_width + 1) / 2;
7179 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007180 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007181 {
7182 row = Rows - 1;
7183 --maxwidth; /* writing in last column may cause scrolling */
7184 fillchar = ' ';
7185 attr = 0;
7186 }
7187
7188# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007189 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007190# endif
7191 }
7192 else
7193 {
7194 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007195 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007196 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007197 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007198# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007199 use_sandbox = was_set_insecurely((char_u *)"statusline",
7200 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007201# endif
7202 }
7203
Bram Moolenaar53f81742017-09-22 14:35:51 +02007204 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007205 }
7206
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007208 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209
Bram Moolenaar61452852011-02-01 18:01:11 +01007210 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7211 * the cursor away and back. */
7212 ewp = wp == NULL ? curwin : wp;
7213 p_crb_save = ewp->w_p_crb;
7214 ewp->w_p_crb = FALSE;
7215
Bram Moolenaar362f3562009-11-03 16:20:34 +00007216 /* Make a copy, because the statusline may include a function call that
7217 * might change the option value and free the memory. */
7218 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007219 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007220 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007221 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007222 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007223 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007225 /* Make all characters printable. */
7226 p = transstr(buf);
7227 if (p != NULL)
7228 {
7229 vim_strncpy(buf, p, sizeof(buf) - 1);
7230 vim_free(p);
7231 }
7232
7233 /* fill up with "fillchar" */
7234 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007235 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 ++width;
7239 }
7240 buf[len] = NUL;
7241
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007242 /*
7243 * Draw each snippet with the specified highlighting.
7244 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 curattr = attr;
7246 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007247 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007249 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 screen_puts_len(p, len, row, col, curattr);
7251 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007252 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007254 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007256 else if (hltab[n].userhl < 0)
7257 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007258#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007259 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7260 && wp->w_status_height != 0)
7261 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007262 else if (wp != NULL && bt_terminal(wp->w_buffer)
7263 && wp->w_status_height != 0)
7264 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007265#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007266 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007267 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007269 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 }
7271 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007272
7273 if (wp == NULL)
7274 {
7275 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7276 col = 0;
7277 len = 0;
7278 p = buf;
7279 fillchar = 0;
7280 for (n = 0; tabtab[n].start != NULL; n++)
7281 {
7282 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7283 while (col < len)
7284 TabPageIdxs[col++] = fillchar;
7285 p = tabtab[n].start;
7286 fillchar = tabtab[n].userhl;
7287 }
7288 while (col < Columns)
7289 TabPageIdxs[col++] = fillchar;
7290 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007291
7292theend:
7293 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294}
7295
7296#endif /* FEAT_STL_OPT */
7297
7298/*
7299 * Output a single character directly to the screen and update ScreenLines.
7300 */
7301 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007302screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 char_u buf[MB_MAXBYTES + 1];
7305
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007306 if (has_mbyte)
7307 buf[(*mb_char2bytes)(c, buf)] = NUL;
7308 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007309 {
7310 buf[0] = c;
7311 buf[1] = NUL;
7312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 screen_puts(buf, row, col, attr);
7314}
7315
7316/*
7317 * Get a single character directly from ScreenLines into "bytes[]".
7318 * Also return its attribute in *attrp;
7319 */
7320 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007321screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322{
7323 unsigned off;
7324
7325 /* safety check */
7326 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7327 {
7328 off = LineOffset[row] + col;
7329 *attrp = ScreenAttrs[off];
7330 bytes[0] = ScreenLines[off];
7331 bytes[1] = NUL;
7332
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 if (enc_utf8 && ScreenLinesUC[off] != 0)
7334 bytes[utfc_char2bytes(off, bytes)] = NUL;
7335 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7336 {
7337 bytes[0] = ScreenLines[off];
7338 bytes[1] = ScreenLines2[off];
7339 bytes[2] = NUL;
7340 }
7341 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7342 {
7343 bytes[1] = ScreenLines[off + 1];
7344 bytes[2] = NUL;
7345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 }
7347}
7348
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007349/*
7350 * Return TRUE if composing characters for screen posn "off" differs from
7351 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007352 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007353 */
7354 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007355screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007356{
7357 int i;
7358
7359 for (i = 0; i < Screen_mco; ++i)
7360 {
7361 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7362 return TRUE;
7363 if (u8cc[i] == 0)
7364 break;
7365 }
7366 return FALSE;
7367}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007368
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369/*
7370 * Put string '*text' on the screen at position 'row' and 'col', with
7371 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7372 * Note: only outputs within one row, message is truncated at screen boundary!
7373 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7374 */
7375 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007376screen_puts(
7377 char_u *text,
7378 int row,
7379 int col,
7380 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381{
7382 screen_puts_len(text, -1, row, col, attr);
7383}
7384
7385/*
7386 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7387 * a NUL.
7388 */
7389 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007390screen_puts_len(
7391 char_u *text,
7392 int textlen,
7393 int row,
7394 int col,
7395 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396{
7397 unsigned off;
7398 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007399 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007401 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 int mbyte_blen = 1;
7403 int mbyte_cells = 1;
7404 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007405 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007407#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007409 int pc, nc, nc1;
7410 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007412 int force_redraw_this;
7413 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007414 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415
7416 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7417 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007418 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419
Bram Moolenaarc236c162008-07-13 17:41:49 +00007420 /* When drawing over the right halve of a double-wide char clear out the
7421 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007422 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007423#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007424 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007425#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007426 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007427 {
7428 ScreenLines[off - 1] = ' ';
7429 ScreenAttrs[off - 1] = 0;
7430 if (enc_utf8)
7431 {
7432 ScreenLinesUC[off - 1] = 0;
7433 ScreenLinesC[0][off - 1] = 0;
7434 }
7435 /* redraw the previous cell, make it empty */
7436 screen_char(off - 1, row, col - 1);
7437 /* force the cell at "col" to be redrawn */
7438 force_redraw_next = TRUE;
7439 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007440
Bram Moolenaar367329b2007-08-30 11:53:22 +00007441 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007442 while (col < screen_Columns
7443 && (len < 0 || (int)(ptr - text) < len)
7444 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 {
7446 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 /* check if this is the first byte of a multibyte */
7448 if (has_mbyte)
7449 {
7450 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007451 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007453 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7455 mbyte_cells = 1;
7456 else if (enc_dbcs != 0)
7457 mbyte_cells = mbyte_blen;
7458 else /* enc_utf8 */
7459 {
7460 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007461 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 (int)((text + len) - ptr));
7463 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007464 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007466#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7468 {
7469 /* Do Arabic shaping. */
7470 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7471 {
7472 /* Past end of string to be displayed. */
7473 nc = NUL;
7474 nc1 = NUL;
7475 }
7476 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007477 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007478 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7479 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007480 nc1 = pcc[0];
7481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 pc = prev_c;
7483 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007484 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 }
7486 else
7487 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007488#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007489 if (col + mbyte_cells > screen_Columns)
7490 {
7491 /* Only 1 cell left, but character requires 2 cells:
7492 * display a '>' in the last column to avoid wrapping. */
7493 c = '>';
7494 mbyte_cells = 1;
7495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 }
7497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007499 force_redraw_this = force_redraw_next;
7500 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007501
7502 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 || (mbyte_cells == 2
7504 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7505 || (enc_dbcs == DBCS_JPNU
7506 && c == 0x8e
7507 && ScreenLines2[off] != ptr[1])
7508 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007509 && (ScreenLinesUC[off] !=
7510 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7511 || (ScreenLinesUC[off] != 0
7512 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007514 || exmode_active;
7515
Bram Moolenaara12a1612019-01-24 16:39:02 +01007516 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 {
7518#if defined(FEAT_GUI) || defined(UNIX)
7519 /* The bold trick makes a single row of pixels appear in the next
7520 * character. When a bold character is removed, the next
7521 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007522 * and for some xterms. */
7523 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524# ifdef FEAT_GUI
7525 gui.in_use
7526# endif
7527# if defined(FEAT_GUI) && defined(UNIX)
7528 ||
7529# endif
7530# ifdef UNIX
7531 term_is_xterm
7532# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007533 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007535 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007537 if (n > HL_ALL)
7538 n = syn_attr2attr(n);
7539 if (n & HL_BOLD)
7540 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 }
7542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 /* When at the end of the text and overwriting a two-cell
7544 * character with a one-cell character, need to clear the next
7545 * cell. Also when overwriting the left halve of a two-cell char
7546 * with the right halve of a two-cell char. Do this only once
7547 * (mb_off2cells() may return 2 on the right halve). */
7548 if (clear_next_cell)
7549 clear_next_cell = FALSE;
7550 else if (has_mbyte
7551 && (len < 0 ? ptr[mbyte_blen] == NUL
7552 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007553 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007555 && (*mb_off2cells)(off, max_off) == 1
7556 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 clear_next_cell = TRUE;
7558
7559 /* Make sure we never leave a second byte of a double-byte behind,
7560 * it confuses mb_off2cells(). */
7561 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007562 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007564 && (*mb_off2cells)(off, max_off) == 1
7565 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 ScreenLines[off] = c;
7568 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 if (enc_utf8)
7570 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007571 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 ScreenLinesUC[off] = 0;
7573 else
7574 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007575 int i;
7576
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007578 for (i = 0; i < Screen_mco; ++i)
7579 {
7580 ScreenLinesC[i][off] = u8cc[i];
7581 if (u8cc[i] == 0)
7582 break;
7583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 }
7585 if (mbyte_cells == 2)
7586 {
7587 ScreenLines[off + 1] = 0;
7588 ScreenAttrs[off + 1] = attr;
7589 }
7590 screen_char(off, row, col);
7591 }
7592 else if (mbyte_cells == 2)
7593 {
7594 ScreenLines[off + 1] = ptr[1];
7595 ScreenAttrs[off + 1] = attr;
7596 screen_char_2(off, row, col);
7597 }
7598 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7599 {
7600 ScreenLines2[off] = ptr[1];
7601 screen_char(off, row, col);
7602 }
7603 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 screen_char(off, row, col);
7605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 if (has_mbyte)
7607 {
7608 off += mbyte_cells;
7609 col += mbyte_cells;
7610 ptr += mbyte_blen;
7611 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007612 {
7613 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007615 len = -1;
7616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 }
7618 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 {
7620 ++off;
7621 ++col;
7622 ++ptr;
7623 }
7624 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007625
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007626 /* If we detected the next character needs to be redrawn, but the text
7627 * doesn't extend up to there, update the character here. */
7628 if (force_redraw_next && col < screen_Columns)
7629 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007630 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7631 screen_char_2(off, row, col);
7632 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007633 screen_char(off, row, col);
7634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635}
7636
7637#ifdef FEAT_SEARCH_EXTRA
7638/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007639 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 */
7641 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007642start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643{
7644 if (p_hls && !no_hlsearch)
7645 {
7646 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007647 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007648# ifdef FEAT_RELTIME
7649 /* Set the time limit to 'redrawtime'. */
7650 profile_setlimit(p_rdt, &search_hl.tm);
7651# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 }
7653}
7654
7655/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007656 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 */
7658 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007659end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660{
7661 if (search_hl.rm.regprog != NULL)
7662 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007663 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 search_hl.rm.regprog = NULL;
7665 }
7666}
7667
7668/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007669 * Init for calling prepare_search_hl().
7670 */
7671 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007672init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007673{
7674 matchitem_T *cur;
7675
7676 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7677 * match */
7678 cur = wp->w_match_head;
7679 while (cur != NULL)
7680 {
7681 cur->hl.rm = cur->match;
7682 if (cur->hlg_id == 0)
7683 cur->hl.attr = 0;
7684 else
7685 cur->hl.attr = syn_id2attr(cur->hlg_id);
7686 cur->hl.buf = wp->w_buffer;
7687 cur->hl.lnum = 0;
7688 cur->hl.first_lnum = 0;
7689# ifdef FEAT_RELTIME
7690 /* Set the time limit to 'redrawtime'. */
7691 profile_setlimit(p_rdt, &(cur->hl.tm));
7692# endif
7693 cur = cur->next;
7694 }
7695 search_hl.buf = wp->w_buffer;
7696 search_hl.lnum = 0;
7697 search_hl.first_lnum = 0;
7698 /* time limit is set at the toplevel, for all windows */
7699}
7700
7701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 * Advance to the match in window "wp" line "lnum" or past it.
7703 */
7704 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007705prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007707 matchitem_T *cur; /* points to the match list */
7708 match_T *shl; /* points to search_hl or a match */
7709 int shl_flag; /* flag to indicate whether search_hl
7710 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007711 int pos_inprogress; /* marks that position match search is
7712 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 int n;
7714
7715 /*
7716 * When using a multi-line pattern, start searching at the top
7717 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007718 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007720 cur = wp->w_match_head;
7721 shl_flag = FALSE;
7722 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007724 if (shl_flag == FALSE)
7725 {
7726 shl = &search_hl;
7727 shl_flag = TRUE;
7728 }
7729 else
7730 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 if (shl->rm.regprog != NULL
7732 && shl->lnum == 0
7733 && re_multiline(shl->rm.regprog))
7734 {
7735 if (shl->first_lnum == 0)
7736 {
7737# ifdef FEAT_FOLDING
7738 for (shl->first_lnum = lnum;
7739 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7740 if (hasFoldingWin(wp, shl->first_lnum - 1,
7741 NULL, NULL, TRUE, NULL))
7742 break;
7743# else
7744 shl->first_lnum = wp->w_topline;
7745# endif
7746 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007747 if (cur != NULL)
7748 cur->pos.cur = 0;
7749 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007751 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7752 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007754 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7755 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007756 pos_inprogress = cur == NULL || cur->pos.cur == 0
7757 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 if (shl->lnum != 0)
7759 {
7760 shl->first_lnum = shl->lnum
7761 + shl->rm.endpos[0].lnum
7762 - shl->rm.startpos[0].lnum;
7763 n = shl->rm.endpos[0].col;
7764 }
7765 else
7766 {
7767 ++shl->first_lnum;
7768 n = 0;
7769 }
7770 }
7771 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007772 if (shl != &search_hl && cur != NULL)
7773 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 }
7775}
7776
7777/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007778 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 * Uses shl->buf.
7780 * Sets shl->lnum and shl->rm contents.
7781 * Note: Assumes a previous match is always before "lnum", unless
7782 * shl->lnum is zero.
7783 * Careful: Any pointers for buffer lines will become invalid.
7784 */
7785 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007786next_search_hl(
7787 win_T *win,
7788 match_T *shl, /* points to search_hl or a match */
7789 linenr_T lnum,
7790 colnr_T mincol, /* minimal column for a match */
7791 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
7793 linenr_T l;
7794 colnr_T matchcol;
7795 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007796 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007798 // for :{range}s/pat only highlight inside the range
7799 if (lnum < search_first_line || lnum > search_last_line)
7800 {
7801 shl->lnum = 0;
7802 return;
7803 }
7804
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 if (shl->lnum != 0)
7806 {
7807 /* Check for three situations:
7808 * 1. If the "lnum" is below a previous match, start a new search.
7809 * 2. If the previous match includes "mincol", use it.
7810 * 3. Continue after the previous match.
7811 */
7812 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7813 if (lnum > l)
7814 shl->lnum = 0;
7815 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7816 return;
7817 }
7818
7819 /*
7820 * Repeat searching for a match until one is found that includes "mincol"
7821 * or none is found in this line.
7822 */
7823 called_emsg = FALSE;
7824 for (;;)
7825 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007826#ifdef FEAT_RELTIME
7827 /* Stop searching after passing the time limit. */
7828 if (profile_passed_limit(&(shl->tm)))
7829 {
7830 shl->lnum = 0; /* no match found in time */
7831 break;
7832 }
7833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 /* Three situations:
7835 * 1. No useful previous match: search from start of line.
7836 * 2. Not Vi compatible or empty match: continue at next character.
7837 * Break the loop if this is beyond the end of the line.
7838 * 3. Vi compatible searching: continue at end of previous match.
7839 */
7840 if (shl->lnum == 0)
7841 matchcol = 0;
7842 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7843 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007844 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007846 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007847
7848 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007849 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007850 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007852 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 shl->lnum = 0;
7854 break;
7855 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007856 if (has_mbyte)
7857 matchcol += mb_ptr2len(ml);
7858 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007859 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 }
7861 else
7862 matchcol = shl->rm.endpos[0].col;
7863
7864 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007865 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007867 /* Remember whether shl->rm is using a copy of the regprog in
7868 * cur->match. */
7869 int regprog_is_copy = (shl != &search_hl && cur != NULL
7870 && shl == &cur->hl
7871 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007872 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007873
Bram Moolenaarb3414592014-06-17 17:48:32 +02007874 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7875 matchcol,
7876#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007877 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007878#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007879 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007880#endif
7881 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007882 /* Copy the regprog, in case it got freed and recompiled. */
7883 if (regprog_is_copy)
7884 cur->match.regprog = cur->hl.rm.regprog;
7885
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007886 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007887 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007888 /* Error while handling regexp: stop using this regexp. */
7889 if (shl == &search_hl)
7890 {
7891 /* don't free regprog in the match list, it's a copy */
7892 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02007893 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007894 }
7895 shl->rm.regprog = NULL;
7896 shl->lnum = 0;
7897 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7898 message */
7899 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007900 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007901 }
7902 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007903 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007904 else
7905 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 if (nmatched == 0)
7907 {
7908 shl->lnum = 0; /* no match found */
7909 break;
7910 }
7911 if (shl->rm.startpos[0].lnum > 0
7912 || shl->rm.startpos[0].col >= mincol
7913 || nmatched > 1
7914 || shl->rm.endpos[0].col > mincol)
7915 {
7916 shl->lnum += shl->rm.startpos[0].lnum;
7917 break; /* useful match found */
7918 }
7919 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007920
7921 // Restore called_emsg for assert_fails().
7922 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924
Bram Moolenaar85077472016-10-16 14:35:48 +02007925/*
7926 * If there is a match fill "shl" and return one.
7927 * Return zero otherwise.
7928 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007929 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007930next_search_hl_pos(
7931 match_T *shl, /* points to a match */
7932 linenr_T lnum,
7933 posmatch_T *posmatch, /* match positions */
7934 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007935{
7936 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007937 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007938
Bram Moolenaarb3414592014-06-17 17:48:32 +02007939 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7940 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007941 llpos_T *pos = &posmatch->pos[i];
7942
7943 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007944 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007945 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007946 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007947 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007948 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007949 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007950 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007951 /* if this match comes before the one at "found" then swap
7952 * them */
7953 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007954 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007955 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007956
Bram Moolenaar85077472016-10-16 14:35:48 +02007957 *pos = posmatch->pos[found];
7958 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007959 }
7960 }
7961 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007962 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007963 }
7964 }
7965 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007966 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007967 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007968 colnr_T start = posmatch->pos[found].col == 0
7969 ? 0 : posmatch->pos[found].col - 1;
7970 colnr_T end = posmatch->pos[found].col == 0
7971 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007972
Bram Moolenaar85077472016-10-16 14:35:48 +02007973 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007974 shl->rm.startpos[0].lnum = 0;
7975 shl->rm.startpos[0].col = start;
7976 shl->rm.endpos[0].lnum = 0;
7977 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007978 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007979 posmatch->cur = found + 1;
7980 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007981 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007982 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007983}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007984#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007985
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007987screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988{
7989 attrentry_T *aep = NULL;
7990
7991 screen_attr = attr;
7992 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01007993#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 && termcap_active
7995#endif
7996 )
7997 {
7998#ifdef FEAT_GUI
7999 if (gui.in_use)
8000 {
8001 char buf[20];
8002
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008003 /* The GUI handles this internally. */
8004 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 OUT_STR(buf);
8006 }
8007 else
8008#endif
8009 {
8010 if (attr > HL_ALL) /* special HL attr. */
8011 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008012 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 aep = syn_cterm_attr2entry(attr);
8014 else
8015 aep = syn_term_attr2entry(attr);
8016 if (aep == NULL) /* did ":syntax clear" */
8017 attr = 0;
8018 else
8019 attr = aep->ae_attr;
8020 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008021 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008023 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008024#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008025 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8026 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8027 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008028#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008029 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008030 /* If the Normal FG color has BOLD attribute and the new HL
8031 * has a FG color defined, clear BOLD. */
8032 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008033 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008035 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008036 out_str(T_UCS);
8037 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008038 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8039 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008041 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008043 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008045 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008046 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047
8048 /*
8049 * Output the color or start string after bold etc., in case the
8050 * bold etc. override the color setting.
8051 */
8052 if (aep != NULL)
8053 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008054#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008055 /* When 'termguicolors' is set but fg or bg is unset,
8056 * fall back to the cterm colors. This helps for SpellBad,
8057 * where the GUI uses a red undercurl. */
8058 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008060 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008061 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008062 }
8063 else
8064#endif
8065 if (t_colors > 1)
8066 {
8067 if (aep->ae_u.cterm.fg_color)
8068 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8069 }
8070#ifdef FEAT_TERMGUICOLORS
8071 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8072 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008073 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008074 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 }
8076 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008077#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008078 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008080 if (aep->ae_u.cterm.bg_color)
8081 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8082 }
8083
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008084 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008085 {
8086 if (aep->ae_u.term.start != NULL)
8087 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 }
8089 }
8090 }
8091 }
8092}
8093
8094 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008095screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096{
8097 int do_ME = FALSE; /* output T_ME code */
8098
8099 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008100#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 && termcap_active
8102#endif
8103 )
8104 {
8105#ifdef FEAT_GUI
8106 if (gui.in_use)
8107 {
8108 char buf[20];
8109
8110 /* use internal GUI code */
8111 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8112 OUT_STR(buf);
8113 }
8114 else
8115#endif
8116 {
8117 if (screen_attr > HL_ALL) /* special HL attr. */
8118 {
8119 attrentry_T *aep;
8120
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008121 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {
8123 /*
8124 * Assume that t_me restores the original colors!
8125 */
8126 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008127 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008128#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008129 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8130 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8131 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008132#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008133 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008134#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008135 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8136 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8137 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008138#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008139 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 do_ME = TRUE;
8141 }
8142 else
8143 {
8144 aep = syn_term_attr2entry(screen_attr);
8145 if (aep != NULL && aep->ae_u.term.stop != NULL)
8146 {
8147 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8148 do_ME = TRUE;
8149 else
8150 out_str(aep->ae_u.term.stop);
8151 }
8152 }
8153 if (aep == NULL) /* did ":syntax clear" */
8154 screen_attr = 0;
8155 else
8156 screen_attr = aep->ae_attr;
8157 }
8158
8159 /*
8160 * Often all ending-codes are equal to T_ME. Avoid outputting the
8161 * same sequence several times.
8162 */
8163 if (screen_attr & HL_STANDOUT)
8164 {
8165 if (STRCMP(T_SE, T_ME) == 0)
8166 do_ME = TRUE;
8167 else
8168 out_str(T_SE);
8169 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008170 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008171 {
8172 if (STRCMP(T_UCE, T_ME) == 0)
8173 do_ME = TRUE;
8174 else
8175 out_str(T_UCE);
8176 }
8177 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008178 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {
8180 if (STRCMP(T_UE, T_ME) == 0)
8181 do_ME = TRUE;
8182 else
8183 out_str(T_UE);
8184 }
8185 if (screen_attr & HL_ITALIC)
8186 {
8187 if (STRCMP(T_CZR, T_ME) == 0)
8188 do_ME = TRUE;
8189 else
8190 out_str(T_CZR);
8191 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008192 if (screen_attr & HL_STRIKETHROUGH)
8193 {
8194 if (STRCMP(T_STE, T_ME) == 0)
8195 do_ME = TRUE;
8196 else
8197 out_str(T_STE);
8198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8200 out_str(T_ME);
8201
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008202#ifdef FEAT_TERMGUICOLORS
8203 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008205 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008206 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008207 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008208 term_bg_rgb_color(cterm_normal_bg_gui_color);
8209 }
8210 else
8211#endif
8212 {
8213 if (t_colors > 1)
8214 {
8215 /* set Normal cterm colors */
8216 if (cterm_normal_fg_color != 0)
8217 term_fg_color(cterm_normal_fg_color - 1);
8218 if (cterm_normal_bg_color != 0)
8219 term_bg_color(cterm_normal_bg_color - 1);
8220 if (cterm_normal_fg_bold)
8221 out_str(T_MD);
8222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 }
8224 }
8225 }
8226 screen_attr = 0;
8227}
8228
8229/*
8230 * Reset the colors for a cterm. Used when leaving Vim.
8231 * The machine specific code may override this again.
8232 */
8233 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008234reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008236 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {
8238 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008239#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008240 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8241 || cterm_normal_bg_gui_color != INVALCOLOR)
8242 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008243#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 {
8247 out_str(T_OP);
8248 screen_attr = -1;
8249 }
8250 if (cterm_normal_fg_bold)
8251 {
8252 out_str(T_ME);
8253 screen_attr = -1;
8254 }
8255 }
8256}
8257
8258/*
8259 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8260 * using the attributes from ScreenAttrs["off"].
8261 */
8262 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008263screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264{
8265 int attr;
8266
8267 /* Check for illegal values, just in case (could happen just after
8268 * resizing). */
8269 if (row >= screen_Rows || col >= screen_Columns)
8270 return;
8271
Bram Moolenaarae654382019-01-17 21:09:05 +01008272#ifdef FEAT_INS_EXPAND
8273 if (pum_under_menu(row, col))
8274 return;
8275#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008276 /* Outputting a character in the last cell on the screen may scroll the
8277 * screen up. Only do it when the "xn" termcap property is set, otherwise
8278 * mark the character invalid (update it when scrolled up). */
8279 if (*T_XN == NUL
8280 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281#ifdef FEAT_RIGHTLEFT
8282 /* account for first command-line character in rightleft mode */
8283 && !cmdmsg_rl
8284#endif
8285 )
8286 {
8287 ScreenAttrs[off] = (sattr_T)-1;
8288 return;
8289 }
8290
8291 /*
8292 * Stop highlighting first, so it's easier to move the cursor.
8293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 if (screen_char_attr != 0)
8295 attr = screen_char_attr;
8296 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 attr = ScreenAttrs[off];
8298 if (screen_attr != attr)
8299 screen_stop_highlight();
8300
8301 windgoto(row, col);
8302
8303 if (screen_attr != attr)
8304 screen_start_highlight(attr);
8305
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 if (enc_utf8 && ScreenLinesUC[off] != 0)
8307 {
8308 char_u buf[MB_MAXBYTES + 1];
8309
Bram Moolenaarcb070082016-04-02 22:14:51 +02008310 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008311 {
8312 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008313#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008314 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008315#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008316 )
8317 {
8318 /* Clear the two screen cells. If the character is actually
8319 * single width it won't change the second cell. */
8320 out_str((char_u *)" ");
8321 term_windgoto(row, col);
8322 }
8323 /* not sure where the cursor is after drawing the ambiguous width
8324 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008325 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008326 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008327 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008329
8330 /* Convert the UTF-8 character to bytes and write it. */
8331 buf[utfc_char2bytes(off, buf)] = NUL;
8332 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 }
8334 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 /* double-byte character in single-width cell */
8339 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8340 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 }
8342
8343 screen_cur_col++;
8344}
8345
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346/*
8347 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8348 * on the screen at position 'row' and 'col'.
8349 * The attributes of the first byte is used for all. This is required to
8350 * output the two bytes of a double-byte character with nothing in between.
8351 */
8352 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008353screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354{
8355 /* Check for illegal values (could be wrong when screen was resized). */
8356 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8357 return;
8358
8359 /* Outputting the last character on the screen may scrollup the screen.
8360 * Don't to it! Mark the character invalid (update it when scrolled up) */
8361 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8362 {
8363 ScreenAttrs[off] = (sattr_T)-1;
8364 return;
8365 }
8366
8367 /* Output the first byte normally (positions the cursor), then write the
8368 * second byte directly. */
8369 screen_char(off, row, col);
8370 out_char(ScreenLines[off + 1]);
8371 ++screen_cur_col;
8372}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374/*
8375 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8376 * This uses the contents of ScreenLines[] and doesn't change it.
8377 */
8378 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008379screen_draw_rectangle(
8380 int row,
8381 int col,
8382 int height,
8383 int width,
8384 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385{
8386 int r, c;
8387 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008388 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008390 /* Can't use ScreenLines unless initialized */
8391 if (ScreenLines == NULL)
8392 return;
8393
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 if (invert)
8395 screen_char_attr = HL_INVERSE;
8396 for (r = row; r < row + height; ++r)
8397 {
8398 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008399 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 for (c = col; c < col + width; ++c)
8401 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008402 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 {
8404 screen_char_2(off + c, r, c);
8405 ++c;
8406 }
8407 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 {
8409 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008410 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 }
8413 }
8414 }
8415 screen_char_attr = 0;
8416}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418/*
8419 * Redraw the characters for a vertically split window.
8420 */
8421 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008422redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423{
8424 int col;
8425 int width;
8426
8427# ifdef FEAT_CLIPBOARD
8428 clip_may_clear_selection(row, end - 1);
8429# endif
8430
8431 if (wp == NULL)
8432 {
8433 col = 0;
8434 width = Columns;
8435 }
8436 else
8437 {
8438 col = wp->w_wincol;
8439 width = wp->w_width;
8440 }
8441 screen_draw_rectangle(row, col, end - row, width, FALSE);
8442}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008444 static void
8445space_to_screenline(int off, int attr)
8446{
8447 ScreenLines[off] = ' ';
8448 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008449 if (enc_utf8)
8450 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008451}
8452
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453/*
8454 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8455 * with character 'c1' in first column followed by 'c2' in the other columns.
8456 * Use attributes 'attr'.
8457 */
8458 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008459screen_fill(
8460 int start_row,
8461 int end_row,
8462 int start_col,
8463 int end_col,
8464 int c1,
8465 int c2,
8466 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467{
8468 int row;
8469 int col;
8470 int off;
8471 int end_off;
8472 int did_delete;
8473 int c;
8474 int norm_term;
8475#if defined(FEAT_GUI) || defined(UNIX)
8476 int force_next = FALSE;
8477#endif
8478
8479 if (end_row > screen_Rows) /* safety check */
8480 end_row = screen_Rows;
8481 if (end_col > screen_Columns) /* safety check */
8482 end_col = screen_Columns;
8483 if (ScreenLines == NULL
8484 || start_row >= end_row
8485 || start_col >= end_col) /* nothing to do */
8486 return;
8487
8488 /* it's a "normal" terminal when not in a GUI or cterm */
8489 norm_term = (
8490#ifdef FEAT_GUI
8491 !gui.in_use &&
8492#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008493 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 for (row = start_row; row < end_row; ++row)
8495 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008496 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008497#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008498 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008499#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008500 )
8501 {
8502 /* When drawing over the right halve of a double-wide char clear
8503 * out the left halve. When drawing over the left halve of a
8504 * double wide-char clear out the right halve. Only needed in a
8505 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008506 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008507 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008508 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008509 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 /*
8512 * Try to use delete-line termcap code, when no attributes or in a
8513 * "normal" terminal, where a bold/italic space is just a
8514 * space.
8515 */
8516 did_delete = FALSE;
8517 if (c2 == ' '
8518 && end_col == Columns
8519 && can_clear(T_CE)
8520 && (attr == 0
8521 || (norm_term
8522 && attr <= HL_ALL
8523 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8524 {
8525 /*
8526 * check if we really need to clear something
8527 */
8528 col = start_col;
8529 if (c1 != ' ') /* don't clear first char */
8530 ++col;
8531
8532 off = LineOffset[row] + col;
8533 end_off = LineOffset[row] + end_col;
8534
8535 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 if (enc_utf8)
8537 while (off < end_off && ScreenLines[off] == ' '
8538 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8539 ++off;
8540 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 while (off < end_off && ScreenLines[off] == ' '
8542 && ScreenAttrs[off] == 0)
8543 ++off;
8544 if (off < end_off) /* something to be cleared */
8545 {
8546 col = off - LineOffset[row];
8547 screen_stop_highlight();
8548 term_windgoto(row, col);/* clear rest of this screen line */
8549 out_str(T_CE);
8550 screen_start(); /* don't know where cursor is now */
8551 col = end_col - col;
8552 while (col--) /* clear chars in ScreenLines */
8553 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008554 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 ++off;
8556 }
8557 }
8558 did_delete = TRUE; /* the chars are cleared now */
8559 }
8560
8561 off = LineOffset[row] + start_col;
8562 c = c1;
8563 for (col = start_col; col < end_col; ++col)
8564 {
8565 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008566 || (enc_utf8 && (int)ScreenLinesUC[off]
8567 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 || ScreenAttrs[off] != attr
8569#if defined(FEAT_GUI) || defined(UNIX)
8570 || force_next
8571#endif
8572 )
8573 {
8574#if defined(FEAT_GUI) || defined(UNIX)
8575 /* The bold trick may make a single row of pixels appear in
8576 * the next character. When a bold character is removed, the
8577 * next character should be redrawn too. This happens for our
8578 * own GUI and for some xterms. */
8579 if (
8580# ifdef FEAT_GUI
8581 gui.in_use
8582# endif
8583# if defined(FEAT_GUI) && defined(UNIX)
8584 ||
8585# endif
8586# ifdef UNIX
8587 term_is_xterm
8588# endif
8589 )
8590 {
8591 if (ScreenLines[off] != ' '
8592 && (ScreenAttrs[off] > HL_ALL
8593 || ScreenAttrs[off] & HL_BOLD))
8594 force_next = TRUE;
8595 else
8596 force_next = FALSE;
8597 }
8598#endif
8599 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 if (enc_utf8)
8601 {
8602 if (c >= 0x80)
8603 {
8604 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008605 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 }
8607 else
8608 ScreenLinesUC[off] = 0;
8609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 ScreenAttrs[off] = attr;
8611 if (!did_delete || c != ' ')
8612 screen_char(off, row, col);
8613 }
8614 ++off;
8615 if (col == start_col)
8616 {
8617 if (did_delete)
8618 break;
8619 c = c2;
8620 }
8621 }
8622 if (end_col == Columns)
8623 LineWraps[row] = FALSE;
8624 if (row == Rows - 1) /* overwritten the command line */
8625 {
8626 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008627 if (start_col == 0 && end_col == Columns
8628 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008630 if (start_col == 0)
8631 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 }
8633 }
8634}
8635
8636/*
8637 * Check if there should be a delay. Used before clearing or redrawing the
8638 * screen or the command line.
8639 */
8640 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008641check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642{
8643 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8644 && !did_wait_return
8645 && emsg_silent == 0)
8646 {
8647 out_flush();
8648 ui_delay(1000L, TRUE);
8649 emsg_on_display = FALSE;
8650 if (check_msg_scroll)
8651 msg_scroll = FALSE;
8652 }
8653}
8654
8655/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008656 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8657 */
8658 static void
8659clear_TabPageIdxs(void)
8660{
8661 int scol;
8662
8663 for (scol = 0; scol < Columns; ++scol)
8664 TabPageIdxs[scol] = 0;
8665}
8666
8667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008669 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 * Returns TRUE if there is a valid screen to write to.
8671 * Returns FALSE when starting up and screen not initialized yet.
8672 */
8673 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008674screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008676 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 return (ScreenLines != NULL);
8678}
8679
8680/*
8681 * Resize the shell to Rows and Columns.
8682 * Allocate ScreenLines[] and associated items.
8683 *
8684 * There may be some time between setting Rows and Columns and (re)allocating
8685 * ScreenLines[]. This happens when starting up and when (manually) changing
8686 * the shell size. Always use screen_Rows and screen_Columns to access items
8687 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8688 * final size of the shell is needed.
8689 */
8690 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008691screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692{
8693 int new_row, old_row;
8694#ifdef FEAT_GUI
8695 int old_Rows;
8696#endif
8697 win_T *wp;
8698 int outofmem = FALSE;
8699 int len;
8700 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008702 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008704 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 sattr_T *new_ScreenAttrs;
8706 unsigned *new_LineOffset;
8707 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008708 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008709 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008711 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008712 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008714retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 /*
8716 * Allocation of the screen buffers is done only when the size changes and
8717 * when Rows and Columns have been set and we have started doing full
8718 * screen stuff.
8719 */
8720 if ((ScreenLines != NULL
8721 && Rows == screen_Rows
8722 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 && enc_utf8 == (ScreenLinesUC != NULL)
8724 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008725 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 || Rows == 0
8727 || Columns == 0
8728 || (!full_screen && ScreenLines == NULL))
8729 return;
8730
8731 /*
8732 * It's possible that we produce an out-of-memory message below, which
8733 * will cause this function to be called again. To break the loop, just
8734 * return here.
8735 */
8736 if (entered)
8737 return;
8738 entered = TRUE;
8739
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008740 /*
8741 * Note that the window sizes are updated before reallocating the arrays,
8742 * thus we must not redraw here!
8743 */
8744 ++RedrawingDisabled;
8745
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 win_new_shellsize(); /* fit the windows in the new sized shell */
8747
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 comp_col(); /* recompute columns for shown command and ruler */
8749
8750 /*
8751 * We're changing the size of the screen.
8752 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8753 * - Move lines from the old arrays into the new arrays, clear extra
8754 * lines (unless the screen is going to be cleared).
8755 * - Free the old arrays.
8756 *
8757 * If anything fails, make ScreenLines NULL, so we don't do anything!
8758 * Continuing with the old ScreenLines may result in a crash, because the
8759 * size is wrong.
8760 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008761 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008763 if (aucmd_win != NULL)
8764 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765
8766 new_ScreenLines = (schar_T *)lalloc((long_u)(
8767 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008768 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 if (enc_utf8)
8770 {
8771 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8772 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008773 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008774 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8776 }
8777 if (enc_dbcs == DBCS_JPNU)
8778 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8779 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8781 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8782 new_LineOffset = (unsigned *)lalloc((long_u)(
8783 Rows * sizeof(unsigned)), FALSE);
8784 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008785 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008787 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 {
8789 if (win_alloc_lines(wp) == FAIL)
8790 {
8791 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008792 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 }
8794 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008795 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8796 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008797 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008798give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008800 for (i = 0; i < p_mco; ++i)
8801 if (new_ScreenLinesC[i] == NULL)
8802 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008804 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 || new_ScreenAttrs == NULL
8807 || new_LineOffset == NULL
8808 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008809 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 || outofmem)
8811 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008812 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008813 {
8814 /* guess the size */
8815 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8816
8817 /* Remember we did this to avoid getting outofmem messages over
8818 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008819 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008820 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008821 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008822 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008823 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008824 VIM_CLEAR(new_ScreenLinesC[i]);
8825 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008826 VIM_CLEAR(new_ScreenAttrs);
8827 VIM_CLEAR(new_LineOffset);
8828 VIM_CLEAR(new_LineWraps);
8829 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 }
8831 else
8832 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008833 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008834
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 for (new_row = 0; new_row < Rows; ++new_row)
8836 {
8837 new_LineOffset[new_row] = new_row * Columns;
8838 new_LineWraps[new_row] = FALSE;
8839
8840 /*
8841 * If the screen is not going to be cleared, copy as much as
8842 * possible from the old screen to the new one and clear the rest
8843 * (used when resizing the window at the "--more--" prompt or when
8844 * executing an external command, for the GUI).
8845 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008846 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 {
8848 (void)vim_memset(new_ScreenLines + new_row * Columns,
8849 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 if (enc_utf8)
8851 {
8852 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8853 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008854 for (i = 0; i < p_mco; ++i)
8855 (void)vim_memset(new_ScreenLinesC[i]
8856 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 0, (size_t)Columns * sizeof(u8char_T));
8858 }
8859 if (enc_dbcs == DBCS_JPNU)
8860 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8861 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8863 0, (size_t)Columns * sizeof(sattr_T));
8864 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008865 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 {
8867 if (screen_Columns < Columns)
8868 len = screen_Columns;
8869 else
8870 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008871 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008872 * may be invalid now. Also when p_mco changes. */
8873 if (!(enc_utf8 && ScreenLinesUC == NULL)
8874 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008875 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8876 ScreenLines + LineOffset[old_row],
8877 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008878 if (enc_utf8 && ScreenLinesUC != NULL
8879 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 {
8881 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8882 ScreenLinesUC + LineOffset[old_row],
8883 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008884 for (i = 0; i < p_mco; ++i)
8885 mch_memmove(new_ScreenLinesC[i]
8886 + new_LineOffset[new_row],
8887 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 (size_t)len * sizeof(u8char_T));
8889 }
8890 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8891 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8892 ScreenLines2 + LineOffset[old_row],
8893 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8895 ScreenAttrs + LineOffset[old_row],
8896 (size_t)len * sizeof(sattr_T));
8897 }
8898 }
8899 }
8900 /* Use the last line of the screen for the current line. */
8901 current_ScreenLine = new_ScreenLines + Rows * Columns;
8902 }
8903
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008904 free_screenlines();
8905
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008908 for (i = 0; i < p_mco; ++i)
8909 ScreenLinesC[i] = new_ScreenLinesC[i];
8910 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 ScreenAttrs = new_ScreenAttrs;
8913 LineOffset = new_LineOffset;
8914 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008915 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916
8917 /* It's important that screen_Rows and screen_Columns reflect the actual
8918 * size of ScreenLines[]. Set them before calling anything. */
8919#ifdef FEAT_GUI
8920 old_Rows = screen_Rows;
8921#endif
8922 screen_Rows = Rows;
8923 screen_Columns = Columns;
8924
8925 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008926 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928#ifdef FEAT_GUI
8929 else if (gui.in_use
8930 && !gui.starting
8931 && ScreenLines != NULL
8932 && old_Rows != Rows)
8933 {
8934 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8935 /*
8936 * Adjust the position of the cursor, for when executing an external
8937 * command.
8938 */
8939 if (msg_row >= Rows) /* Rows got smaller */
8940 msg_row = Rows - 1; /* put cursor at last row */
8941 else if (Rows > old_Rows) /* Rows got bigger */
8942 msg_row += Rows - old_Rows; /* put cursor in same place */
8943 if (msg_col >= Columns) /* Columns got smaller */
8944 msg_col = Columns - 1; /* put cursor at last column */
8945 }
8946#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008947 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008950 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008951
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008952 /*
8953 * Do not apply autocommands more than 3 times to avoid an endless loop
8954 * in case applying autocommands always changes Rows or Columns.
8955 */
8956 if (starting == 0 && ++retry_count <= 3)
8957 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008958 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008959 /* In rare cases, autocommands may have altered Rows or Columns,
8960 * jump back to check if we need to allocate the screen again. */
8961 goto retry;
8962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963}
8964
8965 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008966free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008967{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008968 int i;
8969
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008970 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008971 for (i = 0; i < Screen_mco; ++i)
8972 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008973 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008974 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008975 vim_free(ScreenAttrs);
8976 vim_free(LineOffset);
8977 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008978 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008979}
8980
8981 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008982screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983{
8984 check_for_delay(FALSE);
8985 screenalloc(FALSE); /* allocate screen buffers if size changed */
8986 screenclear2(); /* clear the screen */
8987}
8988
8989 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008990screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991{
8992 int i;
8993
8994 if (starting == NO_SCREEN || ScreenLines == NULL
8995#ifdef FEAT_GUI
8996 || (gui.in_use && gui.starting)
8997#endif
8998 )
8999 return;
9000
9001#ifdef FEAT_GUI
9002 if (!gui.in_use)
9003#endif
9004 screen_attr = -1; /* force setting the Normal colors */
9005 screen_stop_highlight(); /* don't want highlighting here */
9006
9007#ifdef FEAT_CLIPBOARD
9008 /* disable selection without redrawing it */
9009 clip_scroll_selection(9999);
9010#endif
9011
9012 /* blank out ScreenLines */
9013 for (i = 0; i < Rows; ++i)
9014 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009015 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 LineWraps[i] = FALSE;
9017 }
9018
9019 if (can_clear(T_CL))
9020 {
9021 out_str(T_CL); /* clear the display */
9022 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009023 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 }
9025 else
9026 {
9027 /* can't clear the screen, mark all chars with invalid attributes */
9028 for (i = 0; i < Rows; ++i)
9029 lineinvalid(LineOffset[i], (int)Columns);
9030 clear_cmdline = TRUE;
9031 }
9032
9033 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9034
9035 win_rest_invalid(firstwin);
9036 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009037 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 if (must_redraw == CLEAR) /* no need to clear again */
9039 must_redraw = NOT_VALID;
9040 compute_cmdrow();
9041 msg_row = cmdline_row; /* put cursor on last line for messages */
9042 msg_col = 0;
9043 screen_start(); /* don't know where cursor is now */
9044 msg_scrolled = 0; /* can't scroll back */
9045 msg_didany = FALSE;
9046 msg_didout = FALSE;
9047}
9048
9049/*
9050 * Clear one line in ScreenLines.
9051 */
9052 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009053lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054{
9055 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 if (enc_utf8)
9057 (void)vim_memset(ScreenLinesUC + off, 0,
9058 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009059 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060}
9061
9062/*
9063 * Mark one line in ScreenLines invalid by setting the attributes to an
9064 * invalid value.
9065 */
9066 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009067lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068{
9069 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9070}
9071
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072/*
9073 * Copy part of a Screenline for vertically split window "wp".
9074 */
9075 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009076linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077{
9078 unsigned off_to = LineOffset[to] + wp->w_wincol;
9079 unsigned off_from = LineOffset[from] + wp->w_wincol;
9080
9081 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9082 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 if (enc_utf8)
9084 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009085 int i;
9086
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9088 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009089 for (i = 0; i < p_mco; ++i)
9090 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9091 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 }
9093 if (enc_dbcs == DBCS_JPNU)
9094 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9095 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9097 wp->w_width * sizeof(sattr_T));
9098}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099
9100/*
9101 * Return TRUE if clearing with term string "p" would work.
9102 * It can't work when the string is empty or it won't set the right background.
9103 */
9104 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009105can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106{
9107 return (*p != NUL && (t_colors <= 1
9108#ifdef FEAT_GUI
9109 || gui.in_use
9110#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009111#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009112 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009113 || (!p_tgc && cterm_normal_bg_color == 0)
9114#else
9115 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009116#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009117 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118}
9119
9120/*
9121 * Reset cursor position. Use whenever cursor was moved because of outputting
9122 * something directly to the screen (shell commands) or a terminal control
9123 * code.
9124 */
9125 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009126screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127{
9128 screen_cur_row = screen_cur_col = 9999;
9129}
9130
9131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 * Move the cursor to position "row","col" in the screen.
9133 * This tries to find the most efficient way to move, minimizing the number of
9134 * characters sent to the terminal.
9135 */
9136 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009137windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009139 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 int i;
9141 int plan;
9142 int cost;
9143 int wouldbe_col;
9144 int noinvcurs;
9145 char_u *bs;
9146 int goto_cost;
9147 int attr;
9148
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009149#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9151
9152#define PLAN_LE 1
9153#define PLAN_CR 2
9154#define PLAN_NL 3
9155#define PLAN_WRITE 4
9156 /* Can't use ScreenLines unless initialized */
9157 if (ScreenLines == NULL)
9158 return;
9159
9160 if (col != screen_cur_col || row != screen_cur_row)
9161 {
9162 /* Check for valid position. */
9163 if (row < 0) /* window without text lines? */
9164 row = 0;
9165 if (row >= screen_Rows)
9166 row = screen_Rows - 1;
9167 if (col >= screen_Columns)
9168 col = screen_Columns - 1;
9169
9170 /* check if no cursor movement is allowed in highlight mode */
9171 if (screen_attr && *T_MS == NUL)
9172 noinvcurs = HIGHL_COST;
9173 else
9174 noinvcurs = 0;
9175 goto_cost = GOTO_COST + noinvcurs;
9176
9177 /*
9178 * Plan how to do the positioning:
9179 * 1. Use CR to move it to column 0, same row.
9180 * 2. Use T_LE to move it a few columns to the left.
9181 * 3. Use NL to move a few lines down, column 0.
9182 * 4. Move a few columns to the right with T_ND or by writing chars.
9183 *
9184 * Don't do this if the cursor went beyond the last column, the cursor
9185 * position is unknown then (some terminals wrap, some don't )
9186 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009187 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 * characters to move the cursor to the right.
9189 */
9190 if (row >= screen_cur_row && screen_cur_col < Columns)
9191 {
9192 /*
9193 * If the cursor is in the same row, bigger col, we can use CR
9194 * or T_LE.
9195 */
9196 bs = NULL; /* init for GCC */
9197 attr = screen_attr;
9198 if (row == screen_cur_row && col < screen_cur_col)
9199 {
9200 /* "le" is preferred over "bc", because "bc" is obsolete */
9201 if (*T_LE)
9202 bs = T_LE; /* "cursor left" */
9203 else
9204 bs = T_BC; /* "backspace character (old) */
9205 if (*bs)
9206 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9207 else
9208 cost = 999;
9209 if (col + 1 < cost) /* using CR is less characters */
9210 {
9211 plan = PLAN_CR;
9212 wouldbe_col = 0;
9213 cost = 1; /* CR is just one character */
9214 }
9215 else
9216 {
9217 plan = PLAN_LE;
9218 wouldbe_col = col;
9219 }
9220 if (noinvcurs) /* will stop highlighting */
9221 {
9222 cost += noinvcurs;
9223 attr = 0;
9224 }
9225 }
9226
9227 /*
9228 * If the cursor is above where we want to be, we can use CR LF.
9229 */
9230 else if (row > screen_cur_row)
9231 {
9232 plan = PLAN_NL;
9233 wouldbe_col = 0;
9234 cost = (row - screen_cur_row) * 2; /* CR LF */
9235 if (noinvcurs) /* will stop highlighting */
9236 {
9237 cost += noinvcurs;
9238 attr = 0;
9239 }
9240 }
9241
9242 /*
9243 * If the cursor is in the same row, smaller col, just use write.
9244 */
9245 else
9246 {
9247 plan = PLAN_WRITE;
9248 wouldbe_col = screen_cur_col;
9249 cost = 0;
9250 }
9251
9252 /*
9253 * Check if any characters that need to be written have the
9254 * correct attributes. Also avoid UTF-8 characters.
9255 */
9256 i = col - wouldbe_col;
9257 if (i > 0)
9258 cost += i;
9259 if (cost < goto_cost && i > 0)
9260 {
9261 /*
9262 * Check if the attributes are correct without additionally
9263 * stopping highlighting.
9264 */
9265 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9266 while (i && *p++ == attr)
9267 --i;
9268 if (i != 0)
9269 {
9270 /*
9271 * Try if it works when highlighting is stopped here.
9272 */
9273 if (*--p == 0)
9274 {
9275 cost += noinvcurs;
9276 while (i && *p++ == 0)
9277 --i;
9278 }
9279 if (i != 0)
9280 cost = 999; /* different attributes, don't do it */
9281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 if (enc_utf8)
9283 {
9284 /* Don't use an UTF-8 char for positioning, it's slow. */
9285 for (i = wouldbe_col; i < col; ++i)
9286 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9287 {
9288 cost = 999;
9289 break;
9290 }
9291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 }
9293
9294 /*
9295 * We can do it without term_windgoto()!
9296 */
9297 if (cost < goto_cost)
9298 {
9299 if (plan == PLAN_LE)
9300 {
9301 if (noinvcurs)
9302 screen_stop_highlight();
9303 while (screen_cur_col > col)
9304 {
9305 out_str(bs);
9306 --screen_cur_col;
9307 }
9308 }
9309 else if (plan == PLAN_CR)
9310 {
9311 if (noinvcurs)
9312 screen_stop_highlight();
9313 out_char('\r');
9314 screen_cur_col = 0;
9315 }
9316 else if (plan == PLAN_NL)
9317 {
9318 if (noinvcurs)
9319 screen_stop_highlight();
9320 while (screen_cur_row < row)
9321 {
9322 out_char('\n');
9323 ++screen_cur_row;
9324 }
9325 screen_cur_col = 0;
9326 }
9327
9328 i = col - screen_cur_col;
9329 if (i > 0)
9330 {
9331 /*
9332 * Use cursor-right if it's one character only. Avoids
9333 * removing a line of pixels from the last bold char, when
9334 * using the bold trick in the GUI.
9335 */
9336 if (T_ND[0] != NUL && T_ND[1] == NUL)
9337 {
9338 while (i-- > 0)
9339 out_char(*T_ND);
9340 }
9341 else
9342 {
9343 int off;
9344
9345 off = LineOffset[row] + screen_cur_col;
9346 while (i-- > 0)
9347 {
9348 if (ScreenAttrs[off] != screen_attr)
9349 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 if (enc_dbcs == DBCS_JPNU
9353 && ScreenLines[off] == 0x8e)
9354 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 ++off;
9356 }
9357 }
9358 }
9359 }
9360 }
9361 else
9362 cost = 999;
9363
9364 if (cost >= goto_cost)
9365 {
9366 if (noinvcurs)
9367 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009368 if (row == screen_cur_row && (col > screen_cur_col)
9369 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 term_cursor_right(col - screen_cur_col);
9371 else
9372 term_windgoto(row, col);
9373 }
9374 screen_cur_row = row;
9375 screen_cur_col = col;
9376 }
9377}
9378
9379/*
9380 * Set cursor to its position in the current window.
9381 */
9382 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009383setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009385 setcursor_mayforce(FALSE);
9386}
9387
9388/*
9389 * Set cursor to its position in the current window.
9390 * When "force" is TRUE also when not redrawing.
9391 */
9392 void
9393setcursor_mayforce(int force)
9394{
9395 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 {
9397 validate_cursor();
9398 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009399 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009401 /* With 'rightleft' set and the cursor on a double-wide
9402 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009403 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9404 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009405 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009406 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407#endif
9408 curwin->w_wcol));
9409 }
9410}
9411
9412
9413/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009414 * Insert 'line_count' lines at 'row' in window 'wp'.
9415 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9416 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 * scrolling.
9418 * Returns FAIL if the lines are not inserted, OK for success.
9419 */
9420 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009421win_ins_lines(
9422 win_T *wp,
9423 int row,
9424 int line_count,
9425 int invalid,
9426 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427{
9428 int did_delete;
9429 int nextrow;
9430 int lastrow;
9431 int retval;
9432
9433 if (invalid)
9434 wp->w_lines_valid = 0;
9435
9436 if (wp->w_height < 5)
9437 return FAIL;
9438
9439 if (line_count > wp->w_height - row)
9440 line_count = wp->w_height - row;
9441
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009442 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 if (retval != MAYBE)
9444 return retval;
9445
9446 /*
9447 * If there is a next window or a status line, we first try to delete the
9448 * lines at the bottom to avoid messing what is after the window.
9449 * If this fails and there are following windows, don't do anything to avoid
9450 * messing up those windows, better just redraw.
9451 */
9452 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 if (wp->w_next != NULL || wp->w_status_height)
9454 {
9455 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009456 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 did_delete = TRUE;
9458 else if (wp->w_next)
9459 return FAIL;
9460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 /*
9462 * if no lines deleted, blank the lines that will end up below the window
9463 */
9464 if (!did_delete)
9465 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009468 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 lastrow = nextrow + line_count;
9470 if (lastrow > Rows)
9471 lastrow = Rows;
9472 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009473 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 ' ', ' ', 0);
9475 }
9476
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009477 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 == FAIL)
9479 {
9480 /* deletion will have messed up other windows */
9481 if (did_delete)
9482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 win_rest_invalid(W_NEXT(wp));
9485 }
9486 return FAIL;
9487 }
9488
9489 return OK;
9490}
9491
9492/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009493 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9495 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9496 * scrolling
9497 * Return OK for success, FAIL if the lines are not deleted.
9498 */
9499 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009500win_del_lines(
9501 win_T *wp,
9502 int row,
9503 int line_count,
9504 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009505 int mayclear,
9506 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507{
9508 int retval;
9509
9510 if (invalid)
9511 wp->w_lines_valid = 0;
9512
9513 if (line_count > wp->w_height - row)
9514 line_count = wp->w_height - row;
9515
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009516 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 if (retval != MAYBE)
9518 return retval;
9519
9520 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009521 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 return FAIL;
9523
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 /*
9525 * If there are windows or status lines below, try to put them at the
9526 * correct place. If we can't do that, they have to be redrawn.
9527 */
9528 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9529 {
9530 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009531 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 {
9533 wp->w_redr_status = TRUE;
9534 win_rest_invalid(wp->w_next);
9535 }
9536 }
9537 /*
9538 * If this is the last window and there is no status line, redraw the
9539 * command line later.
9540 */
9541 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 redraw_cmdline = TRUE;
9543 return OK;
9544}
9545
9546/*
9547 * Common code for win_ins_lines() and win_del_lines().
9548 * Returns OK or FAIL when the work has been done.
9549 * Returns MAYBE when not finished yet.
9550 */
9551 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009552win_do_lines(
9553 win_T *wp,
9554 int row,
9555 int line_count,
9556 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009557 int del,
9558 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559{
9560 int retval;
9561
9562 if (!redrawing() || line_count <= 0)
9563 return FAIL;
9564
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009565 /* When inserting lines would result in loss of command output, just redraw
9566 * the lines. */
9567 if (no_win_do_lines_ins && !del)
9568 return FAIL;
9569
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009571 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009573 if (!no_win_do_lines_ins)
9574 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 return FAIL;
9576 }
9577
9578 /*
9579 * Delete all remaining lines
9580 */
9581 if (row + line_count >= wp->w_height)
9582 {
9583 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009584 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 ' ', ' ', 0);
9586 return OK;
9587 }
9588
9589 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009590 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009592 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009594 if (!no_win_do_lines_ins)
9595 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596
9597 /*
9598 * If the terminal can set a scroll region, use that.
9599 * Always do this in a vertically split window. This will redraw from
9600 * ScreenLines[] when t_CV isn't defined. That's faster than using
9601 * win_line().
9602 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009603 * a character in the lower right corner of the scroll region may cause a
9604 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009606 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 scroll_region_set(wp, row);
9610 if (del)
9611 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009612 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 else
9614 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009615 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 scroll_region_reset();
9618 return retval;
9619 }
9620
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9622 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623
9624 return MAYBE;
9625}
9626
9627/*
9628 * window 'wp' and everything after it is messed up, mark it for redraw
9629 */
9630 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009631win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 {
9635 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 wp->w_redr_status = TRUE;
9637 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 }
9639 redraw_cmdline = TRUE;
9640}
9641
9642/*
9643 * The rest of the routines in this file perform screen manipulations. The
9644 * given operation is performed physically on the screen. The corresponding
9645 * change is also made to the internal screen image. In this way, the editor
9646 * anticipates the effect of editing changes on the appearance of the screen.
9647 * That way, when we call screenupdate a complete redraw isn't usually
9648 * necessary. Another advantage is that we can keep adding code to anticipate
9649 * screen changes, and in the meantime, everything still works.
9650 */
9651
9652/*
9653 * types for inserting or deleting lines
9654 */
9655#define USE_T_CAL 1
9656#define USE_T_CDL 2
9657#define USE_T_AL 3
9658#define USE_T_CE 4
9659#define USE_T_DL 5
9660#define USE_T_SR 6
9661#define USE_NL 7
9662#define USE_T_CD 8
9663#define USE_REDRAW 9
9664
9665/*
9666 * insert lines on the screen and update ScreenLines[]
9667 * 'end' is the line after the scrolled part. Normally it is Rows.
9668 * When scrolling region used 'off' is the offset from the top for the region.
9669 * 'row' and 'end' are relative to the start of the region.
9670 *
9671 * return FAIL for failure, OK for success.
9672 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009673 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009674screen_ins_lines(
9675 int off,
9676 int row,
9677 int line_count,
9678 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009679 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009680 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681{
9682 int i;
9683 int j;
9684 unsigned temp;
9685 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009686 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 int type;
9688 int result_empty;
9689 int can_ce = can_clear(T_CE);
9690
9691 /*
9692 * FAIL if
9693 * - there is no valid screen
9694 * - the screen has to be redrawn completely
9695 * - the line count is less than one
9696 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009697 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009699 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9700#ifdef FEAT_CLIPBOARD
9701 || (clip_star.state != SELECT_CLEARED
9702 && redrawing_for_callback > 0)
9703#endif
9704 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 return FAIL;
9706
9707 /*
9708 * There are seven ways to insert lines:
9709 * 0. When in a vertically split window and t_CV isn't set, redraw the
9710 * characters from ScreenLines[].
9711 * 1. Use T_CD (clear to end of display) if it exists and the result of
9712 * the insert is just empty lines
9713 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9714 * present or line_count > 1. It looks better if we do all the inserts
9715 * at once.
9716 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9717 * insert is just empty lines and T_CE is not present or line_count >
9718 * 1.
9719 * 4. Use T_AL (insert line) if it exists.
9720 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9721 * just empty lines.
9722 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9723 * just empty lines.
9724 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9725 * the 'da' flag is not set or we have clear line capability.
9726 * 8. redraw the characters from ScreenLines[].
9727 *
9728 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9729 * the scrollbar for the window. It does have insert line, use that if it
9730 * exists.
9731 */
9732 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9734 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009735 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 type = USE_T_CD;
9737 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9738 type = USE_T_CAL;
9739 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9740 type = USE_T_CDL;
9741 else if (*T_AL != NUL)
9742 type = USE_T_AL;
9743 else if (can_ce && result_empty)
9744 type = USE_T_CE;
9745 else if (*T_DL != NUL && result_empty)
9746 type = USE_T_DL;
9747 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9748 type = USE_T_SR;
9749 else
9750 return FAIL;
9751
9752 /*
9753 * For clearing the lines screen_del_lines() is used. This will also take
9754 * care of t_db if necessary.
9755 */
9756 if (type == USE_T_CD || type == USE_T_CDL ||
9757 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009758 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759
9760 /*
9761 * If text is retained below the screen, first clear or delete as many
9762 * lines at the bottom of the window as are about to be inserted so that
9763 * the deleted lines won't later surface during a screen_del_lines.
9764 */
9765 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009766 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767
9768#ifdef FEAT_CLIPBOARD
9769 /* Remove a modeless selection when inserting lines halfway the screen
9770 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009771 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009772 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 else
9774 clip_scroll_selection(-line_count);
9775#endif
9776
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777#ifdef FEAT_GUI
9778 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9779 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009780 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781#endif
9782
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009783 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9784 cursor_col = wp->w_wincol;
9785
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 if (*T_CCS != NUL) /* cursor relative to region */
9787 cursor_row = row;
9788 else
9789 cursor_row = row + off;
9790
9791 /*
9792 * Shift LineOffset[] line_count down to reflect the inserted lines.
9793 * Clear the inserted lines in ScreenLines[].
9794 */
9795 row += off;
9796 end += off;
9797 for (i = 0; i < line_count; ++i)
9798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 if (wp != NULL && wp->w_width != Columns)
9800 {
9801 /* need to copy part of a line */
9802 j = end - 1 - i;
9803 while ((j -= line_count) >= row)
9804 linecopy(j + line_count, j, wp);
9805 j += line_count;
9806 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009807 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9808 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 else
9810 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9811 LineWraps[j] = FALSE;
9812 }
9813 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 {
9815 j = end - 1 - i;
9816 temp = LineOffset[j];
9817 while ((j -= line_count) >= row)
9818 {
9819 LineOffset[j + line_count] = LineOffset[j];
9820 LineWraps[j + line_count] = LineWraps[j];
9821 }
9822 LineOffset[j + line_count] = temp;
9823 LineWraps[j + line_count] = FALSE;
9824 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009825 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 else
9827 lineinvalid(temp, (int)Columns);
9828 }
9829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830
9831 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009832 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009833 if (clear_attr != 0)
9834 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 /* redraw the characters */
9837 if (type == USE_REDRAW)
9838 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009839 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 {
9841 term_append_lines(line_count);
9842 screen_start(); /* don't know where cursor is now */
9843 }
9844 else
9845 {
9846 for (i = 0; i < line_count; i++)
9847 {
9848 if (type == USE_T_AL)
9849 {
9850 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009851 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 out_str(T_AL);
9853 }
9854 else /* type == USE_T_SR */
9855 out_str(T_SR);
9856 screen_start(); /* don't know where cursor is now */
9857 }
9858 }
9859
9860 /*
9861 * With scroll-reverse and 'da' flag set we need to clear the lines that
9862 * have been scrolled down into the region.
9863 */
9864 if (type == USE_T_SR && *T_DA)
9865 {
9866 for (i = 0; i < line_count; ++i)
9867 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009868 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 out_str(T_CE);
9870 screen_start(); /* don't know where cursor is now */
9871 }
9872 }
9873
9874#ifdef FEAT_GUI
9875 gui_can_update_cursor();
9876 if (gui.in_use)
9877 out_flush(); /* always flush after a scroll */
9878#endif
9879 return OK;
9880}
9881
9882/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009883 * Delete lines on the screen and update ScreenLines[].
9884 * "end" is the line after the scrolled part. Normally it is Rows.
9885 * When scrolling region used "off" is the offset from the top for the region.
9886 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 *
9888 * Return OK for success, FAIL if the lines are not deleted.
9889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009891screen_del_lines(
9892 int off,
9893 int row,
9894 int line_count,
9895 int end,
9896 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009897 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009898 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899{
9900 int j;
9901 int i;
9902 unsigned temp;
9903 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009904 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 int cursor_end;
9906 int result_empty; /* result is empty until end of region */
9907 int can_delete; /* deleting line codes can be used */
9908 int type;
9909
9910 /*
9911 * FAIL if
9912 * - there is no valid screen
9913 * - the screen has to be redrawn completely
9914 * - the line count is less than one
9915 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009916 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009918 if (!screen_valid(TRUE) || line_count <= 0
9919 || (!force && line_count > p_ttyscroll)
9920#ifdef FEAT_CLIPBOARD
9921 || (clip_star.state != SELECT_CLEARED
9922 && redrawing_for_callback > 0)
9923#endif
9924 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 return FAIL;
9926
9927 /*
9928 * Check if the rest of the current region will become empty.
9929 */
9930 result_empty = row + line_count >= end;
9931
9932 /*
9933 * We can delete lines only when 'db' flag not set or when 'ce' option
9934 * available.
9935 */
9936 can_delete = (*T_DB == NUL || can_clear(T_CE));
9937
9938 /*
9939 * There are six ways to delete lines:
9940 * 0. When in a vertically split window and t_CV isn't set, redraw the
9941 * characters from ScreenLines[].
9942 * 1. Use T_CD if it exists and the result is empty.
9943 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9944 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9945 * none of the other ways work.
9946 * 4. Use T_CE (erase line) if the result is empty.
9947 * 5. Use T_DL (delete line) if it exists.
9948 * 6. redraw the characters from ScreenLines[].
9949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9951 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009952 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 type = USE_T_CD;
9954#if defined(__BEOS__) && defined(BEOS_DR8)
9955 /*
9956 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9957 * its internal termcap... this works okay for tests which test *T_DB !=
9958 * NUL. It has the disadvantage that the user cannot use any :set t_*
9959 * command to get T_DB (back) to empty_option, only :set term=... will do
9960 * the trick...
9961 * Anyway, this hack will hopefully go away with the next OS release.
9962 * (Olaf Seibert)
9963 */
9964 else if (row == 0 && T_DB == empty_option
9965 && (line_count == 1 || *T_CDL == NUL))
9966#else
9967 else if (row == 0 && (
9968#ifndef AMIGA
9969 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9970 * up, so use delete-line command */
9971 line_count == 1 ||
9972#endif
9973 *T_CDL == NUL))
9974#endif
9975 type = USE_NL;
9976 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9977 type = USE_T_CDL;
9978 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02009979 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 type = USE_T_CE;
9981 else if (*T_DL != NUL && can_delete)
9982 type = USE_T_DL;
9983 else if (*T_CDL != NUL && can_delete)
9984 type = USE_T_CDL;
9985 else
9986 return FAIL;
9987
9988#ifdef FEAT_CLIPBOARD
9989 /* Remove a modeless selection when deleting lines halfway the screen or
9990 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009991 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009992 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 else
9994 clip_scroll_selection(line_count);
9995#endif
9996
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997#ifdef FEAT_GUI
9998 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9999 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010000 gui_dont_update_cursor(gui.cursor_row >= row + off
10001 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002#endif
10003
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010004 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10005 cursor_col = wp->w_wincol;
10006
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 if (*T_CCS != NUL) /* cursor relative to region */
10008 {
10009 cursor_row = row;
10010 cursor_end = end;
10011 }
10012 else
10013 {
10014 cursor_row = row + off;
10015 cursor_end = end + off;
10016 }
10017
10018 /*
10019 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10020 * Clear the inserted lines in ScreenLines[].
10021 */
10022 row += off;
10023 end += off;
10024 for (i = 0; i < line_count; ++i)
10025 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 if (wp != NULL && wp->w_width != Columns)
10027 {
10028 /* need to copy part of a line */
10029 j = row + i;
10030 while ((j += line_count) <= end - 1)
10031 linecopy(j - line_count, j, wp);
10032 j -= line_count;
10033 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010034 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10035 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 else
10037 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10038 LineWraps[j] = FALSE;
10039 }
10040 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 {
10042 /* whole width, moving the line pointers is faster */
10043 j = row + i;
10044 temp = LineOffset[j];
10045 while ((j += line_count) <= end - 1)
10046 {
10047 LineOffset[j - line_count] = LineOffset[j];
10048 LineWraps[j - line_count] = LineWraps[j];
10049 }
10050 LineOffset[j - line_count] = temp;
10051 LineWraps[j - line_count] = FALSE;
10052 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010053 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 else
10055 lineinvalid(temp, (int)Columns);
10056 }
10057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010059 if (screen_attr != clear_attr)
10060 screen_stop_highlight();
10061 if (clear_attr != 0)
10062 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 /* redraw the characters */
10065 if (type == USE_REDRAW)
10066 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010067 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010069 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 out_str(T_CD);
10071 screen_start(); /* don't know where cursor is now */
10072 }
10073 else if (type == USE_T_CDL)
10074 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010075 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 term_delete_lines(line_count);
10077 screen_start(); /* don't know where cursor is now */
10078 }
10079 /*
10080 * Deleting lines at top of the screen or scroll region: Just scroll
10081 * the whole screen (scroll region) up by outputting newlines on the
10082 * last line.
10083 */
10084 else if (type == USE_NL)
10085 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010086 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 for (i = line_count; --i >= 0; )
10088 out_char('\n'); /* cursor will remain on same line */
10089 }
10090 else
10091 {
10092 for (i = line_count; --i >= 0; )
10093 {
10094 if (type == USE_T_DL)
10095 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010096 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 out_str(T_DL); /* delete a line */
10098 }
10099 else /* type == USE_T_CE */
10100 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010101 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 out_str(T_CE); /* erase a line */
10103 }
10104 screen_start(); /* don't know where cursor is now */
10105 }
10106 }
10107
10108 /*
10109 * If the 'db' flag is set, we need to clear the lines that have been
10110 * scrolled up at the bottom of the region.
10111 */
10112 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10113 {
10114 for (i = line_count; i > 0; --i)
10115 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010116 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 out_str(T_CE); /* erase a line */
10118 screen_start(); /* don't know where cursor is now */
10119 }
10120 }
10121
10122#ifdef FEAT_GUI
10123 gui_can_update_cursor();
10124 if (gui.in_use)
10125 out_flush(); /* always flush after a scroll */
10126#endif
10127
10128 return OK;
10129}
10130
10131/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010132 * Return TRUE when postponing displaying the mode message: when not redrawing
10133 * or inside a mapping.
10134 */
10135 int
10136skip_showmode()
10137{
10138 // Call char_avail() only when we are going to show something, because it
10139 // takes a bit of time. redrawing() may also call char_avail_avail().
10140 if (global_busy
10141 || msg_silent != 0
10142 || !redrawing()
10143 || (char_avail() && !KeyTyped))
10144 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010145 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010146 return TRUE;
10147 }
10148 return FALSE;
10149}
10150
10151/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010152 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 *
10154 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10155 * If clear_cmdline is FALSE there may be a message there that needs to be
10156 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010157 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 * Return the length of the message (0 if no message).
10159 */
10160 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010161showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162{
10163 int need_clear;
10164 int length = 0;
10165 int do_mode;
10166 int attr;
10167 int nwr_save;
10168#ifdef FEAT_INS_EXPAND
10169 int sub_attr;
10170#endif
10171
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010172 do_mode = ((p_smd && msg_silent == 0)
10173 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010174 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010175 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010176 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010178 if (skip_showmode())
10179 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180
10181 nwr_save = need_wait_return;
10182
10183 /* wait a bit before overwriting an important message */
10184 check_for_delay(FALSE);
10185
10186 /* if the cmdline is more than one line high, erase top lines */
10187 need_clear = clear_cmdline;
10188 if (clear_cmdline && cmdline_row < Rows - 1)
10189 msg_clr_cmdline(); /* will reset clear_cmdline */
10190
10191 /* Position on the last line in the window, column 0 */
10192 msg_pos_mode();
10193 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010194 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 if (do_mode)
10196 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010197 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010199 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010200# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010201 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010202# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010203 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010204# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010205 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010206# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010207 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010209 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210# endif
10211#endif
10212#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10213 if (gui.in_use)
10214 {
10215 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010216 {
10217 /* HANGUL */
10218 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010219 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010220 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010221 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 }
10224#endif
10225#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010226 /* CTRL-X in Insert mode */
10227 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 {
10229 /* These messages can get long, avoid a wrap in a narrow
10230 * window. Prefer showing edit_submode_extra. */
10231 length = (Rows - msg_row) * Columns - 3;
10232 if (edit_submode_extra != NULL)
10233 length -= vim_strsize(edit_submode_extra);
10234 if (length > 0)
10235 {
10236 if (edit_submode_pre != NULL)
10237 length -= vim_strsize(edit_submode_pre);
10238 if (length - vim_strsize(edit_submode) > 0)
10239 {
10240 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010241 msg_puts_attr((char *)edit_submode_pre, attr);
10242 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 }
10244 if (edit_submode_extra != NULL)
10245 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010246 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010248 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 else
10250 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010251 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 }
10253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 }
10255 else
10256#endif
10257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010259 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010260 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010261 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 else if (State & INSERT)
10263 {
10264#ifdef FEAT_RIGHTLEFT
10265 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010266 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010268 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010270 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010271 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010273 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010275 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276#ifdef FEAT_RIGHTLEFT
10277 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010278 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279#endif
10280#ifdef FEAT_KEYMAP
10281 if (State & LANGMAP)
10282 {
10283# ifdef FEAT_ARABIC
10284 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010285 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286 else
10287# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010288 if (get_keymap_str(curwin, (char_u *)" (%s)",
10289 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010290 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 }
10292#endif
10293 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010294 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 if (VIsual_active)
10297 {
10298 char *p;
10299
10300 /* Don't concatenate separate words to avoid translation
10301 * problems. */
10302 switch ((VIsual_select ? 4 : 0)
10303 + (VIsual_mode == Ctrl_V) * 2
10304 + (VIsual_mode == 'V'))
10305 {
10306 case 0: p = N_(" VISUAL"); break;
10307 case 1: p = N_(" VISUAL LINE"); break;
10308 case 2: p = N_(" VISUAL BLOCK"); break;
10309 case 4: p = N_(" SELECT"); break;
10310 case 5: p = N_(" SELECT LINE"); break;
10311 default: p = N_(" SELECT BLOCK"); break;
10312 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010313 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010315 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010317
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 need_clear = TRUE;
10319 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010320 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321#ifdef FEAT_INS_EXPAND
10322 && edit_submode == NULL /* otherwise it gets too long */
10323#endif
10324 )
10325 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010326 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 need_clear = TRUE;
10328 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010329
10330 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010331 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 msg_clr_eos();
10333 msg_didout = FALSE; /* overwrite this message */
10334 length = msg_col;
10335 msg_col = 0;
10336 need_wait_return = nwr_save; /* never ask for hit-return for this */
10337 }
10338 else if (clear_cmdline && msg_silent == 0)
10339 /* Clear the whole command line. Will reset "clear_cmdline". */
10340 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010341 else if (redraw_mode)
10342 {
10343 msg_pos_mode();
10344 msg_clr_eos();
10345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346
10347#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 /* In Visual mode the size of the selected area must be redrawn. */
10349 if (VIsual_active)
10350 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351
10352 /* If the last window has no status line, the ruler is after the mode
10353 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010354 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010355 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356#endif
10357 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010358 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 clear_cmdline = FALSE;
10360
10361 return length;
10362}
10363
10364/*
10365 * Position for a mode message.
10366 */
10367 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010368msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369{
10370 msg_col = 0;
10371 msg_row = Rows - 1;
10372}
10373
10374/*
10375 * Delete mode message. Used when ESC is typed which is expected to end
10376 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010377 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 */
10379 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010380unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381{
10382 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010383 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 */
10385 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10386 redraw_cmdline = TRUE; /* delete mode later */
10387 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010388 clearmode();
10389}
10390
10391/*
10392 * Clear the mode message.
10393 */
10394 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010395clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010396{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010397 int save_msg_row = msg_row;
10398 int save_msg_col = msg_col;
10399
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010400 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010401 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010402 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010403 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010404
10405 msg_col = save_msg_col;
10406 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407}
10408
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010409 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010410recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010411{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010412 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010413 if (!shortmess(SHM_RECORDING))
10414 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010415 char s[4];
10416
10417 sprintf(s, " @%c", reg_recording);
10418 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010419 }
10420}
10421
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010422/*
10423 * Draw the tab pages line at the top of the Vim window.
10424 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010425 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010426draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010427{
10428 int tabcount = 0;
10429 tabpage_T *tp;
10430 int tabwidth;
10431 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010432 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010433 int attr;
10434 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010435 win_T *cwp;
10436 int wincount;
10437 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010438 int c;
10439 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010440 int attr_sel = HL_ATTR(HLF_TPS);
10441 int attr_nosel = HL_ATTR(HLF_TP);
10442 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010443 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010444 int room;
10445 int use_sep_chars = (t_colors < 8
10446#ifdef FEAT_GUI
10447 && !gui.in_use
10448#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010449#ifdef FEAT_TERMGUICOLORS
10450 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010451#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010452 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010453
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010454 if (ScreenLines == NULL)
10455 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010456 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010457
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010458#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010459 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010460 if (gui_use_tabline())
10461 {
10462 gui_update_tabline();
10463 return;
10464 }
10465#endif
10466
10467 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010468 return;
10469
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010470#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010471 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010472
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010473 /* Use the 'tabline' option if it's set. */
10474 if (*p_tal != NUL)
10475 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010476 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010477
10478 /* Check for an error. If there is one we would loop in redrawing the
10479 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010480 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010481 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010482 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010483 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010484 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010485 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010486 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010487 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010488#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010489 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010490 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010491 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010492
Bram Moolenaar238a5642006-02-21 22:12:05 +000010493 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10494 if (tabwidth < 6)
10495 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010496
Bram Moolenaar238a5642006-02-21 22:12:05 +000010497 attr = attr_nosel;
10498 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010499 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10500 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010501 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010502 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010503
Bram Moolenaar238a5642006-02-21 22:12:05 +000010504 if (tp->tp_topframe == topframe)
10505 attr = attr_sel;
10506 if (use_sep_chars && col > 0)
10507 screen_putchar('|', 0, col++, attr);
10508
10509 if (tp->tp_topframe != topframe)
10510 attr = attr_nosel;
10511
10512 screen_putchar(' ', 0, col++, attr);
10513
10514 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010515 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010516 cwp = curwin;
10517 wp = firstwin;
10518 }
10519 else
10520 {
10521 cwp = tp->tp_curwin;
10522 wp = tp->tp_firstwin;
10523 }
10524
10525 modified = FALSE;
10526 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10527 if (bufIsChanged(wp->w_buffer))
10528 modified = TRUE;
10529 if (modified || wincount > 1)
10530 {
10531 if (wincount > 1)
10532 {
10533 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010534 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010535 if (col + len >= Columns - 3)
10536 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010537 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010538#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010539 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010540#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010541 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010542#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010543 );
10544 col += len;
10545 }
10546 if (modified)
10547 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10548 screen_putchar(' ', 0, col++, attr);
10549 }
10550
10551 room = scol - col + tabwidth - 1;
10552 if (room > 0)
10553 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010554 /* Get buffer name in NameBuff[] */
10555 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010556 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010557 len = vim_strsize(NameBuff);
10558 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010559 if (has_mbyte)
10560 while (len > room)
10561 {
10562 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010563 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010564 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010565 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010566 {
10567 p += len - room;
10568 len = room;
10569 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010570 if (len > Columns - col - 1)
10571 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010572
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010573 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010574 col += len;
10575 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010576 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010577
10578 /* Store the tab page number in TabPageIdxs[], so that
10579 * jump_to_mouse() knows where each one is. */
10580 ++tabcount;
10581 while (scol < col)
10582 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010583 }
10584
Bram Moolenaar238a5642006-02-21 22:12:05 +000010585 if (use_sep_chars)
10586 c = '_';
10587 else
10588 c = ' ';
10589 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010590
10591 /* Put an "X" for closing the current tab if there are several. */
10592 if (first_tabpage->tp_next != NULL)
10593 {
10594 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10595 TabPageIdxs[Columns - 1] = -999;
10596 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010597 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010598
10599 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10600 * set. */
10601 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010602}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010603
10604/*
10605 * Get buffer name for "buf" into NameBuff[].
10606 * Takes care of special buffer names and translates special characters.
10607 */
10608 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010609get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010610{
10611 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010612 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010613 else
10614 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10615 trans_characters(NameBuff, MAXPATHL);
10616}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010617
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618/*
10619 * Get the character to use in a status line. Get its attributes in "*attr".
10620 */
10621 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010622fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623{
10624 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010625
10626#ifdef FEAT_TERMINAL
10627 if (bt_terminal(wp->w_buffer))
10628 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010629 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010630 {
10631 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010632 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010633 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010634 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010635 {
10636 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010637 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010638 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010639 }
10640 else
10641#endif
10642 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010644 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 fill = fill_stl;
10646 }
10647 else
10648 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010649 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 fill = fill_stlnc;
10651 }
10652 /* Use fill when there is highlighting, and highlighting of current
10653 * window differs, or the fillchars differ, or this is not the
10654 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010655 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010656 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 || (fill_stl != fill_stlnc)))
10658 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010659 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 return '^';
10661 return '=';
10662}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664/*
10665 * Get the character to use in a separator between vertically split windows.
10666 * Get its attributes in "*attr".
10667 */
10668 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010669fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010671 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 if (*attr == 0 && fill_vert == ' ')
10673 return '|';
10674 else
10675 return fill_vert;
10676}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677
10678/*
10679 * Return TRUE if redrawing should currently be done.
10680 */
10681 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010682redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010684#ifdef FEAT_EVAL
10685 if (disable_redraw_for_testing)
10686 return 0;
10687 else
10688#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010689 return ((!RedrawingDisabled
10690#ifdef FEAT_EVAL
10691 || ignore_redraw_flag_for_testing
10692#endif
10693 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694}
10695
10696/*
10697 * Return TRUE if printing messages should currently be done.
10698 */
10699 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010700messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701{
10702 return (!(p_lz && char_avail() && !KeyTyped));
10703}
10704
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010705#ifdef FEAT_MENU
10706/*
10707 * Draw the window toolbar.
10708 */
10709 static void
10710redraw_win_toolbar(win_T *wp)
10711{
10712 vimmenu_T *menu;
10713 int item_idx = 0;
10714 int item_count = 0;
10715 int col = 0;
10716 int next_col;
10717 int off = (int)(current_ScreenLine - ScreenLines);
10718 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10719 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10720
10721 vim_free(wp->w_winbar_items);
10722 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10723 ++item_count;
10724 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10725 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10726
10727 /* TODO: use fewer spaces if there is not enough room */
10728 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010729 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010730 {
10731 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010732 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010733 break;
10734 if (col > 1)
10735 {
10736 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010737 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010738 break;
10739 }
10740
10741 wp->w_winbar_items[item_idx].wb_startcol = col;
10742 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010743 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010744 break;
10745
10746 next_col = text_to_screenline(wp, menu->name, col);
10747 while (col < next_col)
10748 {
10749 ScreenAttrs[off + col] = button_attr;
10750 ++col;
10751 }
10752 wp->w_winbar_items[item_idx].wb_endcol = col;
10753 wp->w_winbar_items[item_idx].wb_menu = menu;
10754 ++item_idx;
10755
Bram Moolenaar02631462017-09-22 15:20:32 +020010756 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010757 break;
10758 space_to_screenline(off + col, button_attr);
10759 ++col;
10760 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010761 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010762 {
10763 space_to_screenline(off + col, fill_attr);
10764 ++col;
10765 }
10766 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10767
Bram Moolenaar02631462017-09-22 15:20:32 +020010768 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10769 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010770}
10771#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010772
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773/*
10774 * Show current status info in ruler and various other places
10775 * If always is FALSE, only show ruler if position has changed.
10776 */
10777 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010778showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779{
10780 if (!always && !redrawing())
10781 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010782#ifdef FEAT_INS_EXPAND
10783 if (pum_visible())
10784 {
10785 /* Don't redraw right now, do it later. */
10786 curwin->w_redr_status = TRUE;
10787 return;
10788 }
10789#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010790#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010791 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010792 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 else
10794#endif
10795#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010796 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797#endif
10798
10799#ifdef FEAT_TITLE
10800 if (need_maketitle
10801# ifdef FEAT_STL_OPT
10802 || (p_icon && (stl_syntax & STL_IN_ICON))
10803 || (p_title && (stl_syntax & STL_IN_TITLE))
10804# endif
10805 )
10806 maketitle();
10807#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010808 /* Redraw the tab pages line if needed. */
10809 if (redraw_tabline)
10810 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811}
10812
10813#ifdef FEAT_CMDL_INFO
10814 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010815win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010817#define RULER_BUF_LEN 70
10818 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 int row;
10820 int fillchar;
10821 int attr;
10822 int empty_line = FALSE;
10823 colnr_T virtcol;
10824 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010825 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 int this_ru_col;
10828 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010829 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830
10831 /* If 'ruler' off or redrawing disabled, don't do anything */
10832 if (!p_ru)
10833 return;
10834
10835 /*
10836 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10837 * after deleting lines, before cursor.lnum is corrected.
10838 */
10839 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10840 return;
10841
10842#ifdef FEAT_INS_EXPAND
10843 /* Don't draw the ruler while doing insert-completion, it might overwrite
10844 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 if (edit_submode != NULL)
10847 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010848 // Don't draw the ruler when the popup menu is visible, it may overlap.
10849 // Except when the popup menu will be redrawn anyway.
10850 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010851 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852#endif
10853
10854#ifdef FEAT_STL_OPT
10855 if (*p_ruf)
10856 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010857 int save_called_emsg = called_emsg;
10858
10859 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010861 if (called_emsg)
10862 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010863 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010864 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 return;
10866 }
10867#endif
10868
10869 /*
10870 * Check if not in Insert mode and the line is empty (will show "0-1").
10871 */
10872 if (!(State & INSERT)
10873 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10874 empty_line = TRUE;
10875
10876 /*
10877 * Only draw the ruler when something changed.
10878 */
10879 validate_virtcol_win(wp);
10880 if ( redraw_cmdline
10881 || always
10882 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10883 || wp->w_cursor.col != wp->w_ru_cursor.col
10884 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 || wp->w_topline != wp->w_ru_topline
10887 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10888#ifdef FEAT_DIFF
10889 || wp->w_topfill != wp->w_ru_topfill
10890#endif
10891 || empty_line != wp->w_ru_empty)
10892 {
10893 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 if (wp->w_status_height)
10895 {
10896 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010897 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010898 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010899 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 }
10901 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 {
10903 row = Rows - 1;
10904 fillchar = ' ';
10905 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 width = Columns;
10907 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 }
10909
10910 /* In list mode virtcol needs to be recomputed */
10911 virtcol = wp->w_virtcol;
10912 if (wp->w_p_list && lcs_tab1 == NUL)
10913 {
10914 wp->w_p_list = FALSE;
10915 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10916 wp->w_p_list = TRUE;
10917 }
10918
10919 /*
10920 * Some sprintfs return the length, some return a pointer.
10921 * To avoid portability problems we use strlen() here.
10922 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010923 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10925 ? 0L
10926 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010927 len = STRLEN(buffer);
10928 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10930 (int)virtcol + 1);
10931
10932 /*
10933 * Add a "50%" if there is room for it.
10934 * On the last line, don't print in the last column (scrolls the
10935 * screen up on some terminals).
10936 */
10937 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010938 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 this_ru_col = ru_col - (Columns - width);
10943 if (this_ru_col < 0)
10944 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 /* Never use more than half the window/screen width, leave the other
10946 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010947 if (this_ru_col < (width + 1) / 2)
10948 this_ru_col = (width + 1) / 2;
10949 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010951 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010952 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 if (has_mbyte)
10955 i += (*mb_char2bytes)(fillchar, buffer + i);
10956 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 buffer[i++] = fillchar;
10958 ++o;
10959 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010960 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 }
10962 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 if (has_mbyte)
10964 {
10965 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010966 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 {
10968 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010969 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 {
10971 buffer[i] = NUL;
10972 break;
10973 }
10974 }
10975 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010976 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020010977 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978
Bram Moolenaar4033c552017-09-16 20:54:51 +020010979 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 i = redraw_cmdline;
10981 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020010982 this_ru_col + off + (int)STRLEN(buffer),
10983 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984 fillchar, fillchar, attr);
10985 /* don't redraw the cmdline because of showing the ruler */
10986 redraw_cmdline = i;
10987 wp->w_ru_cursor = wp->w_cursor;
10988 wp->w_ru_virtcol = wp->w_virtcol;
10989 wp->w_ru_empty = empty_line;
10990 wp->w_ru_topline = wp->w_topline;
10991 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10992#ifdef FEAT_DIFF
10993 wp->w_ru_topfill = wp->w_topfill;
10994#endif
10995 }
10996}
10997#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010998
10999#if defined(FEAT_LINEBREAK) || defined(PROTO)
11000/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011001 * Return the width of the 'number' and 'relativenumber' column.
11002 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011003 * Otherwise it depends on 'numberwidth' and the line count.
11004 */
11005 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011006number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011007{
11008 int n;
11009 linenr_T lnum;
11010
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011011 if (wp->w_p_rnu && !wp->w_p_nu)
11012 /* cursor line shows "0" */
11013 lnum = wp->w_height;
11014 else
11015 /* cursor line shows absolute line number */
11016 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011017
Bram Moolenaar6b314672015-03-20 15:42:10 +010011018 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011019 return wp->w_nrwidth_width;
11020 wp->w_nrwidth_line_count = lnum;
11021
11022 n = 0;
11023 do
11024 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011025 lnum /= 10;
11026 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011027 } while (lnum > 0);
11028
11029 /* 'numberwidth' gives the minimal width plus one */
11030 if (n < wp->w_p_nuw - 1)
11031 n = wp->w_p_nuw - 1;
11032
11033 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011034 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011035 return n;
11036}
11037#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011038
Bram Moolenaar113e1072019-01-20 15:30:40 +010011039#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011040/*
11041 * Return the current cursor column. This is the actual position on the
11042 * screen. First column is 0.
11043 */
11044 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011045screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011046{
11047 return screen_cur_col;
11048}
11049
11050/*
11051 * Return the current cursor row. This is the actual position on the screen.
11052 * First row is 0.
11053 */
11054 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011055screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011056{
11057 return screen_cur_row;
11058}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011059#endif