blob: 95a739af0faf3c11b8c6ac69eca86fca0ac2989b [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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100126static void win_draw_end(win_T *wp, int c1, int c2, 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)
235 {
236 redraw_win_later(wp, type);
237 }
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100238 // This may be needed when switching tabs.
239 if (must_redraw < type)
240 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241}
242
243/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000244 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
246 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100247redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248{
249 redraw_buf_later(curbuf, type);
250}
251
252 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100253redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255 win_T *wp;
256
257 FOR_ALL_WINDOWS(wp)
258 {
259 if (wp->w_buffer == buf)
260 redraw_win_later(wp, type);
261 }
262}
263
Bram Moolenaar113e1072019-01-20 15:30:40 +0100264#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200265 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100266redraw_buf_line_later(buf_T *buf, linenr_T lnum)
267{
268 win_T *wp;
269
270 FOR_ALL_WINDOWS(wp)
271 if (wp->w_buffer == buf && lnum >= wp->w_topline
272 && lnum < wp->w_botline)
273 redrawWinline(wp, lnum);
274}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100275#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100276
Bram Moolenaar113e1072019-01-20 15:30:40 +0100277#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100278 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200279redraw_buf_and_status_later(buf_T *buf, int type)
280{
281 win_T *wp;
282
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200283#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200284 if (wild_menu_showing != 0)
285 /* Don't redraw while the command line completion is displayed, it
286 * would disappear. */
287 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200288#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200289 FOR_ALL_WINDOWS(wp)
290 {
291 if (wp->w_buffer == buf)
292 {
293 redraw_win_later(wp, type);
294 wp->w_redr_status = TRUE;
295 }
296 }
297}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100298#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200299
Bram Moolenaar113e1072019-01-20 15:30:40 +0100300#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200302 * Redraw as soon as possible. When the command line is not scrolled redraw
303 * right away and restore what was on the command line.
304 * Return a code indicating what happened.
305 */
306 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100307redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308{
309 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200310 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200311 int r;
312 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200313 schar_T *screenline; /* copy from ScreenLines[] */
314 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200315 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200316 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200317 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200318 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200319
320 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200321 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 return ret;
323
324 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200325 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200326 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200327 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200328 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200329 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200330 if (screenline == NULL || screenattr == NULL)
331 ret = 2;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200332 if (enc_utf8)
333 {
334 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200335 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200336 if (screenlineUC == NULL)
337 ret = 2;
338 for (i = 0; i < p_mco; ++i)
339 {
340 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200341 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200342 if (screenlineC[i] == NULL)
343 ret = 2;
344 }
345 }
346 if (enc_dbcs == DBCS_JPNU)
347 {
348 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200349 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200350 if (screenline2 == NULL)
351 ret = 2;
352 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200353
354 if (ret != 2)
355 {
356 /* Save the text displayed in the command line area. */
357 for (r = 0; r < rows; ++r)
358 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200359 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200360 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 (size_t)cols * sizeof(schar_T));
362 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200364 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 if (enc_utf8)
366 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 mch_memmove(screenlineC[i] + r * cols,
372 ScreenLinesC[i] + LineOffset[cmdline_row + r],
373 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200374 }
375 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200376 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200377 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200379 }
380
381 update_screen(0);
382 ret = 3;
383
384 if (must_redraw == 0)
385 {
386 int off = (int)(current_ScreenLine - ScreenLines);
387
388 /* Restore the text displayed in the command line area. */
389 for (r = 0; r < rows; ++r)
390 {
391 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200392 screenline + r * cols,
393 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200394 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200395 screenattr + r * cols,
396 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200397 if (enc_utf8)
398 {
399 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200400 screenlineUC + r * cols,
401 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402 for (i = 0; i < p_mco; ++i)
403 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200404 screenlineC[i] + r * cols,
405 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200406 }
407 if (enc_dbcs == DBCS_JPNU)
408 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200409 screenline2 + r * cols,
410 (size_t)cols * sizeof(schar_T));
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200411 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200412 }
413 ret = 4;
414 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200415 }
416
417 vim_free(screenline);
418 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200419 if (enc_utf8)
420 {
421 vim_free(screenlineUC);
422 for (i = 0; i < p_mco; ++i)
423 vim_free(screenlineC[i]);
424 }
425 if (enc_dbcs == DBCS_JPNU)
426 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200427
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200428 /* Show the intro message when appropriate. */
429 maybe_intro_message();
430
431 setcursor();
432
Bram Moolenaar2951b772013-07-03 12:45:31 +0200433 return ret;
434}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100435#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200436
437/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100438 * Invoked after an asynchronous callback is called.
439 * If an echo command was used the cursor needs to be put back where
440 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200441 * If "call_update_screen" is FALSE don't call update_screen() when at the
442 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100443 */
444 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200445redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100446{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200447 ++redrawing_for_callback;
448
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100449 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200450 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100451 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200452 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200453 // Don't redraw when in prompt_for_number().
454 if (cmdline_row > 0)
455 {
456 // Redrawing only works when the screen didn't scroll. Don't clear
457 // wildmenu entries.
458 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200459#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200460 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200461#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200462 && call_update_screen)
463 update_screen(0);
464
465 // Redraw in the same position, so that the user can continue
466 // editing the command.
467 redrawcmdline_ex(FALSE);
468 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200469 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200470 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100471 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200472 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200473 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100474 setcursor();
475 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100476 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100477#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100478 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200479 // Don't update the cursor when it is blinking and off to avoid
480 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100481 out_flush_cursor(FALSE, FALSE);
482 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100483#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100484 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200485
486 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100487}
488
489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 * Changed something in the current window, at buffer line "lnum", that
491 * requires that line and possibly other lines to be redrawn.
492 * Used when entering/leaving Insert mode with the cursor on a folded line.
493 * Used to remove the "$" from a change command.
494 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
495 * may become invalid and the whole window will have to be redrawn.
496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100498redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200499 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100500 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200502 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
503 wp->w_redraw_top = lnum;
504 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
505 wp->w_redraw_bot = lnum;
506 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507}
508
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200509 void
510reset_updating_screen(int may_resize_shell UNUSED)
511{
512 updating_screen = FALSE;
513#ifdef FEAT_GUI
514 if (may_resize_shell)
515 gui_may_resize_shell();
516#endif
517#ifdef FEAT_TERMINAL
518 term_check_channel_closed_recently();
519#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200520
521#ifdef HAVE_DROP_FILE
522 // If handle_drop() was called while updating_screen was TRUE need to
523 // handle the drop now.
524 handle_any_postponed_drop();
525#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200526}
527
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200529 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 */
531 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100532update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
534 redraw_curbuf_later(type);
535 update_screen(type);
536}
537
538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 * Based on the current value of curwin->w_topline, transfer a screenfull
540 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200541 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200543 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200544update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200546 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 win_T *wp;
548 static int did_intro = FALSE;
549#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
550 int did_one;
551#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200552#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200553 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200554 int gui_cursor_col;
555 int gui_cursor_row;
556#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200557 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000559 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200561 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200563 if (type == VALID_NO_UPDATE)
564 {
565 no_update = TRUE;
566 type = 0;
567 }
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 if (must_redraw)
570 {
571 if (type < must_redraw) /* use maximal type */
572 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000573
574 /* must_redraw is reset here, so that when we run into some weird
575 * reason to redraw while busy redrawing (e.g., asynchronous
576 * scrolling), or update_topline() in win_update() will cause a
577 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 must_redraw = 0;
579 }
580
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200581 /* May need to update w_lines[]. */
582 if (curwin->w_lines_valid == 0 && type < NOT_VALID
583#ifdef FEAT_TERMINAL
584 && !term_do_update_window(curwin)
585#endif
586 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 type = NOT_VALID;
588
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000589 /* Postpone the redrawing when it's not needed and when being called
590 * recursively. */
591 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 {
593 redraw_later(type); /* remember type for next time */
594 must_redraw = type;
595 if (type > INVERTED_ALL)
596 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200597 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 }
599
600 updating_screen = TRUE;
601#ifdef FEAT_SYN_HL
602 ++display_tick; /* let syntax code know we're in a next round of
603 * display updating */
604#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200605 if (no_update)
606 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608 /*
609 * if the screen was scrolled up when displaying a message, scroll it down
610 */
611 if (msg_scrolled)
612 {
613 clear_cmdline = TRUE;
614 if (msg_scrolled > Rows - 5) /* clearing is faster */
615 type = CLEAR;
616 else if (type != CLEAR)
617 {
618 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200619 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
620 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 type = CLEAR;
622 FOR_ALL_WINDOWS(wp)
623 {
624 if (W_WINROW(wp) < msg_scrolled)
625 {
626 if (W_WINROW(wp) + wp->w_height > msg_scrolled
627 && wp->w_redr_type < REDRAW_TOP
628 && wp->w_lines_valid > 0
629 && wp->w_topline == wp->w_lines[0].wl_lnum)
630 {
631 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
632 wp->w_redr_type = REDRAW_TOP;
633 }
634 else
635 {
636 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200637 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
638 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 }
641 }
642 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200643 if (!no_update)
644 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000645 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 }
647 msg_scrolled = 0;
648 need_wait_return = FALSE;
649 }
650
651 /* reset cmdline_row now (may have been changed temporarily) */
652 compute_cmdrow();
653
654 /* Check for changed highlighting */
655 if (need_highlight_changed)
656 highlight_changed();
657
658 if (type == CLEAR) /* first clear screen */
659 {
660 screenclear(); /* will reset clear_cmdline */
661 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200662 /* must_redraw may be set indirectly, avoid another redraw later */
663 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
665
666 if (clear_cmdline) /* going to clear cmdline (done below) */
667 check_for_delay(FALSE);
668
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000669#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200670 /* Force redraw when width of 'number' or 'relativenumber' column
671 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000672 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200673 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
674 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000675 curwin->w_redr_type = NOT_VALID;
676#endif
677
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 /*
679 * Only start redrawing if there is really something to do.
680 */
681 if (type == INVERTED)
682 update_curswant();
683 if (curwin->w_redr_type < type
684 && !((type == VALID
685 && curwin->w_lines[0].wl_valid
686#ifdef FEAT_DIFF
687 && curwin->w_topfill == curwin->w_old_topfill
688 && curwin->w_botfill == curwin->w_old_botfill
689#endif
690 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000692 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
694 && curwin->w_old_visual_mode == VIsual_mode
695 && (curwin->w_valid & VALID_VIRTCOL)
696 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 ))
698 curwin->w_redr_type = type;
699
Bram Moolenaar5a305422006-04-28 22:38:25 +0000700 /* Redraw the tab pages line if needed. */
701 if (redraw_tabline || type >= NOT_VALID)
702 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000703
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#ifdef FEAT_SYN_HL
705 /*
706 * Correct stored syntax highlighting info for changes in each displayed
707 * buffer. Each buffer must only be done once.
708 */
709 FOR_ALL_WINDOWS(wp)
710 {
711 if (wp->w_buffer->b_mod_set)
712 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 win_T *wwp;
714
715 /* Check if we already did this buffer. */
716 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
717 if (wwp->w_buffer == wp->w_buffer)
718 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200719 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 syn_stack_apply_changes(wp->w_buffer);
721 }
722 }
723#endif
724
725 /*
726 * Go from top to bottom through the windows, redrawing the ones that need
727 * it.
728 */
729#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
730 did_one = FALSE;
731#endif
732#ifdef FEAT_SEARCH_EXTRA
733 search_hl.rm.regprog = NULL;
734#endif
735 FOR_ALL_WINDOWS(wp)
736 {
737 if (wp->w_redr_type != 0)
738 {
739 cursor_off();
740#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
741 if (!did_one)
742 {
743 did_one = TRUE;
744# ifdef FEAT_SEARCH_EXTRA
745 start_search_hl();
746# endif
747# ifdef FEAT_CLIPBOARD
748 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200749 if (clip_star.available && clip_isautosel_star())
750 clip_update_selection(&clip_star);
751 if (clip_plus.available && clip_isautosel_plus())
752 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753# endif
754#ifdef FEAT_GUI
755 /* Remove the cursor before starting to do anything, because
756 * scrolling may make it difficult to redraw the text under
757 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200758 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200759 {
760 gui_cursor_col = gui.cursor_col;
761 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200763 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765#endif
766 }
767#endif
768 win_update(wp);
769 }
770
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 /* redraw status line after the window to minimize cursor movement */
772 if (wp->w_redr_status)
773 {
774 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200775 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 }
778#if defined(FEAT_SEARCH_EXTRA)
779 end_search_hl();
780#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100781#ifdef FEAT_INS_EXPAND
782 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200783 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 /* Reset b_mod_set flags. Going through all windows is probably faster
787 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200788 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200791 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
793 /* Clear or redraw the command line. Done last, because scrolling may
794 * mess up the command line. */
795 if (clear_cmdline || redraw_cmdline)
796 showmode();
797
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200798 if (no_update)
799 --no_win_do_lines_ins;
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200802 if (!did_intro)
803 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 did_intro = TRUE;
805
806#ifdef FEAT_GUI
807 /* Redraw the cursor and update the scrollbars when all screen updating is
808 * done. */
809 if (gui.in_use)
810 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200811 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200812 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100813 mch_disable_flush();
814 out_flush(); /* required before updating the cursor */
815 mch_enable_flush();
816
Bram Moolenaar144445d2016-07-08 21:41:54 +0200817 /* Put the GUI position where the cursor was, gui_update_cursor()
818 * uses that. */
819 gui.col = gui_cursor_col;
820 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200821 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100823 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200824 screen_cur_col = gui.col;
825 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200826 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100827 else
828 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 gui_update_scrollbars(FALSE);
830 }
831#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200832 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833}
834
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100835#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
836/*
837 * Prepare for updating one or more windows.
838 * Caller must check for "updating_screen" already set to avoid recursiveness.
839 */
840 static void
841update_prepare(void)
842{
843 cursor_off();
844 updating_screen = TRUE;
845#ifdef FEAT_GUI
846 /* Remove the cursor before starting to do anything, because scrolling may
847 * make it difficult to redraw the text under it. */
848 if (gui.in_use)
849 gui_undraw_cursor();
850#endif
851#ifdef FEAT_SEARCH_EXTRA
852 start_search_hl();
853#endif
854}
855
856/*
857 * Finish updating one or more windows.
858 */
859 static void
860update_finish(void)
861{
862 if (redraw_cmdline)
863 showmode();
864
865# ifdef FEAT_SEARCH_EXTRA
866 end_search_hl();
867# endif
868
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200869 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100870
871# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100872 /* Redraw the cursor and update the scrollbars when all screen updating is
873 * done. */
874 if (gui.in_use)
875 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100876 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100877 gui_update_scrollbars(FALSE);
878 }
879# endif
880}
881#endif
882
Bram Moolenaar860cae12010-06-05 23:22:07 +0200883#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200884/*
885 * Return TRUE if the cursor line in window "wp" may be concealed, according
886 * to the 'concealcursor' option.
887 */
888 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100889conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200890{
891 int c;
892
893 if (*wp->w_p_cocu == NUL)
894 return FALSE;
895 if (get_real_state() & VISUAL)
896 c = 'v';
897 else if (State & INSERT)
898 c = 'i';
899 else if (State & NORMAL)
900 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200901 else if (State & CMDLINE)
902 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200903 else
904 return FALSE;
905 return vim_strchr(wp->w_p_cocu, c) != NULL;
906}
907
908/*
909 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
910 */
911 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200912conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200913{
914 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
915 {
916 need_cursor_line_redraw = TRUE;
917 /* Need to recompute cursor column, e.g., when starting Visual mode
918 * without concealing. */
919 curs_columns(TRUE);
920 }
921}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922#endif
923
Bram Moolenaar113e1072019-01-20 15:30:40 +0100924#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100926update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
928 win_T *wp;
929 int doit = FALSE;
930
931# ifdef FEAT_FOLDING
932 win_foldinfo.fi_level = 0;
933# endif
934
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100935 // update/delete a specific sign
936 redraw_buf_line_later(buf, lnum);
937
938 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 if (wp->w_redr_type != 0)
941 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100943 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200944 * happening (recursive call), messages on the screen or still starting up.
945 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100946 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200947 || State == ASKMORE || State == HITRETURN
948 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100949#ifdef FEAT_GUI
950 || gui.starting
951#endif
952 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 return;
954
955 /* update all windows that need updating */
956 update_prepare();
957
Bram Moolenaar29323592016-07-24 22:04:11 +0200958 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 {
960 if (wp->w_redr_type != 0)
961 win_update(wp);
962 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200963 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965
966 update_finish();
967}
968#endif
969
970
971#if defined(FEAT_GUI) || defined(PROTO)
972/*
973 * Update a single window, its status line and maybe the command line msg.
974 * Used for the GUI scrollbar.
975 */
976 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100977updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000979 /* return if already busy updating */
980 if (updating_screen)
981 return;
982
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 update_prepare();
984
985#ifdef FEAT_CLIPBOARD
986 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200987 if (clip_star.available && clip_isautosel_star())
988 clip_update_selection(&clip_star);
989 if (clip_plus.available && clip_isautosel_plus())
990 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000994
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000995 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000996 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000997 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 if (wp->w_redr_status
1000# ifdef FEAT_CMDL_INFO
1001 || p_ru
1002# endif
1003# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001004 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005# endif
1006 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001007 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
1009 update_finish();
1010}
1011#endif
1012
1013/*
1014 * Update a single window.
1015 *
1016 * This may cause the windows below it also to be redrawn (when clearing the
1017 * screen or scrolling lines).
1018 *
1019 * How the window is redrawn depends on wp->w_redr_type. Each type also
1020 * implies the one below it.
1021 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001022 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1024 * INVERTED redraw the changed part of the Visual area
1025 * INVERTED_ALL redraw the whole Visual area
1026 * VALID 1. scroll up/down to adjust for a changed w_topline
1027 * 2. update lines at the top when scrolled down
1028 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001029 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 * b_mod_top and b_mod_bot.
1031 * - if wp->w_redraw_top non-zero, redraw lines between
1032 * wp->w_redraw_top and wp->w_redr_bot.
1033 * - continue redrawing when syntax status is invalid.
1034 * 4. if scrolled up, update lines at the bottom.
1035 * This results in three areas that may need updating:
1036 * top: from first row to top_end (when scrolled down)
1037 * mid: from mid_start to mid_end (update inversion or changed text)
1038 * bot: from bot_start to last row (when scrolled up)
1039 */
1040 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001041win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042{
1043 buf_T *buf = wp->w_buffer;
1044 int type;
1045 int top_end = 0; /* Below last row of the top area that needs
1046 updating. 0 when no top area updating. */
1047 int mid_start = 999;/* first row of the mid area that needs
1048 updating. 999 when no mid area updating. */
1049 int mid_end = 0; /* Below last row of the mid area that needs
1050 updating. 0 when no mid area updating. */
1051 int bot_start = 999;/* first row of the bot area that needs
1052 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 int scrolled_down = FALSE; /* TRUE when scrolled down when
1054 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001056 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 int top_to_mod = FALSE; /* redraw above mod_top */
1058#endif
1059
1060 int row; /* current window row to display */
1061 linenr_T lnum; /* current buffer lnum to display */
1062 int idx; /* current index in w_lines[] */
1063 int srow; /* starting row of the current line */
1064
1065 int eof = FALSE; /* if TRUE, we hit the end of the file */
1066 int didline = FALSE; /* if TRUE, we finished the last line */
1067 int i;
1068 long j;
1069 static int recursive = FALSE; /* being called recursively */
1070 int old_botline = wp->w_botline;
1071#ifdef FEAT_FOLDING
1072 long fold_count;
1073#endif
1074#ifdef FEAT_SYN_HL
1075 /* remember what happened to the previous line, to know if
1076 * check_visual_highlight() can be used */
1077#define DID_NONE 1 /* didn't update a line */
1078#define DID_LINE 2 /* updated a normal line */
1079#define DID_FOLD 3 /* updated a folded line */
1080 int did_update = DID_NONE;
1081 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1082#endif
1083 linenr_T mod_top = 0;
1084 linenr_T mod_bot = 0;
1085#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1086 int save_got_int;
1087#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001088#ifdef SYN_TIME_LIMIT
1089 proftime_T syntax_tm;
1090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091
1092 type = wp->w_redr_type;
1093
1094 if (type == NOT_VALID)
1095 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 wp->w_lines_valid = 0;
1098 }
1099
1100 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001101 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {
1103 wp->w_redr_type = 0;
1104 return;
1105 }
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 /* Window is zero-width: Only need to draw the separator. */
1108 if (wp->w_width == 0)
1109 {
1110 /* draw the vertical separator right of this window */
1111 draw_vsep_win(wp, 0);
1112 wp->w_redr_type = 0;
1113 return;
1114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001116#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001117 // If this window contains a terminal, redraw works completely differently.
1118 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001119 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001120 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001121# ifdef FEAT_MENU
1122 /* Draw the window toolbar, if there is one. */
1123 if (winbar_height(wp) > 0)
1124 redraw_win_toolbar(wp);
1125# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001126 wp->w_redr_type = 0;
1127 return;
1128 }
1129#endif
1130
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001132 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133#endif
1134
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001135#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001136 /* Force redraw when width of 'number' or 'relativenumber' column
1137 * changes. */
1138 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001139 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001140 {
1141 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001142 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001143 }
1144 else
1145#endif
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1148 {
1149 /*
1150 * When there are both inserted/deleted lines and specific lines to be
1151 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1152 * everything (only happens when redrawing is off for while).
1153 */
1154 type = NOT_VALID;
1155 }
1156 else
1157 {
1158 /*
1159 * Set mod_top to the first line that needs displaying because of
1160 * changes. Set mod_bot to the first line after the changes.
1161 */
1162 mod_top = wp->w_redraw_top;
1163 if (wp->w_redraw_bot != 0)
1164 mod_bot = wp->w_redraw_bot + 1;
1165 else
1166 mod_bot = 0;
1167 wp->w_redraw_top = 0; /* reset for next time */
1168 wp->w_redraw_bot = 0;
1169 if (buf->b_mod_set)
1170 {
1171 if (mod_top == 0 || mod_top > buf->b_mod_top)
1172 {
1173 mod_top = buf->b_mod_top;
1174#ifdef FEAT_SYN_HL
1175 /* Need to redraw lines above the change that may be included
1176 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001177 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001179 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (mod_top < 1)
1181 mod_top = 1;
1182 }
1183#endif
1184 }
1185 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1186 mod_bot = buf->b_mod_bot;
1187
1188#ifdef FEAT_SEARCH_EXTRA
1189 /* When 'hlsearch' is on and using a multi-line search pattern, a
1190 * change in one line may make the Search highlighting in a
1191 * previous line invalid. Simple solution: redraw all visible
1192 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001193 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001195 if (search_hl.rm.regprog != NULL
1196 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001198 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001199 {
1200 cur = wp->w_match_head;
1201 while (cur != NULL)
1202 {
1203 if (cur->match.regprog != NULL
1204 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001205 {
1206 top_to_mod = TRUE;
1207 break;
1208 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001209 cur = cur->next;
1210 }
1211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212#endif
1213 }
1214#ifdef FEAT_FOLDING
1215 if (mod_top != 0 && hasAnyFolding(wp))
1216 {
1217 linenr_T lnumt, lnumb;
1218
1219 /*
1220 * A change in a line can cause lines above it to become folded or
1221 * unfolded. Find the top most buffer line that may be affected.
1222 * If the line was previously folded and displayed, get the first
1223 * line of that fold. If the line is folded now, get the first
1224 * folded line. Use the minimum of these two.
1225 */
1226
1227 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1228 * the line below it. If there is no valid entry, use w_topline.
1229 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1230 * to this line. If there is no valid entry, use MAXLNUM. */
1231 lnumt = wp->w_topline;
1232 lnumb = MAXLNUM;
1233 for (i = 0; i < wp->w_lines_valid; ++i)
1234 if (wp->w_lines[i].wl_valid)
1235 {
1236 if (wp->w_lines[i].wl_lastlnum < mod_top)
1237 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1238 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1239 {
1240 lnumb = wp->w_lines[i].wl_lnum;
1241 /* When there is a fold column it might need updating
1242 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001243 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 ++lnumb;
1245 }
1246 }
1247
1248 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1249 if (mod_top > lnumt)
1250 mod_top = lnumt;
1251
1252 /* Now do the same for the bottom line (one above mod_bot). */
1253 --mod_bot;
1254 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1255 ++mod_bot;
1256 if (mod_bot < lnumb)
1257 mod_bot = lnumb;
1258 }
1259#endif
1260
1261 /* When a change starts above w_topline and the end is below
1262 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001263 * If the end of the change is above w_topline: do like no change was
1264 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 if (mod_top != 0 && mod_top < wp->w_topline)
1266 {
1267 if (mod_bot > wp->w_topline)
1268 mod_top = wp->w_topline;
1269#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001270 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 top_end = 1;
1272#endif
1273 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001274
1275 /* When line numbers are displayed need to redraw all lines below
1276 * inserted/deleted lines. */
1277 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1278 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 }
1280
1281 /*
1282 * When only displaying the lines at the top, set top_end. Used when
1283 * window has scrolled down for msg_scrolled.
1284 */
1285 if (type == REDRAW_TOP)
1286 {
1287 j = 0;
1288 for (i = 0; i < wp->w_lines_valid; ++i)
1289 {
1290 j += wp->w_lines[i].wl_size;
1291 if (j >= wp->w_upd_rows)
1292 {
1293 top_end = j;
1294 break;
1295 }
1296 }
1297 if (top_end == 0)
1298 /* not found (cannot happen?): redraw everything */
1299 type = NOT_VALID;
1300 else
1301 /* top area defined, the rest is VALID */
1302 type = VALID;
1303 }
1304
Bram Moolenaar367329b2007-08-30 11:53:22 +00001305 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001306 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1307 * non-zero and thus not FALSE) will indicate that screenclear() was not
1308 * called. */
1309 if (screen_cleared)
1310 screen_cleared = MAYBE;
1311
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 /*
1313 * If there are no changes on the screen that require a complete redraw,
1314 * handle three cases:
1315 * 1: we are off the top of the screen by a few lines: scroll down
1316 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1317 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1318 * w_lines[] that needs updating.
1319 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001320 if ((type == VALID || type == SOME_VALID
1321 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322#ifdef FEAT_DIFF
1323 && !wp->w_botfill && !wp->w_old_botfill
1324#endif
1325 )
1326 {
1327 if (mod_top != 0 && wp->w_topline == mod_top)
1328 {
1329 /*
1330 * w_topline is the first changed line, the scrolling will be done
1331 * further down.
1332 */
1333 }
1334 else if (wp->w_lines[0].wl_valid
1335 && (wp->w_topline < wp->w_lines[0].wl_lnum
1336#ifdef FEAT_DIFF
1337 || (wp->w_topline == wp->w_lines[0].wl_lnum
1338 && wp->w_topfill > wp->w_old_topfill)
1339#endif
1340 ))
1341 {
1342 /*
1343 * New topline is above old topline: May scroll down.
1344 */
1345#ifdef FEAT_FOLDING
1346 if (hasAnyFolding(wp))
1347 {
1348 linenr_T ln;
1349
1350 /* count the number of lines we are off, counting a sequence
1351 * of folded lines as one */
1352 j = 0;
1353 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1354 {
1355 ++j;
1356 if (j >= wp->w_height - 2)
1357 break;
1358 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1359 }
1360 }
1361 else
1362#endif
1363 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1364 if (j < wp->w_height - 2) /* not too far off */
1365 {
1366 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1367#ifdef FEAT_DIFF
1368 /* insert extra lines for previously invisible filler lines */
1369 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1370 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1371 - wp->w_old_topfill;
1372#endif
1373 if (i < wp->w_height - 2) /* less than a screen off */
1374 {
1375 /*
1376 * Try to insert the correct number of lines.
1377 * If not the last window, delete the lines at the bottom.
1378 * win_ins_lines may fail when the terminal can't do it.
1379 */
1380 if (i > 0)
1381 check_for_delay(FALSE);
1382 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1383 {
1384 if (wp->w_lines_valid != 0)
1385 {
1386 /* Need to update rows that are new, stop at the
1387 * first one that scrolled down. */
1388 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390
1391 /* Move the entries that were scrolled, disable
1392 * the entries for the lines to be redrawn. */
1393 if ((wp->w_lines_valid += j) > wp->w_height)
1394 wp->w_lines_valid = wp->w_height;
1395 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1396 wp->w_lines[idx] = wp->w_lines[idx - j];
1397 while (idx >= 0)
1398 wp->w_lines[idx--].wl_valid = FALSE;
1399 }
1400 }
1401 else
1402 mid_start = 0; /* redraw all lines */
1403 }
1404 else
1405 mid_start = 0; /* redraw all lines */
1406 }
1407 else
1408 mid_start = 0; /* redraw all lines */
1409 }
1410 else
1411 {
1412 /*
1413 * New topline is at or below old topline: May scroll up.
1414 * When topline didn't change, find first entry in w_lines[] that
1415 * needs updating.
1416 */
1417
1418 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1419 j = -1;
1420 row = 0;
1421 for (i = 0; i < wp->w_lines_valid; i++)
1422 {
1423 if (wp->w_lines[i].wl_valid
1424 && wp->w_lines[i].wl_lnum == wp->w_topline)
1425 {
1426 j = i;
1427 break;
1428 }
1429 row += wp->w_lines[i].wl_size;
1430 }
1431 if (j == -1)
1432 {
1433 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1434 * lines */
1435 mid_start = 0;
1436 }
1437 else
1438 {
1439 /*
1440 * Try to delete the correct number of lines.
1441 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1442 */
1443#ifdef FEAT_DIFF
1444 /* If the topline didn't change, delete old filler lines,
1445 * otherwise delete filler lines of the new topline... */
1446 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1447 row += wp->w_old_topfill;
1448 else
1449 row += diff_check_fill(wp, wp->w_topline);
1450 /* ... but don't delete new filler lines. */
1451 row -= wp->w_topfill;
1452#endif
1453 if (row > 0)
1454 {
1455 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001456 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1457 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 bot_start = wp->w_height - row;
1459 else
1460 mid_start = 0; /* redraw all lines */
1461 }
1462 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1463 {
1464 /*
1465 * Skip the lines (below the deleted lines) that are still
1466 * valid and don't need redrawing. Copy their info
1467 * upwards, to compensate for the deleted lines. Set
1468 * bot_start to the first row that needs redrawing.
1469 */
1470 bot_start = 0;
1471 idx = 0;
1472 for (;;)
1473 {
1474 wp->w_lines[idx] = wp->w_lines[j];
1475 /* stop at line that didn't fit, unless it is still
1476 * valid (no lines deleted) */
1477 if (row > 0 && bot_start + row
1478 + (int)wp->w_lines[j].wl_size > wp->w_height)
1479 {
1480 wp->w_lines_valid = idx + 1;
1481 break;
1482 }
1483 bot_start += wp->w_lines[idx++].wl_size;
1484
1485 /* stop at the last valid entry in w_lines[].wl_size */
1486 if (++j >= wp->w_lines_valid)
1487 {
1488 wp->w_lines_valid = idx;
1489 break;
1490 }
1491 }
1492#ifdef FEAT_DIFF
1493 /* Correct the first entry for filler lines at the top
1494 * when it won't get updated below. */
1495 if (wp->w_p_diff && bot_start > 0)
1496 wp->w_lines[0].wl_size =
1497 plines_win_nofill(wp, wp->w_topline, TRUE)
1498 + wp->w_topfill;
1499#endif
1500 }
1501 }
1502 }
1503
1504 /* When starting redraw in the first line, redraw all lines. When
1505 * there is only one window it's probably faster to clear the screen
1506 * first. */
1507 if (mid_start == 0)
1508 {
1509 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001510 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001511 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001512 /* Clear the screen when it was not done by win_del_lines() or
1513 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1514 * then. */
1515 if (screen_cleared != TRUE)
1516 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001517 /* The screen was cleared, redraw the tab pages line. */
1518 if (redraw_tabline)
1519 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001522
1523 /* When win_del_lines() or win_ins_lines() caused the screen to be
1524 * cleared (only happens for the first window) or when screenclear()
1525 * was called directly above, "must_redraw" will have been set to
1526 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1527 if (screen_cleared == TRUE)
1528 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 }
1530 else
1531 {
1532 /* Not VALID or INVERTED: redraw all lines. */
1533 mid_start = 0;
1534 mid_end = wp->w_height;
1535 }
1536
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001537 if (type == SOME_VALID)
1538 {
1539 /* SOME_VALID: redraw all lines. */
1540 mid_start = 0;
1541 mid_end = wp->w_height;
1542 type = NOT_VALID;
1543 }
1544
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 /* check if we are updating or removing the inverted part */
1546 if ((VIsual_active && buf == curwin->w_buffer)
1547 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1548 {
1549 linenr_T from, to;
1550
1551 if (VIsual_active)
1552 {
1553 if (VIsual_active
1554 && (VIsual_mode != wp->w_old_visual_mode
1555 || type == INVERTED_ALL))
1556 {
1557 /*
1558 * If the type of Visual selection changed, redraw the whole
1559 * selection. Also when the ownership of the X selection is
1560 * gained or lost.
1561 */
1562 if (curwin->w_cursor.lnum < VIsual.lnum)
1563 {
1564 from = curwin->w_cursor.lnum;
1565 to = VIsual.lnum;
1566 }
1567 else
1568 {
1569 from = VIsual.lnum;
1570 to = curwin->w_cursor.lnum;
1571 }
1572 /* redraw more when the cursor moved as well */
1573 if (wp->w_old_cursor_lnum < from)
1574 from = wp->w_old_cursor_lnum;
1575 if (wp->w_old_cursor_lnum > to)
1576 to = wp->w_old_cursor_lnum;
1577 if (wp->w_old_visual_lnum < from)
1578 from = wp->w_old_visual_lnum;
1579 if (wp->w_old_visual_lnum > to)
1580 to = wp->w_old_visual_lnum;
1581 }
1582 else
1583 {
1584 /*
1585 * Find the line numbers that need to be updated: The lines
1586 * between the old cursor position and the current cursor
1587 * position. Also check if the Visual position changed.
1588 */
1589 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1590 {
1591 from = curwin->w_cursor.lnum;
1592 to = wp->w_old_cursor_lnum;
1593 }
1594 else
1595 {
1596 from = wp->w_old_cursor_lnum;
1597 to = curwin->w_cursor.lnum;
1598 if (from == 0) /* Visual mode just started */
1599 from = to;
1600 }
1601
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001602 if (VIsual.lnum != wp->w_old_visual_lnum
1603 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 {
1605 if (wp->w_old_visual_lnum < from
1606 && wp->w_old_visual_lnum != 0)
1607 from = wp->w_old_visual_lnum;
1608 if (wp->w_old_visual_lnum > to)
1609 to = wp->w_old_visual_lnum;
1610 if (VIsual.lnum < from)
1611 from = VIsual.lnum;
1612 if (VIsual.lnum > to)
1613 to = VIsual.lnum;
1614 }
1615 }
1616
1617 /*
1618 * If in block mode and changed column or curwin->w_curswant:
1619 * update all lines.
1620 * First compute the actual start and end column.
1621 */
1622 if (VIsual_mode == Ctrl_V)
1623 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001624 colnr_T fromc, toc;
1625#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1626 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
Bram Moolenaar404406a2014-10-09 13:24:43 +02001628 if (curwin->w_p_lbr)
1629 ve_flags = VE_ALL;
1630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001632#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1633 ve_flags = save_ve_flags;
1634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 ++toc;
1636 if (curwin->w_curswant == MAXCOL)
1637 toc = MAXCOL;
1638
1639 if (fromc != wp->w_old_cursor_fcol
1640 || toc != wp->w_old_cursor_lcol)
1641 {
1642 if (from > VIsual.lnum)
1643 from = VIsual.lnum;
1644 if (to < VIsual.lnum)
1645 to = VIsual.lnum;
1646 }
1647 wp->w_old_cursor_fcol = fromc;
1648 wp->w_old_cursor_lcol = toc;
1649 }
1650 }
1651 else
1652 {
1653 /* Use the line numbers of the old Visual area. */
1654 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1655 {
1656 from = wp->w_old_cursor_lnum;
1657 to = wp->w_old_visual_lnum;
1658 }
1659 else
1660 {
1661 from = wp->w_old_visual_lnum;
1662 to = wp->w_old_cursor_lnum;
1663 }
1664 }
1665
1666 /*
1667 * There is no need to update lines above the top of the window.
1668 */
1669 if (from < wp->w_topline)
1670 from = wp->w_topline;
1671
1672 /*
1673 * If we know the value of w_botline, use it to restrict the update to
1674 * the lines that are visible in the window.
1675 */
1676 if (wp->w_valid & VALID_BOTLINE)
1677 {
1678 if (from >= wp->w_botline)
1679 from = wp->w_botline - 1;
1680 if (to >= wp->w_botline)
1681 to = wp->w_botline - 1;
1682 }
1683
1684 /*
1685 * Find the minimal part to be updated.
1686 * Watch out for scrolling that made entries in w_lines[] invalid.
1687 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1688 * top_end; need to redraw from top_end to the "to" line.
1689 * A middle mouse click with a Visual selection may change the text
1690 * above the Visual area and reset wl_valid, do count these for
1691 * mid_end (in srow).
1692 */
1693 if (mid_start > 0)
1694 {
1695 lnum = wp->w_topline;
1696 idx = 0;
1697 srow = 0;
1698 if (scrolled_down)
1699 mid_start = top_end;
1700 else
1701 mid_start = 0;
1702 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1703 {
1704 if (wp->w_lines[idx].wl_valid)
1705 mid_start += wp->w_lines[idx].wl_size;
1706 else if (!scrolled_down)
1707 srow += wp->w_lines[idx].wl_size;
1708 ++idx;
1709# ifdef FEAT_FOLDING
1710 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1711 lnum = wp->w_lines[idx].wl_lnum;
1712 else
1713# endif
1714 ++lnum;
1715 }
1716 srow += mid_start;
1717 mid_end = wp->w_height;
1718 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1719 {
1720 if (wp->w_lines[idx].wl_valid
1721 && wp->w_lines[idx].wl_lnum >= to + 1)
1722 {
1723 /* Only update until first row of this line */
1724 mid_end = srow;
1725 break;
1726 }
1727 srow += wp->w_lines[idx].wl_size;
1728 }
1729 }
1730 }
1731
1732 if (VIsual_active && buf == curwin->w_buffer)
1733 {
1734 wp->w_old_visual_mode = VIsual_mode;
1735 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1736 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001737 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 wp->w_old_curswant = curwin->w_curswant;
1739 }
1740 else
1741 {
1742 wp->w_old_visual_mode = 0;
1743 wp->w_old_cursor_lnum = 0;
1744 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001745 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
1748#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1749 /* reset got_int, otherwise regexp won't work */
1750 save_got_int = got_int;
1751 got_int = 0;
1752#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001753#ifdef SYN_TIME_LIMIT
1754 /* Set the time limit to 'redrawtime'. */
1755 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001756 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758#ifdef FEAT_FOLDING
1759 win_foldinfo.fi_level = 0;
1760#endif
1761
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001762#ifdef FEAT_MENU
1763 /*
1764 * Draw the window toolbar, if there is one.
1765 * TODO: only when needed.
1766 */
1767 if (winbar_height(wp) > 0)
1768 redraw_win_toolbar(wp);
1769#endif
1770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 /*
1772 * Update all the window rows.
1773 */
1774 idx = 0; /* first entry in w_lines[].wl_size */
1775 row = 0;
1776 srow = 0;
1777 lnum = wp->w_topline; /* first line shown in window */
1778 for (;;)
1779 {
1780 /* stop updating when reached the end of the window (check for _past_
1781 * the end of the window is at the end of the loop) */
1782 if (row == wp->w_height)
1783 {
1784 didline = TRUE;
1785 break;
1786 }
1787
1788 /* stop updating when hit the end of the file */
1789 if (lnum > buf->b_ml.ml_line_count)
1790 {
1791 eof = TRUE;
1792 break;
1793 }
1794
1795 /* Remember the starting row of the line that is going to be dealt
1796 * with. It is used further down when the line doesn't fit. */
1797 srow = row;
1798
1799 /*
1800 * Update a line when it is in an area that needs updating, when it
1801 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001802 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 * win_del_lines(), check if the current line includes it.
1804 * When syntax folding is being used, the saved syntax states will
1805 * already have been updated, we can't see where the syntax state is
1806 * the same again, just update until the end of the window.
1807 */
1808 if (row < top_end
1809 || (row >= mid_start && row < mid_end)
1810#ifdef FEAT_SEARCH_EXTRA
1811 || top_to_mod
1812#endif
1813 || idx >= wp->w_lines_valid
1814 || (row + wp->w_lines[idx].wl_size > bot_start)
1815 || (mod_top != 0
1816 && (lnum == mod_top
1817 || (lnum >= mod_top
1818 && (lnum < mod_bot
1819#ifdef FEAT_SYN_HL
1820 || did_update == DID_FOLD
1821 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001822 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 && (
1824# ifdef FEAT_FOLDING
1825 (foldmethodIsSyntax(wp)
1826 && hasAnyFolding(wp)) ||
1827# endif
1828 syntax_check_changed(lnum)))
1829#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001830#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001831 /* match in fixed position might need redraw
1832 * if lines were inserted or deleted */
1833 || (wp->w_match_head != NULL
1834 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 )))))
1837 {
1838#ifdef FEAT_SEARCH_EXTRA
1839 if (lnum == mod_top)
1840 top_to_mod = FALSE;
1841#endif
1842
1843 /*
1844 * When at start of changed lines: May scroll following lines
1845 * up or down to minimize redrawing.
1846 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001847 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 */
1849 if (lnum == mod_top
1850 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001851 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
1853 int old_rows = 0;
1854 int new_rows = 0;
1855 int xtra_rows;
1856 linenr_T l;
1857
1858 /* Count the old number of window rows, using w_lines[], which
1859 * should still contain the sizes for the lines as they are
1860 * currently displayed. */
1861 for (i = idx; i < wp->w_lines_valid; ++i)
1862 {
1863 /* Only valid lines have a meaningful wl_lnum. Invalid
1864 * lines are part of the changed area. */
1865 if (wp->w_lines[i].wl_valid
1866 && wp->w_lines[i].wl_lnum == mod_bot)
1867 break;
1868 old_rows += wp->w_lines[i].wl_size;
1869#ifdef FEAT_FOLDING
1870 if (wp->w_lines[i].wl_valid
1871 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1872 {
1873 /* Must have found the last valid entry above mod_bot.
1874 * Add following invalid entries. */
1875 ++i;
1876 while (i < wp->w_lines_valid
1877 && !wp->w_lines[i].wl_valid)
1878 old_rows += wp->w_lines[i++].wl_size;
1879 break;
1880 }
1881#endif
1882 }
1883
1884 if (i >= wp->w_lines_valid)
1885 {
1886 /* We can't find a valid line below the changed lines,
1887 * need to redraw until the end of the window.
1888 * Inserting/deleting lines has no use. */
1889 bot_start = 0;
1890 }
1891 else
1892 {
1893 /* Able to count old number of rows: Count new window
1894 * rows, and may insert/delete lines */
1895 j = idx;
1896 for (l = lnum; l < mod_bot; ++l)
1897 {
1898#ifdef FEAT_FOLDING
1899 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1900 ++new_rows;
1901 else
1902#endif
1903#ifdef FEAT_DIFF
1904 if (l == wp->w_topline)
1905 new_rows += plines_win_nofill(wp, l, TRUE)
1906 + wp->w_topfill;
1907 else
1908#endif
1909 new_rows += plines_win(wp, l, TRUE);
1910 ++j;
1911 if (new_rows > wp->w_height - row - 2)
1912 {
1913 /* it's getting too much, must redraw the rest */
1914 new_rows = 9999;
1915 break;
1916 }
1917 }
1918 xtra_rows = new_rows - old_rows;
1919 if (xtra_rows < 0)
1920 {
1921 /* May scroll text up. If there is not enough
1922 * remaining text or scrolling fails, must redraw the
1923 * rest. If scrolling works, must redraw the text
1924 * below the scrolled text. */
1925 if (row - xtra_rows >= wp->w_height - 2)
1926 mod_bot = MAXLNUM;
1927 else
1928 {
1929 check_for_delay(FALSE);
1930 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001931 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 mod_bot = MAXLNUM;
1933 else
1934 bot_start = wp->w_height + xtra_rows;
1935 }
1936 }
1937 else if (xtra_rows > 0)
1938 {
1939 /* May scroll text down. If there is not enough
1940 * remaining text of scrolling fails, must redraw the
1941 * rest. */
1942 if (row + xtra_rows >= wp->w_height - 2)
1943 mod_bot = MAXLNUM;
1944 else
1945 {
1946 check_for_delay(FALSE);
1947 if (win_ins_lines(wp, row + old_rows,
1948 xtra_rows, FALSE, FALSE) == FAIL)
1949 mod_bot = MAXLNUM;
1950 else if (top_end > row + old_rows)
1951 /* Scrolled the part at the top that requires
1952 * updating down. */
1953 top_end += xtra_rows;
1954 }
1955 }
1956
1957 /* When not updating the rest, may need to move w_lines[]
1958 * entries. */
1959 if (mod_bot != MAXLNUM && i != j)
1960 {
1961 if (j < i)
1962 {
1963 int x = row + new_rows;
1964
1965 /* move entries in w_lines[] upwards */
1966 for (;;)
1967 {
1968 /* stop at last valid entry in w_lines[] */
1969 if (i >= wp->w_lines_valid)
1970 {
1971 wp->w_lines_valid = j;
1972 break;
1973 }
1974 wp->w_lines[j] = wp->w_lines[i];
1975 /* stop at a line that won't fit */
1976 if (x + (int)wp->w_lines[j].wl_size
1977 > wp->w_height)
1978 {
1979 wp->w_lines_valid = j + 1;
1980 break;
1981 }
1982 x += wp->w_lines[j++].wl_size;
1983 ++i;
1984 }
1985 if (bot_start > x)
1986 bot_start = x;
1987 }
1988 else /* j > i */
1989 {
1990 /* move entries in w_lines[] downwards */
1991 j -= i;
1992 wp->w_lines_valid += j;
1993 if (wp->w_lines_valid > wp->w_height)
1994 wp->w_lines_valid = wp->w_height;
1995 for (i = wp->w_lines_valid; i - j >= idx; --i)
1996 wp->w_lines[i] = wp->w_lines[i - j];
1997
1998 /* The w_lines[] entries for inserted lines are
1999 * now invalid, but wl_size may be used above.
2000 * Reset to zero. */
2001 while (i >= idx)
2002 {
2003 wp->w_lines[i].wl_size = 0;
2004 wp->w_lines[i--].wl_valid = FALSE;
2005 }
2006 }
2007 }
2008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
2010
2011#ifdef FEAT_FOLDING
2012 /*
2013 * When lines are folded, display one line for all of them.
2014 * Otherwise, display normally (can be several display lines when
2015 * 'wrap' is on).
2016 */
2017 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2018 if (fold_count != 0)
2019 {
2020 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2021 ++row;
2022 --fold_count;
2023 wp->w_lines[idx].wl_folded = TRUE;
2024 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2025# ifdef FEAT_SYN_HL
2026 did_update = DID_FOLD;
2027# endif
2028 }
2029 else
2030#endif
2031 if (idx < wp->w_lines_valid
2032 && wp->w_lines[idx].wl_valid
2033 && wp->w_lines[idx].wl_lnum == lnum
2034 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002035 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 && srow + wp->w_lines[idx].wl_size > wp->w_height
2037#ifdef FEAT_DIFF
2038 && diff_check_fill(wp, lnum) == 0
2039#endif
2040 )
2041 {
2042 /* This line is not going to fit. Don't draw anything here,
2043 * will draw "@ " lines below. */
2044 row = wp->w_height + 1;
2045 }
2046 else
2047 {
2048#ifdef FEAT_SEARCH_EXTRA
2049 prepare_search_hl(wp, lnum);
2050#endif
2051#ifdef FEAT_SYN_HL
2052 /* Let the syntax stuff know we skipped a few lines. */
2053 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002054 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 syntax_end_parsing(syntax_last_parsed + 1);
2056#endif
2057
2058 /*
2059 * Display one line.
2060 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002061 row = win_line(wp, lnum, srow, wp->w_height,
2062 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
2064#ifdef FEAT_FOLDING
2065 wp->w_lines[idx].wl_folded = FALSE;
2066 wp->w_lines[idx].wl_lastlnum = lnum;
2067#endif
2068#ifdef FEAT_SYN_HL
2069 did_update = DID_LINE;
2070 syntax_last_parsed = lnum;
2071#endif
2072 }
2073
2074 wp->w_lines[idx].wl_lnum = lnum;
2075 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002076
2077 /* Past end of the window or end of the screen. Note that after
2078 * resizing wp->w_height may be end up too big. That's a problem
2079 * elsewhere, but prevent a crash here. */
2080 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 {
2082 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002083 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2085 ++idx;
2086 break;
2087 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002088 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 wp->w_lines[idx].wl_size = row - srow;
2090 ++idx;
2091#ifdef FEAT_FOLDING
2092 lnum += fold_count + 1;
2093#else
2094 ++lnum;
2095#endif
2096 }
2097 else
2098 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002099 if (wp->w_p_rnu)
2100 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002101#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002102 // 'relativenumber' set: The text doesn't need to be drawn, but
2103 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002104 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2105 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002106 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002107 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002108#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002109 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002110 }
2111
2112 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 row += wp->w_lines[idx++].wl_size;
2114 if (row > wp->w_height) /* past end of screen */
2115 break;
2116#ifdef FEAT_FOLDING
2117 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2118#else
2119 ++lnum;
2120#endif
2121#ifdef FEAT_SYN_HL
2122 did_update = DID_NONE;
2123#endif
2124 }
2125
2126 if (lnum > buf->b_ml.ml_line_count)
2127 {
2128 eof = TRUE;
2129 break;
2130 }
2131 }
2132 /*
2133 * End of loop over all window lines.
2134 */
2135
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002136#ifdef FEAT_VTP
2137 /* Rewrite the character at the end of the screen line. */
2138 if (use_vtp())
2139 {
2140 int i;
2141
2142 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002143 if (enc_utf8)
2144 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2145 LineOffset[i] + screen_Columns) > 1)
2146 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2147 else
2148 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2149 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002150 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2151 }
2152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
2154 if (idx > wp->w_lines_valid)
2155 wp->w_lines_valid = idx;
2156
2157#ifdef FEAT_SYN_HL
2158 /*
2159 * Let the syntax stuff know we stop parsing here.
2160 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002161 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 syntax_end_parsing(syntax_last_parsed + 1);
2163#endif
2164
2165 /*
2166 * If we didn't hit the end of the file, and we didn't finish the last
2167 * line we were working on, then the line didn't fit.
2168 */
2169 wp->w_empty_rows = 0;
2170#ifdef FEAT_DIFF
2171 wp->w_filler_rows = 0;
2172#endif
2173 if (!eof && !didline)
2174 {
2175 if (lnum == wp->w_topline)
2176 {
2177 /*
2178 * Single line that does not fit!
2179 * Don't overwrite it, it can be edited.
2180 */
2181 wp->w_botline = lnum + 1;
2182 }
2183#ifdef FEAT_DIFF
2184 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2185 {
2186 /* Window ends in filler lines. */
2187 wp->w_botline = lnum;
2188 wp->w_filler_rows = wp->w_height - srow;
2189 }
2190#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002191 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2192 {
2193 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2194
2195 /*
2196 * Last line isn't finished: Display "@@@" in the last screen line.
2197 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002198 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002199 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002200 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002201 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002202 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002203 set_empty_rows(wp, srow);
2204 wp->w_botline = lnum;
2205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2207 {
2208 /*
2209 * Last line isn't finished: Display "@@@" at the end.
2210 */
2211 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2212 W_WINROW(wp) + wp->w_height,
2213 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002214 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 set_empty_rows(wp, srow);
2216 wp->w_botline = lnum;
2217 }
2218 else
2219 {
2220 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2221 wp->w_botline = lnum;
2222 }
2223 }
2224 else
2225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 if (eof) /* we hit the end of the file */
2228 {
2229 wp->w_botline = buf->b_ml.ml_line_count + 1;
2230#ifdef FEAT_DIFF
2231 j = diff_check_fill(wp, wp->w_botline);
2232 if (j > 0 && !wp->w_botfill)
2233 {
2234 /*
2235 * Display filler lines at the end of the file
2236 */
2237 if (char2cells(fill_diff) > 1)
2238 i = '-';
2239 else
2240 i = fill_diff;
2241 if (row + j > wp->w_height)
2242 j = wp->w_height - row;
2243 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2244 row += j;
2245 }
2246#endif
2247 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002248 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 wp->w_botline = lnum;
2250
2251 /* make sure the rest of the screen is blank */
2252 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002253 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 }
2255
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002256#ifdef SYN_TIME_LIMIT
2257 syn_set_timeout(NULL);
2258#endif
2259
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 /* Reset the type of redrawing required, the window has been updated. */
2261 wp->w_redr_type = 0;
2262#ifdef FEAT_DIFF
2263 wp->w_old_topfill = wp->w_topfill;
2264 wp->w_old_botfill = wp->w_botfill;
2265#endif
2266
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002267 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {
2269 /*
2270 * There is a trick with w_botline. If we invalidate it on each
2271 * change that might modify it, this will cause a lot of expensive
2272 * calls to plines() in update_topline() each time. Therefore the
2273 * value of w_botline is often approximated, and this value is used to
2274 * compute the value of w_topline. If the value of w_botline was
2275 * wrong, check that the value of w_topline is correct (cursor is on
2276 * the visible part of the text). If it's not, we need to redraw
2277 * again. Mostly this just means scrolling up a few lines, so it
2278 * doesn't look too bad. Only do this for the current window (where
2279 * changes are relevant).
2280 */
2281 wp->w_valid |= VALID_BOTLINE;
2282 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2283 {
2284 recursive = TRUE;
2285 curwin->w_valid &= ~VALID_TOPLINE;
2286 update_topline(); /* may invalidate w_botline again */
2287 if (must_redraw != 0)
2288 {
2289 /* Don't update for changes in buffer again. */
2290 i = curbuf->b_mod_set;
2291 curbuf->b_mod_set = FALSE;
2292 win_update(curwin);
2293 must_redraw = 0;
2294 curbuf->b_mod_set = i;
2295 }
2296 recursive = FALSE;
2297 }
2298 }
2299
2300#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2301 /* restore got_int, unless CTRL-C was hit while redrawing */
2302 if (!got_int)
2303 got_int = save_got_int;
2304#endif
2305}
2306
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307/*
2308 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2309 * as the filler character.
2310 */
2311 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002312win_draw_end(
2313 win_T *wp,
2314 int c1,
2315 int c2,
2316 int row,
2317 int endrow,
2318 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
2320#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2321 int n = 0;
2322# define FDC_OFF n
2323#else
2324# define FDC_OFF 0
2325#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002326#ifdef FEAT_FOLDING
2327 int fdc = compute_foldcolumn(wp, 0);
2328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329
2330#ifdef FEAT_RIGHTLEFT
2331 if (wp->w_p_rl)
2332 {
2333 /* No check for cmdline window: should never be right-left. */
2334# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002335 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
2337 if (n > 0)
2338 {
2339 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002340 if (n > wp->w_width)
2341 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2343 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002344 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346# endif
2347# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002348 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
2350 int nn = n + 2;
2351
2352 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002353 if (nn > wp->w_width)
2354 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2356 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002357 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 n = nn;
2359 }
2360# endif
2361 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002362 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002363 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2365 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002366 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368 else
2369#endif
2370 {
2371#ifdef FEAT_CMDWIN
2372 if (cmdwin_type != 0 && wp == curwin)
2373 {
2374 /* draw the cmdline character in the leftmost column */
2375 n = 1;
2376 if (n > wp->w_width)
2377 n = wp->w_width;
2378 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002379 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002380 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 }
2382#endif
2383#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002384 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002386 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
2388 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002389 if (nn > wp->w_width)
2390 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002392 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002393 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 n = nn;
2395 }
2396#endif
2397#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002398 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
2400 int nn = n + 2;
2401
2402 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002403 if (nn > wp->w_width)
2404 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002406 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002407 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 n = nn;
2409 }
2410#endif
2411 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002412 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002413 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 }
2415 set_empty_rows(wp, row);
2416}
2417
Bram Moolenaar1a384422010-07-14 19:53:30 +02002418#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002419/*
2420 * Advance **color_cols and return TRUE when there are columns to draw.
2421 */
2422 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002423advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002424{
2425 while (**color_cols >= 0 && vcol > **color_cols)
2426 ++*color_cols;
2427 return (**color_cols >= 0);
2428}
2429#endif
2430
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002431#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2432/*
2433 * Copy "text" to ScreenLines using "attr".
2434 * Returns the next screen column.
2435 */
2436 static int
2437text_to_screenline(win_T *wp, char_u *text, int col)
2438{
2439 int off = (int)(current_ScreenLine - ScreenLines);
2440
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002441 if (has_mbyte)
2442 {
2443 int cells;
2444 int u8c, u8cc[MAX_MCO];
2445 int i;
2446 int idx;
2447 int c_len;
2448 char_u *p;
2449# ifdef FEAT_ARABIC
2450 int prev_c = 0; /* previous Arabic character */
2451 int prev_c1 = 0; /* first composing char for prev_c */
2452# endif
2453
2454# ifdef FEAT_RIGHTLEFT
2455 if (wp->w_p_rl)
2456 idx = off;
2457 else
2458# endif
2459 idx = off + col;
2460
2461 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2462 for (p = text; *p != NUL; )
2463 {
2464 cells = (*mb_ptr2cells)(p);
2465 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002466 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002467# ifdef FEAT_RIGHTLEFT
2468 - (wp->w_p_rl ? col : 0)
2469# endif
2470 )
2471 break;
2472 ScreenLines[idx] = *p;
2473 if (enc_utf8)
2474 {
2475 u8c = utfc_ptr2char(p, u8cc);
2476 if (*p < 0x80 && u8cc[0] == 0)
2477 {
2478 ScreenLinesUC[idx] = 0;
2479#ifdef FEAT_ARABIC
2480 prev_c = u8c;
2481#endif
2482 }
2483 else
2484 {
2485#ifdef FEAT_ARABIC
2486 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2487 {
2488 /* Do Arabic shaping. */
2489 int pc, pc1, nc;
2490 int pcc[MAX_MCO];
2491 int firstbyte = *p;
2492
2493 /* The idea of what is the previous and next
2494 * character depends on 'rightleft'. */
2495 if (wp->w_p_rl)
2496 {
2497 pc = prev_c;
2498 pc1 = prev_c1;
2499 nc = utf_ptr2char(p + c_len);
2500 prev_c1 = u8cc[0];
2501 }
2502 else
2503 {
2504 pc = utfc_ptr2char(p + c_len, pcc);
2505 nc = prev_c;
2506 pc1 = pcc[0];
2507 }
2508 prev_c = u8c;
2509
2510 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2511 pc, pc1, nc);
2512 ScreenLines[idx] = firstbyte;
2513 }
2514 else
2515 prev_c = u8c;
2516#endif
2517 /* Non-BMP character: display as ? or fullwidth ?. */
2518#ifdef UNICODE16
2519 if (u8c >= 0x10000)
2520 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2521 else
2522#endif
2523 ScreenLinesUC[idx] = u8c;
2524 for (i = 0; i < Screen_mco; ++i)
2525 {
2526 ScreenLinesC[i][idx] = u8cc[i];
2527 if (u8cc[i] == 0)
2528 break;
2529 }
2530 }
2531 if (cells > 1)
2532 ScreenLines[idx + 1] = 0;
2533 }
2534 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2535 /* double-byte single width character */
2536 ScreenLines2[idx] = p[1];
2537 else if (cells > 1)
2538 /* double-width character */
2539 ScreenLines[idx + 1] = p[1];
2540 col += cells;
2541 idx += cells;
2542 p += c_len;
2543 }
2544 }
2545 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002546 {
2547 int len = (int)STRLEN(text);
2548
Bram Moolenaar02631462017-09-22 15:20:32 +02002549 if (len > wp->w_width - col)
2550 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002551 if (len > 0)
2552 {
2553#ifdef FEAT_RIGHTLEFT
2554 if (wp->w_p_rl)
2555 STRNCPY(current_ScreenLine, text, len);
2556 else
2557#endif
2558 STRNCPY(current_ScreenLine + col, text, len);
2559 col += len;
2560 }
2561 }
2562 return col;
2563}
2564#endif
2565
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566#ifdef FEAT_FOLDING
2567/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002568 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2569 * space is available for window "wp", minus "col".
2570 */
2571 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002572compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002573{
2574 int fdc = wp->w_p_fdc;
2575 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002576 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002577
2578 if (fdc > wwidth - (col + wmw))
2579 fdc = wwidth - (col + wmw);
2580 return fdc;
2581}
2582
2583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 * Display one folded line.
2585 */
2586 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002587fold_line(
2588 win_T *wp,
2589 long fold_count,
2590 foldinfo_T *foldinfo,
2591 linenr_T lnum,
2592 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002594 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 pos_T *top, *bot;
2596 linenr_T lnume = lnum + fold_count - 1;
2597 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002598 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 int col;
2601 int txtcol;
2602 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002603 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
2605 /* Build the fold line:
2606 * 1. Add the cmdwin_type for the command-line window
2607 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002608 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 * 4. Compose the text
2610 * 5. Add the text
2611 * 6. set highlighting for the Visual area an other text
2612 */
2613 col = 0;
2614
2615 /*
2616 * 1. Add the cmdwin_type for the command-line window
2617 * Ignores 'rightleft', this window is never right-left.
2618 */
2619#ifdef FEAT_CMDWIN
2620 if (cmdwin_type != 0 && wp == curwin)
2621 {
2622 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002623 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 if (enc_utf8)
2625 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 ++col;
2627 }
2628#endif
2629
2630 /*
2631 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002632 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002634 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 if (fdc > 0)
2636 {
2637 fill_foldcolumn(buf, wp, TRUE, lnum);
2638#ifdef FEAT_RIGHTLEFT
2639 if (wp->w_p_rl)
2640 {
2641 int i;
2642
Bram Moolenaar02631462017-09-22 15:20:32 +02002643 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002644 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 /* reverse the fold column */
2646 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002647 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
2649 else
2650#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002651 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 col += fdc;
2653 }
2654
2655#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002656# define RL_MEMSET(p, v, l) \
2657 do { \
2658 if (wp->w_p_rl) \
2659 for (ri = 0; ri < l; ++ri) \
2660 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2661 else \
2662 for (ri = 0; ri < l; ++ri) \
2663 ScreenAttrs[off + (p) + ri] = v; \
2664 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002666# define RL_MEMSET(p, v, l) \
2667 do { \
2668 for (ri = 0; ri < l; ++ri) \
2669 ScreenAttrs[off + (p) + ri] = v; \
2670 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671#endif
2672
Bram Moolenaar64486672010-05-16 15:46:46 +02002673 /* Set all attributes of the 'number' or 'relativenumber' column and the
2674 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002675 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676
2677#ifdef FEAT_SIGNS
2678 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002679 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002681 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if (len > 0)
2683 {
2684 if (len > 2)
2685 len = 2;
2686# ifdef FEAT_RIGHTLEFT
2687 if (wp->w_p_rl)
2688 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002689 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002690 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 else
2692# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002693 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 col += len;
2695 }
2696 }
2697#endif
2698
2699 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002700 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002702 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002704 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 if (len > 0)
2706 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002707 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002708 long num;
2709 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002710
2711 if (len > w + 1)
2712 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002713
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002714 if (wp->w_p_nu && !wp->w_p_rnu)
2715 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002716 num = (long)lnum;
2717 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002718 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002719 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002720 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002721 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002722 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002723 /* 'number' + 'relativenumber': cursor line shows absolute
2724 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002725 num = lnum;
2726 fmt = "%-*ld ";
2727 }
2728 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002729
Bram Moolenaar700e7342013-01-30 12:31:36 +01002730 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731#ifdef FEAT_RIGHTLEFT
2732 if (wp->w_p_rl)
2733 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002734 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002735 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 else
2737#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002738 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 col += len;
2740 }
2741 }
2742
2743 /*
2744 * 4. Compose the folded-line string with 'foldtext', if set.
2745 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002746 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
2748 txtcol = col; /* remember where text starts */
2749
2750 /*
2751 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2752 * Right-left text is put in columns 0 - number-col, normal text is put
2753 * in columns number-col - window-width.
2754 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002755 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
2757 /* Fill the rest of the line with the fold filler */
2758#ifdef FEAT_RIGHTLEFT
2759 if (wp->w_p_rl)
2760 col -= txtcol;
2761#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002762 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763#ifdef FEAT_RIGHTLEFT
2764 - (wp->w_p_rl ? txtcol : 0)
2765#endif
2766 )
2767 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 if (enc_utf8)
2769 {
2770 if (fill_fold >= 0x80)
2771 {
2772 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002773 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002774 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 }
2776 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002777 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002779 ScreenLines[off + col] = fill_fold;
2780 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002781 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002783 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002784 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
2786
2787 if (text != buf)
2788 vim_free(text);
2789
2790 /*
2791 * 6. set highlighting for the Visual area an other text.
2792 * If all folded lines are in the Visual area, highlight the line.
2793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2795 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002796 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 {
2798 /* Visual is after curwin->w_cursor */
2799 top = &curwin->w_cursor;
2800 bot = &VIsual;
2801 }
2802 else
2803 {
2804 /* Visual is before curwin->w_cursor */
2805 top = &VIsual;
2806 bot = &curwin->w_cursor;
2807 }
2808 if (lnum >= top->lnum
2809 && lnume <= bot->lnum
2810 && (VIsual_mode != 'v'
2811 || ((lnum > top->lnum
2812 || (lnum == top->lnum
2813 && top->col == 0))
2814 && (lnume < bot->lnum
2815 || (lnume == bot->lnum
2816 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002817 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 {
2819 if (VIsual_mode == Ctrl_V)
2820 {
2821 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002822 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002824 if (wp->w_old_cursor_lcol != MAXCOL
2825 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002826 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 len = wp->w_old_cursor_lcol;
2828 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002829 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002830 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002831 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 }
2833 }
2834 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002837 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
2840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002842#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002843 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2844 if (wp->w_p_cc_cols)
2845 {
2846 int i = 0;
2847 int j = wp->w_p_cc_cols[i];
2848 int old_txtcol = txtcol;
2849
2850 while (j > -1)
2851 {
2852 txtcol += j;
2853 if (wp->w_p_wrap)
2854 txtcol -= wp->w_skipcol;
2855 else
2856 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002857 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002858 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002859 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002860 txtcol = old_txtcol;
2861 j = wp->w_p_cc_cols[++i];
2862 }
2863 }
2864
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002865 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002866 if (wp->w_p_cuc)
2867 {
2868 txtcol += wp->w_virtcol;
2869 if (wp->w_p_wrap)
2870 txtcol -= wp->w_skipcol;
2871 else
2872 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002873 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002874 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002875 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002876 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878
Bram Moolenaar02631462017-09-22 15:20:32 +02002879 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2880 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881
2882 /*
2883 * Update w_cline_height and w_cline_folded if the cursor line was
2884 * updated (saves a call to plines() later).
2885 */
2886 if (wp == curwin
2887 && lnum <= curwin->w_cursor.lnum
2888 && lnume >= curwin->w_cursor.lnum)
2889 {
2890 curwin->w_cline_row = row;
2891 curwin->w_cline_height = 1;
2892 curwin->w_cline_folded = TRUE;
2893 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2894 }
2895}
2896
2897/*
2898 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2899 */
2900 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002901copy_text_attr(
2902 int off,
2903 char_u *buf,
2904 int len,
2905 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002907 int i;
2908
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 if (enc_utf8)
2911 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002912 for (i = 0; i < len; ++i)
2913 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914}
2915
2916/*
2917 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002918 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 */
2920 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002921fill_foldcolumn(
2922 char_u *p,
2923 win_T *wp,
2924 int closed, /* TRUE of FALSE */
2925 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926{
2927 int i = 0;
2928 int level;
2929 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002930 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002931 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
2933 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002934 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935
2936 level = win_foldinfo.fi_level;
2937 if (level > 0)
2938 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002939 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002940 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002941
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 /* If the column is too narrow, we start at the lowest level that
2943 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002944 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 if (first_level < 1)
2946 first_level = 1;
2947
Bram Moolenaar1c934292015-01-27 16:39:29 +01002948 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 {
2950 if (win_foldinfo.fi_lnum == lnum
2951 && first_level + i >= win_foldinfo.fi_low_level)
2952 p[i] = '-';
2953 else if (first_level == 1)
2954 p[i] = '|';
2955 else if (first_level + i <= 9)
2956 p[i] = '0' + first_level + i;
2957 else
2958 p[i] = '>';
2959 if (first_level + i == level)
2960 break;
2961 }
2962 }
2963 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002964 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965}
2966#endif /* FEAT_FOLDING */
2967
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01002968#ifdef FEAT_TEXT_PROP
2969static textprop_T *current_text_props = NULL;
2970static buf_T *current_buf = NULL;
2971
2972 static int
2973#ifdef __BORLANDC__
2974_RTLENTRYF
2975#endif
2976text_prop_compare(const void *s1, const void *s2)
2977{
2978 int idx1, idx2;
2979 proptype_T *pt1, *pt2;
2980 colnr_T col1, col2;
2981
2982 idx1 = *(int *)s1;
2983 idx2 = *(int *)s2;
2984 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
2985 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
2986 if (pt1 == pt2)
2987 return 0;
2988 if (pt1 == NULL)
2989 return -1;
2990 if (pt2 == NULL)
2991 return 1;
2992 if (pt1->pt_priority != pt2->pt_priority)
2993 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
2994 col1 = current_text_props[idx1].tp_col;
2995 col2 = current_text_props[idx2].tp_col;
2996 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
2997}
2998#endif
2999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000/*
3001 * Display line "lnum" of window 'wp' on the screen.
3002 * Start at row "startrow", stop when "endrow" is reached.
3003 * wp->w_virtcol needs to be valid.
3004 *
3005 * Return the number of last row the line occupies.
3006 */
3007 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003008win_line(
3009 win_T *wp,
3010 linenr_T lnum,
3011 int startrow,
3012 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003013 int nochange UNUSED, // not updating for changed text
3014 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003016 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3018 int c = 0; /* init for GCC */
3019 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003020#ifdef FEAT_LINEBREAK
3021 long vcol_sbr = -1; /* virtual column after showbreak */
3022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 long vcol_prev = -1; /* "vcol" of previous character */
3024 char_u *line; /* current line */
3025 char_u *ptr; /* current position in "line" */
3026 int row; /* row in the window, excl w_winrow */
3027 int screen_row; /* row on the screen, incl w_winrow */
3028
3029 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3030 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003031 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003032 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003034 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 int extra_attr = 0; /* attributes when n_extra != 0 */
3036 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3037 displaying lcs_eol at end-of-line */
3038 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3039 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3040
3041 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3042 int saved_n_extra = 0;
3043 char_u *saved_p_extra = NULL;
3044 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003045 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 int saved_char_attr = 0;
3047
3048 int n_attr = 0; /* chars with special attr */
3049 int saved_attr2 = 0; /* char_attr saved for n_attr */
3050 int n_attr3 = 0; /* chars with overruling special attr */
3051 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3052
3053 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3054
3055 int fromcol, tocol; /* start/end of inverting */
3056 int fromcol_prev = -2; /* start of inverting after cursor */
3057 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003059 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 pos_T pos;
3061 long v;
3062
3063 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003064 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3066 in this line */
3067 int attr = 0; /* attributes for area highlighting */
3068 int area_attr = 0; /* attributes desired by highlighting */
3069 int search_attr = 0; /* attributes desired by 'hlsearch' */
3070#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003071 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 int syntax_attr = 0; /* attributes desired by syntax */
3073 int has_syntax = FALSE; /* this buffer has syntax highl. */
3074 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003075 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003076 int draw_color_col = FALSE; /* highlight colorcolumn */
3077 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003078#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003079#ifdef FEAT_TEXT_PROP
3080 int text_prop_count;
3081 int text_prop_next = 0; // next text property to use
3082 textprop_T *text_props = NULL;
3083 int *text_prop_idxs = NULL;
3084 int text_props_active = 0;
3085 proptype_T *text_prop_type = NULL;
3086 int text_prop_attr = 0;
3087#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003088#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003089 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003090# define SPWORDLEN 150
3091 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003092 int nextlinecol = 0; /* column where nextline[] starts */
3093 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003094 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003095 int spell_attr = 0; /* attributes desired by spelling */
3096 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003097 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3098 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003099 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003100 static int cap_col = -1; /* column to check for Cap word */
3101 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003102 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003104 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 int multi_attr = 0; /* attributes desired by multibyte */
3106 int mb_l = 1; /* multi-byte byte length */
3107 int mb_c = 0; /* decoded multi-byte character */
3108 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003109 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110#ifdef FEAT_DIFF
3111 int filler_lines; /* nr of filler lines to be drawn */
3112 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003113 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 int change_start = MAXCOL; /* first col of changed area */
3115 int change_end = -1; /* last col of changed area */
3116#endif
3117 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3118#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003119 int need_showbreak = FALSE; /* overlong line, skipping first x
3120 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003122#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003123 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003125 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126#endif
3127#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003128 matchitem_T *cur; /* points to the match list */
3129 match_T *shl; /* points to search_hl or a match */
3130 int shl_flag; /* flag to indicate whether search_hl
3131 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003132 int pos_inprogress; /* marks that position match search is
3133 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003134 int prevcol_hl_flag; /* flag to indicate whether prevcol
3135 equals startcol of search_hl or one
3136 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137#endif
3138#ifdef FEAT_ARABIC
3139 int prev_c = 0; /* previous Arabic character */
3140 int prev_c1 = 0; /* first composing char for prev_c */
3141#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003142#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003143 int did_line_attr = 0;
3144#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003145#ifdef FEAT_TERMINAL
3146 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003147 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
3150 /* draw_state: items that are drawn in sequence: */
3151#define WL_START 0 /* nothing done yet */
3152#ifdef FEAT_CMDWIN
3153# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3154#else
3155# define WL_CMDLINE WL_START
3156#endif
3157#ifdef FEAT_FOLDING
3158# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3159#else
3160# define WL_FOLD WL_CMDLINE
3161#endif
3162#ifdef FEAT_SIGNS
3163# define WL_SIGN WL_FOLD + 1 /* column for signs */
3164#else
3165# define WL_SIGN WL_FOLD /* column for signs */
3166#endif
3167#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003168#ifdef FEAT_LINEBREAK
3169# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003171# define WL_BRI WL_NR
3172#endif
3173#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3174# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3175#else
3176# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177#endif
3178#define WL_LINE WL_SBR + 1 /* text in the line */
3179 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003180#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 int feedback_col = 0;
3182 int feedback_old_attr = -1;
3183#endif
3184
Bram Moolenaar860cae12010-06-05 23:22:07 +02003185#ifdef FEAT_CONCEAL
3186 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003187 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003188 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003189 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003190 int is_concealing = FALSE;
3191 int boguscols = 0; /* nonexistent columns added to force
3192 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003193 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003194 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003195 int match_conc = 0; /* cchar for match functions */
3196 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003197 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003198# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003199# define FIX_FOR_BOGUSCOLS \
3200 { \
3201 n_extra += vcol_off; \
3202 vcol -= vcol_off; \
3203 vcol_off = 0; \
3204 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003205 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003206 boguscols = 0; \
3207 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003208#else
3209# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211
3212 if (startrow > endrow) /* past the end already! */
3213 return startrow;
3214
3215 row = startrow;
3216 screen_row = row + W_WINROW(wp);
3217
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003218 if (!number_only)
3219 {
3220 /*
3221 * To speed up the loop below, set extra_check when there is linebreak,
3222 * trailing white space and/or syntax processing to be done.
3223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003225 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226#endif
3227#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003228 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003229# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003230 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003231# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003232 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003234 /* Prepare for syntax highlighting in this line. When there is an
3235 * error, stop syntax highlighting. */
3236 save_did_emsg = did_emsg;
3237 did_emsg = FALSE;
3238 syntax_start(wp, lnum);
3239 if (did_emsg)
3240 wp->w_s->b_syn_error = TRUE;
3241 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003242 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003243 did_emsg = save_did_emsg;
3244#ifdef SYN_TIME_LIMIT
3245 if (!wp->w_s->b_syn_slow)
3246#endif
3247 {
3248 has_syntax = TRUE;
3249 extra_check = TRUE;
3250 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003253
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003254 /* Check for columns to display for 'colorcolumn'. */
3255 color_cols = wp->w_p_cc_cols;
3256 if (color_cols != NULL)
3257 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003258#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003259
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003260#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003261 if (term_show_buffer(wp->w_buffer))
3262 {
3263 extra_check = TRUE;
3264 get_term_attr = TRUE;
3265 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3266 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003267#endif
3268
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003269#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003270 if (wp->w_p_spell
3271 && *wp->w_s->b_p_spl != NUL
3272 && wp->w_s->b_langp.ga_len > 0
3273 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003274 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003275 /* Prepare for spell checking. */
3276 has_spell = TRUE;
3277 extra_check = TRUE;
3278
Bram Moolenaar7701f302018-10-02 21:20:32 +02003279 /* Get the start of the next line, so that words that wrap to the
3280 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003281 * Trick: skip a few chars for C/shell/Vim comments */
3282 nextline[SPWORDLEN] = NUL;
3283 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3284 {
3285 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3286 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3287 }
3288
Bram Moolenaar7701f302018-10-02 21:20:32 +02003289 /* When a word wrapped from the previous line the start of the
3290 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003291 if (lnum == checked_lnum)
3292 cur_checked_col = checked_col;
3293 checked_lnum = 0;
3294
3295 /* When there was a sentence end in the previous line may require a
3296 * word starting with capital in this line. In line 1 always check
3297 * the first word. */
3298 if (lnum != capcol_lnum)
3299 cap_col = -1;
3300 if (lnum == 1)
3301 cap_col = 0;
3302 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#endif
3305
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003306 /*
3307 * handle visual active in this window
3308 */
3309 fromcol = -10;
3310 tocol = MAXCOL;
3311 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003313 /* Visual is after curwin->w_cursor */
3314 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003316 top = &curwin->w_cursor;
3317 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003319 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003321 top = &VIsual;
3322 bot = &curwin->w_cursor;
3323 }
3324 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3325 if (VIsual_mode == Ctrl_V) /* block mode */
3326 {
3327 if (lnum_in_visual_area)
3328 {
3329 fromcol = wp->w_old_cursor_fcol;
3330 tocol = wp->w_old_cursor_lcol;
3331 }
3332 }
3333 else /* non-block mode */
3334 {
3335 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003337 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003339 if (VIsual_mode == 'V') /* linewise */
3340 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 else
3342 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003343 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3344 if (gchar_pos(top) == NUL)
3345 tocol = fromcol + 1;
3346 }
3347 }
3348 if (VIsual_mode != 'V' && lnum == bot->lnum)
3349 {
3350 if (*p_sel == 'e' && bot->col == 0
3351#ifdef FEAT_VIRTUALEDIT
3352 && bot->coladd == 0
3353#endif
3354 )
3355 {
3356 fromcol = -10;
3357 tocol = MAXCOL;
3358 }
3359 else if (bot->col == MAXCOL)
3360 tocol = MAXCOL;
3361 else
3362 {
3363 pos = *bot;
3364 if (*p_sel == 'e')
3365 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3366 else
3367 {
3368 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3369 ++tocol;
3370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 }
3372 }
3373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003375 /* Check if the character under the cursor should not be inverted */
3376 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003377#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003378 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003379#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003380 )
3381 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003383 /* if inverting in this line set area_highlighting */
3384 if (fromcol >= 0)
3385 {
3386 area_highlighting = TRUE;
3387 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003389 if ((clip_star.available && !clip_star.owned
3390 && clip_isautosel_star())
3391 || (clip_plus.available && !clip_plus.owned
3392 && clip_isautosel_plus()))
3393 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003398 /*
3399 * handle 'incsearch' and ":s///c" highlighting
3400 */
3401 else if (highlight_match
3402 && wp == curwin
3403 && lnum >= curwin->w_cursor.lnum
3404 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003406 if (lnum == curwin->w_cursor.lnum)
3407 getvcol(curwin, &(curwin->w_cursor),
3408 (colnr_T *)&fromcol, NULL, NULL);
3409 else
3410 fromcol = 0;
3411 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3412 {
3413 pos.lnum = lnum;
3414 pos.col = search_match_endcol;
3415 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3416 }
3417 else
3418 tocol = MAXCOL;
3419 /* do at least one character; happens when past end of line */
3420 if (fromcol == tocol)
3421 tocol = fromcol + 1;
3422 area_highlighting = TRUE;
3423 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
3426
3427#ifdef FEAT_DIFF
3428 filler_lines = diff_check(wp, lnum);
3429 if (filler_lines < 0)
3430 {
3431 if (filler_lines == -1)
3432 {
3433 if (diff_find_change(wp, lnum, &change_start, &change_end))
3434 diff_hlf = HLF_ADD; /* added line */
3435 else if (change_start == 0)
3436 diff_hlf = HLF_TXD; /* changed text */
3437 else
3438 diff_hlf = HLF_CHD; /* changed line */
3439 }
3440 else
3441 diff_hlf = HLF_ADD; /* added line */
3442 filler_lines = 0;
3443 area_highlighting = TRUE;
3444 }
3445 if (lnum == wp->w_topline)
3446 filler_lines = wp->w_topfill;
3447 filler_todo = filler_lines;
3448#endif
3449
3450#ifdef LINE_ATTR
3451# ifdef FEAT_SIGNS
3452 /* If this line has a sign with line highlighting set line_attr. */
3453 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3454 if (v != 0)
3455 line_attr = sign_get_attr((int)v, TRUE);
3456# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003457# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003459 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003460 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461# endif
3462 if (line_attr != 0)
3463 area_highlighting = TRUE;
3464#endif
3465
3466 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3467 ptr = line;
3468
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003469#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003470 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003471 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003472 /* For checking first word with a capital skip white space. */
3473 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003474 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003475
Bram Moolenaar30abd282005-06-22 22:35:10 +00003476 /* To be able to spell-check over line boundaries copy the end of the
3477 * current line into nextline[]. Above the start of the next line was
3478 * copied to nextline[SPWORDLEN]. */
3479 if (nextline[SPWORDLEN] == NUL)
3480 {
3481 /* No next line or it is empty. */
3482 nextlinecol = MAXCOL;
3483 nextline_idx = 0;
3484 }
3485 else
3486 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003487 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003488 if (v < SPWORDLEN)
3489 {
3490 /* Short line, use it completely and append the start of the
3491 * next line. */
3492 nextlinecol = 0;
3493 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003494 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003495 nextline_idx = v + 1;
3496 }
3497 else
3498 {
3499 /* Long line, use only the last SPWORDLEN bytes. */
3500 nextlinecol = v - SPWORDLEN;
3501 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3502 nextline_idx = SPWORDLEN + 1;
3503 }
3504 }
3505 }
3506#endif
3507
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003508 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003510 if (lcs_space || lcs_trail)
3511 extra_check = TRUE;
3512 /* find start of trailing whitespace */
3513 if (lcs_trail)
3514 {
3515 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003516 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003517 --trailcol;
3518 trailcol += (colnr_T) (ptr - line);
3519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521
3522 /*
3523 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3524 * first character to be displayed.
3525 */
3526 if (wp->w_p_wrap)
3527 v = wp->w_skipcol;
3528 else
3529 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003530 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003533
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 while (vcol < v && *ptr != NUL)
3535 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003536 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003539 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
3541
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003542 /* When:
3543 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003544 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003545 * - 'virtualedit' is set, or
3546 * - the visual mode is active,
3547 * the end of the line may be before the start of the displayed part.
3548 */
3549 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003550#ifdef FEAT_SYN_HL
3551 wp->w_p_cuc || draw_color_col ||
3552#endif
3553#ifdef FEAT_VIRTUALEDIT
3554 virtual_active() ||
3555#endif
3556 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
3561 /* Handle a character that's not completely on the screen: Put ptr at
3562 * that character but skip the first few screen characters. */
3563 if (vcol > v)
3564 {
3565 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003567 /* If the character fits on the screen, don't need to skip it.
3568 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003569 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003570 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
3572
3573 /*
3574 * Adjust for when the inverted text is before the screen,
3575 * and when the start of the inverted text is before the screen.
3576 */
3577 if (tocol <= vcol)
3578 fromcol = 0;
3579 else if (fromcol >= 0 && fromcol < vcol)
3580 fromcol = vcol;
3581
3582#ifdef FEAT_LINEBREAK
3583 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3584 if (wp->w_p_wrap)
3585 need_showbreak = TRUE;
3586#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003587#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003588 /* When spell checking a word we need to figure out the start of the
3589 * word and if it's badly spelled or not. */
3590 if (has_spell)
3591 {
3592 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003593 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003594 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003595
3596 pos = wp->w_cursor;
3597 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003598 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003599 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003600
3601 /* spell_move_to() may call ml_get() and make "line" invalid */
3602 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3603 ptr = line + linecol;
3604
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003605 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003606 {
3607 /* no bad word found at line start, don't check until end of a
3608 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003609 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003610 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003611 }
3612 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003613 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003614 /* bad word found, use attributes until end of word */
3615 word_end = wp->w_cursor.col + len + 1;
3616
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003617 /* Turn index into actual attributes. */
3618 if (spell_hlf != HLF_COUNT)
3619 spell_attr = highlight_attr[spell_hlf];
3620 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003621 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003622
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003623# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003624 /* Need to restart syntax highlighting for this line. */
3625 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003626 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003627# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003628 }
3629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
3631
3632 /*
3633 * Correct highlighting for cursor that can't be disabled.
3634 * Avoids having to check this for each character.
3635 */
3636 if (fromcol >= 0)
3637 {
3638 if (noinvcur)
3639 {
3640 if ((colnr_T)fromcol == wp->w_virtcol)
3641 {
3642 /* highlighting starts at cursor, let it start just after the
3643 * cursor */
3644 fromcol_prev = fromcol;
3645 fromcol = -1;
3646 }
3647 else if ((colnr_T)fromcol < wp->w_virtcol)
3648 /* restart highlighting after the cursor */
3649 fromcol_prev = wp->w_virtcol;
3650 }
3651 if (fromcol >= tocol)
3652 fromcol = -1;
3653 }
3654
3655#ifdef FEAT_SEARCH_EXTRA
3656 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003657 * Handle highlighting the last used search pattern and matches.
3658 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003660 cur = wp->w_match_head;
3661 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003662 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003664 if (shl_flag == FALSE)
3665 {
3666 shl = &search_hl;
3667 shl_flag = TRUE;
3668 }
3669 else
3670 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003671 shl->startcol = MAXCOL;
3672 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003674 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003675 v = (long)(ptr - line);
3676 if (cur != NULL)
3677 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003678 next_search_hl(wp, shl, lnum, (colnr_T)v,
3679 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003680
3681 /* Need to get the line again, a multi-line regexp may have made it
3682 * invalid. */
3683 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3684 ptr = line + v;
3685
3686 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003688 if (shl->lnum == lnum)
3689 shl->startcol = shl->rm.startpos[0].col;
3690 else
3691 shl->startcol = 0;
3692 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3693 - shl->rm.startpos[0].lnum)
3694 shl->endcol = shl->rm.endpos[0].col;
3695 else
3696 shl->endcol = MAXCOL;
3697 /* Highlight one character for an empty match. */
3698 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003700 if (has_mbyte && line[shl->endcol] != NUL)
3701 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3702 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003703 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003705 if ((long)shl->startcol < v) /* match at leftcol */
3706 {
3707 shl->attr_cur = shl->attr;
3708 search_attr = shl->attr;
3709 }
3710 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003712 if (shl != &search_hl && cur != NULL)
3713 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 }
3715#endif
3716
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003717#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003718 /* Cursor line highlighting for 'cursorline' in the current window. Not
3719 * when Visual mode is active, because it's not clear what is selected
3720 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003721 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003722 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003723 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003724 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003725 area_highlighting = TRUE;
3726 }
3727#endif
3728
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003729#ifdef FEAT_TEXT_PROP
3730 {
3731 char_u *prop_start;
3732
3733 text_prop_count = get_text_props(wp->w_buffer, lnum,
3734 &prop_start, FALSE);
3735 if (text_prop_count > 0)
3736 {
3737 // Make a copy of the properties, so that they are properly
3738 // aligned.
3739 text_props = (textprop_T *)alloc(
3740 text_prop_count * sizeof(textprop_T));
3741 if (text_props != NULL)
3742 mch_memmove(text_props, prop_start,
3743 text_prop_count * sizeof(textprop_T));
3744
3745 // Allocate an array for the indexes.
3746 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3747 area_highlighting = TRUE;
3748 extra_check = TRUE;
3749 }
3750 }
3751#endif
3752
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003753 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 col = 0;
3755#ifdef FEAT_RIGHTLEFT
3756 if (wp->w_p_rl)
3757 {
3758 /* Rightleft window: process the text in the normal direction, but put
3759 * it in current_ScreenLine[] from right to left. Start at the
3760 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003761 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 off += col;
3763 }
3764#endif
3765
3766 /*
3767 * Repeat for the whole displayed line.
3768 */
3769 for (;;)
3770 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003771#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003772 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 /* Skip this quickly when working on the text. */
3775 if (draw_state != WL_LINE)
3776 {
3777#ifdef FEAT_CMDWIN
3778 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3779 {
3780 draw_state = WL_CMDLINE;
3781 if (cmdwin_type != 0 && wp == curwin)
3782 {
3783 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003785 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003786 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003787 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 }
3789 }
3790#endif
3791
3792#ifdef FEAT_FOLDING
3793 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3794 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003795 int fdc = compute_foldcolumn(wp, 0);
3796
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003798 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003800 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003801 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003802 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003803 p_extra_free = alloc(12 + 1);
3804
3805 if (p_extra_free != NULL)
3806 {
3807 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3808 n_extra = fdc;
3809 p_extra_free[n_extra] = NUL;
3810 p_extra = p_extra_free;
3811 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003812 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003813 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816 }
3817#endif
3818
3819#ifdef FEAT_SIGNS
3820 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3821 {
3822 draw_state = WL_SIGN;
3823 /* Show the sign column when there are any signs in this
3824 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003825 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003827 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003829 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830# endif
3831
3832 /* Draw two cells with the sign value or blank. */
3833 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003834 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003835 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 n_extra = 2;
3837
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003838 if (row == startrow
3839#ifdef FEAT_DIFF
3840 + filler_lines && filler_todo <= 0
3841#endif
3842 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 {
3844 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3845 SIGN_TEXT);
3846# ifdef FEAT_SIGN_ICONS
3847 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3848 SIGN_ICON);
3849 if (gui.in_use && icon_sign != 0)
3850 {
3851 /* Use the image in this position. */
3852 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003853 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854# ifdef FEAT_NETBEANS_INTG
3855 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003856 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003858 c_final = NUL;
3859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860# endif
3861 char_attr = icon_sign;
3862 }
3863 else
3864# endif
3865 if (text_sign != 0)
3866 {
3867 p_extra = sign_get_text(text_sign);
3868 if (p_extra != NULL)
3869 {
3870 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003871 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003872 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 }
3874 char_attr = sign_get_attr(text_sign, FALSE);
3875 }
3876 }
3877 }
3878 }
3879#endif
3880
3881 if (draw_state == WL_NR - 1 && n_extra == 0)
3882 {
3883 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003884 /* Display the absolute or relative line number. After the
3885 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3886 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 && (row == startrow
3888#ifdef FEAT_DIFF
3889 + filler_lines
3890#endif
3891 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3892 {
3893 /* Draw the line number (empty space after wrapping). */
3894 if (row == startrow
3895#ifdef FEAT_DIFF
3896 + filler_lines
3897#endif
3898 )
3899 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003900 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003901 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003902
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003903 if (wp->w_p_nu && !wp->w_p_rnu)
3904 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003905 num = (long)lnum;
3906 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003907 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003908 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003909 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003910 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003911 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003912 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003913 num = lnum;
3914 fmt = "%-*ld ";
3915 }
3916 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003917
Bram Moolenaar700e7342013-01-30 12:31:36 +01003918 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003919 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 if (wp->w_skipcol > 0)
3921 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3922 *p_extra = '-';
3923#ifdef FEAT_RIGHTLEFT
3924 if (wp->w_p_rl) /* reverse line numbers */
3925 rl_mirror(extra);
3926#endif
3927 p_extra = extra;
3928 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003929 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01003932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003934 c_final = NUL;
3935 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003936 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003937 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003938#ifdef FEAT_SYN_HL
3939 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003940 * the current line differently.
3941 * TODO: Can we use CursorLine instead of CursorLineNr
3942 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003943 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003944 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003945 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
3948 }
3949
Bram Moolenaar597a4222014-06-25 14:39:50 +02003950#ifdef FEAT_LINEBREAK
3951 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3952 && n_extra == 0 && *p_sbr != NUL)
3953 /* draw indent after showbreak value */
3954 draw_state = WL_BRI;
3955 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3956 /* After the showbreak, draw the breakindent */
3957 draw_state = WL_BRI - 1;
3958
3959 /* draw 'breakindent': indent wrapped text accordingly */
3960 if (draw_state == WL_BRI - 1 && n_extra == 0)
3961 {
3962 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003963 /* if need_showbreak is set, breakindent also applies */
3964 if (wp->w_p_bri && n_extra == 0
3965 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003966# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003967 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003968# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003969 )
3970 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003971 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003972# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003973 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003974 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003975 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003976# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003977 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3978 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003979 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003980# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003981 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003982# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003983 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003984 c_extra = ' ';
3985 n_extra = get_breakindent_win(wp,
3986 ml_get_buf(wp->w_buffer, lnum, FALSE));
3987 /* Correct end of highlighted area for 'breakindent',
3988 * required when 'linebreak' is also set. */
3989 if (tocol == vcol)
3990 tocol += n_extra;
3991 }
3992 }
3993#endif
3994
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3996 if (draw_state == WL_SBR - 1 && n_extra == 0)
3997 {
3998 draw_state = WL_SBR;
3999# ifdef FEAT_DIFF
4000 if (filler_todo > 0)
4001 {
4002 /* Draw "deleted" diff line(s). */
4003 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004004 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004006 c_final = NUL;
4007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004011 c_final = NUL;
4012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013# ifdef FEAT_RIGHTLEFT
4014 if (wp->w_p_rl)
4015 n_extra = col + 1;
4016 else
4017# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004018 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004019 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
4021# endif
4022# ifdef FEAT_LINEBREAK
4023 if (*p_sbr != NUL && need_showbreak)
4024 {
4025 /* Draw 'showbreak' at the start of each broken line. */
4026 p_extra = p_sbr;
4027 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004028 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004030 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004032 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 /* Correct end of highlighted area for 'showbreak',
4034 * required when 'linebreak' is also set. */
4035 if (tocol == vcol)
4036 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004037#ifdef FEAT_SYN_HL
4038 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004039 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004040 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004041 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 }
4044# endif
4045 }
4046#endif
4047
4048 if (draw_state == WL_LINE - 1 && n_extra == 0)
4049 {
4050 draw_state = WL_LINE;
4051 if (saved_n_extra)
4052 {
4053 /* Continue item from end of wrapped line. */
4054 n_extra = saved_n_extra;
4055 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004056 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 p_extra = saved_p_extra;
4058 char_attr = saved_char_attr;
4059 }
4060 else
4061 char_attr = 0;
4062 }
4063 }
4064
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004065 // When still displaying '$' of change command, stop at cursor.
4066 // When only displaying the (relative) line number and that's done,
4067 // stop here.
4068 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070#ifdef FEAT_DIFF
4071 && filler_todo <= 0
4072#endif
4073 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004074 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004076 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004077 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004078 /* Pretend we have finished updating the window. Except when
4079 * 'cursorcolumn' is set. */
4080#ifdef FEAT_SYN_HL
4081 if (wp->w_p_cuc)
4082 row = wp->w_cline_row + wp->w_cline_height;
4083 else
4084#endif
4085 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 break;
4087 }
4088
Bram Moolenaar637532b2019-01-03 21:44:40 +01004089 if (draw_state == WL_LINE && (area_highlighting
4090#ifdef FEAT_SPELL
4091 || has_spell
4092#endif
4093 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
4095 /* handle Visual or match highlighting in this line */
4096 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4098 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004100 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 && vcol < tocol))
4102 area_attr = attr; /* start highlighting */
4103 else if (area_attr != 0
4104 && (vcol == tocol
4105 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107
4108#ifdef FEAT_SEARCH_EXTRA
4109 if (!n_extra)
4110 {
4111 /*
4112 * Check for start/end of search pattern match.
4113 * After end, check for start/end of next match.
4114 * When another match, have to check for start again.
4115 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004116 * Do this for 'search_hl' and the match list (ordered by
4117 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004119 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004120 cur = wp->w_match_head;
4121 shl_flag = FALSE;
4122 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004124 if (shl_flag == FALSE
4125 && ((cur != NULL
4126 && cur->priority > SEARCH_HL_PRIORITY)
4127 || cur == NULL))
4128 {
4129 shl = &search_hl;
4130 shl_flag = TRUE;
4131 }
4132 else
4133 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004134 if (cur != NULL)
4135 cur->pos.cur = 0;
4136 pos_inprogress = TRUE;
4137 while (shl->rm.regprog != NULL
4138 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004140 if (shl->startcol != MAXCOL
4141 && v >= (long)shl->startcol
4142 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004144 int tmp_col = v + MB_PTR2LEN(ptr);
4145
4146 if (shl->endcol < tmp_col)
4147 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004149#ifdef FEAT_CONCEAL
4150 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4151 == cur->hlg_id)
4152 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004153 has_match_conc =
4154 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004155 match_conc = cur->conceal_char;
4156 }
4157 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004158 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004161 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 {
4163 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004164 next_search_hl(wp, shl, lnum, (colnr_T)v,
4165 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004166 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004167 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
4169 /* Need to get the line again, a multi-line regexp
4170 * may have made it invalid. */
4171 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4172 ptr = line + v;
4173
4174 if (shl->lnum == lnum)
4175 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004176 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004178 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004180 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004182 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 {
4184 /* highlight empty match, try again after
4185 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004187 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004188 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004190 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
4192
4193 /* Loop to check if the match starts at the
4194 * current position */
4195 continue;
4196 }
4197 }
4198 break;
4199 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004200 if (shl != &search_hl && cur != NULL)
4201 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004203
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004204 /* Use attributes from match with highest priority among
4205 * 'search_hl' and the match list. */
4206 search_attr = search_hl.attr_cur;
4207 cur = wp->w_match_head;
4208 shl_flag = FALSE;
4209 while (cur != NULL || shl_flag == FALSE)
4210 {
4211 if (shl_flag == FALSE
4212 && ((cur != NULL
4213 && cur->priority > SEARCH_HL_PRIORITY)
4214 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004215 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004216 shl = &search_hl;
4217 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004218 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004219 else
4220 shl = &cur->hl;
4221 if (shl->attr_cur != 0)
4222 search_attr = shl->attr_cur;
4223 if (shl != &search_hl && cur != NULL)
4224 cur = cur->next;
4225 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004226 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004227 if (*ptr == NUL && (did_line_attr >= 1
4228 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004229 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231#endif
4232
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004234 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004236 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4237 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004239 if (diff_hlf == HLF_TXD && ptr - line > change_end
4240 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004242 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004243 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004244 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 }
4246#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004247
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004248#ifdef FEAT_TEXT_PROP
4249 if (text_props != NULL)
4250 {
4251 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004252 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004253
4254 // Check if any active property ends.
4255 for (pi = 0; pi < text_props_active; ++pi)
4256 {
4257 int tpi = text_prop_idxs[pi];
4258
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004259 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004260 + text_props[tpi].tp_len)
4261 {
4262 if (pi + 1 < text_props_active)
4263 mch_memmove(text_prop_idxs + pi,
4264 text_prop_idxs + pi + 1,
4265 sizeof(int)
4266 * (text_props_active - (pi + 1)));
4267 --text_props_active;
4268 --pi;
4269 }
4270 }
4271
4272 // Add any text property that starts in this column.
4273 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004274 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004275 text_prop_idxs[text_props_active++] = text_prop_next++;
4276
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004277 text_prop_attr = 0;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004278 if (text_props_active > 0)
4279 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004280 // Sort the properties on priority and/or starting last.
4281 // Then combine the attributes, highest priority last.
4282 current_text_props = text_props;
4283 current_buf = wp->w_buffer;
4284 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4285 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004286
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004287 for (pi = 0; pi < text_props_active; ++pi)
4288 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004289 int tpi = text_prop_idxs[pi];
4290 proptype_T *pt = text_prop_type_by_id(wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004291
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004292 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004293 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004294 int pt_attr = syn_id2attr(pt->pt_hl_id);
4295
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004296 text_prop_type = pt;
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004297 if (text_prop_attr == 0)
4298 text_prop_attr = pt_attr;
4299 else
4300 text_prop_attr = hl_combine_attr(text_prop_attr, pt_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004301 }
4302 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004303 }
4304 }
4305#endif
4306
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004307 /* Decide which of the highlight attributes to use. */
4308 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004309#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004310 if (area_attr != 0)
4311 char_attr = hl_combine_attr(line_attr, area_attr);
4312 else if (search_attr != 0)
4313 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004314 /* Use line_attr when not in the Visual or 'incsearch' area
4315 * (area_attr may be 0 when "noinvcur" is set). */
4316 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004317 || vcol < fromcol || vcol_prev < fromcol_prev
4318 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004319 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004320#else
4321 if (area_attr != 0)
4322 char_attr = area_attr;
4323 else if (search_attr != 0)
4324 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004325#endif
4326 else
4327 {
4328 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004329#ifdef FEAT_TEXT_PROP
4330 if (text_prop_type != NULL)
4331 char_attr = text_prop_attr;
4332 else
4333#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004334#ifdef FEAT_SYN_HL
4335 if (has_syntax)
4336 char_attr = syntax_attr;
4337 else
4338#endif
4339 char_attr = 0;
4340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
4342
4343 /*
4344 * Get the next character to put on the screen.
4345 */
4346 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004347 * The "p_extra" points to the extra stuff that is inserted to
4348 * represent special characters (non-printable stuff) and other
4349 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004350 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004351 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4352 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4354 */
4355 if (n_extra > 0)
4356 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004357 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004359 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004361 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 {
4363 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004364 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004365 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367 else
4368 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370 else
4371 {
4372 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 if (has_mbyte)
4374 {
4375 mb_c = c;
4376 if (enc_utf8)
4377 {
4378 /* If the UTF-8 character is more than one byte:
4379 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004380 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 mb_utf8 = FALSE;
4382 if (mb_l > n_extra)
4383 mb_l = 1;
4384 else if (mb_l > 1)
4385 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004386 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004388 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 }
4390 }
4391 else
4392 {
4393 /* if this is a DBCS character, put it in "mb_c" */
4394 mb_l = MB_BYTE2LEN(c);
4395 if (mb_l >= n_extra)
4396 mb_l = 1;
4397 else if (mb_l > 1)
4398 mb_c = (c << 8) + p_extra[1];
4399 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004400 if (mb_l == 0) /* at the NUL at end-of-line */
4401 mb_l = 1;
4402
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 /* If a double-width char doesn't fit display a '>' in the
4404 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004405 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406# ifdef FEAT_RIGHTLEFT
4407 wp->w_p_rl ? (col <= 0) :
4408# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004409 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 && (*mb_char2cells)(mb_c) == 2)
4411 {
4412 c = '>';
4413 mb_c = c;
4414 mb_l = 1;
4415 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004416 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 /* put the pointer back to output the double-width
4418 * character at the start of the next line. */
4419 ++n_extra;
4420 --p_extra;
4421 }
4422 else
4423 {
4424 n_extra -= mb_l - 1;
4425 p_extra += mb_l - 1;
4426 }
4427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 ++p_extra;
4429 }
4430 --n_extra;
4431 }
4432 else
4433 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004434#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004435 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004436#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004437
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004438 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004439 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 /*
4441 * Get a character from the line itself.
4442 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004443 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004444#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004445 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 if (has_mbyte)
4448 {
4449 mb_c = c;
4450 if (enc_utf8)
4451 {
4452 /* If the UTF-8 character is more than one byte: Decode it
4453 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004454 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 mb_utf8 = FALSE;
4456 if (mb_l > 1)
4457 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004458 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 /* Overlong encoded ASCII or ASCII with composing char
4460 * is displayed normally, except a NUL. */
4461 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004462 {
4463 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004464#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004465 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004466#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004469
4470 /* At start of the line we can have a composing char.
4471 * Draw it as a space with a composing char. */
4472 if (utf_iscomposing(mb_c))
4473 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004474 int i;
4475
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004476 for (i = Screen_mco - 1; i > 0; --i)
4477 u8cc[i] = u8cc[i - 1];
4478 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004479 mb_c = ' ';
4480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 }
4482
4483 if ((mb_l == 1 && c >= 0x80)
4484 || (mb_l >= 1 && mb_c == 0)
4485 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004486# ifdef UNICODE16
4487 || mb_c >= 0x10000
4488# endif
4489 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
4491 /*
4492 * Illegal UTF-8 byte: display as <xx>.
4493 * Non-BMP character : display as ? or fullwidth ?.
4494 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004495# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004497# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 {
4499 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004500# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 if (wp->w_p_rl) /* reverse */
4502 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004503# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004505# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 else if (utf_char2cells(mb_c) != 2)
4507 STRCPY(extra, "?");
4508 else
4509 /* 0xff1f in UTF-8: full-width '?' */
4510 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512
4513 p_extra = extra;
4514 c = *p_extra;
4515 mb_c = mb_ptr2char_adv(&p_extra);
4516 mb_utf8 = (c >= 0x80);
4517 n_extra = (int)STRLEN(p_extra);
4518 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004519 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 if (area_attr == 0 && search_attr == 0)
4521 {
4522 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004523 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 saved_attr2 = char_attr; /* save current attr */
4525 }
4526 }
4527 else if (mb_l == 0) /* at the NUL at end-of-line */
4528 mb_l = 1;
4529#ifdef FEAT_ARABIC
4530 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4531 {
4532 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004533 int pc, pc1, nc;
4534 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
4536 /* The idea of what is the previous and next
4537 * character depends on 'rightleft'. */
4538 if (wp->w_p_rl)
4539 {
4540 pc = prev_c;
4541 pc1 = prev_c1;
4542 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004543 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 }
4545 else
4546 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004547 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004549 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 }
4551 prev_c = mb_c;
4552
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004553 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
4555 else
4556 prev_c = mb_c;
4557#endif
4558 }
4559 else /* enc_dbcs */
4560 {
4561 mb_l = MB_BYTE2LEN(c);
4562 if (mb_l == 0) /* at the NUL at end-of-line */
4563 mb_l = 1;
4564 else if (mb_l > 1)
4565 {
4566 /* We assume a second byte below 32 is illegal.
4567 * Hopefully this is OK for all double-byte encodings!
4568 */
4569 if (ptr[1] >= 32)
4570 mb_c = (c << 8) + ptr[1];
4571 else
4572 {
4573 if (ptr[1] == NUL)
4574 {
4575 /* head byte at end of line */
4576 mb_l = 1;
4577 transchar_nonprint(extra, c);
4578 }
4579 else
4580 {
4581 /* illegal tail byte */
4582 mb_l = 2;
4583 STRCPY(extra, "XX");
4584 }
4585 p_extra = extra;
4586 n_extra = (int)STRLEN(extra) - 1;
4587 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004588 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 c = *p_extra++;
4590 if (area_attr == 0 && search_attr == 0)
4591 {
4592 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004593 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 saved_attr2 = char_attr; /* save current attr */
4595 }
4596 mb_c = c;
4597 }
4598 }
4599 }
4600 /* If a double-width char doesn't fit display a '>' in the
4601 * last column; the character is displayed at the start of the
4602 * next line. */
4603 if ((
4604# ifdef FEAT_RIGHTLEFT
4605 wp->w_p_rl ? (col <= 0) :
4606# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004607 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 && (*mb_char2cells)(mb_c) == 2)
4609 {
4610 c = '>';
4611 mb_c = c;
4612 mb_utf8 = FALSE;
4613 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004614 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 /* Put pointer back so that the character will be
4616 * displayed at the start of the next line. */
4617 --ptr;
4618 }
4619 else if (*ptr != NUL)
4620 ptr += mb_l - 1;
4621
4622 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004623 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004624 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004625 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004628 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004629 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 c = ' ';
4631 if (area_attr == 0 && search_attr == 0)
4632 {
4633 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004634 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 saved_attr2 = char_attr; /* save current attr */
4636 }
4637 mb_c = c;
4638 mb_utf8 = FALSE;
4639 mb_l = 1;
4640 }
4641
4642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 ++ptr;
4644
4645 if (extra_check)
4646 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004647#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004648 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004649#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004650
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004651#ifdef FEAT_TERMINAL
4652 if (get_term_attr)
4653 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004654 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004655
4656 if (!attr_pri)
4657 char_attr = syntax_attr;
4658 else
4659 char_attr = hl_combine_attr(syntax_attr, char_attr);
4660 }
4661#endif
4662
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004663#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004664 // Get syntax attribute, unless still at the start of the line
4665 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004666 v = (long)(ptr - line);
4667 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 {
4669 /* Get the syntax attribute for the character. If there
4670 * is an error, disable syntax highlighting. */
4671 save_did_emsg = did_emsg;
4672 did_emsg = FALSE;
4673
Bram Moolenaar217ad922005-03-20 22:37:15 +00004674 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004675# ifdef FEAT_SPELL
4676 has_spell ? &can_spell :
4677# endif
4678 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679
4680 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004681 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004682 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004683 has_syntax = FALSE;
4684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 else
4686 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004687#ifdef SYN_TIME_LIMIT
4688 if (wp->w_s->b_syn_slow)
4689 has_syntax = FALSE;
4690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691
4692 /* Need to get the line again, a multi-line regexp may
4693 * have made it invalid. */
4694 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4695 ptr = line + v;
4696
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004697# ifdef FEAT_TEXT_PROP
4698 // Text properties overrule syntax highlighting.
4699 if (text_prop_attr == 0)
4700#endif
4701 {
4702 if (!attr_pri)
4703 char_attr = syntax_attr;
4704 else
4705 char_attr = hl_combine_attr(syntax_attr, char_attr);
4706 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004707# ifdef FEAT_CONCEAL
4708 /* no concealing past the end of the line, it interferes
4709 * with line highlighting */
4710 if (c == NUL)
4711 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004712 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004713 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004716#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004717
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004718#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004719 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004720 * Only do this when there is no syntax highlighting, the
4721 * @Spell cluster is not used or the current syntax item
4722 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004723 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004724 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004725 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004726 if (c != 0 && (
4727# ifdef FEAT_SYN_HL
4728 !has_syntax ||
4729# endif
4730 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004731 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004732 char_u *prev_ptr, *p;
4733 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004734 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004735 if (has_mbyte)
4736 {
4737 prev_ptr = ptr - mb_l;
4738 v -= mb_l - 1;
4739 }
4740 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004741 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004742
4743 /* Use nextline[] if possible, it has the start of the
4744 * next line concatenated. */
4745 if ((prev_ptr - line) - nextlinecol >= 0)
4746 p = nextline + (prev_ptr - line) - nextlinecol;
4747 else
4748 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004749 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004750 len = spell_check(wp, p, &spell_hlf, &cap_col,
4751 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004752 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004753
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004754 /* In Insert mode only highlight a word that
4755 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004756 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004757 && (State & INSERT) != 0
4758 && wp->w_cursor.lnum == lnum
4759 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004760 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004761 && wp->w_cursor.col < (colnr_T)word_end)
4762 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004763 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004764 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004765 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004766
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004767 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004768 && (p - nextline) + len > nextline_idx)
4769 {
4770 /* Remember that the good word continues at the
4771 * start of the next line. */
4772 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004773 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004774 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004775
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004776 /* Turn index into actual attributes. */
4777 if (spell_hlf != HLF_COUNT)
4778 spell_attr = highlight_attr[spell_hlf];
4779
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004780 if (cap_col > 0)
4781 {
4782 if (p != prev_ptr
4783 && (p - nextline) + cap_col >= nextline_idx)
4784 {
4785 /* Remember that the word in the next line
4786 * must start with a capital. */
4787 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004788 cap_col = (int)((p - nextline) + cap_col
4789 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004790 }
4791 else
4792 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004793 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004794 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004795 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004796 }
4797 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004798 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004799 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004800 char_attr = hl_combine_attr(char_attr, spell_attr);
4801 else
4802 char_attr = hl_combine_attr(spell_attr, char_attr);
4803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804#endif
4805#ifdef FEAT_LINEBREAK
4806 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004807 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004809 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004810 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004812 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004813 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004814
Bram Moolenaar597a4222014-06-25 14:39:50 +02004815 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004816 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004817 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004818 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004819# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004820 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004821 wp->w_buffer->b_p_vts_array) - 1;
4822# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004823 n_extra = (int)wp->w_buffer->b_p_ts
4824 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004825# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004826
Bram Moolenaar4df70292015-03-21 14:20:16 +01004827 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004828 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004829 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004830 {
4831#ifdef FEAT_CONCEAL
4832 if (c == TAB)
4833 /* See "Tab alignment" below. */
4834 FIX_FOR_BOGUSCOLS;
4835#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004836 if (!wp->w_p_list)
4837 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
4840#endif
4841
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004842 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4843 */
4844 if (wp->w_p_list
4845 && (((c == 160
Bram Moolenaara12a1612019-01-24 16:39:02 +01004846 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f)))
4847 && lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004848 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4849 {
4850 c = (c == ' ') ? lcs_space : lcs_nbsp;
4851 if (area_attr == 0 && search_attr == 0)
4852 {
4853 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004854 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004855 saved_attr2 = char_attr; /* save current attr */
4856 }
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004857 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004858 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004859 {
4860 mb_utf8 = TRUE;
4861 u8cc[0] = 0;
4862 c = 0xc0;
4863 }
4864 else
4865 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004866 }
4867
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4869 {
4870 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004871 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
4873 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004874 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 saved_attr2 = char_attr; /* save current attr */
4876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004878 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
4880 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004881 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004882 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
4884 else
4885 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 }
4887 }
4888
4889 /*
4890 * Handling of non-printable characters.
4891 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004892 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
4894 /*
4895 * when getting a character from the file, we may have to
4896 * turn it into something else on the way to putting it
4897 * into "ScreenLines".
4898 */
4899 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4900 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004901 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004902 long vcol_adjusted = vcol; /* removed showbreak length */
4903#ifdef FEAT_LINEBREAK
4904 /* only adjust the tab_len, when at the first column
4905 * after the showbreak value was drawn */
4906 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4907 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004910#ifdef FEAT_VARTABS
4911 tab_len = tabstop_padding(vcol_adjusted,
4912 wp->w_buffer->b_p_ts,
4913 wp->w_buffer->b_p_vts_array) - 1;
4914#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004915 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004916 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4917#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004918
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004919#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004920 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004921#endif
4922 /* tab amount depends on current column */
4923 n_extra = tab_len;
4924#ifdef FEAT_LINEBREAK
4925 else
4926 {
4927 char_u *p;
4928 int len = n_extra;
4929 int i;
4930 int saved_nextra = n_extra;
4931
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004932#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004933 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004934 /* there are characters to conceal */
4935 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004936 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4937 */
4938 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4939 && n_extra > tab_len)
4940 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004941#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004942
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004943 /* if n_extra > 0, it gives the number of chars, to
4944 * use for a tab, else we need to calculate the width
4945 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004946 len = (tab_len * mb_char2len(lcs_tab2));
4947 if (n_extra > 0)
4948 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004949 c = lcs_tab1;
4950 p = alloc((unsigned)(len + 1));
4951 vim_memset(p, ' ', len);
4952 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004953 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004954 p_extra_free = p;
4955 for (i = 0; i < tab_len; i++)
4956 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004957 if (*p == NUL)
4958 {
4959 tab_len = i;
4960 break;
4961 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004962 mb_char2bytes(lcs_tab2, p);
4963 p += mb_char2len(lcs_tab2);
4964 n_extra += mb_char2len(lcs_tab2)
4965 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004966 }
4967 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004968#ifdef FEAT_CONCEAL
4969 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4970 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004971 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004972 n_extra -= vcol_off;
4973#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004974 }
4975#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004976#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004977 {
4978 int vc_saved = vcol_off;
4979
4980 /* Tab alignment should be identical regardless of
4981 * 'conceallevel' value. So tab compensates of all
4982 * previous concealed characters, and thus resets
4983 * vcol_off and boguscols accumulated so far in the
4984 * line. Note that the tab can be longer than
4985 * 'tabstop' when there are concealed characters. */
4986 FIX_FOR_BOGUSCOLS;
4987
4988 /* Make sure, the highlighting for the tab char will be
4989 * correctly set further below (effectively reverts the
4990 * FIX_FOR_BOGSUCOLS macro */
4991 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004992 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004993 tab_len += vc_saved;
4994 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 if (wp->w_p_list)
4998 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004999 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005000#ifdef FEAT_LINEBREAK
5001 if (wp->w_p_lbr)
5002 c_extra = NUL; /* using p_extra from above */
5003 else
5004#endif
5005 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005006 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005007 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005008 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005011 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
5013 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005014 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005015 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
5018 else
5019 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005020 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 c_extra = ' ';
5022 c = ' ';
5023 }
5024 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005025 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005026 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005027 || ((fromcol >= 0 || fromcol_prev >= 0)
5028 && tocol > vcol
5029 && VIsual_mode != Ctrl_V
5030 && (
5031# ifdef FEAT_RIGHTLEFT
5032 wp->w_p_rl ? (col >= 0) :
5033# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005034 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005035 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005036 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005037 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005038 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005040 /* Display a '$' after the line or highlight an extra
5041 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5043 /* For a diff line the highlighting continues after the
5044 * "$". */
5045 if (
5046# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005047 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048# ifdef LINE_ATTR
5049 &&
5050# endif
5051# endif
5052# ifdef LINE_ATTR
5053 line_attr == 0
5054# endif
5055 )
5056#endif
5057 {
5058#ifdef FEAT_VIRTUALEDIT
5059 /* In virtualedit, visual selections may extend
5060 * beyond end of line. */
5061 if (area_highlighting && virtual_active()
5062 && tocol != MAXCOL && vcol < tocol)
5063 n_extra = 0;
5064 else
5065#endif
5066 {
5067 p_extra = at_end_str;
5068 n_extra = 1;
5069 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005070 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005073 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005074 c = lcs_eol;
5075 else
5076 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 lcs_eol_one = -1;
5078 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005079 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005081 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 n_attr = 1;
5083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005085 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 {
5087 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005088 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005089 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 }
5091 else
5092 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
5094 else if (c != NUL)
5095 {
5096 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005097 if (n_extra == 0)
5098 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099#ifdef FEAT_RIGHTLEFT
5100 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5101 rl_mirror(p_extra); /* reverse "<12>" */
5102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005104 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005105#ifdef FEAT_LINEBREAK
5106 if (wp->w_p_lbr)
5107 {
5108 char_u *p;
5109
5110 c = *p_extra;
5111 p = alloc((unsigned)n_extra + 1);
5112 vim_memset(p, ' ', n_extra);
5113 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5114 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005115 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005116 p_extra_free = p_extra = p;
5117 }
5118 else
5119#endif
5120 {
5121 n_extra = byte2cells(c) - 1;
5122 c = *p_extra++;
5123 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005124 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 {
5126 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005127 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 saved_attr2 = char_attr; /* save current attr */
5129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 }
5132#ifdef FEAT_VIRTUALEDIT
5133 else if (VIsual_active
5134 && (VIsual_mode == Ctrl_V
5135 || VIsual_mode == 'v')
5136 && virtual_active()
5137 && tocol != MAXCOL
5138 && vcol < tocol
5139 && (
5140# ifdef FEAT_RIGHTLEFT
5141 wp->w_p_rl ? (col >= 0) :
5142# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005143 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 {
5145 c = ' ';
5146 --ptr; /* put it back at the NUL */
5147 }
5148#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005149#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 else if ((
5151# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005152 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005154# ifdef FEAT_TERMINAL
5155 term_attr != 0 ||
5156# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 ) && (
5159# ifdef FEAT_RIGHTLEFT
5160 wp->w_p_rl ? (col >= 0) :
5161# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005162 (col
5163# ifdef FEAT_CONCEAL
5164 - boguscols
5165# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005166 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 {
5168 /* Highlight until the right side of the window */
5169 c = ' ';
5170 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005171
5172 /* Remember we do the char for line highlighting. */
5173 ++did_line_attr;
5174
5175 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005176 if (line_attr != 0 && char_attr == search_attr
5177 && (did_line_attr > 1
5178 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005179 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180# ifdef FEAT_DIFF
5181 if (diff_hlf == HLF_TXD)
5182 {
5183 diff_hlf = HLF_CHD;
5184 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005185 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005186 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005187 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5188 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005189 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 }
5192# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005193# ifdef FEAT_TERMINAL
5194 if (term_attr != 0)
5195 {
5196 char_attr = term_attr;
5197 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5198 char_attr = hl_combine_attr(char_attr,
5199 HL_ATTR(HLF_CUL));
5200 }
5201# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
5203#endif
5204 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005205
5206#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005207 if ( wp->w_p_cole > 0
5208 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005209 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005210 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005211 && !(lnum_in_visual_area
5212 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005213 {
5214 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005215 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005216 && (syn_get_sub_char() != NUL || match_conc
5217 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005218 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005219 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005220 /* First time at this concealed item: display one
5221 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005222 if (match_conc)
5223 c = match_conc;
5224 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005225 c = syn_get_sub_char();
5226 else if (lcs_conceal != NUL)
5227 c = lcs_conceal;
5228 else
5229 c = ' ';
5230
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005231 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005232
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005233 if (n_extra > 0)
5234 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005235 vcol += n_extra;
5236 if (wp->w_p_wrap && n_extra > 0)
5237 {
5238# ifdef FEAT_RIGHTLEFT
5239 if (wp->w_p_rl)
5240 {
5241 col -= n_extra;
5242 boguscols -= n_extra;
5243 }
5244 else
5245# endif
5246 {
5247 boguscols += n_extra;
5248 col += n_extra;
5249 }
5250 }
5251 n_extra = 0;
5252 n_attr = 0;
5253 }
5254 else if (n_skip == 0)
5255 {
5256 is_concealing = TRUE;
5257 n_skip = 1;
5258 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005259 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005260 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005261 {
5262 mb_utf8 = TRUE;
5263 u8cc[0] = 0;
5264 c = 0xc0;
5265 }
5266 else
5267 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005268 }
5269 else
5270 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005271 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005272 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005273 }
5274#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 }
5276
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005277#ifdef FEAT_CONCEAL
5278 /* In the cursor line and we may be concealing characters: correct
5279 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005280 if (!did_wcol && draw_state == WL_LINE
5281 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005282 && conceal_cursor_line(wp)
5283 && (int)wp->w_virtcol <= vcol + n_skip)
5284 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005285# ifdef FEAT_RIGHTLEFT
5286 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005287 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005288 else
5289# endif
5290 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005291 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005292 did_wcol = TRUE;
5293 }
5294#endif
5295
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 /* Don't override visual selection highlighting. */
5297 if (n_attr > 0
5298 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005299 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 char_attr = extra_attr;
5301
Bram Moolenaar81695252004-12-29 20:58:21 +00005302#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 /* XIM don't send preedit_start and preedit_end, but they send
5304 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5305 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005306 if (p_imst == IM_ON_THE_SPOT
5307 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005308 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 && (State & INSERT)
5310 && !p_imdisable
5311 && im_is_preediting()
5312 && draw_state == WL_LINE)
5313 {
5314 colnr_T tcol;
5315
5316 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005317 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 else
5319 tcol = preedit_end_col;
5320 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5321 {
5322 if (feedback_old_attr < 0)
5323 {
5324 feedback_col = 0;
5325 feedback_old_attr = char_attr;
5326 }
5327 char_attr = im_get_feedback_attr(feedback_col);
5328 if (char_attr < 0)
5329 char_attr = feedback_old_attr;
5330 feedback_col++;
5331 }
5332 else if (feedback_old_attr >= 0)
5333 {
5334 char_attr = feedback_old_attr;
5335 feedback_old_attr = -1;
5336 feedback_col = 0;
5337 }
5338 }
5339#endif
5340 /*
5341 * Handle the case where we are in column 0 but not on the first
5342 * character of the line and the user wants us to show us a
5343 * special character (via 'listchars' option "precedes:<char>".
5344 */
5345 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005346 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5348#ifdef FEAT_DIFF
5349 && filler_todo <= 0
5350#endif
5351 && draw_state > WL_NR
5352 && c != NUL)
5353 {
5354 c = lcs_prec;
5355 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005356 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5357 {
5358 /* Double-width character being overwritten by the "precedes"
5359 * character, need to fill up half the character. */
5360 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005361 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005362 n_extra = 1;
5363 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005364 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005367 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 {
5369 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005370 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005371 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 }
5373 else
5374 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005375 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
5377 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005378 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 n_attr3 = 1;
5380 }
5381 }
5382
5383 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005384 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005386 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005387#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005388 || did_line_attr == 1
5389#endif
5390 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005392#ifdef FEAT_SEARCH_EXTRA
5393 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005394
5395 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005396 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005397 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005398#endif
5399
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005400 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 * highlight match at end of line. If it's beyond the last
5402 * char on the screen, just overwrite that one (tricky!) Not
5403 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005404#ifdef FEAT_SEARCH_EXTRA
5405 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005406 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005407 prevcol_hl_flag = TRUE;
5408 else
5409 {
5410 cur = wp->w_match_head;
5411 while (cur != NULL)
5412 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005413 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005414 {
5415 prevcol_hl_flag = TRUE;
5416 break;
5417 }
5418 cur = cur->next;
5419 }
5420 }
5421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005423 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005424 && (VIsual_mode != Ctrl_V
5425 || lnum == VIsual.lnum
5426 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005427 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428#ifdef FEAT_SEARCH_EXTRA
5429 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005430 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005431# ifdef FEAT_SYN_HL
5432 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5433 && !(wp == curwin && VIsual_active))
5434# endif
5435# ifdef FEAT_DIFF
5436 && diff_hlf == (hlf_T)0
5437# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005438# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005439 && did_line_attr <= 1
5440# endif
5441 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442#endif
5443 ))
5444 {
5445 int n = 0;
5446
5447#ifdef FEAT_RIGHTLEFT
5448 if (wp->w_p_rl)
5449 {
5450 if (col < 0)
5451 n = 1;
5452 }
5453 else
5454#endif
5455 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005456 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 n = -1;
5458 }
5459 if (n != 0)
5460 {
5461 /* At the window boundary, highlight the last character
5462 * instead (better than nothing). */
5463 off += n;
5464 col += n;
5465 }
5466 else
5467 {
5468 /* Add a blank character to highlight. */
5469 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 if (enc_utf8)
5471 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 }
5473#ifdef FEAT_SEARCH_EXTRA
5474 if (area_attr == 0)
5475 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005476 /* Use attributes from match with highest priority among
5477 * 'search_hl' and the match list. */
5478 char_attr = search_hl.attr;
5479 cur = wp->w_match_head;
5480 shl_flag = FALSE;
5481 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005482 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005483 if (shl_flag == FALSE
5484 && ((cur != NULL
5485 && cur->priority > SEARCH_HL_PRIORITY)
5486 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005487 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005488 shl = &search_hl;
5489 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005490 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005491 else
5492 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005493 if ((ptr - line) - 1 == (long)shl->startcol
5494 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005495 char_attr = shl->attr;
5496 if (shl != &search_hl && cur != NULL)
5497 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 }
5500#endif
5501 ScreenAttrs[off] = char_attr;
5502#ifdef FEAT_RIGHTLEFT
5503 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005504 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005506 --off;
5507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 else
5509#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005510 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005512 ++off;
5513 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005514 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005515#ifdef FEAT_SYN_HL
5516 eol_hl_off = 1;
5517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520
Bram Moolenaar91170f82006-05-05 21:15:17 +00005521 /*
5522 * At end of the text line.
5523 */
5524 if (c == NUL)
5525 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005526#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005527 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005528 if (wp->w_p_wrap)
5529 v = wp->w_skipcol;
5530 else
5531 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005532
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005533 /* check if line ends before left margin */
5534 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005535 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005536#ifdef FEAT_CONCEAL
5537 /* Get rid of the boguscols now, we want to draw until the right
5538 * edge for 'cursorcolumn'. */
5539 col -= boguscols;
5540 boguscols = 0;
5541#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005542
5543 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005544 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005545
5546 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005547 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5548 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005549 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005550 && lnum != wp->w_cursor.lnum)
5551 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005552# ifdef FEAT_RIGHTLEFT
5553 && !wp->w_p_rl
5554# endif
5555 )
5556 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005557 int rightmost_vcol = 0;
5558 int i;
5559
5560 if (wp->w_p_cuc)
5561 rightmost_vcol = wp->w_virtcol;
5562 if (draw_color_col)
5563 /* determine rightmost colorcolumn to possibly draw */
5564 for (i = 0; color_cols[i] >= 0; ++i)
5565 if (rightmost_vcol < color_cols[i])
5566 rightmost_vcol = color_cols[i];
5567
Bram Moolenaar02631462017-09-22 15:20:32 +02005568 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005569 {
5570 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005571 if (enc_utf8)
5572 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005573 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005574 if (draw_color_col)
5575 draw_color_col = advance_color_col(VCOL_HLC,
5576 &color_cols);
5577
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005578 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005579 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005580 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005581 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005582 else
5583 ScreenAttrs[off++] = 0;
5584
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005585 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005586 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005587
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005588 ++vcol;
5589 }
5590 }
5591#endif
5592
Bram Moolenaar53f81742017-09-22 14:35:51 +02005593 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005594 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 row++;
5596
5597 /*
5598 * Update w_cline_height and w_cline_folded if the cursor line was
5599 * updated (saves a call to plines() later).
5600 */
5601 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5602 {
5603 curwin->w_cline_row = startrow;
5604 curwin->w_cline_height = row - startrow;
5605#ifdef FEAT_FOLDING
5606 curwin->w_cline_folded = FALSE;
5607#endif
5608 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5609 }
5610
5611 break;
5612 }
5613
5614 /* line continues beyond line end */
5615 if (lcs_ext
5616 && !wp->w_p_wrap
5617#ifdef FEAT_DIFF
5618 && filler_todo <= 0
5619#endif
5620 && (
5621#ifdef FEAT_RIGHTLEFT
5622 wp->w_p_rl ? col == 0 :
5623#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005624 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005626 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5628 {
5629 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005630 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005632 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 {
5634 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005635 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005636 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 }
5638 else
5639 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 }
5641
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005642#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005643 /* advance to the next 'colorcolumn' */
5644 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005645 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005646
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005647 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005648 * highlight the cursor position itself.
5649 * Also highlight the 'colorcolumn' if it is different than
5650 * 'cursorcolumn' */
5651 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005652 if (draw_state == WL_LINE && !lnum_in_visual_area
5653 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005654 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005655 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005656 && lnum != wp->w_cursor.lnum)
5657 {
5658 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005659 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005660 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005661 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005662 {
5663 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005664 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005665 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005666 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005667#endif
5668
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 /*
5670 * Store character to be displayed.
5671 * Skip characters that are left of the screen for 'nowrap'.
5672 */
5673 vcol_prev = vcol;
5674 if (draw_state < WL_LINE || n_skip <= 0)
5675 {
5676 /*
5677 * Store the character.
5678 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005679#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5681 {
5682 /* A double-wide character is: put first halve in left cell. */
5683 --off;
5684 --col;
5685 }
5686#endif
5687 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005689 {
5690 if ((mb_c & 0xff00) == 0x8e00)
5691 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 else if (enc_utf8)
5695 {
5696 if (mb_utf8)
5697 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005698 int i;
5699
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005701 if ((c & 0xff) == 0)
5702 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005703 for (i = 0; i < Screen_mco; ++i)
5704 {
5705 ScreenLinesC[i][off] = u8cc[i];
5706 if (u8cc[i] == 0)
5707 break;
5708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 }
5710 else
5711 ScreenLinesUC[off] = 0;
5712 }
5713 if (multi_attr)
5714 {
5715 ScreenAttrs[off] = multi_attr;
5716 multi_attr = 0;
5717 }
5718 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 ScreenAttrs[off] = char_attr;
5720
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5722 {
5723 /* Need to fill two screen columns. */
5724 ++off;
5725 ++col;
5726 if (enc_utf8)
5727 /* UTF-8: Put a 0 in the second screen char. */
5728 ScreenLines[off] = 0;
5729 else
5730 /* DBCS: Put second byte in the second screen char. */
5731 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005732 if (draw_state > WL_NR
5733#ifdef FEAT_DIFF
5734 && filler_todo <= 0
5735#endif
5736 )
5737 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 /* When "tocol" is halfway a character, set it to the end of
5739 * the character, otherwise highlighting won't stop. */
5740 if (tocol == vcol)
5741 ++tocol;
5742#ifdef FEAT_RIGHTLEFT
5743 if (wp->w_p_rl)
5744 {
5745 /* now it's time to backup one cell */
5746 --off;
5747 --col;
5748 }
5749#endif
5750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751#ifdef FEAT_RIGHTLEFT
5752 if (wp->w_p_rl)
5753 {
5754 --off;
5755 --col;
5756 }
5757 else
5758#endif
5759 {
5760 ++off;
5761 ++col;
5762 }
5763 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005764#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005765 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005766 {
5767 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005768 ++vcol_off;
5769 if (n_extra > 0)
5770 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005771 if (wp->w_p_wrap)
5772 {
5773 /*
5774 * Special voodoo required if 'wrap' is on.
5775 *
5776 * Advance the column indicator to force the line
5777 * drawing to wrap early. This will make the line
5778 * take up the same screen space when parts are concealed,
5779 * so that cursor line computations aren't messed up.
5780 *
5781 * To avoid the fictitious advance of 'col' causing
5782 * trailing junk to be written out of the screen line
5783 * we are building, 'boguscols' keeps track of the number
5784 * of bad columns we have advanced.
5785 */
5786 if (n_extra > 0)
5787 {
5788 vcol += n_extra;
5789# ifdef FEAT_RIGHTLEFT
5790 if (wp->w_p_rl)
5791 {
5792 col -= n_extra;
5793 boguscols -= n_extra;
5794 }
5795 else
5796# endif
5797 {
5798 col += n_extra;
5799 boguscols += n_extra;
5800 }
5801 n_extra = 0;
5802 n_attr = 0;
5803 }
5804
5805
Bram Moolenaar860cae12010-06-05 23:22:07 +02005806 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5807 {
5808 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005809# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005810 if (wp->w_p_rl)
5811 {
5812 --boguscols;
5813 --col;
5814 }
5815 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005816# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005817 {
5818 ++boguscols;
5819 ++col;
5820 }
5821 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005822
5823# ifdef FEAT_RIGHTLEFT
5824 if (wp->w_p_rl)
5825 {
5826 --boguscols;
5827 --col;
5828 }
5829 else
5830# endif
5831 {
5832 ++boguscols;
5833 ++col;
5834 }
5835 }
5836 else
5837 {
5838 if (n_extra > 0)
5839 {
5840 vcol += n_extra;
5841 n_extra = 0;
5842 n_attr = 0;
5843 }
5844 }
5845
5846 }
5847#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 else
5849 --n_skip;
5850
Bram Moolenaar64486672010-05-16 15:46:46 +02005851 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5852 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005853 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854#ifdef FEAT_DIFF
5855 && filler_todo <= 0
5856#endif
5857 )
5858 ++vcol;
5859
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005860#ifdef FEAT_SYN_HL
5861 if (vcol_save_attr >= 0)
5862 char_attr = vcol_save_attr;
5863#endif
5864
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 /* restore attributes after "predeces" in 'listchars' */
5866 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5867 char_attr = saved_attr3;
5868
5869 /* restore attributes after last 'listchars' or 'number' char */
5870 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5871 char_attr = saved_attr2;
5872
5873 /*
5874 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005875 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 */
5877 if ((
5878#ifdef FEAT_RIGHTLEFT
5879 wp->w_p_rl ? (col < 0) :
5880#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005881 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 && (*ptr != NUL
5883#ifdef FEAT_DIFF
5884 || filler_todo > 0
5885#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005886 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5888 )
5889 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005890#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005891 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005892 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005893 boguscols = 0;
5894#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005895 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005896 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 ++row;
5899 ++screen_row;
5900
5901 /* When not wrapping and finished diff lines, or when displayed
5902 * '$' and highlighting until last column, break here. */
5903 if ((!wp->w_p_wrap
5904#ifdef FEAT_DIFF
5905 && filler_todo <= 0
5906#endif
5907 ) || lcs_eol_one == -1)
5908 break;
5909
5910 /* When the window is too narrow draw all "@" lines. */
5911 if (draw_state != WL_LINE
5912#ifdef FEAT_DIFF
5913 && filler_todo <= 0
5914#endif
5915 )
5916 {
5917 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 row = endrow;
5920 }
5921
5922 /* When line got too long for screen break here. */
5923 if (row == endrow)
5924 {
5925 ++row;
5926 break;
5927 }
5928
5929 if (screen_cur_row == screen_row - 1
5930#ifdef FEAT_DIFF
5931 && filler_todo <= 0
5932#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005933 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 {
5935 /* Remember that the line wraps, used for modeless copy. */
5936 LineWraps[screen_row - 1] = TRUE;
5937
5938 /*
5939 * Special trick to make copy/paste of wrapped lines work with
5940 * xterm/screen: write an extra character beyond the end of
5941 * the line. This will work with all terminal types
5942 * (regardless of the xn,am settings).
5943 * Only do this on a fast tty.
5944 * Only do this if the cursor is on the current line
5945 * (something has been written in it).
5946 * Don't do this for the GUI.
5947 * Don't do this for double-width characters.
5948 * Don't do this for a window not at the right screen border.
5949 */
5950 if (p_tf
5951#ifdef FEAT_GUI
5952 && !gui.in_use
5953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005955 && ((*mb_off2cells)(LineOffset[screen_row],
5956 LineOffset[screen_row] + screen_Columns)
5957 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005959 + (int)Columns - 2,
5960 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01005961 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 {
5963 /* First make sure we are at the end of the screen line,
5964 * then output the same character again to let the
5965 * terminal know about the wrap. If the terminal doesn't
5966 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005967 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 screen_char(LineOffset[screen_row - 1]
5969 + (unsigned)Columns - 1,
5970 screen_row - 1, (int)(Columns - 1));
5971
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 /* When there is a multi-byte character, just output a
5973 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005974 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5975 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 out_char(' ');
5977 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 out_char(ScreenLines[LineOffset[screen_row - 1]
5979 + (Columns - 1)]);
5980 /* force a redraw of the first char on the next line */
5981 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5982 screen_start(); /* don't know where cursor is now */
5983 }
5984 }
5985
5986 col = 0;
5987 off = (unsigned)(current_ScreenLine - ScreenLines);
5988#ifdef FEAT_RIGHTLEFT
5989 if (wp->w_p_rl)
5990 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005991 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 off += col;
5993 }
5994#endif
5995
5996 /* reset the drawing state for the start of a wrapped line */
5997 draw_state = WL_START;
5998 saved_n_extra = n_extra;
5999 saved_p_extra = p_extra;
6000 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006001 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 saved_char_attr = char_attr;
6003 n_extra = 0;
6004 lcs_prec_todo = lcs_prec;
6005#ifdef FEAT_LINEBREAK
6006# ifdef FEAT_DIFF
6007 if (filler_todo <= 0)
6008# endif
6009 need_showbreak = TRUE;
6010#endif
6011#ifdef FEAT_DIFF
6012 --filler_todo;
6013 /* When the filler lines are actually below the last line of the
6014 * file, don't draw the line itself, break here. */
6015 if (filler_todo == 0 && wp->w_botfill)
6016 break;
6017#endif
6018 }
6019
6020 } /* for every character in the line */
6021
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006022#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006023 /* After an empty line check first word for capital. */
6024 if (*skipwhite(line) == NUL)
6025 {
6026 capcol_lnum = lnum + 1;
6027 cap_col = 0;
6028 }
6029#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006030#ifdef FEAT_TEXT_PROP
6031 vim_free(text_props);
6032 vim_free(text_prop_idxs);
6033#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006034
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006035 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 return row;
6037}
6038
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006039/*
6040 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006041 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006042 */
6043 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006044comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006045{
6046 int i;
6047
6048 for (i = 0; i < Screen_mco; ++i)
6049 {
6050 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6051 return TRUE;
6052 if (ScreenLinesC[i][off_from] == 0)
6053 break;
6054 }
6055 return FALSE;
6056}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006057
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058/*
6059 * Check whether the given character needs redrawing:
6060 * - the (first byte of the) character is different
6061 * - the attributes are different
6062 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006063 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 */
6065 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006066char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067{
6068 if (cols > 0
6069 && ((ScreenLines[off_from] != ScreenLines[off_to]
6070 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 || (enc_dbcs != 0
6072 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6073 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6074 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6075 : (cols > 1 && ScreenLines[off_from + 1]
6076 != ScreenLines[off_to + 1])))
6077 || (enc_utf8
6078 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6079 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006080 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006081 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6082 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006083 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 return TRUE;
6085 return FALSE;
6086}
6087
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006088#if defined(FEAT_TERMINAL) || defined(PROTO)
6089/*
6090 * Return the index in ScreenLines[] for the current screen line.
6091 */
6092 int
6093screen_get_current_line_off()
6094{
6095 return (int)(current_ScreenLine - ScreenLines);
6096}
6097#endif
6098
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099/*
6100 * Move one "cooked" screen line to the screen, but only the characters that
6101 * have actually changed. Handle insert/delete character.
6102 * "coloff" gives the first column on the screen for this line.
6103 * "endcol" gives the columns where valid characters are.
6104 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6105 * needs to be cleared, negative otherwise.
6106 * "rlflag" is TRUE in a rightleft window:
6107 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6108 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6109 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006110 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006111screen_line(
6112 int row,
6113 int coloff,
6114 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006115 int clear_width,
6116 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117{
6118 unsigned off_from;
6119 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006120 unsigned max_off_from;
6121 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 int force = FALSE; /* force update rest of the line */
6125 int redraw_this /* bool: does character need redraw? */
6126#ifdef FEAT_GUI
6127 = TRUE /* For GUI when while-loop empty */
6128#endif
6129 ;
6130 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 int clear_next = FALSE;
6132 int char_cells; /* 1: normal char */
6133 /* 2: occupies two display cells */
6134# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006136 /* Check for illegal row and col, just in case. */
6137 if (row >= Rows)
6138 row = Rows - 1;
6139 if (endcol > Columns)
6140 endcol = Columns;
6141
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142# ifdef FEAT_CLIPBOARD
6143 clip_may_clear_selection(row, row);
6144# endif
6145
6146 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6147 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006148 max_off_from = off_from + screen_Columns;
6149 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150
6151#ifdef FEAT_RIGHTLEFT
6152 if (rlflag)
6153 {
6154 /* Clear rest first, because it's left of the text. */
6155 if (clear_width > 0)
6156 {
6157 while (col <= endcol && ScreenLines[off_to] == ' '
6158 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006159 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 {
6161 ++off_to;
6162 ++col;
6163 }
6164 if (col <= endcol)
6165 screen_fill(row, row + 1, col + coloff,
6166 endcol + coloff + 1, ' ', ' ', 0);
6167 }
6168 col = endcol + 1;
6169 off_to = LineOffset[row] + col + coloff;
6170 off_from += col;
6171 endcol = (clear_width > 0 ? clear_width : -clear_width);
6172 }
6173#endif /* FEAT_RIGHTLEFT */
6174
6175 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6176
6177 while (col < endcol)
6178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006180 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 else
6182 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183
6184 redraw_this = redraw_next;
6185 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6186 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6187
6188#ifdef FEAT_GUI
6189 /* If the next character was bold, then redraw the current character to
6190 * remove any pixels that might have spilt over into us. This only
6191 * happens in the GUI.
6192 */
6193 if (redraw_next && gui.in_use)
6194 {
6195 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006196 if (hl > HL_ALL)
6197 hl = syn_attr2attr(hl);
6198 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 redraw_this = TRUE;
6200 }
6201#endif
6202
6203 if (redraw_this)
6204 {
6205 /*
6206 * Special handling when 'xs' termcap flag set (hpterm):
6207 * Attributes for characters are stored at the position where the
6208 * cursor is when writing the highlighting code. The
6209 * start-highlighting code must be written with the cursor on the
6210 * first highlighted character. The stop-highlighting code must
6211 * be written with the cursor just after the last highlighted
6212 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006213 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 * to clear the rest of the line, and force redrawing it
6215 * completely.
6216 */
6217 if ( p_wiv
6218 && !force
6219#ifdef FEAT_GUI
6220 && !gui.in_use
6221#endif
6222 && ScreenAttrs[off_to] != 0
6223 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6224 {
6225 /*
6226 * Need to remove highlighting attributes here.
6227 */
6228 windgoto(row, col + coloff);
6229 out_str(T_CE); /* clear rest of this screen line */
6230 screen_start(); /* don't know where cursor is now */
6231 force = TRUE; /* force redraw of rest of the line */
6232 redraw_next = TRUE; /* or else next char would miss out */
6233
6234 /*
6235 * If the previous character was highlighted, need to stop
6236 * highlighting at this character.
6237 */
6238 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6239 {
6240 screen_attr = ScreenAttrs[off_to - 1];
6241 term_windgoto(row, col + coloff);
6242 screen_stop_highlight();
6243 }
6244 else
6245 screen_attr = 0; /* highlighting has stopped */
6246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 if (enc_dbcs != 0)
6248 {
6249 /* Check if overwriting a double-byte with a single-byte or
6250 * the other way around requires another character to be
6251 * redrawn. For UTF-8 this isn't needed, because comparing
6252 * ScreenLinesUC[] is sufficient. */
6253 if (char_cells == 1
6254 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006255 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 {
6257 /* Writing a single-cell character over a double-cell
6258 * character: need to redraw the next cell. */
6259 ScreenLines[off_to + 1] = 0;
6260 redraw_next = TRUE;
6261 }
6262 else if (char_cells == 2
6263 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006264 && (*mb_off2cells)(off_to, max_off_to) == 1
6265 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 {
6267 /* Writing the second half of a double-cell character over
6268 * a double-cell character: need to redraw the second
6269 * cell. */
6270 ScreenLines[off_to + 2] = 0;
6271 redraw_next = TRUE;
6272 }
6273
6274 if (enc_dbcs == DBCS_JPNU)
6275 ScreenLines2[off_to] = ScreenLines2[off_from];
6276 }
6277 /* When writing a single-width character over a double-width
6278 * character and at the end of the redrawn text, need to clear out
6279 * the right halve of the old character.
6280 * Also required when writing the right halve of a double-width
6281 * char over the left halve of an existing one. */
6282 if (has_mbyte && col + char_cells == endcol
6283 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006284 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006286 && (*mb_off2cells)(off_to, max_off_to) == 1
6287 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289
6290 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 if (enc_utf8)
6292 {
6293 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6294 if (ScreenLinesUC[off_from] != 0)
6295 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006296 int i;
6297
6298 for (i = 0; i < Screen_mco; ++i)
6299 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 }
6301 }
6302 if (char_cells == 2)
6303 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304
6305#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006306 /* The bold trick makes a single column of pixels appear in the
6307 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 * character should be redrawn too. This happens for our own GUI
6309 * and for some xterms. */
6310 if (
6311# ifdef FEAT_GUI
6312 gui.in_use
6313# endif
6314# if defined(FEAT_GUI) && defined(UNIX)
6315 ||
6316# endif
6317# ifdef UNIX
6318 term_is_xterm
6319# endif
6320 )
6321 {
6322 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006323 if (hl > HL_ALL)
6324 hl = syn_attr2attr(hl);
6325 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 redraw_next = TRUE;
6327 }
6328#endif
6329 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006330
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006331 /* For simplicity set the attributes of second half of a
6332 * double-wide character equal to the first half. */
6333 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006335
6336 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 screen_char(off_to, row, col + coloff);
6340 }
6341 else if ( p_wiv
6342#ifdef FEAT_GUI
6343 && !gui.in_use
6344#endif
6345 && col + coloff > 0)
6346 {
6347 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6348 {
6349 /*
6350 * Don't output stop-highlight when moving the cursor, it will
6351 * stop the highlighting when it should continue.
6352 */
6353 screen_attr = 0;
6354 }
6355 else if (screen_attr != 0)
6356 screen_stop_highlight();
6357 }
6358
6359 off_to += CHAR_CELLS;
6360 off_from += CHAR_CELLS;
6361 col += CHAR_CELLS;
6362 }
6363
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 if (clear_next)
6365 {
6366 /* Clear the second half of a double-wide character of which the left
6367 * half was overwritten with a single-wide character. */
6368 ScreenLines[off_to] = ' ';
6369 if (enc_utf8)
6370 ScreenLinesUC[off_to] = 0;
6371 screen_char(off_to, row, col + coloff);
6372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373
6374 if (clear_width > 0
6375#ifdef FEAT_RIGHTLEFT
6376 && !rlflag
6377#endif
6378 )
6379 {
6380#ifdef FEAT_GUI
6381 int startCol = col;
6382#endif
6383
6384 /* blank out the rest of the line */
6385 while (col < clear_width && ScreenLines[off_to] == ' '
6386 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006387 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 {
6389 ++off_to;
6390 ++col;
6391 }
6392 if (col < clear_width)
6393 {
6394#ifdef FEAT_GUI
6395 /*
6396 * In the GUI, clearing the rest of the line may leave pixels
6397 * behind if the first character cleared was bold. Some bold
6398 * fonts spill over the left. In this case we redraw the previous
6399 * character too. If we didn't skip any blanks above, then we
6400 * only redraw if the character wasn't already redrawn anyway.
6401 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006402 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 {
6404 hl = ScreenAttrs[off_to];
6405 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006406 {
6407 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006408
Bram Moolenaar9c697322006-10-09 20:11:17 +00006409 if (enc_utf8)
6410 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6411 * that its width is 2. */
6412 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6413 else if (enc_dbcs != 0)
6414 {
6415 /* find previous character by counting from first
6416 * column and get its width. */
6417 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006418 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006419
6420 while (off < off_to)
6421 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006422 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006423 off += prev_cells;
6424 }
6425 }
6426
6427 if (enc_dbcs != 0 && prev_cells > 1)
6428 screen_char_2(off_to - prev_cells, row,
6429 col + coloff - prev_cells);
6430 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006431 screen_char(off_to - prev_cells, row,
6432 col + coloff - prev_cells);
6433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 }
6435#endif
6436 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6437 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 off_to += clear_width - col;
6439 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 }
6441 }
6442
6443 if (clear_width > 0)
6444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 /* For a window that's left of another, draw the separator char. */
6446 if (col + coloff < Columns)
6447 {
6448 int c;
6449
6450 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006451 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006452 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6453 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 || ScreenAttrs[off_to] != hl)
6455 {
6456 ScreenLines[off_to] = c;
6457 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 if (enc_utf8)
6459 {
6460 if (c >= 0x80)
6461 {
6462 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006463 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 }
6465 else
6466 ScreenLinesUC[off_to] = 0;
6467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 screen_char(off_to, row, col + coloff);
6469 }
6470 }
6471 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 LineWraps[row] = FALSE;
6473 }
6474}
6475
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006476#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006478 * Mirror text "str" for right-left displaying.
6479 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006481 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006482rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483{
6484 char_u *p1, *p2;
6485 int t;
6486
6487 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6488 {
6489 t = *p1;
6490 *p1 = *p2;
6491 *p2 = t;
6492 }
6493}
6494#endif
6495
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496/*
6497 * mark all status lines for redraw; used after first :cd
6498 */
6499 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006500status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501{
6502 win_T *wp;
6503
Bram Moolenaar29323592016-07-24 22:04:11 +02006504 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 if (wp->w_status_height)
6506 {
6507 wp->w_redr_status = TRUE;
6508 redraw_later(VALID);
6509 }
6510}
6511
6512/*
6513 * mark all status lines of the current buffer for redraw
6514 */
6515 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006516status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517{
6518 win_T *wp;
6519
Bram Moolenaar29323592016-07-24 22:04:11 +02006520 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6522 {
6523 wp->w_redr_status = TRUE;
6524 redraw_later(VALID);
6525 }
6526}
6527
6528/*
6529 * Redraw all status lines that need to be redrawn.
6530 */
6531 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006532redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533{
6534 win_T *wp;
6535
Bram Moolenaar29323592016-07-24 22:04:11 +02006536 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006538 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006539 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006540 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542
Bram Moolenaar4033c552017-09-16 20:54:51 +02006543#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544/*
6545 * Redraw all status lines at the bottom of frame "frp".
6546 */
6547 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006548win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549{
6550 if (frp->fr_layout == FR_LEAF)
6551 frp->fr_win->w_redr_status = TRUE;
6552 else if (frp->fr_layout == FR_ROW)
6553 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006554 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 win_redraw_last_status(frp);
6556 }
6557 else /* frp->fr_layout == FR_COL */
6558 {
6559 frp = frp->fr_child;
6560 while (frp->fr_next != NULL)
6561 frp = frp->fr_next;
6562 win_redraw_last_status(frp);
6563 }
6564}
6565#endif
6566
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567/*
6568 * Draw the verticap separator right of window "wp" starting with line "row".
6569 */
6570 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006571draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572{
6573 int hl;
6574 int c;
6575
6576 if (wp->w_vsep_width)
6577 {
6578 /* draw the vertical separator right of this window */
6579 c = fillchar_vsep(&hl);
6580 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6581 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6582 c, ' ', hl);
6583 }
6584}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585
6586#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006587static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588
6589/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006590 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 */
6592 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006593status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594{
6595 int len = 0;
6596
6597#ifdef FEAT_MENU
6598 int emenu = (xp->xp_context == EXPAND_MENUS
6599 || xp->xp_context == EXPAND_MENUNAMES);
6600
6601 /* Check for menu separators - replace with '|'. */
6602 if (emenu && menu_is_separator(s))
6603 return 1;
6604#endif
6605
6606 while (*s != NUL)
6607 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006608 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006609 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006610 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 }
6612
6613 return len;
6614}
6615
6616/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006617 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006618 * These are backslashes used for escaping. Do show backslashes in help tags.
6619 */
6620 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006621skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006622{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006623 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006624#ifdef FEAT_MENU
6625 || ((xp->xp_context == EXPAND_MENUS
6626 || xp->xp_context == EXPAND_MENUNAMES)
6627 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6628#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006629 )
6630 {
6631#ifndef BACKSLASH_IN_FILENAME
6632 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6633 return 2;
6634#endif
6635 return 1;
6636 }
6637 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006638}
6639
6640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 * Show wildchar matches in the status line.
6642 * Show at least the "match" item.
6643 * We start at item 'first_match' in the list and show all matches that fit.
6644 *
6645 * If inversion is possible we use it. Else '=' characters are used.
6646 */
6647 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006648win_redr_status_matches(
6649 expand_T *xp,
6650 int num_matches,
6651 char_u **matches, /* list of matches */
6652 int match,
6653 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654{
6655#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6656 int row;
6657 char_u *buf;
6658 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006659 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 int fillchar;
6661 int attr;
6662 int i;
6663 int highlight = TRUE;
6664 char_u *selstart = NULL;
6665 int selstart_col = 0;
6666 char_u *selend = NULL;
6667 static int first_match = 0;
6668 int add_left = FALSE;
6669 char_u *s;
6670#ifdef FEAT_MENU
6671 int emenu;
6672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674
6675 if (matches == NULL) /* interrupted completion? */
6676 return;
6677
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006678 if (has_mbyte)
6679 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6680 else
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006681 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 if (buf == NULL)
6683 return;
6684
6685 if (match == -1) /* don't show match but original text */
6686 {
6687 match = 0;
6688 highlight = FALSE;
6689 }
6690 /* count 1 for the ending ">" */
6691 clen = status_match_len(xp, L_MATCH(match)) + 3;
6692 if (match == 0)
6693 first_match = 0;
6694 else if (match < first_match)
6695 {
6696 /* jumping left, as far as we can go */
6697 first_match = match;
6698 add_left = TRUE;
6699 }
6700 else
6701 {
6702 /* check if match fits on the screen */
6703 for (i = first_match; i < match; ++i)
6704 clen += status_match_len(xp, L_MATCH(i)) + 2;
6705 if (first_match > 0)
6706 clen += 2;
6707 /* jumping right, put match at the left */
6708 if ((long)clen > Columns)
6709 {
6710 first_match = match;
6711 /* if showing the last match, we can add some on the left */
6712 clen = 2;
6713 for (i = match; i < num_matches; ++i)
6714 {
6715 clen += status_match_len(xp, L_MATCH(i)) + 2;
6716 if ((long)clen >= Columns)
6717 break;
6718 }
6719 if (i == num_matches)
6720 add_left = TRUE;
6721 }
6722 }
6723 if (add_left)
6724 while (first_match > 0)
6725 {
6726 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6727 if ((long)clen >= Columns)
6728 break;
6729 --first_match;
6730 }
6731
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006732 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733
6734 if (first_match == 0)
6735 {
6736 *buf = NUL;
6737 len = 0;
6738 }
6739 else
6740 {
6741 STRCPY(buf, "< ");
6742 len = 2;
6743 }
6744 clen = len;
6745
6746 i = first_match;
6747 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6748 {
6749 if (i == match)
6750 {
6751 selstart = buf + len;
6752 selstart_col = clen;
6753 }
6754
6755 s = L_MATCH(i);
6756 /* Check for menu separators - replace with '|' */
6757#ifdef FEAT_MENU
6758 emenu = (xp->xp_context == EXPAND_MENUS
6759 || xp->xp_context == EXPAND_MENUNAMES);
6760 if (emenu && menu_is_separator(s))
6761 {
6762 STRCPY(buf + len, transchar('|'));
6763 l = (int)STRLEN(buf + len);
6764 len += l;
6765 clen += l;
6766 }
6767 else
6768#endif
6769 for ( ; *s != NUL; ++s)
6770 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006771 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006773 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 {
6775 STRNCPY(buf + len, s, l);
6776 s += l - 1;
6777 len += l;
6778 }
6779 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 {
6781 STRCPY(buf + len, transchar_byte(*s));
6782 len += (int)STRLEN(buf + len);
6783 }
6784 }
6785 if (i == match)
6786 selend = buf + len;
6787
6788 *(buf + len++) = ' ';
6789 *(buf + len++) = ' ';
6790 clen += 2;
6791 if (++i == num_matches)
6792 break;
6793 }
6794
6795 if (i != num_matches)
6796 {
6797 *(buf + len++) = '>';
6798 ++clen;
6799 }
6800
6801 buf[len] = NUL;
6802
6803 row = cmdline_row - 1;
6804 if (row >= 0)
6805 {
6806 if (wild_menu_showing == 0)
6807 {
6808 if (msg_scrolled > 0)
6809 {
6810 /* Put the wildmenu just above the command line. If there is
6811 * no room, scroll the screen one line up. */
6812 if (cmdline_row == Rows - 1)
6813 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006814 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 ++msg_scrolled;
6816 }
6817 else
6818 {
6819 ++cmdline_row;
6820 ++row;
6821 }
6822 wild_menu_showing = WM_SCROLLED;
6823 }
6824 else
6825 {
6826 /* Create status line if needed by setting 'laststatus' to 2.
6827 * Set 'winminheight' to zero to avoid that the window is
6828 * resized. */
6829 if (lastwin->w_status_height == 0)
6830 {
6831 save_p_ls = p_ls;
6832 save_p_wmh = p_wmh;
6833 p_ls = 2;
6834 p_wmh = 0;
6835 last_status(FALSE);
6836 }
6837 wild_menu_showing = WM_SHOWN;
6838 }
6839 }
6840
6841 screen_puts(buf, row, 0, attr);
6842 if (selstart != NULL && highlight)
6843 {
6844 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006845 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 }
6847
6848 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6849 }
6850
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 vim_free(buf);
6853}
6854#endif
6855
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856/*
6857 * Redraw the status line of window wp.
6858 *
6859 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006860 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6861 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006863 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006864win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865{
6866 int row;
6867 char_u *p;
6868 int len;
6869 int fillchar;
6870 int attr;
6871 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006872 static int busy = FALSE;
6873
6874 /* It's possible to get here recursively when 'statusline' (indirectly)
6875 * invokes ":redrawstatus". Simply ignore the call then. */
6876 if (busy)
6877 return;
6878 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879
6880 wp->w_redr_status = FALSE;
6881 if (wp->w_status_height == 0)
6882 {
6883 /* no status line, can only be last window */
6884 redraw_cmdline = TRUE;
6885 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006886 else if (!redrawing()
6887#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006888 // don't update status line when popup menu is visible and may be
6889 // drawn over it, unless it will be redrawn later
6890 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006891#endif
6892 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 {
6894 /* Don't redraw right now, do it later. */
6895 wp->w_redr_status = TRUE;
6896 }
6897#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006898 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 {
6900 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006901 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 }
6903#endif
6904 else
6905 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006906 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006908 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 p = NameBuff;
6910 len = (int)STRLEN(p);
6911
Bram Moolenaard85f2712017-07-28 21:51:57 +02006912 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913#ifdef FEAT_QUICKFIX
6914 || wp->w_p_pvw
6915#endif
6916 || bufIsChanged(wp->w_buffer)
6917 || wp->w_buffer->b_p_ro)
6918 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006919 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006921 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 len += (int)STRLEN(p + len);
6923 }
6924#ifdef FEAT_QUICKFIX
6925 if (wp->w_p_pvw)
6926 {
6927 STRCPY(p + len, _("[Preview]"));
6928 len += (int)STRLEN(p + len);
6929 }
6930#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006931 if (bufIsChanged(wp->w_buffer)
6932#ifdef FEAT_TERMINAL
6933 && !bt_terminal(wp->w_buffer)
6934#endif
6935 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 {
6937 STRCPY(p + len, "[+]");
6938 len += 3;
6939 }
6940 if (wp->w_buffer->b_p_ro)
6941 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006942 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006943 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 }
6945
Bram Moolenaar02631462017-09-22 15:20:32 +02006946 this_ru_col = ru_col - (Columns - wp->w_width);
6947 if (this_ru_col < (wp->w_width + 1) / 2)
6948 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 if (this_ru_col <= 1)
6950 {
6951 p = (char_u *)"<"; /* No room for file name! */
6952 len = 1;
6953 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01006954 else if (has_mbyte)
6955 {
6956 int clen = 0, i;
6957
6958 /* Count total number of display cells. */
6959 clen = mb_string2cells(p, -1);
6960
6961 /* Find first character that will fit.
6962 * Going from start to end is much faster for DBCS. */
6963 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
6964 i += (*mb_ptr2len)(p + i))
6965 clen -= (*mb_ptr2cells)(p + i);
6966 len = clen;
6967 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01006969 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01006971 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 }
6973
Bram Moolenaara12a1612019-01-24 16:39:02 +01006974 }
6975 else if (len > this_ru_col - 1)
6976 {
6977 p += len - (this_ru_col - 1);
6978 *p = '<';
6979 len = this_ru_col - 1;
6980 }
6981
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006983 screen_puts(p, row, wp->w_wincol, attr);
6984 screen_fill(row, row + 1, len + wp->w_wincol,
6985 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006987 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6989 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006990 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991
6992#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02006993 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994#endif
6995 }
6996
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 /*
6998 * May need to draw the character below the vertical separator.
6999 */
7000 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7001 {
7002 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007003 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 else
7005 fillchar = fillchar_vsep(&attr);
7006 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7007 attr);
7008 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007009 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010}
7011
Bram Moolenaar238a5642006-02-21 22:12:05 +00007012#ifdef FEAT_STL_OPT
7013/*
7014 * Redraw the status line according to 'statusline' and take care of any
7015 * errors encountered.
7016 */
7017 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007018redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007019{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007020 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007021 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007022
7023 /* When called recursively return. This can happen when the statusline
7024 * contains an expression that triggers a redraw. */
7025 if (entered)
7026 return;
7027 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007028
Bram Moolenaara742e082016-04-05 21:10:38 +02007029 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007030 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007031 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007032 {
7033 /* When there is an error disable the statusline, otherwise the
7034 * display is messed up with errors and a redraw triggers the problem
7035 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007036 set_string_option_direct((char_u *)"statusline", -1,
7037 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007038 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007039 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007040 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007041 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007042}
7043#endif
7044
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045/*
7046 * Return TRUE if the status line of window "wp" is connected to the status
7047 * line of the window right of it. If not, then it's a vertical separator.
7048 * Only call if (wp->w_vsep_width != 0).
7049 */
7050 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007051stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052{
7053 frame_T *fr;
7054
7055 fr = wp->w_frame;
7056 while (fr->fr_parent != NULL)
7057 {
7058 if (fr->fr_parent->fr_layout == FR_COL)
7059 {
7060 if (fr->fr_next != NULL)
7061 break;
7062 }
7063 else
7064 {
7065 if (fr->fr_next != NULL)
7066 return TRUE;
7067 }
7068 fr = fr->fr_parent;
7069 }
7070 return FALSE;
7071}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074/*
7075 * Get the value to show for the language mappings, active 'keymap'.
7076 */
7077 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007078get_keymap_str(
7079 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007080 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007081 char_u *buf, /* buffer for the result */
7082 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083{
7084 char_u *p;
7085
7086 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7087 return FALSE;
7088
7089 {
7090#ifdef FEAT_EVAL
7091 buf_T *old_curbuf = curbuf;
7092 win_T *old_curwin = curwin;
7093 char_u *s;
7094
7095 curbuf = wp->w_buffer;
7096 curwin = wp;
7097 STRCPY(buf, "b:keymap_name"); /* must be writable */
7098 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007099 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 --emsg_skip;
7101 curbuf = old_curbuf;
7102 curwin = old_curwin;
7103 if (p == NULL || *p == NUL)
7104#endif
7105 {
7106#ifdef FEAT_KEYMAP
7107 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7108 p = wp->w_buffer->b_p_keymap;
7109 else
7110#endif
7111 p = (char_u *)"lang";
7112 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007113 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 buf[0] = NUL;
7115#ifdef FEAT_EVAL
7116 vim_free(s);
7117#endif
7118 }
7119 return buf[0] != NUL;
7120}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121
7122#if defined(FEAT_STL_OPT) || defined(PROTO)
7123/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007124 * Redraw the status line or ruler of window "wp".
7125 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 */
7127 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007128win_redr_custom(
7129 win_T *wp,
7130 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007132 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 int attr;
7134 int curattr;
7135 int row;
7136 int col = 0;
7137 int maxwidth;
7138 int width;
7139 int n;
7140 int len;
7141 int fillchar;
7142 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007143 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007145 struct stl_hlrec hltab[STL_MAX_ITEM];
7146 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007147 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007148 win_T *ewp;
7149 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150
Bram Moolenaar1d633412013-12-11 15:52:01 +01007151 /* There is a tiny chance that this gets called recursively: When
7152 * redrawing a status line triggers redrawing the ruler or tabline.
7153 * Avoid trouble by not allowing recursion. */
7154 if (entered)
7155 return;
7156 entered = TRUE;
7157
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007159 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007161 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007162 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007163 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007164 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007165 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007166 maxwidth = Columns;
7167# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007168 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007169# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007171 else
7172 {
7173 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007174 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007175 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007176
7177 if (draw_ruler)
7178 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007179 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007180 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007181 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007182 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007183 if (*++stl == '-')
7184 stl++;
7185 if (atoi((char *)stl))
7186 while (VIM_ISDIGIT(*stl))
7187 stl++;
7188 if (*stl++ != '(')
7189 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007190 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007191 col = ru_col - (Columns - wp->w_width);
7192 if (col < (wp->w_width + 1) / 2)
7193 col = (wp->w_width + 1) / 2;
7194 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007195 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007196 {
7197 row = Rows - 1;
7198 --maxwidth; /* writing in last column may cause scrolling */
7199 fillchar = ' ';
7200 attr = 0;
7201 }
7202
7203# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007204 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007205# endif
7206 }
7207 else
7208 {
7209 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007210 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007211 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007212 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007213# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007214 use_sandbox = was_set_insecurely((char_u *)"statusline",
7215 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007216# endif
7217 }
7218
Bram Moolenaar53f81742017-09-22 14:35:51 +02007219 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007220 }
7221
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007223 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224
Bram Moolenaar61452852011-02-01 18:01:11 +01007225 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7226 * the cursor away and back. */
7227 ewp = wp == NULL ? curwin : wp;
7228 p_crb_save = ewp->w_p_crb;
7229 ewp->w_p_crb = FALSE;
7230
Bram Moolenaar362f3562009-11-03 16:20:34 +00007231 /* Make a copy, because the statusline may include a function call that
7232 * might change the option value and free the memory. */
7233 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007234 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007235 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007236 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007237 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007238 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007240 /* Make all characters printable. */
7241 p = transstr(buf);
7242 if (p != NULL)
7243 {
7244 vim_strncpy(buf, p, sizeof(buf) - 1);
7245 vim_free(p);
7246 }
7247
7248 /* fill up with "fillchar" */
7249 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007250 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 ++width;
7254 }
7255 buf[len] = NUL;
7256
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007257 /*
7258 * Draw each snippet with the specified highlighting.
7259 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 curattr = attr;
7261 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007262 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007264 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 screen_puts_len(p, len, row, col, curattr);
7266 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007267 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007269 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007271 else if (hltab[n].userhl < 0)
7272 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007273#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007274 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7275 && wp->w_status_height != 0)
7276 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007277 else if (wp != NULL && bt_terminal(wp->w_buffer)
7278 && wp->w_status_height != 0)
7279 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007280#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007281 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007282 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007284 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 }
7286 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007287
7288 if (wp == NULL)
7289 {
7290 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7291 col = 0;
7292 len = 0;
7293 p = buf;
7294 fillchar = 0;
7295 for (n = 0; tabtab[n].start != NULL; n++)
7296 {
7297 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7298 while (col < len)
7299 TabPageIdxs[col++] = fillchar;
7300 p = tabtab[n].start;
7301 fillchar = tabtab[n].userhl;
7302 }
7303 while (col < Columns)
7304 TabPageIdxs[col++] = fillchar;
7305 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007306
7307theend:
7308 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309}
7310
7311#endif /* FEAT_STL_OPT */
7312
7313/*
7314 * Output a single character directly to the screen and update ScreenLines.
7315 */
7316 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007317screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 char_u buf[MB_MAXBYTES + 1];
7320
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007321 if (has_mbyte)
7322 buf[(*mb_char2bytes)(c, buf)] = NUL;
7323 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007324 {
7325 buf[0] = c;
7326 buf[1] = NUL;
7327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 screen_puts(buf, row, col, attr);
7329}
7330
7331/*
7332 * Get a single character directly from ScreenLines into "bytes[]".
7333 * Also return its attribute in *attrp;
7334 */
7335 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007336screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337{
7338 unsigned off;
7339
7340 /* safety check */
7341 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7342 {
7343 off = LineOffset[row] + col;
7344 *attrp = ScreenAttrs[off];
7345 bytes[0] = ScreenLines[off];
7346 bytes[1] = NUL;
7347
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 if (enc_utf8 && ScreenLinesUC[off] != 0)
7349 bytes[utfc_char2bytes(off, bytes)] = NUL;
7350 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7351 {
7352 bytes[0] = ScreenLines[off];
7353 bytes[1] = ScreenLines2[off];
7354 bytes[2] = NUL;
7355 }
7356 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7357 {
7358 bytes[1] = ScreenLines[off + 1];
7359 bytes[2] = NUL;
7360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 }
7362}
7363
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007364/*
7365 * Return TRUE if composing characters for screen posn "off" differs from
7366 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007367 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007368 */
7369 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007370screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007371{
7372 int i;
7373
7374 for (i = 0; i < Screen_mco; ++i)
7375 {
7376 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7377 return TRUE;
7378 if (u8cc[i] == 0)
7379 break;
7380 }
7381 return FALSE;
7382}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007383
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384/*
7385 * Put string '*text' on the screen at position 'row' and 'col', with
7386 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7387 * Note: only outputs within one row, message is truncated at screen boundary!
7388 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7389 */
7390 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007391screen_puts(
7392 char_u *text,
7393 int row,
7394 int col,
7395 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396{
7397 screen_puts_len(text, -1, row, col, attr);
7398}
7399
7400/*
7401 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7402 * a NUL.
7403 */
7404 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007405screen_puts_len(
7406 char_u *text,
7407 int textlen,
7408 int row,
7409 int col,
7410 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411{
7412 unsigned off;
7413 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007414 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007416 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 int mbyte_blen = 1;
7418 int mbyte_cells = 1;
7419 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007420 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007422#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007424 int pc, nc, nc1;
7425 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007427 int force_redraw_this;
7428 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007429 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
7431 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7432 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007433 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434
Bram Moolenaarc236c162008-07-13 17:41:49 +00007435 /* When drawing over the right halve of a double-wide char clear out the
7436 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007437 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007438#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007439 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007440#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007441 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007442 {
7443 ScreenLines[off - 1] = ' ';
7444 ScreenAttrs[off - 1] = 0;
7445 if (enc_utf8)
7446 {
7447 ScreenLinesUC[off - 1] = 0;
7448 ScreenLinesC[0][off - 1] = 0;
7449 }
7450 /* redraw the previous cell, make it empty */
7451 screen_char(off - 1, row, col - 1);
7452 /* force the cell at "col" to be redrawn */
7453 force_redraw_next = TRUE;
7454 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007455
Bram Moolenaar367329b2007-08-30 11:53:22 +00007456 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007457 while (col < screen_Columns
7458 && (len < 0 || (int)(ptr - text) < len)
7459 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 {
7461 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 /* check if this is the first byte of a multibyte */
7463 if (has_mbyte)
7464 {
7465 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007466 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007468 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7470 mbyte_cells = 1;
7471 else if (enc_dbcs != 0)
7472 mbyte_cells = mbyte_blen;
7473 else /* enc_utf8 */
7474 {
7475 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007476 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 (int)((text + len) - ptr));
7478 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007479 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007481#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 /* Non-BMP character: display as ? or fullwidth ?. */
7483 if (u8c >= 0x10000)
7484 {
7485 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7486 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007487 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007489#endif
7490#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7492 {
7493 /* Do Arabic shaping. */
7494 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7495 {
7496 /* Past end of string to be displayed. */
7497 nc = NUL;
7498 nc1 = NUL;
7499 }
7500 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007501 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007502 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7503 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007504 nc1 = pcc[0];
7505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 pc = prev_c;
7507 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007508 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 }
7510 else
7511 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007512#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007513 if (col + mbyte_cells > screen_Columns)
7514 {
7515 /* Only 1 cell left, but character requires 2 cells:
7516 * display a '>' in the last column to avoid wrapping. */
7517 c = '>';
7518 mbyte_cells = 1;
7519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 }
7521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007523 force_redraw_this = force_redraw_next;
7524 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007525
7526 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 || (mbyte_cells == 2
7528 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7529 || (enc_dbcs == DBCS_JPNU
7530 && c == 0x8e
7531 && ScreenLines2[off] != ptr[1])
7532 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007533 && (ScreenLinesUC[off] !=
7534 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7535 || (ScreenLinesUC[off] != 0
7536 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007538 || exmode_active;
7539
Bram Moolenaara12a1612019-01-24 16:39:02 +01007540 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 {
7542#if defined(FEAT_GUI) || defined(UNIX)
7543 /* The bold trick makes a single row of pixels appear in the next
7544 * character. When a bold character is removed, the next
7545 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007546 * and for some xterms. */
7547 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548# ifdef FEAT_GUI
7549 gui.in_use
7550# endif
7551# if defined(FEAT_GUI) && defined(UNIX)
7552 ||
7553# endif
7554# ifdef UNIX
7555 term_is_xterm
7556# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007557 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007559 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007561 if (n > HL_ALL)
7562 n = syn_attr2attr(n);
7563 if (n & HL_BOLD)
7564 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 }
7566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 /* When at the end of the text and overwriting a two-cell
7568 * character with a one-cell character, need to clear the next
7569 * cell. Also when overwriting the left halve of a two-cell char
7570 * with the right halve of a two-cell char. Do this only once
7571 * (mb_off2cells() may return 2 on the right halve). */
7572 if (clear_next_cell)
7573 clear_next_cell = FALSE;
7574 else if (has_mbyte
7575 && (len < 0 ? ptr[mbyte_blen] == NUL
7576 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007577 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007579 && (*mb_off2cells)(off, max_off) == 1
7580 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 clear_next_cell = TRUE;
7582
7583 /* Make sure we never leave a second byte of a double-byte behind,
7584 * it confuses mb_off2cells(). */
7585 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007586 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007588 && (*mb_off2cells)(off, max_off) == 1
7589 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 ScreenLines[off] = c;
7592 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 if (enc_utf8)
7594 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007595 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 ScreenLinesUC[off] = 0;
7597 else
7598 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007599 int i;
7600
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007602 for (i = 0; i < Screen_mco; ++i)
7603 {
7604 ScreenLinesC[i][off] = u8cc[i];
7605 if (u8cc[i] == 0)
7606 break;
7607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 }
7609 if (mbyte_cells == 2)
7610 {
7611 ScreenLines[off + 1] = 0;
7612 ScreenAttrs[off + 1] = attr;
7613 }
7614 screen_char(off, row, col);
7615 }
7616 else if (mbyte_cells == 2)
7617 {
7618 ScreenLines[off + 1] = ptr[1];
7619 ScreenAttrs[off + 1] = attr;
7620 screen_char_2(off, row, col);
7621 }
7622 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7623 {
7624 ScreenLines2[off] = ptr[1];
7625 screen_char(off, row, col);
7626 }
7627 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 screen_char(off, row, col);
7629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 if (has_mbyte)
7631 {
7632 off += mbyte_cells;
7633 col += mbyte_cells;
7634 ptr += mbyte_blen;
7635 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007636 {
7637 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007639 len = -1;
7640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 }
7642 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 {
7644 ++off;
7645 ++col;
7646 ++ptr;
7647 }
7648 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007649
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007650 /* If we detected the next character needs to be redrawn, but the text
7651 * doesn't extend up to there, update the character here. */
7652 if (force_redraw_next && col < screen_Columns)
7653 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007654 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7655 screen_char_2(off, row, col);
7656 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007657 screen_char(off, row, col);
7658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659}
7660
7661#ifdef FEAT_SEARCH_EXTRA
7662/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007663 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 */
7665 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007666start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667{
7668 if (p_hls && !no_hlsearch)
7669 {
7670 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007671 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007672# ifdef FEAT_RELTIME
7673 /* Set the time limit to 'redrawtime'. */
7674 profile_setlimit(p_rdt, &search_hl.tm);
7675# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 }
7677}
7678
7679/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007680 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 */
7682 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007683end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684{
7685 if (search_hl.rm.regprog != NULL)
7686 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007687 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 search_hl.rm.regprog = NULL;
7689 }
7690}
7691
7692/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007693 * Init for calling prepare_search_hl().
7694 */
7695 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007696init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007697{
7698 matchitem_T *cur;
7699
7700 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7701 * match */
7702 cur = wp->w_match_head;
7703 while (cur != NULL)
7704 {
7705 cur->hl.rm = cur->match;
7706 if (cur->hlg_id == 0)
7707 cur->hl.attr = 0;
7708 else
7709 cur->hl.attr = syn_id2attr(cur->hlg_id);
7710 cur->hl.buf = wp->w_buffer;
7711 cur->hl.lnum = 0;
7712 cur->hl.first_lnum = 0;
7713# ifdef FEAT_RELTIME
7714 /* Set the time limit to 'redrawtime'. */
7715 profile_setlimit(p_rdt, &(cur->hl.tm));
7716# endif
7717 cur = cur->next;
7718 }
7719 search_hl.buf = wp->w_buffer;
7720 search_hl.lnum = 0;
7721 search_hl.first_lnum = 0;
7722 /* time limit is set at the toplevel, for all windows */
7723}
7724
7725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 * Advance to the match in window "wp" line "lnum" or past it.
7727 */
7728 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007729prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007731 matchitem_T *cur; /* points to the match list */
7732 match_T *shl; /* points to search_hl or a match */
7733 int shl_flag; /* flag to indicate whether search_hl
7734 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007735 int pos_inprogress; /* marks that position match search is
7736 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 int n;
7738
7739 /*
7740 * When using a multi-line pattern, start searching at the top
7741 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007742 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007744 cur = wp->w_match_head;
7745 shl_flag = FALSE;
7746 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007748 if (shl_flag == FALSE)
7749 {
7750 shl = &search_hl;
7751 shl_flag = TRUE;
7752 }
7753 else
7754 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 if (shl->rm.regprog != NULL
7756 && shl->lnum == 0
7757 && re_multiline(shl->rm.regprog))
7758 {
7759 if (shl->first_lnum == 0)
7760 {
7761# ifdef FEAT_FOLDING
7762 for (shl->first_lnum = lnum;
7763 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7764 if (hasFoldingWin(wp, shl->first_lnum - 1,
7765 NULL, NULL, TRUE, NULL))
7766 break;
7767# else
7768 shl->first_lnum = wp->w_topline;
7769# endif
7770 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007771 if (cur != NULL)
7772 cur->pos.cur = 0;
7773 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007775 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7776 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007778 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7779 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007780 pos_inprogress = cur == NULL || cur->pos.cur == 0
7781 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 if (shl->lnum != 0)
7783 {
7784 shl->first_lnum = shl->lnum
7785 + shl->rm.endpos[0].lnum
7786 - shl->rm.startpos[0].lnum;
7787 n = shl->rm.endpos[0].col;
7788 }
7789 else
7790 {
7791 ++shl->first_lnum;
7792 n = 0;
7793 }
7794 }
7795 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007796 if (shl != &search_hl && cur != NULL)
7797 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 }
7799}
7800
7801/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007802 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 * Uses shl->buf.
7804 * Sets shl->lnum and shl->rm contents.
7805 * Note: Assumes a previous match is always before "lnum", unless
7806 * shl->lnum is zero.
7807 * Careful: Any pointers for buffer lines will become invalid.
7808 */
7809 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007810next_search_hl(
7811 win_T *win,
7812 match_T *shl, /* points to search_hl or a match */
7813 linenr_T lnum,
7814 colnr_T mincol, /* minimal column for a match */
7815 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816{
7817 linenr_T l;
7818 colnr_T matchcol;
7819 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007820 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007822 // for :{range}s/pat only highlight inside the range
7823 if (lnum < search_first_line || lnum > search_last_line)
7824 {
7825 shl->lnum = 0;
7826 return;
7827 }
7828
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 if (shl->lnum != 0)
7830 {
7831 /* Check for three situations:
7832 * 1. If the "lnum" is below a previous match, start a new search.
7833 * 2. If the previous match includes "mincol", use it.
7834 * 3. Continue after the previous match.
7835 */
7836 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7837 if (lnum > l)
7838 shl->lnum = 0;
7839 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7840 return;
7841 }
7842
7843 /*
7844 * Repeat searching for a match until one is found that includes "mincol"
7845 * or none is found in this line.
7846 */
7847 called_emsg = FALSE;
7848 for (;;)
7849 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007850#ifdef FEAT_RELTIME
7851 /* Stop searching after passing the time limit. */
7852 if (profile_passed_limit(&(shl->tm)))
7853 {
7854 shl->lnum = 0; /* no match found in time */
7855 break;
7856 }
7857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 /* Three situations:
7859 * 1. No useful previous match: search from start of line.
7860 * 2. Not Vi compatible or empty match: continue at next character.
7861 * Break the loop if this is beyond the end of the line.
7862 * 3. Vi compatible searching: continue at end of previous match.
7863 */
7864 if (shl->lnum == 0)
7865 matchcol = 0;
7866 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7867 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007868 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007870 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007871
7872 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007873 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007874 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007876 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 shl->lnum = 0;
7878 break;
7879 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007880 if (has_mbyte)
7881 matchcol += mb_ptr2len(ml);
7882 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007883 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 }
7885 else
7886 matchcol = shl->rm.endpos[0].col;
7887
7888 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007889 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007891 /* Remember whether shl->rm is using a copy of the regprog in
7892 * cur->match. */
7893 int regprog_is_copy = (shl != &search_hl && cur != NULL
7894 && shl == &cur->hl
7895 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007896 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007897
Bram Moolenaarb3414592014-06-17 17:48:32 +02007898 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7899 matchcol,
7900#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007901 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007902#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007903 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007904#endif
7905 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007906 /* Copy the regprog, in case it got freed and recompiled. */
7907 if (regprog_is_copy)
7908 cur->match.regprog = cur->hl.rm.regprog;
7909
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007910 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007911 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007912 /* Error while handling regexp: stop using this regexp. */
7913 if (shl == &search_hl)
7914 {
7915 /* don't free regprog in the match list, it's a copy */
7916 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02007917 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007918 }
7919 shl->rm.regprog = NULL;
7920 shl->lnum = 0;
7921 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7922 message */
7923 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007924 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007925 }
7926 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007927 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007928 else
7929 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 if (nmatched == 0)
7931 {
7932 shl->lnum = 0; /* no match found */
7933 break;
7934 }
7935 if (shl->rm.startpos[0].lnum > 0
7936 || shl->rm.startpos[0].col >= mincol
7937 || nmatched > 1
7938 || shl->rm.endpos[0].col > mincol)
7939 {
7940 shl->lnum += shl->rm.startpos[0].lnum;
7941 break; /* useful match found */
7942 }
7943 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007944
7945 // Restore called_emsg for assert_fails().
7946 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948
Bram Moolenaar85077472016-10-16 14:35:48 +02007949/*
7950 * If there is a match fill "shl" and return one.
7951 * Return zero otherwise.
7952 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007953 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007954next_search_hl_pos(
7955 match_T *shl, /* points to a match */
7956 linenr_T lnum,
7957 posmatch_T *posmatch, /* match positions */
7958 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007959{
7960 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007961 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007962
Bram Moolenaarb3414592014-06-17 17:48:32 +02007963 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7964 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007965 llpos_T *pos = &posmatch->pos[i];
7966
7967 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007968 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007969 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007970 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007971 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007972 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007973 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007974 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007975 /* if this match comes before the one at "found" then swap
7976 * them */
7977 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007978 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007979 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007980
Bram Moolenaar85077472016-10-16 14:35:48 +02007981 *pos = posmatch->pos[found];
7982 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007983 }
7984 }
7985 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007986 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007987 }
7988 }
7989 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007990 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007991 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007992 colnr_T start = posmatch->pos[found].col == 0
7993 ? 0 : posmatch->pos[found].col - 1;
7994 colnr_T end = posmatch->pos[found].col == 0
7995 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007996
Bram Moolenaar85077472016-10-16 14:35:48 +02007997 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007998 shl->rm.startpos[0].lnum = 0;
7999 shl->rm.startpos[0].col = start;
8000 shl->rm.endpos[0].lnum = 0;
8001 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008002 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008003 posmatch->cur = found + 1;
8004 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008005 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008006 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008007}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008008#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008009
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008011screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012{
8013 attrentry_T *aep = NULL;
8014
8015 screen_attr = attr;
8016 if (full_screen
8017#ifdef WIN3264
8018 && termcap_active
8019#endif
8020 )
8021 {
8022#ifdef FEAT_GUI
8023 if (gui.in_use)
8024 {
8025 char buf[20];
8026
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008027 /* The GUI handles this internally. */
8028 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 OUT_STR(buf);
8030 }
8031 else
8032#endif
8033 {
8034 if (attr > HL_ALL) /* special HL attr. */
8035 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008036 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 aep = syn_cterm_attr2entry(attr);
8038 else
8039 aep = syn_term_attr2entry(attr);
8040 if (aep == NULL) /* did ":syntax clear" */
8041 attr = 0;
8042 else
8043 attr = aep->ae_attr;
8044 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008045 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008047 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008048#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008049 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8050 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8051 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008052#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008053 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008054 /* If the Normal FG color has BOLD attribute and the new HL
8055 * has a FG color defined, clear BOLD. */
8056 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008057 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008059 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008060 out_str(T_UCS);
8061 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008062 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8063 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008065 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008067 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008069 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008070 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071
8072 /*
8073 * Output the color or start string after bold etc., in case the
8074 * bold etc. override the color setting.
8075 */
8076 if (aep != NULL)
8077 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008078#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008079 /* When 'termguicolors' is set but fg or bg is unset,
8080 * fall back to the cterm colors. This helps for SpellBad,
8081 * where the GUI uses a red undercurl. */
8082 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008084 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008085 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008086 }
8087 else
8088#endif
8089 if (t_colors > 1)
8090 {
8091 if (aep->ae_u.cterm.fg_color)
8092 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8093 }
8094#ifdef FEAT_TERMGUICOLORS
8095 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8096 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008097 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008098 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 }
8100 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008101#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008102 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008104 if (aep->ae_u.cterm.bg_color)
8105 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8106 }
8107
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008108 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008109 {
8110 if (aep->ae_u.term.start != NULL)
8111 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 }
8113 }
8114 }
8115 }
8116}
8117
8118 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008119screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120{
8121 int do_ME = FALSE; /* output T_ME code */
8122
8123 if (screen_attr != 0
8124#ifdef WIN3264
8125 && termcap_active
8126#endif
8127 )
8128 {
8129#ifdef FEAT_GUI
8130 if (gui.in_use)
8131 {
8132 char buf[20];
8133
8134 /* use internal GUI code */
8135 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8136 OUT_STR(buf);
8137 }
8138 else
8139#endif
8140 {
8141 if (screen_attr > HL_ALL) /* special HL attr. */
8142 {
8143 attrentry_T *aep;
8144
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008145 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {
8147 /*
8148 * Assume that t_me restores the original colors!
8149 */
8150 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008151 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008152#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008153 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8154 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8155 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008156#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008157 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008158#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008159 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8160 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8161 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008162#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008163 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 do_ME = TRUE;
8165 }
8166 else
8167 {
8168 aep = syn_term_attr2entry(screen_attr);
8169 if (aep != NULL && aep->ae_u.term.stop != NULL)
8170 {
8171 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8172 do_ME = TRUE;
8173 else
8174 out_str(aep->ae_u.term.stop);
8175 }
8176 }
8177 if (aep == NULL) /* did ":syntax clear" */
8178 screen_attr = 0;
8179 else
8180 screen_attr = aep->ae_attr;
8181 }
8182
8183 /*
8184 * Often all ending-codes are equal to T_ME. Avoid outputting the
8185 * same sequence several times.
8186 */
8187 if (screen_attr & HL_STANDOUT)
8188 {
8189 if (STRCMP(T_SE, T_ME) == 0)
8190 do_ME = TRUE;
8191 else
8192 out_str(T_SE);
8193 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008194 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008195 {
8196 if (STRCMP(T_UCE, T_ME) == 0)
8197 do_ME = TRUE;
8198 else
8199 out_str(T_UCE);
8200 }
8201 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008202 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {
8204 if (STRCMP(T_UE, T_ME) == 0)
8205 do_ME = TRUE;
8206 else
8207 out_str(T_UE);
8208 }
8209 if (screen_attr & HL_ITALIC)
8210 {
8211 if (STRCMP(T_CZR, T_ME) == 0)
8212 do_ME = TRUE;
8213 else
8214 out_str(T_CZR);
8215 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008216 if (screen_attr & HL_STRIKETHROUGH)
8217 {
8218 if (STRCMP(T_STE, T_ME) == 0)
8219 do_ME = TRUE;
8220 else
8221 out_str(T_STE);
8222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8224 out_str(T_ME);
8225
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008226#ifdef FEAT_TERMGUICOLORS
8227 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008229 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008230 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008231 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008232 term_bg_rgb_color(cterm_normal_bg_gui_color);
8233 }
8234 else
8235#endif
8236 {
8237 if (t_colors > 1)
8238 {
8239 /* set Normal cterm colors */
8240 if (cterm_normal_fg_color != 0)
8241 term_fg_color(cterm_normal_fg_color - 1);
8242 if (cterm_normal_bg_color != 0)
8243 term_bg_color(cterm_normal_bg_color - 1);
8244 if (cterm_normal_fg_bold)
8245 out_str(T_MD);
8246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 }
8248 }
8249 }
8250 screen_attr = 0;
8251}
8252
8253/*
8254 * Reset the colors for a cterm. Used when leaving Vim.
8255 * The machine specific code may override this again.
8256 */
8257 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008258reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008260 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {
8262 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008263#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008264 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8265 || cterm_normal_bg_gui_color != INVALCOLOR)
8266 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008267#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {
8271 out_str(T_OP);
8272 screen_attr = -1;
8273 }
8274 if (cterm_normal_fg_bold)
8275 {
8276 out_str(T_ME);
8277 screen_attr = -1;
8278 }
8279 }
8280}
8281
8282/*
8283 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8284 * using the attributes from ScreenAttrs["off"].
8285 */
8286 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008287screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288{
8289 int attr;
8290
8291 /* Check for illegal values, just in case (could happen just after
8292 * resizing). */
8293 if (row >= screen_Rows || col >= screen_Columns)
8294 return;
8295
Bram Moolenaarae654382019-01-17 21:09:05 +01008296#ifdef FEAT_INS_EXPAND
8297 if (pum_under_menu(row, col))
8298 return;
8299#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008300 /* Outputting a character in the last cell on the screen may scroll the
8301 * screen up. Only do it when the "xn" termcap property is set, otherwise
8302 * mark the character invalid (update it when scrolled up). */
8303 if (*T_XN == NUL
8304 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305#ifdef FEAT_RIGHTLEFT
8306 /* account for first command-line character in rightleft mode */
8307 && !cmdmsg_rl
8308#endif
8309 )
8310 {
8311 ScreenAttrs[off] = (sattr_T)-1;
8312 return;
8313 }
8314
8315 /*
8316 * Stop highlighting first, so it's easier to move the cursor.
8317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 if (screen_char_attr != 0)
8319 attr = screen_char_attr;
8320 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 attr = ScreenAttrs[off];
8322 if (screen_attr != attr)
8323 screen_stop_highlight();
8324
8325 windgoto(row, col);
8326
8327 if (screen_attr != attr)
8328 screen_start_highlight(attr);
8329
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 if (enc_utf8 && ScreenLinesUC[off] != 0)
8331 {
8332 char_u buf[MB_MAXBYTES + 1];
8333
Bram Moolenaarcb070082016-04-02 22:14:51 +02008334 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008335 {
8336 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008337#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008338 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008339#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008340 )
8341 {
8342 /* Clear the two screen cells. If the character is actually
8343 * single width it won't change the second cell. */
8344 out_str((char_u *)" ");
8345 term_windgoto(row, col);
8346 }
8347 /* not sure where the cursor is after drawing the ambiguous width
8348 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008349 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008350 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008351 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008353
8354 /* Convert the UTF-8 character to bytes and write it. */
8355 buf[utfc_char2bytes(off, buf)] = NUL;
8356 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 }
8358 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 /* double-byte character in single-width cell */
8363 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8364 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 }
8366
8367 screen_cur_col++;
8368}
8369
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370/*
8371 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8372 * on the screen at position 'row' and 'col'.
8373 * The attributes of the first byte is used for all. This is required to
8374 * output the two bytes of a double-byte character with nothing in between.
8375 */
8376 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008377screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378{
8379 /* Check for illegal values (could be wrong when screen was resized). */
8380 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8381 return;
8382
8383 /* Outputting the last character on the screen may scrollup the screen.
8384 * Don't to it! Mark the character invalid (update it when scrolled up) */
8385 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8386 {
8387 ScreenAttrs[off] = (sattr_T)-1;
8388 return;
8389 }
8390
8391 /* Output the first byte normally (positions the cursor), then write the
8392 * second byte directly. */
8393 screen_char(off, row, col);
8394 out_char(ScreenLines[off + 1]);
8395 ++screen_cur_col;
8396}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398/*
8399 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8400 * This uses the contents of ScreenLines[] and doesn't change it.
8401 */
8402 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008403screen_draw_rectangle(
8404 int row,
8405 int col,
8406 int height,
8407 int width,
8408 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409{
8410 int r, c;
8411 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008412 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008414 /* Can't use ScreenLines unless initialized */
8415 if (ScreenLines == NULL)
8416 return;
8417
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 if (invert)
8419 screen_char_attr = HL_INVERSE;
8420 for (r = row; r < row + height; ++r)
8421 {
8422 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008423 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 for (c = col; c < col + width; ++c)
8425 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008426 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 {
8428 screen_char_2(off + c, r, c);
8429 ++c;
8430 }
8431 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 {
8433 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008434 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 }
8437 }
8438 }
8439 screen_char_attr = 0;
8440}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442/*
8443 * Redraw the characters for a vertically split window.
8444 */
8445 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008446redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447{
8448 int col;
8449 int width;
8450
8451# ifdef FEAT_CLIPBOARD
8452 clip_may_clear_selection(row, end - 1);
8453# endif
8454
8455 if (wp == NULL)
8456 {
8457 col = 0;
8458 width = Columns;
8459 }
8460 else
8461 {
8462 col = wp->w_wincol;
8463 width = wp->w_width;
8464 }
8465 screen_draw_rectangle(row, col, end - row, width, FALSE);
8466}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008468 static void
8469space_to_screenline(int off, int attr)
8470{
8471 ScreenLines[off] = ' ';
8472 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008473 if (enc_utf8)
8474 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008475}
8476
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477/*
8478 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8479 * with character 'c1' in first column followed by 'c2' in the other columns.
8480 * Use attributes 'attr'.
8481 */
8482 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008483screen_fill(
8484 int start_row,
8485 int end_row,
8486 int start_col,
8487 int end_col,
8488 int c1,
8489 int c2,
8490 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491{
8492 int row;
8493 int col;
8494 int off;
8495 int end_off;
8496 int did_delete;
8497 int c;
8498 int norm_term;
8499#if defined(FEAT_GUI) || defined(UNIX)
8500 int force_next = FALSE;
8501#endif
8502
8503 if (end_row > screen_Rows) /* safety check */
8504 end_row = screen_Rows;
8505 if (end_col > screen_Columns) /* safety check */
8506 end_col = screen_Columns;
8507 if (ScreenLines == NULL
8508 || start_row >= end_row
8509 || start_col >= end_col) /* nothing to do */
8510 return;
8511
8512 /* it's a "normal" terminal when not in a GUI or cterm */
8513 norm_term = (
8514#ifdef FEAT_GUI
8515 !gui.in_use &&
8516#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008517 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 for (row = start_row; row < end_row; ++row)
8519 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008520 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008521#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008522 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008523#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008524 )
8525 {
8526 /* When drawing over the right halve of a double-wide char clear
8527 * out the left halve. When drawing over the left halve of a
8528 * double wide-char clear out the right halve. Only needed in a
8529 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008530 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008531 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008532 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008533 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 /*
8536 * Try to use delete-line termcap code, when no attributes or in a
8537 * "normal" terminal, where a bold/italic space is just a
8538 * space.
8539 */
8540 did_delete = FALSE;
8541 if (c2 == ' '
8542 && end_col == Columns
8543 && can_clear(T_CE)
8544 && (attr == 0
8545 || (norm_term
8546 && attr <= HL_ALL
8547 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8548 {
8549 /*
8550 * check if we really need to clear something
8551 */
8552 col = start_col;
8553 if (c1 != ' ') /* don't clear first char */
8554 ++col;
8555
8556 off = LineOffset[row] + col;
8557 end_off = LineOffset[row] + end_col;
8558
8559 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 if (enc_utf8)
8561 while (off < end_off && ScreenLines[off] == ' '
8562 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8563 ++off;
8564 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 while (off < end_off && ScreenLines[off] == ' '
8566 && ScreenAttrs[off] == 0)
8567 ++off;
8568 if (off < end_off) /* something to be cleared */
8569 {
8570 col = off - LineOffset[row];
8571 screen_stop_highlight();
8572 term_windgoto(row, col);/* clear rest of this screen line */
8573 out_str(T_CE);
8574 screen_start(); /* don't know where cursor is now */
8575 col = end_col - col;
8576 while (col--) /* clear chars in ScreenLines */
8577 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008578 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 ++off;
8580 }
8581 }
8582 did_delete = TRUE; /* the chars are cleared now */
8583 }
8584
8585 off = LineOffset[row] + start_col;
8586 c = c1;
8587 for (col = start_col; col < end_col; ++col)
8588 {
8589 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008590 || (enc_utf8 && (int)ScreenLinesUC[off]
8591 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 || ScreenAttrs[off] != attr
8593#if defined(FEAT_GUI) || defined(UNIX)
8594 || force_next
8595#endif
8596 )
8597 {
8598#if defined(FEAT_GUI) || defined(UNIX)
8599 /* The bold trick may make a single row of pixels appear in
8600 * the next character. When a bold character is removed, the
8601 * next character should be redrawn too. This happens for our
8602 * own GUI and for some xterms. */
8603 if (
8604# ifdef FEAT_GUI
8605 gui.in_use
8606# endif
8607# if defined(FEAT_GUI) && defined(UNIX)
8608 ||
8609# endif
8610# ifdef UNIX
8611 term_is_xterm
8612# endif
8613 )
8614 {
8615 if (ScreenLines[off] != ' '
8616 && (ScreenAttrs[off] > HL_ALL
8617 || ScreenAttrs[off] & HL_BOLD))
8618 force_next = TRUE;
8619 else
8620 force_next = FALSE;
8621 }
8622#endif
8623 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 if (enc_utf8)
8625 {
8626 if (c >= 0x80)
8627 {
8628 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008629 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 }
8631 else
8632 ScreenLinesUC[off] = 0;
8633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 ScreenAttrs[off] = attr;
8635 if (!did_delete || c != ' ')
8636 screen_char(off, row, col);
8637 }
8638 ++off;
8639 if (col == start_col)
8640 {
8641 if (did_delete)
8642 break;
8643 c = c2;
8644 }
8645 }
8646 if (end_col == Columns)
8647 LineWraps[row] = FALSE;
8648 if (row == Rows - 1) /* overwritten the command line */
8649 {
8650 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008651 if (start_col == 0 && end_col == Columns
8652 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008654 if (start_col == 0)
8655 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 }
8657 }
8658}
8659
8660/*
8661 * Check if there should be a delay. Used before clearing or redrawing the
8662 * screen or the command line.
8663 */
8664 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008665check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666{
8667 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8668 && !did_wait_return
8669 && emsg_silent == 0)
8670 {
8671 out_flush();
8672 ui_delay(1000L, TRUE);
8673 emsg_on_display = FALSE;
8674 if (check_msg_scroll)
8675 msg_scroll = FALSE;
8676 }
8677}
8678
8679/*
8680 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008681 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 * Returns TRUE if there is a valid screen to write to.
8683 * Returns FALSE when starting up and screen not initialized yet.
8684 */
8685 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008686screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008688 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 return (ScreenLines != NULL);
8690}
8691
8692/*
8693 * Resize the shell to Rows and Columns.
8694 * Allocate ScreenLines[] and associated items.
8695 *
8696 * There may be some time between setting Rows and Columns and (re)allocating
8697 * ScreenLines[]. This happens when starting up and when (manually) changing
8698 * the shell size. Always use screen_Rows and screen_Columns to access items
8699 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8700 * final size of the shell is needed.
8701 */
8702 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008703screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704{
8705 int new_row, old_row;
8706#ifdef FEAT_GUI
8707 int old_Rows;
8708#endif
8709 win_T *wp;
8710 int outofmem = FALSE;
8711 int len;
8712 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008714 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008716 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 sattr_T *new_ScreenAttrs;
8718 unsigned *new_LineOffset;
8719 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008720 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008721 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008723 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008724 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008726retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 /*
8728 * Allocation of the screen buffers is done only when the size changes and
8729 * when Rows and Columns have been set and we have started doing full
8730 * screen stuff.
8731 */
8732 if ((ScreenLines != NULL
8733 && Rows == screen_Rows
8734 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 && enc_utf8 == (ScreenLinesUC != NULL)
8736 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008737 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 || Rows == 0
8739 || Columns == 0
8740 || (!full_screen && ScreenLines == NULL))
8741 return;
8742
8743 /*
8744 * It's possible that we produce an out-of-memory message below, which
8745 * will cause this function to be called again. To break the loop, just
8746 * return here.
8747 */
8748 if (entered)
8749 return;
8750 entered = TRUE;
8751
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008752 /*
8753 * Note that the window sizes are updated before reallocating the arrays,
8754 * thus we must not redraw here!
8755 */
8756 ++RedrawingDisabled;
8757
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 win_new_shellsize(); /* fit the windows in the new sized shell */
8759
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 comp_col(); /* recompute columns for shown command and ruler */
8761
8762 /*
8763 * We're changing the size of the screen.
8764 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8765 * - Move lines from the old arrays into the new arrays, clear extra
8766 * lines (unless the screen is going to be cleared).
8767 * - Free the old arrays.
8768 *
8769 * If anything fails, make ScreenLines NULL, so we don't do anything!
8770 * Continuing with the old ScreenLines may result in a crash, because the
8771 * size is wrong.
8772 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008773 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008775 if (aucmd_win != NULL)
8776 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777
8778 new_ScreenLines = (schar_T *)lalloc((long_u)(
8779 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008780 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 if (enc_utf8)
8782 {
8783 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8784 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008785 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008786 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8788 }
8789 if (enc_dbcs == DBCS_JPNU)
8790 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8791 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8793 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8794 new_LineOffset = (unsigned *)lalloc((long_u)(
8795 Rows * sizeof(unsigned)), FALSE);
8796 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008797 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008799 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 {
8801 if (win_alloc_lines(wp) == FAIL)
8802 {
8803 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008804 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 }
8806 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008807 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8808 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008809 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008810give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008812 for (i = 0; i < p_mco; ++i)
8813 if (new_ScreenLinesC[i] == NULL)
8814 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008816 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 || new_ScreenAttrs == NULL
8819 || new_LineOffset == NULL
8820 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008821 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 || outofmem)
8823 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008824 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008825 {
8826 /* guess the size */
8827 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8828
8829 /* Remember we did this to avoid getting outofmem messages over
8830 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008831 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008832 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008833 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008834 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008835 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008836 VIM_CLEAR(new_ScreenLinesC[i]);
8837 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008838 VIM_CLEAR(new_ScreenAttrs);
8839 VIM_CLEAR(new_LineOffset);
8840 VIM_CLEAR(new_LineWraps);
8841 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 }
8843 else
8844 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008845 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008846
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 for (new_row = 0; new_row < Rows; ++new_row)
8848 {
8849 new_LineOffset[new_row] = new_row * Columns;
8850 new_LineWraps[new_row] = FALSE;
8851
8852 /*
8853 * If the screen is not going to be cleared, copy as much as
8854 * possible from the old screen to the new one and clear the rest
8855 * (used when resizing the window at the "--more--" prompt or when
8856 * executing an external command, for the GUI).
8857 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008858 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 {
8860 (void)vim_memset(new_ScreenLines + new_row * Columns,
8861 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 if (enc_utf8)
8863 {
8864 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8865 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008866 for (i = 0; i < p_mco; ++i)
8867 (void)vim_memset(new_ScreenLinesC[i]
8868 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 0, (size_t)Columns * sizeof(u8char_T));
8870 }
8871 if (enc_dbcs == DBCS_JPNU)
8872 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8873 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8875 0, (size_t)Columns * sizeof(sattr_T));
8876 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008877 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 {
8879 if (screen_Columns < Columns)
8880 len = screen_Columns;
8881 else
8882 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008883 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008884 * may be invalid now. Also when p_mco changes. */
8885 if (!(enc_utf8 && ScreenLinesUC == NULL)
8886 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008887 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8888 ScreenLines + LineOffset[old_row],
8889 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008890 if (enc_utf8 && ScreenLinesUC != NULL
8891 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 {
8893 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8894 ScreenLinesUC + LineOffset[old_row],
8895 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008896 for (i = 0; i < p_mco; ++i)
8897 mch_memmove(new_ScreenLinesC[i]
8898 + new_LineOffset[new_row],
8899 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 (size_t)len * sizeof(u8char_T));
8901 }
8902 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8903 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8904 ScreenLines2 + LineOffset[old_row],
8905 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8907 ScreenAttrs + LineOffset[old_row],
8908 (size_t)len * sizeof(sattr_T));
8909 }
8910 }
8911 }
8912 /* Use the last line of the screen for the current line. */
8913 current_ScreenLine = new_ScreenLines + Rows * Columns;
8914 }
8915
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008916 free_screenlines();
8917
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008920 for (i = 0; i < p_mco; ++i)
8921 ScreenLinesC[i] = new_ScreenLinesC[i];
8922 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 ScreenAttrs = new_ScreenAttrs;
8925 LineOffset = new_LineOffset;
8926 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008927 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928
8929 /* It's important that screen_Rows and screen_Columns reflect the actual
8930 * size of ScreenLines[]. Set them before calling anything. */
8931#ifdef FEAT_GUI
8932 old_Rows = screen_Rows;
8933#endif
8934 screen_Rows = Rows;
8935 screen_Columns = Columns;
8936
8937 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008938 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 screenclear2();
8940
8941#ifdef FEAT_GUI
8942 else if (gui.in_use
8943 && !gui.starting
8944 && ScreenLines != NULL
8945 && old_Rows != Rows)
8946 {
8947 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8948 /*
8949 * Adjust the position of the cursor, for when executing an external
8950 * command.
8951 */
8952 if (msg_row >= Rows) /* Rows got smaller */
8953 msg_row = Rows - 1; /* put cursor at last row */
8954 else if (Rows > old_Rows) /* Rows got bigger */
8955 msg_row += Rows - old_Rows; /* put cursor in same place */
8956 if (msg_col >= Columns) /* Columns got smaller */
8957 msg_col = Columns - 1; /* put cursor at last column */
8958 }
8959#endif
8960
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008962 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008963
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008964 /*
8965 * Do not apply autocommands more than 3 times to avoid an endless loop
8966 * in case applying autocommands always changes Rows or Columns.
8967 */
8968 if (starting == 0 && ++retry_count <= 3)
8969 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008970 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008971 /* In rare cases, autocommands may have altered Rows or Columns,
8972 * jump back to check if we need to allocate the screen again. */
8973 goto retry;
8974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975}
8976
8977 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008978free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008979{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008980 int i;
8981
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008982 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008983 for (i = 0; i < Screen_mco; ++i)
8984 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008985 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008986 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008987 vim_free(ScreenAttrs);
8988 vim_free(LineOffset);
8989 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008990 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008991}
8992
8993 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008994screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995{
8996 check_for_delay(FALSE);
8997 screenalloc(FALSE); /* allocate screen buffers if size changed */
8998 screenclear2(); /* clear the screen */
8999}
9000
9001 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009002screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003{
9004 int i;
9005
9006 if (starting == NO_SCREEN || ScreenLines == NULL
9007#ifdef FEAT_GUI
9008 || (gui.in_use && gui.starting)
9009#endif
9010 )
9011 return;
9012
9013#ifdef FEAT_GUI
9014 if (!gui.in_use)
9015#endif
9016 screen_attr = -1; /* force setting the Normal colors */
9017 screen_stop_highlight(); /* don't want highlighting here */
9018
9019#ifdef FEAT_CLIPBOARD
9020 /* disable selection without redrawing it */
9021 clip_scroll_selection(9999);
9022#endif
9023
9024 /* blank out ScreenLines */
9025 for (i = 0; i < Rows; ++i)
9026 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009027 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 LineWraps[i] = FALSE;
9029 }
9030
9031 if (can_clear(T_CL))
9032 {
9033 out_str(T_CL); /* clear the display */
9034 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009035 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 }
9037 else
9038 {
9039 /* can't clear the screen, mark all chars with invalid attributes */
9040 for (i = 0; i < Rows; ++i)
9041 lineinvalid(LineOffset[i], (int)Columns);
9042 clear_cmdline = TRUE;
9043 }
9044
9045 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9046
9047 win_rest_invalid(firstwin);
9048 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009049 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 if (must_redraw == CLEAR) /* no need to clear again */
9051 must_redraw = NOT_VALID;
9052 compute_cmdrow();
9053 msg_row = cmdline_row; /* put cursor on last line for messages */
9054 msg_col = 0;
9055 screen_start(); /* don't know where cursor is now */
9056 msg_scrolled = 0; /* can't scroll back */
9057 msg_didany = FALSE;
9058 msg_didout = FALSE;
9059}
9060
9061/*
9062 * Clear one line in ScreenLines.
9063 */
9064 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009065lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066{
9067 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 if (enc_utf8)
9069 (void)vim_memset(ScreenLinesUC + off, 0,
9070 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009071 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072}
9073
9074/*
9075 * Mark one line in ScreenLines invalid by setting the attributes to an
9076 * invalid value.
9077 */
9078 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009079lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
9081 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9082}
9083
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084/*
9085 * Copy part of a Screenline for vertically split window "wp".
9086 */
9087 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009088linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089{
9090 unsigned off_to = LineOffset[to] + wp->w_wincol;
9091 unsigned off_from = LineOffset[from] + wp->w_wincol;
9092
9093 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9094 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 if (enc_utf8)
9096 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009097 int i;
9098
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9100 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009101 for (i = 0; i < p_mco; ++i)
9102 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9103 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 }
9105 if (enc_dbcs == DBCS_JPNU)
9106 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9107 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9109 wp->w_width * sizeof(sattr_T));
9110}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111
9112/*
9113 * Return TRUE if clearing with term string "p" would work.
9114 * It can't work when the string is empty or it won't set the right background.
9115 */
9116 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009117can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118{
9119 return (*p != NUL && (t_colors <= 1
9120#ifdef FEAT_GUI
9121 || gui.in_use
9122#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009123#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009124 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009125 || (!p_tgc && cterm_normal_bg_color == 0)
9126#else
9127 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009128#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009129 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130}
9131
9132/*
9133 * Reset cursor position. Use whenever cursor was moved because of outputting
9134 * something directly to the screen (shell commands) or a terminal control
9135 * code.
9136 */
9137 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009138screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139{
9140 screen_cur_row = screen_cur_col = 9999;
9141}
9142
9143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 * Move the cursor to position "row","col" in the screen.
9145 * This tries to find the most efficient way to move, minimizing the number of
9146 * characters sent to the terminal.
9147 */
9148 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009149windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009151 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 int i;
9153 int plan;
9154 int cost;
9155 int wouldbe_col;
9156 int noinvcurs;
9157 char_u *bs;
9158 int goto_cost;
9159 int attr;
9160
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009161#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9163
9164#define PLAN_LE 1
9165#define PLAN_CR 2
9166#define PLAN_NL 3
9167#define PLAN_WRITE 4
9168 /* Can't use ScreenLines unless initialized */
9169 if (ScreenLines == NULL)
9170 return;
9171
9172 if (col != screen_cur_col || row != screen_cur_row)
9173 {
9174 /* Check for valid position. */
9175 if (row < 0) /* window without text lines? */
9176 row = 0;
9177 if (row >= screen_Rows)
9178 row = screen_Rows - 1;
9179 if (col >= screen_Columns)
9180 col = screen_Columns - 1;
9181
9182 /* check if no cursor movement is allowed in highlight mode */
9183 if (screen_attr && *T_MS == NUL)
9184 noinvcurs = HIGHL_COST;
9185 else
9186 noinvcurs = 0;
9187 goto_cost = GOTO_COST + noinvcurs;
9188
9189 /*
9190 * Plan how to do the positioning:
9191 * 1. Use CR to move it to column 0, same row.
9192 * 2. Use T_LE to move it a few columns to the left.
9193 * 3. Use NL to move a few lines down, column 0.
9194 * 4. Move a few columns to the right with T_ND or by writing chars.
9195 *
9196 * Don't do this if the cursor went beyond the last column, the cursor
9197 * position is unknown then (some terminals wrap, some don't )
9198 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009199 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 * characters to move the cursor to the right.
9201 */
9202 if (row >= screen_cur_row && screen_cur_col < Columns)
9203 {
9204 /*
9205 * If the cursor is in the same row, bigger col, we can use CR
9206 * or T_LE.
9207 */
9208 bs = NULL; /* init for GCC */
9209 attr = screen_attr;
9210 if (row == screen_cur_row && col < screen_cur_col)
9211 {
9212 /* "le" is preferred over "bc", because "bc" is obsolete */
9213 if (*T_LE)
9214 bs = T_LE; /* "cursor left" */
9215 else
9216 bs = T_BC; /* "backspace character (old) */
9217 if (*bs)
9218 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9219 else
9220 cost = 999;
9221 if (col + 1 < cost) /* using CR is less characters */
9222 {
9223 plan = PLAN_CR;
9224 wouldbe_col = 0;
9225 cost = 1; /* CR is just one character */
9226 }
9227 else
9228 {
9229 plan = PLAN_LE;
9230 wouldbe_col = col;
9231 }
9232 if (noinvcurs) /* will stop highlighting */
9233 {
9234 cost += noinvcurs;
9235 attr = 0;
9236 }
9237 }
9238
9239 /*
9240 * If the cursor is above where we want to be, we can use CR LF.
9241 */
9242 else if (row > screen_cur_row)
9243 {
9244 plan = PLAN_NL;
9245 wouldbe_col = 0;
9246 cost = (row - screen_cur_row) * 2; /* CR LF */
9247 if (noinvcurs) /* will stop highlighting */
9248 {
9249 cost += noinvcurs;
9250 attr = 0;
9251 }
9252 }
9253
9254 /*
9255 * If the cursor is in the same row, smaller col, just use write.
9256 */
9257 else
9258 {
9259 plan = PLAN_WRITE;
9260 wouldbe_col = screen_cur_col;
9261 cost = 0;
9262 }
9263
9264 /*
9265 * Check if any characters that need to be written have the
9266 * correct attributes. Also avoid UTF-8 characters.
9267 */
9268 i = col - wouldbe_col;
9269 if (i > 0)
9270 cost += i;
9271 if (cost < goto_cost && i > 0)
9272 {
9273 /*
9274 * Check if the attributes are correct without additionally
9275 * stopping highlighting.
9276 */
9277 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9278 while (i && *p++ == attr)
9279 --i;
9280 if (i != 0)
9281 {
9282 /*
9283 * Try if it works when highlighting is stopped here.
9284 */
9285 if (*--p == 0)
9286 {
9287 cost += noinvcurs;
9288 while (i && *p++ == 0)
9289 --i;
9290 }
9291 if (i != 0)
9292 cost = 999; /* different attributes, don't do it */
9293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 if (enc_utf8)
9295 {
9296 /* Don't use an UTF-8 char for positioning, it's slow. */
9297 for (i = wouldbe_col; i < col; ++i)
9298 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9299 {
9300 cost = 999;
9301 break;
9302 }
9303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 }
9305
9306 /*
9307 * We can do it without term_windgoto()!
9308 */
9309 if (cost < goto_cost)
9310 {
9311 if (plan == PLAN_LE)
9312 {
9313 if (noinvcurs)
9314 screen_stop_highlight();
9315 while (screen_cur_col > col)
9316 {
9317 out_str(bs);
9318 --screen_cur_col;
9319 }
9320 }
9321 else if (plan == PLAN_CR)
9322 {
9323 if (noinvcurs)
9324 screen_stop_highlight();
9325 out_char('\r');
9326 screen_cur_col = 0;
9327 }
9328 else if (plan == PLAN_NL)
9329 {
9330 if (noinvcurs)
9331 screen_stop_highlight();
9332 while (screen_cur_row < row)
9333 {
9334 out_char('\n');
9335 ++screen_cur_row;
9336 }
9337 screen_cur_col = 0;
9338 }
9339
9340 i = col - screen_cur_col;
9341 if (i > 0)
9342 {
9343 /*
9344 * Use cursor-right if it's one character only. Avoids
9345 * removing a line of pixels from the last bold char, when
9346 * using the bold trick in the GUI.
9347 */
9348 if (T_ND[0] != NUL && T_ND[1] == NUL)
9349 {
9350 while (i-- > 0)
9351 out_char(*T_ND);
9352 }
9353 else
9354 {
9355 int off;
9356
9357 off = LineOffset[row] + screen_cur_col;
9358 while (i-- > 0)
9359 {
9360 if (ScreenAttrs[off] != screen_attr)
9361 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 if (enc_dbcs == DBCS_JPNU
9365 && ScreenLines[off] == 0x8e)
9366 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 ++off;
9368 }
9369 }
9370 }
9371 }
9372 }
9373 else
9374 cost = 999;
9375
9376 if (cost >= goto_cost)
9377 {
9378 if (noinvcurs)
9379 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009380 if (row == screen_cur_row && (col > screen_cur_col)
9381 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 term_cursor_right(col - screen_cur_col);
9383 else
9384 term_windgoto(row, col);
9385 }
9386 screen_cur_row = row;
9387 screen_cur_col = col;
9388 }
9389}
9390
9391/*
9392 * Set cursor to its position in the current window.
9393 */
9394 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009395setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009397 setcursor_mayforce(FALSE);
9398}
9399
9400/*
9401 * Set cursor to its position in the current window.
9402 * When "force" is TRUE also when not redrawing.
9403 */
9404 void
9405setcursor_mayforce(int force)
9406{
9407 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 {
9409 validate_cursor();
9410 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009411 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009413 /* With 'rightleft' set and the cursor on a double-wide
9414 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009415 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9416 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009417 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009418 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419#endif
9420 curwin->w_wcol));
9421 }
9422}
9423
9424
9425/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009426 * Insert 'line_count' lines at 'row' in window 'wp'.
9427 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9428 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 * scrolling.
9430 * Returns FAIL if the lines are not inserted, OK for success.
9431 */
9432 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009433win_ins_lines(
9434 win_T *wp,
9435 int row,
9436 int line_count,
9437 int invalid,
9438 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439{
9440 int did_delete;
9441 int nextrow;
9442 int lastrow;
9443 int retval;
9444
9445 if (invalid)
9446 wp->w_lines_valid = 0;
9447
9448 if (wp->w_height < 5)
9449 return FAIL;
9450
9451 if (line_count > wp->w_height - row)
9452 line_count = wp->w_height - row;
9453
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009454 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 if (retval != MAYBE)
9456 return retval;
9457
9458 /*
9459 * If there is a next window or a status line, we first try to delete the
9460 * lines at the bottom to avoid messing what is after the window.
9461 * If this fails and there are following windows, don't do anything to avoid
9462 * messing up those windows, better just redraw.
9463 */
9464 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 if (wp->w_next != NULL || wp->w_status_height)
9466 {
9467 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009468 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 did_delete = TRUE;
9470 else if (wp->w_next)
9471 return FAIL;
9472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 /*
9474 * if no lines deleted, blank the lines that will end up below the window
9475 */
9476 if (!did_delete)
9477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009480 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 lastrow = nextrow + line_count;
9482 if (lastrow > Rows)
9483 lastrow = Rows;
9484 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009485 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 ' ', ' ', 0);
9487 }
9488
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009489 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 == FAIL)
9491 {
9492 /* deletion will have messed up other windows */
9493 if (did_delete)
9494 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 win_rest_invalid(W_NEXT(wp));
9497 }
9498 return FAIL;
9499 }
9500
9501 return OK;
9502}
9503
9504/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009505 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9507 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9508 * scrolling
9509 * Return OK for success, FAIL if the lines are not deleted.
9510 */
9511 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009512win_del_lines(
9513 win_T *wp,
9514 int row,
9515 int line_count,
9516 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009517 int mayclear,
9518 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519{
9520 int retval;
9521
9522 if (invalid)
9523 wp->w_lines_valid = 0;
9524
9525 if (line_count > wp->w_height - row)
9526 line_count = wp->w_height - row;
9527
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009528 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 if (retval != MAYBE)
9530 return retval;
9531
9532 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009533 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 return FAIL;
9535
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 /*
9537 * If there are windows or status lines below, try to put them at the
9538 * correct place. If we can't do that, they have to be redrawn.
9539 */
9540 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9541 {
9542 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009543 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 {
9545 wp->w_redr_status = TRUE;
9546 win_rest_invalid(wp->w_next);
9547 }
9548 }
9549 /*
9550 * If this is the last window and there is no status line, redraw the
9551 * command line later.
9552 */
9553 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 redraw_cmdline = TRUE;
9555 return OK;
9556}
9557
9558/*
9559 * Common code for win_ins_lines() and win_del_lines().
9560 * Returns OK or FAIL when the work has been done.
9561 * Returns MAYBE when not finished yet.
9562 */
9563 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009564win_do_lines(
9565 win_T *wp,
9566 int row,
9567 int line_count,
9568 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009569 int del,
9570 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571{
9572 int retval;
9573
9574 if (!redrawing() || line_count <= 0)
9575 return FAIL;
9576
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009577 /* When inserting lines would result in loss of command output, just redraw
9578 * the lines. */
9579 if (no_win_do_lines_ins && !del)
9580 return FAIL;
9581
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009583 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009585 if (!no_win_do_lines_ins)
9586 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 return FAIL;
9588 }
9589
9590 /*
9591 * Delete all remaining lines
9592 */
9593 if (row + line_count >= wp->w_height)
9594 {
9595 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009596 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 ' ', ' ', 0);
9598 return OK;
9599 }
9600
9601 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009602 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009604 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009606 if (!no_win_do_lines_ins)
9607 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608
9609 /*
9610 * If the terminal can set a scroll region, use that.
9611 * Always do this in a vertically split window. This will redraw from
9612 * ScreenLines[] when t_CV isn't defined. That's faster than using
9613 * win_line().
9614 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009615 * a character in the lower right corner of the scroll region may cause a
9616 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009618 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 scroll_region_set(wp, row);
9622 if (del)
9623 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009624 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 else
9626 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009627 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 scroll_region_reset();
9630 return retval;
9631 }
9632
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9634 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635
9636 return MAYBE;
9637}
9638
9639/*
9640 * window 'wp' and everything after it is messed up, mark it for redraw
9641 */
9642 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009643win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 {
9647 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 wp->w_redr_status = TRUE;
9649 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 }
9651 redraw_cmdline = TRUE;
9652}
9653
9654/*
9655 * The rest of the routines in this file perform screen manipulations. The
9656 * given operation is performed physically on the screen. The corresponding
9657 * change is also made to the internal screen image. In this way, the editor
9658 * anticipates the effect of editing changes on the appearance of the screen.
9659 * That way, when we call screenupdate a complete redraw isn't usually
9660 * necessary. Another advantage is that we can keep adding code to anticipate
9661 * screen changes, and in the meantime, everything still works.
9662 */
9663
9664/*
9665 * types for inserting or deleting lines
9666 */
9667#define USE_T_CAL 1
9668#define USE_T_CDL 2
9669#define USE_T_AL 3
9670#define USE_T_CE 4
9671#define USE_T_DL 5
9672#define USE_T_SR 6
9673#define USE_NL 7
9674#define USE_T_CD 8
9675#define USE_REDRAW 9
9676
9677/*
9678 * insert lines on the screen and update ScreenLines[]
9679 * 'end' is the line after the scrolled part. Normally it is Rows.
9680 * When scrolling region used 'off' is the offset from the top for the region.
9681 * 'row' and 'end' are relative to the start of the region.
9682 *
9683 * return FAIL for failure, OK for success.
9684 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009685 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009686screen_ins_lines(
9687 int off,
9688 int row,
9689 int line_count,
9690 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009691 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009692 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693{
9694 int i;
9695 int j;
9696 unsigned temp;
9697 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009698 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 int type;
9700 int result_empty;
9701 int can_ce = can_clear(T_CE);
9702
9703 /*
9704 * FAIL if
9705 * - there is no valid screen
9706 * - the screen has to be redrawn completely
9707 * - the line count is less than one
9708 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009709 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009711 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9712#ifdef FEAT_CLIPBOARD
9713 || (clip_star.state != SELECT_CLEARED
9714 && redrawing_for_callback > 0)
9715#endif
9716 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 return FAIL;
9718
9719 /*
9720 * There are seven ways to insert lines:
9721 * 0. When in a vertically split window and t_CV isn't set, redraw the
9722 * characters from ScreenLines[].
9723 * 1. Use T_CD (clear to end of display) if it exists and the result of
9724 * the insert is just empty lines
9725 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9726 * present or line_count > 1. It looks better if we do all the inserts
9727 * at once.
9728 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9729 * insert is just empty lines and T_CE is not present or line_count >
9730 * 1.
9731 * 4. Use T_AL (insert line) if it exists.
9732 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9733 * just empty lines.
9734 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9735 * just empty lines.
9736 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9737 * the 'da' flag is not set or we have clear line capability.
9738 * 8. redraw the characters from ScreenLines[].
9739 *
9740 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9741 * the scrollbar for the window. It does have insert line, use that if it
9742 * exists.
9743 */
9744 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9746 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009747 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 type = USE_T_CD;
9749 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9750 type = USE_T_CAL;
9751 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9752 type = USE_T_CDL;
9753 else if (*T_AL != NUL)
9754 type = USE_T_AL;
9755 else if (can_ce && result_empty)
9756 type = USE_T_CE;
9757 else if (*T_DL != NUL && result_empty)
9758 type = USE_T_DL;
9759 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9760 type = USE_T_SR;
9761 else
9762 return FAIL;
9763
9764 /*
9765 * For clearing the lines screen_del_lines() is used. This will also take
9766 * care of t_db if necessary.
9767 */
9768 if (type == USE_T_CD || type == USE_T_CDL ||
9769 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009770 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771
9772 /*
9773 * If text is retained below the screen, first clear or delete as many
9774 * lines at the bottom of the window as are about to be inserted so that
9775 * the deleted lines won't later surface during a screen_del_lines.
9776 */
9777 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009778 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779
9780#ifdef FEAT_CLIPBOARD
9781 /* Remove a modeless selection when inserting lines halfway the screen
9782 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009783 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009784 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 else
9786 clip_scroll_selection(-line_count);
9787#endif
9788
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789#ifdef FEAT_GUI
9790 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9791 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009792 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793#endif
9794
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009795 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9796 cursor_col = wp->w_wincol;
9797
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 if (*T_CCS != NUL) /* cursor relative to region */
9799 cursor_row = row;
9800 else
9801 cursor_row = row + off;
9802
9803 /*
9804 * Shift LineOffset[] line_count down to reflect the inserted lines.
9805 * Clear the inserted lines in ScreenLines[].
9806 */
9807 row += off;
9808 end += off;
9809 for (i = 0; i < line_count; ++i)
9810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 if (wp != NULL && wp->w_width != Columns)
9812 {
9813 /* need to copy part of a line */
9814 j = end - 1 - i;
9815 while ((j -= line_count) >= row)
9816 linecopy(j + line_count, j, wp);
9817 j += line_count;
9818 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009819 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9820 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 else
9822 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9823 LineWraps[j] = FALSE;
9824 }
9825 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 {
9827 j = end - 1 - i;
9828 temp = LineOffset[j];
9829 while ((j -= line_count) >= row)
9830 {
9831 LineOffset[j + line_count] = LineOffset[j];
9832 LineWraps[j + line_count] = LineWraps[j];
9833 }
9834 LineOffset[j + line_count] = temp;
9835 LineWraps[j + line_count] = FALSE;
9836 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009837 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 else
9839 lineinvalid(temp, (int)Columns);
9840 }
9841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842
9843 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009844 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009845 if (clear_attr != 0)
9846 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 /* redraw the characters */
9849 if (type == USE_REDRAW)
9850 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009851 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 {
9853 term_append_lines(line_count);
9854 screen_start(); /* don't know where cursor is now */
9855 }
9856 else
9857 {
9858 for (i = 0; i < line_count; i++)
9859 {
9860 if (type == USE_T_AL)
9861 {
9862 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009863 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 out_str(T_AL);
9865 }
9866 else /* type == USE_T_SR */
9867 out_str(T_SR);
9868 screen_start(); /* don't know where cursor is now */
9869 }
9870 }
9871
9872 /*
9873 * With scroll-reverse and 'da' flag set we need to clear the lines that
9874 * have been scrolled down into the region.
9875 */
9876 if (type == USE_T_SR && *T_DA)
9877 {
9878 for (i = 0; i < line_count; ++i)
9879 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009880 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 out_str(T_CE);
9882 screen_start(); /* don't know where cursor is now */
9883 }
9884 }
9885
9886#ifdef FEAT_GUI
9887 gui_can_update_cursor();
9888 if (gui.in_use)
9889 out_flush(); /* always flush after a scroll */
9890#endif
9891 return OK;
9892}
9893
9894/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009895 * Delete lines on the screen and update ScreenLines[].
9896 * "end" is the line after the scrolled part. Normally it is Rows.
9897 * When scrolling region used "off" is the offset from the top for the region.
9898 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 *
9900 * Return OK for success, FAIL if the lines are not deleted.
9901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009903screen_del_lines(
9904 int off,
9905 int row,
9906 int line_count,
9907 int end,
9908 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009909 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009910 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911{
9912 int j;
9913 int i;
9914 unsigned temp;
9915 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009916 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 int cursor_end;
9918 int result_empty; /* result is empty until end of region */
9919 int can_delete; /* deleting line codes can be used */
9920 int type;
9921
9922 /*
9923 * FAIL if
9924 * - there is no valid screen
9925 * - the screen has to be redrawn completely
9926 * - the line count is less than one
9927 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009928 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009930 if (!screen_valid(TRUE) || line_count <= 0
9931 || (!force && line_count > p_ttyscroll)
9932#ifdef FEAT_CLIPBOARD
9933 || (clip_star.state != SELECT_CLEARED
9934 && redrawing_for_callback > 0)
9935#endif
9936 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 return FAIL;
9938
9939 /*
9940 * Check if the rest of the current region will become empty.
9941 */
9942 result_empty = row + line_count >= end;
9943
9944 /*
9945 * We can delete lines only when 'db' flag not set or when 'ce' option
9946 * available.
9947 */
9948 can_delete = (*T_DB == NUL || can_clear(T_CE));
9949
9950 /*
9951 * There are six ways to delete lines:
9952 * 0. When in a vertically split window and t_CV isn't set, redraw the
9953 * characters from ScreenLines[].
9954 * 1. Use T_CD if it exists and the result is empty.
9955 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9956 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9957 * none of the other ways work.
9958 * 4. Use T_CE (erase line) if the result is empty.
9959 * 5. Use T_DL (delete line) if it exists.
9960 * 6. redraw the characters from ScreenLines[].
9961 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9963 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009964 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 type = USE_T_CD;
9966#if defined(__BEOS__) && defined(BEOS_DR8)
9967 /*
9968 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9969 * its internal termcap... this works okay for tests which test *T_DB !=
9970 * NUL. It has the disadvantage that the user cannot use any :set t_*
9971 * command to get T_DB (back) to empty_option, only :set term=... will do
9972 * the trick...
9973 * Anyway, this hack will hopefully go away with the next OS release.
9974 * (Olaf Seibert)
9975 */
9976 else if (row == 0 && T_DB == empty_option
9977 && (line_count == 1 || *T_CDL == NUL))
9978#else
9979 else if (row == 0 && (
9980#ifndef AMIGA
9981 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9982 * up, so use delete-line command */
9983 line_count == 1 ||
9984#endif
9985 *T_CDL == NUL))
9986#endif
9987 type = USE_NL;
9988 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9989 type = USE_T_CDL;
9990 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02009991 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 type = USE_T_CE;
9993 else if (*T_DL != NUL && can_delete)
9994 type = USE_T_DL;
9995 else if (*T_CDL != NUL && can_delete)
9996 type = USE_T_CDL;
9997 else
9998 return FAIL;
9999
10000#ifdef FEAT_CLIPBOARD
10001 /* Remove a modeless selection when deleting lines halfway the screen or
10002 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010003 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010004 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 else
10006 clip_scroll_selection(line_count);
10007#endif
10008
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009#ifdef FEAT_GUI
10010 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10011 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010012 gui_dont_update_cursor(gui.cursor_row >= row + off
10013 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014#endif
10015
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010016 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10017 cursor_col = wp->w_wincol;
10018
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 if (*T_CCS != NUL) /* cursor relative to region */
10020 {
10021 cursor_row = row;
10022 cursor_end = end;
10023 }
10024 else
10025 {
10026 cursor_row = row + off;
10027 cursor_end = end + off;
10028 }
10029
10030 /*
10031 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10032 * Clear the inserted lines in ScreenLines[].
10033 */
10034 row += off;
10035 end += off;
10036 for (i = 0; i < line_count; ++i)
10037 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 if (wp != NULL && wp->w_width != Columns)
10039 {
10040 /* need to copy part of a line */
10041 j = row + i;
10042 while ((j += line_count) <= end - 1)
10043 linecopy(j - line_count, j, wp);
10044 j -= line_count;
10045 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010046 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10047 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 else
10049 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10050 LineWraps[j] = FALSE;
10051 }
10052 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 {
10054 /* whole width, moving the line pointers is faster */
10055 j = row + i;
10056 temp = LineOffset[j];
10057 while ((j += line_count) <= end - 1)
10058 {
10059 LineOffset[j - line_count] = LineOffset[j];
10060 LineWraps[j - line_count] = LineWraps[j];
10061 }
10062 LineOffset[j - line_count] = temp;
10063 LineWraps[j - line_count] = FALSE;
10064 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010065 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 else
10067 lineinvalid(temp, (int)Columns);
10068 }
10069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010071 if (screen_attr != clear_attr)
10072 screen_stop_highlight();
10073 if (clear_attr != 0)
10074 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 /* redraw the characters */
10077 if (type == USE_REDRAW)
10078 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010079 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010081 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 out_str(T_CD);
10083 screen_start(); /* don't know where cursor is now */
10084 }
10085 else if (type == USE_T_CDL)
10086 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010087 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 term_delete_lines(line_count);
10089 screen_start(); /* don't know where cursor is now */
10090 }
10091 /*
10092 * Deleting lines at top of the screen or scroll region: Just scroll
10093 * the whole screen (scroll region) up by outputting newlines on the
10094 * last line.
10095 */
10096 else if (type == USE_NL)
10097 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010098 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 for (i = line_count; --i >= 0; )
10100 out_char('\n'); /* cursor will remain on same line */
10101 }
10102 else
10103 {
10104 for (i = line_count; --i >= 0; )
10105 {
10106 if (type == USE_T_DL)
10107 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010108 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 out_str(T_DL); /* delete a line */
10110 }
10111 else /* type == USE_T_CE */
10112 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010113 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 out_str(T_CE); /* erase a line */
10115 }
10116 screen_start(); /* don't know where cursor is now */
10117 }
10118 }
10119
10120 /*
10121 * If the 'db' flag is set, we need to clear the lines that have been
10122 * scrolled up at the bottom of the region.
10123 */
10124 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10125 {
10126 for (i = line_count; i > 0; --i)
10127 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010128 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 out_str(T_CE); /* erase a line */
10130 screen_start(); /* don't know where cursor is now */
10131 }
10132 }
10133
10134#ifdef FEAT_GUI
10135 gui_can_update_cursor();
10136 if (gui.in_use)
10137 out_flush(); /* always flush after a scroll */
10138#endif
10139
10140 return OK;
10141}
10142
10143/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010144 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 *
10146 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10147 * If clear_cmdline is FALSE there may be a message there that needs to be
10148 * cleared only if a mode is shown.
10149 * Return the length of the message (0 if no message).
10150 */
10151 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010152showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153{
10154 int need_clear;
10155 int length = 0;
10156 int do_mode;
10157 int attr;
10158 int nwr_save;
10159#ifdef FEAT_INS_EXPAND
10160 int sub_attr;
10161#endif
10162
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010163 do_mode = ((p_smd && msg_silent == 0)
10164 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010165 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010166 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010167 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 {
10169 /*
10170 * Don't show mode right now, when not redrawing or inside a mapping.
10171 * Call char_avail() only when we are going to show something, because
10172 * it takes a bit of time.
10173 */
10174 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10175 {
10176 redraw_cmdline = TRUE; /* show mode later */
10177 return 0;
10178 }
10179
10180 nwr_save = need_wait_return;
10181
10182 /* wait a bit before overwriting an important message */
10183 check_for_delay(FALSE);
10184
10185 /* if the cmdline is more than one line high, erase top lines */
10186 need_clear = clear_cmdline;
10187 if (clear_cmdline && cmdline_row < Rows - 1)
10188 msg_clr_cmdline(); /* will reset clear_cmdline */
10189
10190 /* Position on the last line in the window, column 0 */
10191 msg_pos_mode();
10192 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010193 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 if (do_mode)
10195 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010196 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010198 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010199# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010200 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010201# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010202 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010203# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010204 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010205# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010206 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010208 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209# endif
10210#endif
10211#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10212 if (gui.in_use)
10213 {
10214 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010215 {
10216 /* HANGUL */
10217 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010218 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010219 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010220 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 }
10223#endif
10224#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010225 /* CTRL-X in Insert mode */
10226 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 {
10228 /* These messages can get long, avoid a wrap in a narrow
10229 * window. Prefer showing edit_submode_extra. */
10230 length = (Rows - msg_row) * Columns - 3;
10231 if (edit_submode_extra != NULL)
10232 length -= vim_strsize(edit_submode_extra);
10233 if (length > 0)
10234 {
10235 if (edit_submode_pre != NULL)
10236 length -= vim_strsize(edit_submode_pre);
10237 if (length - vim_strsize(edit_submode) > 0)
10238 {
10239 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010240 msg_puts_attr((char *)edit_submode_pre, attr);
10241 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 }
10243 if (edit_submode_extra != NULL)
10244 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010245 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010247 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 else
10249 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010250 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 }
10252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 }
10254 else
10255#endif
10256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010258 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010259 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010260 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 else if (State & INSERT)
10262 {
10263#ifdef FEAT_RIGHTLEFT
10264 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010265 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010267 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010269 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010270 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010272 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010274 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275#ifdef FEAT_RIGHTLEFT
10276 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010277 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278# ifdef FEAT_FKMAP
10279 if (p_fkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010280 msg_puts_attr(farsi_text_5, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281# endif
10282#endif
10283#ifdef FEAT_KEYMAP
10284 if (State & LANGMAP)
10285 {
10286# ifdef FEAT_ARABIC
10287 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010288 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 else
10290# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010291 if (get_keymap_str(curwin, (char_u *)" (%s)",
10292 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010293 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 }
10295#endif
10296 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010297 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 if (VIsual_active)
10300 {
10301 char *p;
10302
10303 /* Don't concatenate separate words to avoid translation
10304 * problems. */
10305 switch ((VIsual_select ? 4 : 0)
10306 + (VIsual_mode == Ctrl_V) * 2
10307 + (VIsual_mode == 'V'))
10308 {
10309 case 0: p = N_(" VISUAL"); break;
10310 case 1: p = N_(" VISUAL LINE"); break;
10311 case 2: p = N_(" VISUAL BLOCK"); break;
10312 case 4: p = N_(" SELECT"); break;
10313 case 5: p = N_(" SELECT LINE"); break;
10314 default: p = N_(" SELECT BLOCK"); break;
10315 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010316 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010318 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010320
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 need_clear = TRUE;
10322 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010323 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324#ifdef FEAT_INS_EXPAND
10325 && edit_submode == NULL /* otherwise it gets too long */
10326#endif
10327 )
10328 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010329 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 need_clear = TRUE;
10331 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010332
10333 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 if (need_clear || clear_cmdline)
10335 msg_clr_eos();
10336 msg_didout = FALSE; /* overwrite this message */
10337 length = msg_col;
10338 msg_col = 0;
10339 need_wait_return = nwr_save; /* never ask for hit-return for this */
10340 }
10341 else if (clear_cmdline && msg_silent == 0)
10342 /* Clear the whole command line. Will reset "clear_cmdline". */
10343 msg_clr_cmdline();
10344
10345#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 /* In Visual mode the size of the selected area must be redrawn. */
10347 if (VIsual_active)
10348 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349
10350 /* If the last window has no status line, the ruler is after the mode
10351 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010352 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010353 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354#endif
10355 redraw_cmdline = FALSE;
10356 clear_cmdline = FALSE;
10357
10358 return length;
10359}
10360
10361/*
10362 * Position for a mode message.
10363 */
10364 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010365msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366{
10367 msg_col = 0;
10368 msg_row = Rows - 1;
10369}
10370
10371/*
10372 * Delete mode message. Used when ESC is typed which is expected to end
10373 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010374 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 */
10376 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010377unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378{
10379 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010380 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 */
10382 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10383 redraw_cmdline = TRUE; /* delete mode later */
10384 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010385 clearmode();
10386}
10387
10388/*
10389 * Clear the mode message.
10390 */
10391 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010392clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010393{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010394 int save_msg_row = msg_row;
10395 int save_msg_col = msg_col;
10396
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010397 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010398 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010399 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010400 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010401
10402 msg_col = save_msg_col;
10403 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404}
10405
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010406 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010407recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010408{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010409 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010410 if (!shortmess(SHM_RECORDING))
10411 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010412 char s[4];
10413
10414 sprintf(s, " @%c", reg_recording);
10415 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010416 }
10417}
10418
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010419/*
10420 * Draw the tab pages line at the top of the Vim window.
10421 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010422 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010423draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010424{
10425 int tabcount = 0;
10426 tabpage_T *tp;
10427 int tabwidth;
10428 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010429 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010430 int attr;
10431 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010432 win_T *cwp;
10433 int wincount;
10434 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010435 int c;
10436 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010437 int attr_sel = HL_ATTR(HLF_TPS);
10438 int attr_nosel = HL_ATTR(HLF_TP);
10439 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010440 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010441 int room;
10442 int use_sep_chars = (t_colors < 8
10443#ifdef FEAT_GUI
10444 && !gui.in_use
10445#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010446#ifdef FEAT_TERMGUICOLORS
10447 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010448#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010449 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010450
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010451 if (ScreenLines == NULL)
10452 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010453 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010454
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010455#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010456 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010457 if (gui_use_tabline())
10458 {
10459 gui_update_tabline();
10460 return;
10461 }
10462#endif
10463
10464 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010465 return;
10466
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010467#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010468
10469 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10470 for (scol = 0; scol < Columns; ++scol)
10471 TabPageIdxs[scol] = 0;
10472
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 Moolenaard1f56e62006-02-22 21:25:37 +000010499 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010500 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10501 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010502 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010503 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010504
Bram Moolenaar238a5642006-02-21 22:12:05 +000010505 if (tp->tp_topframe == topframe)
10506 attr = attr_sel;
10507 if (use_sep_chars && col > 0)
10508 screen_putchar('|', 0, col++, attr);
10509
10510 if (tp->tp_topframe != topframe)
10511 attr = attr_nosel;
10512
10513 screen_putchar(' ', 0, col++, attr);
10514
10515 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010516 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010517 cwp = curwin;
10518 wp = firstwin;
10519 }
10520 else
10521 {
10522 cwp = tp->tp_curwin;
10523 wp = tp->tp_firstwin;
10524 }
10525
10526 modified = FALSE;
10527 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10528 if (bufIsChanged(wp->w_buffer))
10529 modified = TRUE;
10530 if (modified || wincount > 1)
10531 {
10532 if (wincount > 1)
10533 {
10534 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010535 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010536 if (col + len >= Columns - 3)
10537 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010538 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010539#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010540 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010541#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010542 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010543#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010544 );
10545 col += len;
10546 }
10547 if (modified)
10548 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10549 screen_putchar(' ', 0, col++, attr);
10550 }
10551
10552 room = scol - col + tabwidth - 1;
10553 if (room > 0)
10554 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010555 /* Get buffer name in NameBuff[] */
10556 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010557 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010558 len = vim_strsize(NameBuff);
10559 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010560 if (has_mbyte)
10561 while (len > room)
10562 {
10563 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010564 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010565 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010566 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010567 {
10568 p += len - room;
10569 len = room;
10570 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010571 if (len > Columns - col - 1)
10572 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010573
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010574 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010575 col += len;
10576 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010577 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010578
10579 /* Store the tab page number in TabPageIdxs[], so that
10580 * jump_to_mouse() knows where each one is. */
10581 ++tabcount;
10582 while (scol < col)
10583 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010584 }
10585
Bram Moolenaar238a5642006-02-21 22:12:05 +000010586 if (use_sep_chars)
10587 c = '_';
10588 else
10589 c = ' ';
10590 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010591
10592 /* Put an "X" for closing the current tab if there are several. */
10593 if (first_tabpage->tp_next != NULL)
10594 {
10595 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10596 TabPageIdxs[Columns - 1] = -999;
10597 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010598 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010599
10600 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10601 * set. */
10602 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010603}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010604
10605/*
10606 * Get buffer name for "buf" into NameBuff[].
10607 * Takes care of special buffer names and translates special characters.
10608 */
10609 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010610get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010611{
10612 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010613 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010614 else
10615 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10616 trans_characters(NameBuff, MAXPATHL);
10617}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010618
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619/*
10620 * Get the character to use in a status line. Get its attributes in "*attr".
10621 */
10622 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010623fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624{
10625 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010626
10627#ifdef FEAT_TERMINAL
10628 if (bt_terminal(wp->w_buffer))
10629 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010630 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010631 {
10632 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010633 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010634 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010635 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010636 {
10637 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010638 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010639 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010640 }
10641 else
10642#endif
10643 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010645 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 fill = fill_stl;
10647 }
10648 else
10649 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010650 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651 fill = fill_stlnc;
10652 }
10653 /* Use fill when there is highlighting, and highlighting of current
10654 * window differs, or the fillchars differ, or this is not the
10655 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010656 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010657 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 || (fill_stl != fill_stlnc)))
10659 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010660 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 return '^';
10662 return '=';
10663}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665/*
10666 * Get the character to use in a separator between vertically split windows.
10667 * Get its attributes in "*attr".
10668 */
10669 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010670fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010672 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 if (*attr == 0 && fill_vert == ' ')
10674 return '|';
10675 else
10676 return fill_vert;
10677}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678
10679/*
10680 * Return TRUE if redrawing should currently be done.
10681 */
10682 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010683redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010685#ifdef FEAT_EVAL
10686 if (disable_redraw_for_testing)
10687 return 0;
10688 else
10689#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010690 return ((!RedrawingDisabled
10691#ifdef FEAT_EVAL
10692 || ignore_redraw_flag_for_testing
10693#endif
10694 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695}
10696
10697/*
10698 * Return TRUE if printing messages should currently be done.
10699 */
10700 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010701messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702{
10703 return (!(p_lz && char_avail() && !KeyTyped));
10704}
10705
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010706#ifdef FEAT_MENU
10707/*
10708 * Draw the window toolbar.
10709 */
10710 static void
10711redraw_win_toolbar(win_T *wp)
10712{
10713 vimmenu_T *menu;
10714 int item_idx = 0;
10715 int item_count = 0;
10716 int col = 0;
10717 int next_col;
10718 int off = (int)(current_ScreenLine - ScreenLines);
10719 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10720 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10721
10722 vim_free(wp->w_winbar_items);
10723 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10724 ++item_count;
10725 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10726 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10727
10728 /* TODO: use fewer spaces if there is not enough room */
10729 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010730 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010731 {
10732 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010733 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010734 break;
10735 if (col > 1)
10736 {
10737 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010738 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010739 break;
10740 }
10741
10742 wp->w_winbar_items[item_idx].wb_startcol = col;
10743 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010744 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010745 break;
10746
10747 next_col = text_to_screenline(wp, menu->name, col);
10748 while (col < next_col)
10749 {
10750 ScreenAttrs[off + col] = button_attr;
10751 ++col;
10752 }
10753 wp->w_winbar_items[item_idx].wb_endcol = col;
10754 wp->w_winbar_items[item_idx].wb_menu = menu;
10755 ++item_idx;
10756
Bram Moolenaar02631462017-09-22 15:20:32 +020010757 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010758 break;
10759 space_to_screenline(off + col, button_attr);
10760 ++col;
10761 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010762 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010763 {
10764 space_to_screenline(off + col, fill_attr);
10765 ++col;
10766 }
10767 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10768
Bram Moolenaar02631462017-09-22 15:20:32 +020010769 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10770 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010771}
10772#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010773
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774/*
10775 * Show current status info in ruler and various other places
10776 * If always is FALSE, only show ruler if position has changed.
10777 */
10778 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010779showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780{
10781 if (!always && !redrawing())
10782 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010783#ifdef FEAT_INS_EXPAND
10784 if (pum_visible())
10785 {
10786 /* Don't redraw right now, do it later. */
10787 curwin->w_redr_status = TRUE;
10788 return;
10789 }
10790#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010791#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010792 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010793 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010794 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796 else
10797#endif
10798#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010799 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800#endif
10801
10802#ifdef FEAT_TITLE
10803 if (need_maketitle
10804# ifdef FEAT_STL_OPT
10805 || (p_icon && (stl_syntax & STL_IN_ICON))
10806 || (p_title && (stl_syntax & STL_IN_TITLE))
10807# endif
10808 )
10809 maketitle();
10810#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010811 /* Redraw the tab pages line if needed. */
10812 if (redraw_tabline)
10813 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814}
10815
10816#ifdef FEAT_CMDL_INFO
10817 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010818win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010820#define RULER_BUF_LEN 70
10821 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 int row;
10823 int fillchar;
10824 int attr;
10825 int empty_line = FALSE;
10826 colnr_T virtcol;
10827 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010828 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830 int this_ru_col;
10831 int off = 0;
10832 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833
10834 /* If 'ruler' off or redrawing disabled, don't do anything */
10835 if (!p_ru)
10836 return;
10837
10838 /*
10839 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10840 * after deleting lines, before cursor.lnum is corrected.
10841 */
10842 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10843 return;
10844
10845#ifdef FEAT_INS_EXPAND
10846 /* Don't draw the ruler while doing insert-completion, it might overwrite
10847 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 if (edit_submode != NULL)
10850 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010851 // Don't draw the ruler when the popup menu is visible, it may overlap.
10852 // Except when the popup menu will be redrawn anyway.
10853 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010854 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855#endif
10856
10857#ifdef FEAT_STL_OPT
10858 if (*p_ruf)
10859 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010860 int save_called_emsg = called_emsg;
10861
10862 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010864 if (called_emsg)
10865 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010866 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010867 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 return;
10869 }
10870#endif
10871
10872 /*
10873 * Check if not in Insert mode and the line is empty (will show "0-1").
10874 */
10875 if (!(State & INSERT)
10876 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10877 empty_line = TRUE;
10878
10879 /*
10880 * Only draw the ruler when something changed.
10881 */
10882 validate_virtcol_win(wp);
10883 if ( redraw_cmdline
10884 || always
10885 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10886 || wp->w_cursor.col != wp->w_ru_cursor.col
10887 || wp->w_virtcol != wp->w_ru_virtcol
10888#ifdef FEAT_VIRTUALEDIT
10889 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10890#endif
10891 || wp->w_topline != wp->w_ru_topline
10892 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10893#ifdef FEAT_DIFF
10894 || wp->w_topfill != wp->w_ru_topfill
10895#endif
10896 || empty_line != wp->w_ru_empty)
10897 {
10898 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 if (wp->w_status_height)
10900 {
10901 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010902 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010903 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010904 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 }
10906 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907 {
10908 row = Rows - 1;
10909 fillchar = ' ';
10910 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 width = Columns;
10912 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 }
10914
10915 /* In list mode virtcol needs to be recomputed */
10916 virtcol = wp->w_virtcol;
10917 if (wp->w_p_list && lcs_tab1 == NUL)
10918 {
10919 wp->w_p_list = FALSE;
10920 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10921 wp->w_p_list = TRUE;
10922 }
10923
10924 /*
10925 * Some sprintfs return the length, some return a pointer.
10926 * To avoid portability problems we use strlen() here.
10927 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010928 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10930 ? 0L
10931 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010932 len = STRLEN(buffer);
10933 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10935 (int)virtcol + 1);
10936
10937 /*
10938 * Add a "50%" if there is room for it.
10939 * On the last line, don't print in the last column (scrolls the
10940 * screen up on some terminals).
10941 */
10942 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010943 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 this_ru_col = ru_col - (Columns - width);
10948 if (this_ru_col < 0)
10949 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 /* Never use more than half the window/screen width, leave the other
10951 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010952 if (this_ru_col < (width + 1) / 2)
10953 this_ru_col = (width + 1) / 2;
10954 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010956 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010957 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 if (has_mbyte)
10960 i += (*mb_char2bytes)(fillchar, buffer + i);
10961 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 buffer[i++] = fillchar;
10963 ++o;
10964 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010965 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 }
10967 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 if (has_mbyte)
10969 {
10970 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010971 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 {
10973 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010974 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 {
10976 buffer[i] = NUL;
10977 break;
10978 }
10979 }
10980 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010981 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020010982 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983
Bram Moolenaar4033c552017-09-16 20:54:51 +020010984 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 i = redraw_cmdline;
10986 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020010987 this_ru_col + off + (int)STRLEN(buffer),
10988 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 fillchar, fillchar, attr);
10990 /* don't redraw the cmdline because of showing the ruler */
10991 redraw_cmdline = i;
10992 wp->w_ru_cursor = wp->w_cursor;
10993 wp->w_ru_virtcol = wp->w_virtcol;
10994 wp->w_ru_empty = empty_line;
10995 wp->w_ru_topline = wp->w_topline;
10996 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10997#ifdef FEAT_DIFF
10998 wp->w_ru_topfill = wp->w_topfill;
10999#endif
11000 }
11001}
11002#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011003
11004#if defined(FEAT_LINEBREAK) || defined(PROTO)
11005/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011006 * Return the width of the 'number' and 'relativenumber' column.
11007 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011008 * Otherwise it depends on 'numberwidth' and the line count.
11009 */
11010 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011011number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011012{
11013 int n;
11014 linenr_T lnum;
11015
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011016 if (wp->w_p_rnu && !wp->w_p_nu)
11017 /* cursor line shows "0" */
11018 lnum = wp->w_height;
11019 else
11020 /* cursor line shows absolute line number */
11021 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011022
Bram Moolenaar6b314672015-03-20 15:42:10 +010011023 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011024 return wp->w_nrwidth_width;
11025 wp->w_nrwidth_line_count = lnum;
11026
11027 n = 0;
11028 do
11029 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011030 lnum /= 10;
11031 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011032 } while (lnum > 0);
11033
11034 /* 'numberwidth' gives the minimal width plus one */
11035 if (n < wp->w_p_nuw - 1)
11036 n = wp->w_p_nuw - 1;
11037
11038 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011039 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011040 return n;
11041}
11042#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011043
Bram Moolenaar113e1072019-01-20 15:30:40 +010011044#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011045/*
11046 * Return the current cursor column. This is the actual position on the
11047 * screen. First column is 0.
11048 */
11049 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011050screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011051{
11052 return screen_cur_col;
11053}
11054
11055/*
11056 * Return the current cursor row. This is the actual position on the screen.
11057 * First row is 0.
11058 */
11059 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011060screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011061{
11062 return screen_cur_row;
11063}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011064#endif