blob: 4effa5286b68788f314d67e3b6918c33f08c50c6 [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 Moolenaar071d4272004-06-13 20:20:40 +0000147#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200151static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200153static 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 +0100154static void win_rest_invalid(win_T *wp);
155static void msg_pos_mode(void);
156static void recording_mode(int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static void draw_tabline(void);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200158static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100159static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200160#ifdef FEAT_MENU
161static void redraw_win_toolbar(win_T *wp);
162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200167static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170/* Ugly global: overrule attribute used by screen_char() */
171static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200173#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
174/* Can limit syntax highlight time to 'redrawtime'. */
175# define SYN_TIME_LIMIT 1
176#endif
177
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200178#ifdef FEAT_RIGHTLEFT
179# define HAS_RIGHTLEFT(x) x
180#else
181# define HAS_RIGHTLEFT(x) FALSE
182#endif
183
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/*
185 * Redraw the current window later, with update_screen(type).
186 * Set must_redraw only if not already set to a higher value.
187 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
188 */
189 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100190redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191{
192 redraw_win_later(curwin, type);
193}
194
195 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100196redraw_win_later(
197 win_T *wp,
198 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200200 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 {
202 wp->w_redr_type = type;
203 if (type >= NOT_VALID)
204 wp->w_lines_valid = 0;
205 if (must_redraw < type) /* must_redraw is the maximum of all windows */
206 must_redraw = type;
207 }
208}
209
210/*
211 * Force a complete redraw later. Also resets the highlighting. To be used
212 * after executing a shell command that messes up the screen.
213 */
214 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100215redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216{
217 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000218#ifdef FEAT_GUI
219 if (gui.in_use)
220 /* Use a code that will reset gui.highlight_mask in
221 * gui_stop_highlight(). */
222 screen_attr = HL_ALL + 1;
223 else
224#endif
225 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200226 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227}
228
229/*
230 * Mark all windows to be redrawn later.
231 */
232 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100233redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234{
235 win_T *wp;
236
237 FOR_ALL_WINDOWS(wp)
238 {
239 redraw_win_later(wp, type);
240 }
241}
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 Moolenaar29ae3772017-04-30 19:39:39 +0200264 void
265redraw_buf_and_status_later(buf_T *buf, int type)
266{
267 win_T *wp;
268
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200269#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200270 if (wild_menu_showing != 0)
271 /* Don't redraw while the command line completion is displayed, it
272 * would disappear. */
273 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200274#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200275 FOR_ALL_WINDOWS(wp)
276 {
277 if (wp->w_buffer == buf)
278 {
279 redraw_win_later(wp, type);
280 wp->w_redr_status = TRUE;
281 }
282 }
283}
284
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200286 * Redraw as soon as possible. When the command line is not scrolled redraw
287 * right away and restore what was on the command line.
288 * Return a code indicating what happened.
289 */
290 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100291redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200292{
293 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200294 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200295 int r;
296 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200297 schar_T *screenline; /* copy from ScreenLines[] */
298 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299#ifdef FEAT_MBYTE
300 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200301 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200302 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200303 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200304#endif
305
306 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200307 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308 return ret;
309
310 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200311 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200315 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 if (screenline == NULL || screenattr == NULL)
317 ret = 2;
318#ifdef FEAT_MBYTE
319 if (enc_utf8)
320 {
321 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200322 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200323 if (screenlineUC == NULL)
324 ret = 2;
325 for (i = 0; i < p_mco; ++i)
326 {
327 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200328 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200329 if (screenlineC[i] == NULL)
330 ret = 2;
331 }
332 }
333 if (enc_dbcs == DBCS_JPNU)
334 {
335 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200336 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200337 if (screenline2 == NULL)
338 ret = 2;
339 }
340#endif
341
342 if (ret != 2)
343 {
344 /* Save the text displayed in the command line area. */
345 for (r = 0; r < rows; ++r)
346 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200347 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200348 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200349 (size_t)cols * sizeof(schar_T));
350 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200352 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200353#ifdef FEAT_MBYTE
354 if (enc_utf8)
355 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200359 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200360 mch_memmove(screenlineC[i] + r * cols,
361 ScreenLinesC[i] + LineOffset[cmdline_row + r],
362 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 }
364 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368#endif
369 }
370
371 update_screen(0);
372 ret = 3;
373
374 if (must_redraw == 0)
375 {
376 int off = (int)(current_ScreenLine - ScreenLines);
377
378 /* Restore the text displayed in the command line area. */
379 for (r = 0; r < rows; ++r)
380 {
381 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200382 screenline + r * cols,
383 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200384 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200385 screenattr + r * cols,
386 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200387#ifdef FEAT_MBYTE
388 if (enc_utf8)
389 {
390 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200391 screenlineUC + r * cols,
392 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393 for (i = 0; i < p_mco; ++i)
394 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200395 screenlineC[i] + r * cols,
396 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200397 }
398 if (enc_dbcs == DBCS_JPNU)
399 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200400 screenline2 + r * cols,
401 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200403 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200404 }
405 ret = 4;
406 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200407 }
408
409 vim_free(screenline);
410 vim_free(screenattr);
411#ifdef FEAT_MBYTE
412 if (enc_utf8)
413 {
414 vim_free(screenlineUC);
415 for (i = 0; i < p_mco; ++i)
416 vim_free(screenlineC[i]);
417 }
418 if (enc_dbcs == DBCS_JPNU)
419 vim_free(screenline2);
420#endif
421
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200422 /* Show the intro message when appropriate. */
423 maybe_intro_message();
424
425 setcursor();
426
Bram Moolenaar2951b772013-07-03 12:45:31 +0200427 return ret;
428}
429
430/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100431 * Invoked after an asynchronous callback is called.
432 * If an echo command was used the cursor needs to be put back where
433 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200434 * If "call_update_screen" is FALSE don't call update_screen() when at the
435 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100436 */
437 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200438redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100439{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200440 ++redrawing_for_callback;
441
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100442 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200443 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100444 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200445 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200446 // Don't redraw when in prompt_for_number().
447 if (cmdline_row > 0)
448 {
449 // Redrawing only works when the screen didn't scroll. Don't clear
450 // wildmenu entries.
451 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200452#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200453 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200454#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200455 && call_update_screen)
456 update_screen(0);
457
458 // Redraw in the same position, so that the user can continue
459 // editing the command.
460 redrawcmdline_ex(FALSE);
461 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200462 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200463 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100464 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200465 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200466 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100467 setcursor();
468 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100469 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100470#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100471 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200472 // Don't update the cursor when it is blinking and off to avoid
473 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100474 out_flush_cursor(FALSE, FALSE);
475 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100476#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100477 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200478
479 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100480}
481
482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 * Changed something in the current window, at buffer line "lnum", that
484 * requires that line and possibly other lines to be redrawn.
485 * Used when entering/leaving Insert mode with the cursor on a folded line.
486 * Used to remove the "$" from a change command.
487 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
488 * may become invalid and the whole window will have to be redrawn.
489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100491redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200492 win_T *wp,
Bram Moolenaar05540972016-01-30 20:31:25 +0100493 linenr_T lnum,
494 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495{
496#ifdef FEAT_FOLDING
497 int i;
498#endif
499
Bram Moolenaar90a99792018-09-12 21:52:18 +0200500 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
501 wp->w_redraw_top = lnum;
502 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
503 wp->w_redraw_bot = lnum;
504 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505
506#ifdef FEAT_FOLDING
507 if (invalid)
508 {
509 /* A w_lines[] entry for this lnum has become invalid. */
Bram Moolenaar90a99792018-09-12 21:52:18 +0200510 i = find_wl_entry(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if (i >= 0)
Bram Moolenaar90a99792018-09-12 21:52:18 +0200512 wp->w_lines[i].wl_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 }
514#endif
515}
516
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200517 void
518reset_updating_screen(int may_resize_shell UNUSED)
519{
520 updating_screen = FALSE;
521#ifdef FEAT_GUI
522 if (may_resize_shell)
523 gui_may_resize_shell();
524#endif
525#ifdef FEAT_TERMINAL
526 term_check_channel_closed_recently();
527#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200528
529#ifdef HAVE_DROP_FILE
530 // If handle_drop() was called while updating_screen was TRUE need to
531 // handle the drop now.
532 handle_any_postponed_drop();
533#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200534}
535
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200537 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 */
539 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100540update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
542 redraw_curbuf_later(type);
543 update_screen(type);
544}
545
546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 * Based on the current value of curwin->w_topline, transfer a screenfull
548 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200549 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200551 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200552update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200554 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 win_T *wp;
556 static int did_intro = FALSE;
557#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
558 int did_one;
559#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200560#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200561 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200562 int gui_cursor_col;
563 int gui_cursor_row;
564#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200565 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000567 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200569 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200571 if (type == VALID_NO_UPDATE)
572 {
573 no_update = TRUE;
574 type = 0;
575 }
576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 if (must_redraw)
578 {
579 if (type < must_redraw) /* use maximal type */
580 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000581
582 /* must_redraw is reset here, so that when we run into some weird
583 * reason to redraw while busy redrawing (e.g., asynchronous
584 * scrolling), or update_topline() in win_update() will cause a
585 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 must_redraw = 0;
587 }
588
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200589 /* May need to update w_lines[]. */
590 if (curwin->w_lines_valid == 0 && type < NOT_VALID
591#ifdef FEAT_TERMINAL
592 && !term_do_update_window(curwin)
593#endif
594 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 type = NOT_VALID;
596
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000597 /* Postpone the redrawing when it's not needed and when being called
598 * recursively. */
599 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 {
601 redraw_later(type); /* remember type for next time */
602 must_redraw = type;
603 if (type > INVERTED_ALL)
604 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200605 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 }
607
608 updating_screen = TRUE;
609#ifdef FEAT_SYN_HL
610 ++display_tick; /* let syntax code know we're in a next round of
611 * display updating */
612#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200613 if (no_update)
614 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615
616 /*
617 * if the screen was scrolled up when displaying a message, scroll it down
618 */
619 if (msg_scrolled)
620 {
621 clear_cmdline = TRUE;
622 if (msg_scrolled > Rows - 5) /* clearing is faster */
623 type = CLEAR;
624 else if (type != CLEAR)
625 {
626 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200627 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
628 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 type = CLEAR;
630 FOR_ALL_WINDOWS(wp)
631 {
632 if (W_WINROW(wp) < msg_scrolled)
633 {
634 if (W_WINROW(wp) + wp->w_height > msg_scrolled
635 && wp->w_redr_type < REDRAW_TOP
636 && wp->w_lines_valid > 0
637 && wp->w_topline == wp->w_lines[0].wl_lnum)
638 {
639 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
640 wp->w_redr_type = REDRAW_TOP;
641 }
642 else
643 {
644 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200645 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
646 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 }
649 }
650 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200651 if (!no_update)
652 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000653 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 }
655 msg_scrolled = 0;
656 need_wait_return = FALSE;
657 }
658
659 /* reset cmdline_row now (may have been changed temporarily) */
660 compute_cmdrow();
661
662 /* Check for changed highlighting */
663 if (need_highlight_changed)
664 highlight_changed();
665
666 if (type == CLEAR) /* first clear screen */
667 {
668 screenclear(); /* will reset clear_cmdline */
669 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200670 /* must_redraw may be set indirectly, avoid another redraw later */
671 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 }
673
674 if (clear_cmdline) /* going to clear cmdline (done below) */
675 check_for_delay(FALSE);
676
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000677#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200678 /* Force redraw when width of 'number' or 'relativenumber' column
679 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000680 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200681 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
682 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000683 curwin->w_redr_type = NOT_VALID;
684#endif
685
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 /*
687 * Only start redrawing if there is really something to do.
688 */
689 if (type == INVERTED)
690 update_curswant();
691 if (curwin->w_redr_type < type
692 && !((type == VALID
693 && curwin->w_lines[0].wl_valid
694#ifdef FEAT_DIFF
695 && curwin->w_topfill == curwin->w_old_topfill
696 && curwin->w_botfill == curwin->w_old_botfill
697#endif
698 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000700 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
702 && curwin->w_old_visual_mode == VIsual_mode
703 && (curwin->w_valid & VALID_VIRTCOL)
704 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 ))
706 curwin->w_redr_type = type;
707
Bram Moolenaar5a305422006-04-28 22:38:25 +0000708 /* Redraw the tab pages line if needed. */
709 if (redraw_tabline || type >= NOT_VALID)
710 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000711
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712#ifdef FEAT_SYN_HL
713 /*
714 * Correct stored syntax highlighting info for changes in each displayed
715 * buffer. Each buffer must only be done once.
716 */
717 FOR_ALL_WINDOWS(wp)
718 {
719 if (wp->w_buffer->b_mod_set)
720 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 win_T *wwp;
722
723 /* Check if we already did this buffer. */
724 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
725 if (wwp->w_buffer == wp->w_buffer)
726 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200727 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 syn_stack_apply_changes(wp->w_buffer);
729 }
730 }
731#endif
732
733 /*
734 * Go from top to bottom through the windows, redrawing the ones that need
735 * it.
736 */
737#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
738 did_one = FALSE;
739#endif
740#ifdef FEAT_SEARCH_EXTRA
741 search_hl.rm.regprog = NULL;
742#endif
743 FOR_ALL_WINDOWS(wp)
744 {
745 if (wp->w_redr_type != 0)
746 {
747 cursor_off();
748#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
749 if (!did_one)
750 {
751 did_one = TRUE;
752# ifdef FEAT_SEARCH_EXTRA
753 start_search_hl();
754# endif
755# ifdef FEAT_CLIPBOARD
756 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200757 if (clip_star.available && clip_isautosel_star())
758 clip_update_selection(&clip_star);
759 if (clip_plus.available && clip_isautosel_plus())
760 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761# endif
762#ifdef FEAT_GUI
763 /* Remove the cursor before starting to do anything, because
764 * scrolling may make it difficult to redraw the text under
765 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200766 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200767 {
768 gui_cursor_col = gui.cursor_col;
769 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200771 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773#endif
774 }
775#endif
776 win_update(wp);
777 }
778
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 /* redraw status line after the window to minimize cursor movement */
780 if (wp->w_redr_status)
781 {
782 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200783 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786#if defined(FEAT_SEARCH_EXTRA)
787 end_search_hl();
788#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100789#ifdef FEAT_INS_EXPAND
790 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200791 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 /* Reset b_mod_set flags. Going through all windows is probably faster
795 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200796 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200799 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801 /* Clear or redraw the command line. Done last, because scrolling may
802 * mess up the command line. */
803 if (clear_cmdline || redraw_cmdline)
804 showmode();
805
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200806 if (no_update)
807 --no_win_do_lines_ins;
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200810 if (!did_intro)
811 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 did_intro = TRUE;
813
814#ifdef FEAT_GUI
815 /* Redraw the cursor and update the scrollbars when all screen updating is
816 * done. */
817 if (gui.in_use)
818 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200819 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200820 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100821 mch_disable_flush();
822 out_flush(); /* required before updating the cursor */
823 mch_enable_flush();
824
Bram Moolenaar144445d2016-07-08 21:41:54 +0200825 /* Put the GUI position where the cursor was, gui_update_cursor()
826 * uses that. */
827 gui.col = gui_cursor_col;
828 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200829# ifdef FEAT_MBYTE
830 gui.col = mb_fix_col(gui.col, gui.row);
831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100833 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200834 screen_cur_col = gui.col;
835 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200836 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100837 else
838 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 gui_update_scrollbars(FALSE);
840 }
841#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200842 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843}
844
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100845#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
846/*
847 * Prepare for updating one or more windows.
848 * Caller must check for "updating_screen" already set to avoid recursiveness.
849 */
850 static void
851update_prepare(void)
852{
853 cursor_off();
854 updating_screen = TRUE;
855#ifdef FEAT_GUI
856 /* Remove the cursor before starting to do anything, because scrolling may
857 * make it difficult to redraw the text under it. */
858 if (gui.in_use)
859 gui_undraw_cursor();
860#endif
861#ifdef FEAT_SEARCH_EXTRA
862 start_search_hl();
863#endif
864}
865
866/*
867 * Finish updating one or more windows.
868 */
869 static void
870update_finish(void)
871{
872 if (redraw_cmdline)
873 showmode();
874
875# ifdef FEAT_SEARCH_EXTRA
876 end_search_hl();
877# endif
878
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200879 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100880
881# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100882 /* Redraw the cursor and update the scrollbars when all screen updating is
883 * done. */
884 if (gui.in_use)
885 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100886 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100887 gui_update_scrollbars(FALSE);
888 }
889# endif
890}
891#endif
892
Bram Moolenaar860cae12010-06-05 23:22:07 +0200893#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200894/*
895 * Return TRUE if the cursor line in window "wp" may be concealed, according
896 * to the 'concealcursor' option.
897 */
898 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100899conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200900{
901 int c;
902
903 if (*wp->w_p_cocu == NUL)
904 return FALSE;
905 if (get_real_state() & VISUAL)
906 c = 'v';
907 else if (State & INSERT)
908 c = 'i';
909 else if (State & NORMAL)
910 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200911 else if (State & CMDLINE)
912 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200913 else
914 return FALSE;
915 return vim_strchr(wp->w_p_cocu, c) != NULL;
916}
917
918/*
919 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
920 */
921 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200922conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200923{
924 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
925 {
926 need_cursor_line_redraw = TRUE;
927 /* Need to recompute cursor column, e.g., when starting Visual mode
928 * without concealing. */
929 curs_columns(TRUE);
930 }
931}
932
Bram Moolenaar860cae12010-06-05 23:22:07 +0200933 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100934update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200935{
936 int row;
937 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200938#ifdef SYN_TIME_LIMIT
939 proftime_T syntax_tm;
940#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200941
Bram Moolenaar908be432016-05-24 10:51:30 +0200942 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100943 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200944 return;
945
Bram Moolenaar860cae12010-06-05 23:22:07 +0200946 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200947 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200948 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200949#ifdef SYN_TIME_LIMIT
950 /* Set the time limit to 'redrawtime'. */
951 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200952 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200953#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100954 update_prepare();
955
Bram Moolenaar860cae12010-06-05 23:22:07 +0200956 row = 0;
957 for (j = 0; j < wp->w_lines_valid; ++j)
958 {
959 if (lnum == wp->w_lines[j].wl_lnum)
960 {
961 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200962# ifdef FEAT_SEARCH_EXTRA
963 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200964 start_search_hl();
965 prepare_search_hl(wp, lnum);
966# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200967 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size,
968 FALSE, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200969# if defined(FEAT_SEARCH_EXTRA)
970 end_search_hl();
971# endif
972 break;
973 }
974 row += wp->w_lines[j].wl_size;
975 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100976
977 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200978
979#ifdef SYN_TIME_LIMIT
980 syn_set_timeout(NULL);
981#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200982 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200983 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984}
985#endif
986
987#if defined(FEAT_SIGNS) || defined(PROTO)
988 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100989update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990{
991 win_T *wp;
992 int doit = FALSE;
993
994# ifdef FEAT_FOLDING
995 win_foldinfo.fi_level = 0;
996# endif
997
998 /* update/delete a specific mark */
999 FOR_ALL_WINDOWS(wp)
1000 {
1001 if (buf != NULL && lnum > 0)
1002 {
1003 if (wp->w_buffer == buf && lnum >= wp->w_topline
1004 && lnum < wp->w_botline)
1005 {
1006 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1007 wp->w_redraw_top = lnum;
1008 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1009 wp->w_redraw_bot = lnum;
1010 redraw_win_later(wp, VALID);
1011 }
1012 }
1013 else
1014 redraw_win_later(wp, VALID);
1015 if (wp->w_redr_type != 0)
1016 doit = TRUE;
1017 }
1018
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001019 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001020 * happening (recursive call), messages on the screen or still starting up.
1021 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001022 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001023 || State == ASKMORE || State == HITRETURN
1024 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001025#ifdef FEAT_GUI
1026 || gui.starting
1027#endif
1028 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 return;
1030
1031 /* update all windows that need updating */
1032 update_prepare();
1033
Bram Moolenaar29323592016-07-24 22:04:11 +02001034 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
1036 if (wp->w_redr_type != 0)
1037 win_update(wp);
1038 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001039 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
1042 update_finish();
1043}
1044#endif
1045
1046
1047#if defined(FEAT_GUI) || defined(PROTO)
1048/*
1049 * Update a single window, its status line and maybe the command line msg.
1050 * Used for the GUI scrollbar.
1051 */
1052 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001053updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001055 /* return if already busy updating */
1056 if (updating_screen)
1057 return;
1058
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 update_prepare();
1060
1061#ifdef FEAT_CLIPBOARD
1062 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001063 if (clip_star.available && clip_isautosel_star())
1064 clip_update_selection(&clip_star);
1065 if (clip_plus.available && clip_isautosel_plus())
1066 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001070
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001071 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001072 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001073 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if (wp->w_redr_status
1076# ifdef FEAT_CMDL_INFO
1077 || p_ru
1078# endif
1079# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001080 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081# endif
1082 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001083 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
1085 update_finish();
1086}
1087#endif
1088
1089/*
1090 * Update a single window.
1091 *
1092 * This may cause the windows below it also to be redrawn (when clearing the
1093 * screen or scrolling lines).
1094 *
1095 * How the window is redrawn depends on wp->w_redr_type. Each type also
1096 * implies the one below it.
1097 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001098 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1100 * INVERTED redraw the changed part of the Visual area
1101 * INVERTED_ALL redraw the whole Visual area
1102 * VALID 1. scroll up/down to adjust for a changed w_topline
1103 * 2. update lines at the top when scrolled down
1104 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001105 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 * b_mod_top and b_mod_bot.
1107 * - if wp->w_redraw_top non-zero, redraw lines between
1108 * wp->w_redraw_top and wp->w_redr_bot.
1109 * - continue redrawing when syntax status is invalid.
1110 * 4. if scrolled up, update lines at the bottom.
1111 * This results in three areas that may need updating:
1112 * top: from first row to top_end (when scrolled down)
1113 * mid: from mid_start to mid_end (update inversion or changed text)
1114 * bot: from bot_start to last row (when scrolled up)
1115 */
1116 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001117win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
1119 buf_T *buf = wp->w_buffer;
1120 int type;
1121 int top_end = 0; /* Below last row of the top area that needs
1122 updating. 0 when no top area updating. */
1123 int mid_start = 999;/* first row of the mid area that needs
1124 updating. 999 when no mid area updating. */
1125 int mid_end = 0; /* Below last row of the mid area that needs
1126 updating. 0 when no mid area updating. */
1127 int bot_start = 999;/* first row of the bot area that needs
1128 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 int scrolled_down = FALSE; /* TRUE when scrolled down when
1130 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001132 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 int top_to_mod = FALSE; /* redraw above mod_top */
1134#endif
1135
1136 int row; /* current window row to display */
1137 linenr_T lnum; /* current buffer lnum to display */
1138 int idx; /* current index in w_lines[] */
1139 int srow; /* starting row of the current line */
1140
1141 int eof = FALSE; /* if TRUE, we hit the end of the file */
1142 int didline = FALSE; /* if TRUE, we finished the last line */
1143 int i;
1144 long j;
1145 static int recursive = FALSE; /* being called recursively */
1146 int old_botline = wp->w_botline;
1147#ifdef FEAT_FOLDING
1148 long fold_count;
1149#endif
1150#ifdef FEAT_SYN_HL
1151 /* remember what happened to the previous line, to know if
1152 * check_visual_highlight() can be used */
1153#define DID_NONE 1 /* didn't update a line */
1154#define DID_LINE 2 /* updated a normal line */
1155#define DID_FOLD 3 /* updated a folded line */
1156 int did_update = DID_NONE;
1157 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1158#endif
1159 linenr_T mod_top = 0;
1160 linenr_T mod_bot = 0;
1161#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1162 int save_got_int;
1163#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001164#ifdef SYN_TIME_LIMIT
1165 proftime_T syntax_tm;
1166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167
1168 type = wp->w_redr_type;
1169
1170 if (type == NOT_VALID)
1171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 wp->w_lines_valid = 0;
1174 }
1175
1176 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001177 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {
1179 wp->w_redr_type = 0;
1180 return;
1181 }
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 /* Window is zero-width: Only need to draw the separator. */
1184 if (wp->w_width == 0)
1185 {
1186 /* draw the vertical separator right of this window */
1187 draw_vsep_win(wp, 0);
1188 wp->w_redr_type = 0;
1189 return;
1190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001192#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001193 // If this window contains a terminal, redraw works completely differently.
1194 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001195 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001196 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001197# ifdef FEAT_MENU
1198 /* Draw the window toolbar, if there is one. */
1199 if (winbar_height(wp) > 0)
1200 redraw_win_toolbar(wp);
1201# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001202 wp->w_redr_type = 0;
1203 return;
1204 }
1205#endif
1206
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001208 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209#endif
1210
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001211#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001212 /* Force redraw when width of 'number' or 'relativenumber' column
1213 * changes. */
1214 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001215 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001216 {
1217 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001218 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001219 }
1220 else
1221#endif
1222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1224 {
1225 /*
1226 * When there are both inserted/deleted lines and specific lines to be
1227 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1228 * everything (only happens when redrawing is off for while).
1229 */
1230 type = NOT_VALID;
1231 }
1232 else
1233 {
1234 /*
1235 * Set mod_top to the first line that needs displaying because of
1236 * changes. Set mod_bot to the first line after the changes.
1237 */
1238 mod_top = wp->w_redraw_top;
1239 if (wp->w_redraw_bot != 0)
1240 mod_bot = wp->w_redraw_bot + 1;
1241 else
1242 mod_bot = 0;
1243 wp->w_redraw_top = 0; /* reset for next time */
1244 wp->w_redraw_bot = 0;
1245 if (buf->b_mod_set)
1246 {
1247 if (mod_top == 0 || mod_top > buf->b_mod_top)
1248 {
1249 mod_top = buf->b_mod_top;
1250#ifdef FEAT_SYN_HL
1251 /* Need to redraw lines above the change that may be included
1252 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001253 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001255 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 if (mod_top < 1)
1257 mod_top = 1;
1258 }
1259#endif
1260 }
1261 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1262 mod_bot = buf->b_mod_bot;
1263
1264#ifdef FEAT_SEARCH_EXTRA
1265 /* When 'hlsearch' is on and using a multi-line search pattern, a
1266 * change in one line may make the Search highlighting in a
1267 * previous line invalid. Simple solution: redraw all visible
1268 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001269 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001271 if (search_hl.rm.regprog != NULL
1272 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001274 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001275 {
1276 cur = wp->w_match_head;
1277 while (cur != NULL)
1278 {
1279 if (cur->match.regprog != NULL
1280 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001281 {
1282 top_to_mod = TRUE;
1283 break;
1284 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001285 cur = cur->next;
1286 }
1287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288#endif
1289 }
1290#ifdef FEAT_FOLDING
1291 if (mod_top != 0 && hasAnyFolding(wp))
1292 {
1293 linenr_T lnumt, lnumb;
1294
1295 /*
1296 * A change in a line can cause lines above it to become folded or
1297 * unfolded. Find the top most buffer line that may be affected.
1298 * If the line was previously folded and displayed, get the first
1299 * line of that fold. If the line is folded now, get the first
1300 * folded line. Use the minimum of these two.
1301 */
1302
1303 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1304 * the line below it. If there is no valid entry, use w_topline.
1305 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1306 * to this line. If there is no valid entry, use MAXLNUM. */
1307 lnumt = wp->w_topline;
1308 lnumb = MAXLNUM;
1309 for (i = 0; i < wp->w_lines_valid; ++i)
1310 if (wp->w_lines[i].wl_valid)
1311 {
1312 if (wp->w_lines[i].wl_lastlnum < mod_top)
1313 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1314 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1315 {
1316 lnumb = wp->w_lines[i].wl_lnum;
1317 /* When there is a fold column it might need updating
1318 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001319 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 ++lnumb;
1321 }
1322 }
1323
1324 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1325 if (mod_top > lnumt)
1326 mod_top = lnumt;
1327
1328 /* Now do the same for the bottom line (one above mod_bot). */
1329 --mod_bot;
1330 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1331 ++mod_bot;
1332 if (mod_bot < lnumb)
1333 mod_bot = lnumb;
1334 }
1335#endif
1336
1337 /* When a change starts above w_topline and the end is below
1338 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001339 * If the end of the change is above w_topline: do like no change was
1340 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 if (mod_top != 0 && mod_top < wp->w_topline)
1342 {
1343 if (mod_bot > wp->w_topline)
1344 mod_top = wp->w_topline;
1345#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001346 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 top_end = 1;
1348#endif
1349 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001350
1351 /* When line numbers are displayed need to redraw all lines below
1352 * inserted/deleted lines. */
1353 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1354 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356
1357 /*
1358 * When only displaying the lines at the top, set top_end. Used when
1359 * window has scrolled down for msg_scrolled.
1360 */
1361 if (type == REDRAW_TOP)
1362 {
1363 j = 0;
1364 for (i = 0; i < wp->w_lines_valid; ++i)
1365 {
1366 j += wp->w_lines[i].wl_size;
1367 if (j >= wp->w_upd_rows)
1368 {
1369 top_end = j;
1370 break;
1371 }
1372 }
1373 if (top_end == 0)
1374 /* not found (cannot happen?): redraw everything */
1375 type = NOT_VALID;
1376 else
1377 /* top area defined, the rest is VALID */
1378 type = VALID;
1379 }
1380
Bram Moolenaar367329b2007-08-30 11:53:22 +00001381 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001382 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1383 * non-zero and thus not FALSE) will indicate that screenclear() was not
1384 * called. */
1385 if (screen_cleared)
1386 screen_cleared = MAYBE;
1387
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 /*
1389 * If there are no changes on the screen that require a complete redraw,
1390 * handle three cases:
1391 * 1: we are off the top of the screen by a few lines: scroll down
1392 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1393 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1394 * w_lines[] that needs updating.
1395 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001396 if ((type == VALID || type == SOME_VALID
1397 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#ifdef FEAT_DIFF
1399 && !wp->w_botfill && !wp->w_old_botfill
1400#endif
1401 )
1402 {
1403 if (mod_top != 0 && wp->w_topline == mod_top)
1404 {
1405 /*
1406 * w_topline is the first changed line, the scrolling will be done
1407 * further down.
1408 */
1409 }
1410 else if (wp->w_lines[0].wl_valid
1411 && (wp->w_topline < wp->w_lines[0].wl_lnum
1412#ifdef FEAT_DIFF
1413 || (wp->w_topline == wp->w_lines[0].wl_lnum
1414 && wp->w_topfill > wp->w_old_topfill)
1415#endif
1416 ))
1417 {
1418 /*
1419 * New topline is above old topline: May scroll down.
1420 */
1421#ifdef FEAT_FOLDING
1422 if (hasAnyFolding(wp))
1423 {
1424 linenr_T ln;
1425
1426 /* count the number of lines we are off, counting a sequence
1427 * of folded lines as one */
1428 j = 0;
1429 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1430 {
1431 ++j;
1432 if (j >= wp->w_height - 2)
1433 break;
1434 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1435 }
1436 }
1437 else
1438#endif
1439 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1440 if (j < wp->w_height - 2) /* not too far off */
1441 {
1442 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1443#ifdef FEAT_DIFF
1444 /* insert extra lines for previously invisible filler lines */
1445 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1446 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1447 - wp->w_old_topfill;
1448#endif
1449 if (i < wp->w_height - 2) /* less than a screen off */
1450 {
1451 /*
1452 * Try to insert the correct number of lines.
1453 * If not the last window, delete the lines at the bottom.
1454 * win_ins_lines may fail when the terminal can't do it.
1455 */
1456 if (i > 0)
1457 check_for_delay(FALSE);
1458 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1459 {
1460 if (wp->w_lines_valid != 0)
1461 {
1462 /* Need to update rows that are new, stop at the
1463 * first one that scrolled down. */
1464 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
1467 /* Move the entries that were scrolled, disable
1468 * the entries for the lines to be redrawn. */
1469 if ((wp->w_lines_valid += j) > wp->w_height)
1470 wp->w_lines_valid = wp->w_height;
1471 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1472 wp->w_lines[idx] = wp->w_lines[idx - j];
1473 while (idx >= 0)
1474 wp->w_lines[idx--].wl_valid = FALSE;
1475 }
1476 }
1477 else
1478 mid_start = 0; /* redraw all lines */
1479 }
1480 else
1481 mid_start = 0; /* redraw all lines */
1482 }
1483 else
1484 mid_start = 0; /* redraw all lines */
1485 }
1486 else
1487 {
1488 /*
1489 * New topline is at or below old topline: May scroll up.
1490 * When topline didn't change, find first entry in w_lines[] that
1491 * needs updating.
1492 */
1493
1494 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1495 j = -1;
1496 row = 0;
1497 for (i = 0; i < wp->w_lines_valid; i++)
1498 {
1499 if (wp->w_lines[i].wl_valid
1500 && wp->w_lines[i].wl_lnum == wp->w_topline)
1501 {
1502 j = i;
1503 break;
1504 }
1505 row += wp->w_lines[i].wl_size;
1506 }
1507 if (j == -1)
1508 {
1509 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1510 * lines */
1511 mid_start = 0;
1512 }
1513 else
1514 {
1515 /*
1516 * Try to delete the correct number of lines.
1517 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1518 */
1519#ifdef FEAT_DIFF
1520 /* If the topline didn't change, delete old filler lines,
1521 * otherwise delete filler lines of the new topline... */
1522 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1523 row += wp->w_old_topfill;
1524 else
1525 row += diff_check_fill(wp, wp->w_topline);
1526 /* ... but don't delete new filler lines. */
1527 row -= wp->w_topfill;
1528#endif
1529 if (row > 0)
1530 {
1531 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001532 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1533 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 bot_start = wp->w_height - row;
1535 else
1536 mid_start = 0; /* redraw all lines */
1537 }
1538 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1539 {
1540 /*
1541 * Skip the lines (below the deleted lines) that are still
1542 * valid and don't need redrawing. Copy their info
1543 * upwards, to compensate for the deleted lines. Set
1544 * bot_start to the first row that needs redrawing.
1545 */
1546 bot_start = 0;
1547 idx = 0;
1548 for (;;)
1549 {
1550 wp->w_lines[idx] = wp->w_lines[j];
1551 /* stop at line that didn't fit, unless it is still
1552 * valid (no lines deleted) */
1553 if (row > 0 && bot_start + row
1554 + (int)wp->w_lines[j].wl_size > wp->w_height)
1555 {
1556 wp->w_lines_valid = idx + 1;
1557 break;
1558 }
1559 bot_start += wp->w_lines[idx++].wl_size;
1560
1561 /* stop at the last valid entry in w_lines[].wl_size */
1562 if (++j >= wp->w_lines_valid)
1563 {
1564 wp->w_lines_valid = idx;
1565 break;
1566 }
1567 }
1568#ifdef FEAT_DIFF
1569 /* Correct the first entry for filler lines at the top
1570 * when it won't get updated below. */
1571 if (wp->w_p_diff && bot_start > 0)
1572 wp->w_lines[0].wl_size =
1573 plines_win_nofill(wp, wp->w_topline, TRUE)
1574 + wp->w_topfill;
1575#endif
1576 }
1577 }
1578 }
1579
1580 /* When starting redraw in the first line, redraw all lines. When
1581 * there is only one window it's probably faster to clear the screen
1582 * first. */
1583 if (mid_start == 0)
1584 {
1585 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001586 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001587 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001588 /* Clear the screen when it was not done by win_del_lines() or
1589 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1590 * then. */
1591 if (screen_cleared != TRUE)
1592 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001593 /* The screen was cleared, redraw the tab pages line. */
1594 if (redraw_tabline)
1595 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001598
1599 /* When win_del_lines() or win_ins_lines() caused the screen to be
1600 * cleared (only happens for the first window) or when screenclear()
1601 * was called directly above, "must_redraw" will have been set to
1602 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1603 if (screen_cleared == TRUE)
1604 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
1606 else
1607 {
1608 /* Not VALID or INVERTED: redraw all lines. */
1609 mid_start = 0;
1610 mid_end = wp->w_height;
1611 }
1612
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001613 if (type == SOME_VALID)
1614 {
1615 /* SOME_VALID: redraw all lines. */
1616 mid_start = 0;
1617 mid_end = wp->w_height;
1618 type = NOT_VALID;
1619 }
1620
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 /* check if we are updating or removing the inverted part */
1622 if ((VIsual_active && buf == curwin->w_buffer)
1623 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1624 {
1625 linenr_T from, to;
1626
1627 if (VIsual_active)
1628 {
1629 if (VIsual_active
1630 && (VIsual_mode != wp->w_old_visual_mode
1631 || type == INVERTED_ALL))
1632 {
1633 /*
1634 * If the type of Visual selection changed, redraw the whole
1635 * selection. Also when the ownership of the X selection is
1636 * gained or lost.
1637 */
1638 if (curwin->w_cursor.lnum < VIsual.lnum)
1639 {
1640 from = curwin->w_cursor.lnum;
1641 to = VIsual.lnum;
1642 }
1643 else
1644 {
1645 from = VIsual.lnum;
1646 to = curwin->w_cursor.lnum;
1647 }
1648 /* redraw more when the cursor moved as well */
1649 if (wp->w_old_cursor_lnum < from)
1650 from = wp->w_old_cursor_lnum;
1651 if (wp->w_old_cursor_lnum > to)
1652 to = wp->w_old_cursor_lnum;
1653 if (wp->w_old_visual_lnum < from)
1654 from = wp->w_old_visual_lnum;
1655 if (wp->w_old_visual_lnum > to)
1656 to = wp->w_old_visual_lnum;
1657 }
1658 else
1659 {
1660 /*
1661 * Find the line numbers that need to be updated: The lines
1662 * between the old cursor position and the current cursor
1663 * position. Also check if the Visual position changed.
1664 */
1665 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1666 {
1667 from = curwin->w_cursor.lnum;
1668 to = wp->w_old_cursor_lnum;
1669 }
1670 else
1671 {
1672 from = wp->w_old_cursor_lnum;
1673 to = curwin->w_cursor.lnum;
1674 if (from == 0) /* Visual mode just started */
1675 from = to;
1676 }
1677
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001678 if (VIsual.lnum != wp->w_old_visual_lnum
1679 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 {
1681 if (wp->w_old_visual_lnum < from
1682 && wp->w_old_visual_lnum != 0)
1683 from = wp->w_old_visual_lnum;
1684 if (wp->w_old_visual_lnum > to)
1685 to = wp->w_old_visual_lnum;
1686 if (VIsual.lnum < from)
1687 from = VIsual.lnum;
1688 if (VIsual.lnum > to)
1689 to = VIsual.lnum;
1690 }
1691 }
1692
1693 /*
1694 * If in block mode and changed column or curwin->w_curswant:
1695 * update all lines.
1696 * First compute the actual start and end column.
1697 */
1698 if (VIsual_mode == Ctrl_V)
1699 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001700 colnr_T fromc, toc;
1701#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1702 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaar404406a2014-10-09 13:24:43 +02001704 if (curwin->w_p_lbr)
1705 ve_flags = VE_ALL;
1706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001708#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1709 ve_flags = save_ve_flags;
1710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 ++toc;
1712 if (curwin->w_curswant == MAXCOL)
1713 toc = MAXCOL;
1714
1715 if (fromc != wp->w_old_cursor_fcol
1716 || toc != wp->w_old_cursor_lcol)
1717 {
1718 if (from > VIsual.lnum)
1719 from = VIsual.lnum;
1720 if (to < VIsual.lnum)
1721 to = VIsual.lnum;
1722 }
1723 wp->w_old_cursor_fcol = fromc;
1724 wp->w_old_cursor_lcol = toc;
1725 }
1726 }
1727 else
1728 {
1729 /* Use the line numbers of the old Visual area. */
1730 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1731 {
1732 from = wp->w_old_cursor_lnum;
1733 to = wp->w_old_visual_lnum;
1734 }
1735 else
1736 {
1737 from = wp->w_old_visual_lnum;
1738 to = wp->w_old_cursor_lnum;
1739 }
1740 }
1741
1742 /*
1743 * There is no need to update lines above the top of the window.
1744 */
1745 if (from < wp->w_topline)
1746 from = wp->w_topline;
1747
1748 /*
1749 * If we know the value of w_botline, use it to restrict the update to
1750 * the lines that are visible in the window.
1751 */
1752 if (wp->w_valid & VALID_BOTLINE)
1753 {
1754 if (from >= wp->w_botline)
1755 from = wp->w_botline - 1;
1756 if (to >= wp->w_botline)
1757 to = wp->w_botline - 1;
1758 }
1759
1760 /*
1761 * Find the minimal part to be updated.
1762 * Watch out for scrolling that made entries in w_lines[] invalid.
1763 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1764 * top_end; need to redraw from top_end to the "to" line.
1765 * A middle mouse click with a Visual selection may change the text
1766 * above the Visual area and reset wl_valid, do count these for
1767 * mid_end (in srow).
1768 */
1769 if (mid_start > 0)
1770 {
1771 lnum = wp->w_topline;
1772 idx = 0;
1773 srow = 0;
1774 if (scrolled_down)
1775 mid_start = top_end;
1776 else
1777 mid_start = 0;
1778 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1779 {
1780 if (wp->w_lines[idx].wl_valid)
1781 mid_start += wp->w_lines[idx].wl_size;
1782 else if (!scrolled_down)
1783 srow += wp->w_lines[idx].wl_size;
1784 ++idx;
1785# ifdef FEAT_FOLDING
1786 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1787 lnum = wp->w_lines[idx].wl_lnum;
1788 else
1789# endif
1790 ++lnum;
1791 }
1792 srow += mid_start;
1793 mid_end = wp->w_height;
1794 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1795 {
1796 if (wp->w_lines[idx].wl_valid
1797 && wp->w_lines[idx].wl_lnum >= to + 1)
1798 {
1799 /* Only update until first row of this line */
1800 mid_end = srow;
1801 break;
1802 }
1803 srow += wp->w_lines[idx].wl_size;
1804 }
1805 }
1806 }
1807
1808 if (VIsual_active && buf == curwin->w_buffer)
1809 {
1810 wp->w_old_visual_mode = VIsual_mode;
1811 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1812 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001813 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 wp->w_old_curswant = curwin->w_curswant;
1815 }
1816 else
1817 {
1818 wp->w_old_visual_mode = 0;
1819 wp->w_old_cursor_lnum = 0;
1820 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001821 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
1824#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1825 /* reset got_int, otherwise regexp won't work */
1826 save_got_int = got_int;
1827 got_int = 0;
1828#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001829#ifdef SYN_TIME_LIMIT
1830 /* Set the time limit to 'redrawtime'. */
1831 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001832 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834#ifdef FEAT_FOLDING
1835 win_foldinfo.fi_level = 0;
1836#endif
1837
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001838#ifdef FEAT_MENU
1839 /*
1840 * Draw the window toolbar, if there is one.
1841 * TODO: only when needed.
1842 */
1843 if (winbar_height(wp) > 0)
1844 redraw_win_toolbar(wp);
1845#endif
1846
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 /*
1848 * Update all the window rows.
1849 */
1850 idx = 0; /* first entry in w_lines[].wl_size */
1851 row = 0;
1852 srow = 0;
1853 lnum = wp->w_topline; /* first line shown in window */
1854 for (;;)
1855 {
1856 /* stop updating when reached the end of the window (check for _past_
1857 * the end of the window is at the end of the loop) */
1858 if (row == wp->w_height)
1859 {
1860 didline = TRUE;
1861 break;
1862 }
1863
1864 /* stop updating when hit the end of the file */
1865 if (lnum > buf->b_ml.ml_line_count)
1866 {
1867 eof = TRUE;
1868 break;
1869 }
1870
1871 /* Remember the starting row of the line that is going to be dealt
1872 * with. It is used further down when the line doesn't fit. */
1873 srow = row;
1874
1875 /*
1876 * Update a line when it is in an area that needs updating, when it
1877 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001878 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 * win_del_lines(), check if the current line includes it.
1880 * When syntax folding is being used, the saved syntax states will
1881 * already have been updated, we can't see where the syntax state is
1882 * the same again, just update until the end of the window.
1883 */
1884 if (row < top_end
1885 || (row >= mid_start && row < mid_end)
1886#ifdef FEAT_SEARCH_EXTRA
1887 || top_to_mod
1888#endif
1889 || idx >= wp->w_lines_valid
1890 || (row + wp->w_lines[idx].wl_size > bot_start)
1891 || (mod_top != 0
1892 && (lnum == mod_top
1893 || (lnum >= mod_top
1894 && (lnum < mod_bot
1895#ifdef FEAT_SYN_HL
1896 || did_update == DID_FOLD
1897 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001898 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 && (
1900# ifdef FEAT_FOLDING
1901 (foldmethodIsSyntax(wp)
1902 && hasAnyFolding(wp)) ||
1903# endif
1904 syntax_check_changed(lnum)))
1905#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001906#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001907 /* match in fixed position might need redraw
1908 * if lines were inserted or deleted */
1909 || (wp->w_match_head != NULL
1910 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 )))))
1913 {
1914#ifdef FEAT_SEARCH_EXTRA
1915 if (lnum == mod_top)
1916 top_to_mod = FALSE;
1917#endif
1918
1919 /*
1920 * When at start of changed lines: May scroll following lines
1921 * up or down to minimize redrawing.
1922 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001923 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 */
1925 if (lnum == mod_top
1926 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001927 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
1929 int old_rows = 0;
1930 int new_rows = 0;
1931 int xtra_rows;
1932 linenr_T l;
1933
1934 /* Count the old number of window rows, using w_lines[], which
1935 * should still contain the sizes for the lines as they are
1936 * currently displayed. */
1937 for (i = idx; i < wp->w_lines_valid; ++i)
1938 {
1939 /* Only valid lines have a meaningful wl_lnum. Invalid
1940 * lines are part of the changed area. */
1941 if (wp->w_lines[i].wl_valid
1942 && wp->w_lines[i].wl_lnum == mod_bot)
1943 break;
1944 old_rows += wp->w_lines[i].wl_size;
1945#ifdef FEAT_FOLDING
1946 if (wp->w_lines[i].wl_valid
1947 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1948 {
1949 /* Must have found the last valid entry above mod_bot.
1950 * Add following invalid entries. */
1951 ++i;
1952 while (i < wp->w_lines_valid
1953 && !wp->w_lines[i].wl_valid)
1954 old_rows += wp->w_lines[i++].wl_size;
1955 break;
1956 }
1957#endif
1958 }
1959
1960 if (i >= wp->w_lines_valid)
1961 {
1962 /* We can't find a valid line below the changed lines,
1963 * need to redraw until the end of the window.
1964 * Inserting/deleting lines has no use. */
1965 bot_start = 0;
1966 }
1967 else
1968 {
1969 /* Able to count old number of rows: Count new window
1970 * rows, and may insert/delete lines */
1971 j = idx;
1972 for (l = lnum; l < mod_bot; ++l)
1973 {
1974#ifdef FEAT_FOLDING
1975 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1976 ++new_rows;
1977 else
1978#endif
1979#ifdef FEAT_DIFF
1980 if (l == wp->w_topline)
1981 new_rows += plines_win_nofill(wp, l, TRUE)
1982 + wp->w_topfill;
1983 else
1984#endif
1985 new_rows += plines_win(wp, l, TRUE);
1986 ++j;
1987 if (new_rows > wp->w_height - row - 2)
1988 {
1989 /* it's getting too much, must redraw the rest */
1990 new_rows = 9999;
1991 break;
1992 }
1993 }
1994 xtra_rows = new_rows - old_rows;
1995 if (xtra_rows < 0)
1996 {
1997 /* May scroll text up. If there is not enough
1998 * remaining text or scrolling fails, must redraw the
1999 * rest. If scrolling works, must redraw the text
2000 * below the scrolled text. */
2001 if (row - xtra_rows >= wp->w_height - 2)
2002 mod_bot = MAXLNUM;
2003 else
2004 {
2005 check_for_delay(FALSE);
2006 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002007 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 mod_bot = MAXLNUM;
2009 else
2010 bot_start = wp->w_height + xtra_rows;
2011 }
2012 }
2013 else if (xtra_rows > 0)
2014 {
2015 /* May scroll text down. If there is not enough
2016 * remaining text of scrolling fails, must redraw the
2017 * rest. */
2018 if (row + xtra_rows >= wp->w_height - 2)
2019 mod_bot = MAXLNUM;
2020 else
2021 {
2022 check_for_delay(FALSE);
2023 if (win_ins_lines(wp, row + old_rows,
2024 xtra_rows, FALSE, FALSE) == FAIL)
2025 mod_bot = MAXLNUM;
2026 else if (top_end > row + old_rows)
2027 /* Scrolled the part at the top that requires
2028 * updating down. */
2029 top_end += xtra_rows;
2030 }
2031 }
2032
2033 /* When not updating the rest, may need to move w_lines[]
2034 * entries. */
2035 if (mod_bot != MAXLNUM && i != j)
2036 {
2037 if (j < i)
2038 {
2039 int x = row + new_rows;
2040
2041 /* move entries in w_lines[] upwards */
2042 for (;;)
2043 {
2044 /* stop at last valid entry in w_lines[] */
2045 if (i >= wp->w_lines_valid)
2046 {
2047 wp->w_lines_valid = j;
2048 break;
2049 }
2050 wp->w_lines[j] = wp->w_lines[i];
2051 /* stop at a line that won't fit */
2052 if (x + (int)wp->w_lines[j].wl_size
2053 > wp->w_height)
2054 {
2055 wp->w_lines_valid = j + 1;
2056 break;
2057 }
2058 x += wp->w_lines[j++].wl_size;
2059 ++i;
2060 }
2061 if (bot_start > x)
2062 bot_start = x;
2063 }
2064 else /* j > i */
2065 {
2066 /* move entries in w_lines[] downwards */
2067 j -= i;
2068 wp->w_lines_valid += j;
2069 if (wp->w_lines_valid > wp->w_height)
2070 wp->w_lines_valid = wp->w_height;
2071 for (i = wp->w_lines_valid; i - j >= idx; --i)
2072 wp->w_lines[i] = wp->w_lines[i - j];
2073
2074 /* The w_lines[] entries for inserted lines are
2075 * now invalid, but wl_size may be used above.
2076 * Reset to zero. */
2077 while (i >= idx)
2078 {
2079 wp->w_lines[i].wl_size = 0;
2080 wp->w_lines[i--].wl_valid = FALSE;
2081 }
2082 }
2083 }
2084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 }
2086
2087#ifdef FEAT_FOLDING
2088 /*
2089 * When lines are folded, display one line for all of them.
2090 * Otherwise, display normally (can be several display lines when
2091 * 'wrap' is on).
2092 */
2093 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2094 if (fold_count != 0)
2095 {
2096 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2097 ++row;
2098 --fold_count;
2099 wp->w_lines[idx].wl_folded = TRUE;
2100 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2101# ifdef FEAT_SYN_HL
2102 did_update = DID_FOLD;
2103# endif
2104 }
2105 else
2106#endif
2107 if (idx < wp->w_lines_valid
2108 && wp->w_lines[idx].wl_valid
2109 && wp->w_lines[idx].wl_lnum == lnum
2110 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002111 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 && srow + wp->w_lines[idx].wl_size > wp->w_height
2113#ifdef FEAT_DIFF
2114 && diff_check_fill(wp, lnum) == 0
2115#endif
2116 )
2117 {
2118 /* This line is not going to fit. Don't draw anything here,
2119 * will draw "@ " lines below. */
2120 row = wp->w_height + 1;
2121 }
2122 else
2123 {
2124#ifdef FEAT_SEARCH_EXTRA
2125 prepare_search_hl(wp, lnum);
2126#endif
2127#ifdef FEAT_SYN_HL
2128 /* Let the syntax stuff know we skipped a few lines. */
2129 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002130 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 syntax_end_parsing(syntax_last_parsed + 1);
2132#endif
2133
2134 /*
2135 * Display one line.
2136 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002137 row = win_line(wp, lnum, srow, wp->w_height,
2138 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139
2140#ifdef FEAT_FOLDING
2141 wp->w_lines[idx].wl_folded = FALSE;
2142 wp->w_lines[idx].wl_lastlnum = lnum;
2143#endif
2144#ifdef FEAT_SYN_HL
2145 did_update = DID_LINE;
2146 syntax_last_parsed = lnum;
2147#endif
2148 }
2149
2150 wp->w_lines[idx].wl_lnum = lnum;
2151 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002152
2153 /* Past end of the window or end of the screen. Note that after
2154 * resizing wp->w_height may be end up too big. That's a problem
2155 * elsewhere, but prevent a crash here. */
2156 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
2158 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002159 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2161 ++idx;
2162 break;
2163 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002164 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 wp->w_lines[idx].wl_size = row - srow;
2166 ++idx;
2167#ifdef FEAT_FOLDING
2168 lnum += fold_count + 1;
2169#else
2170 ++lnum;
2171#endif
2172 }
2173 else
2174 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002175 if (wp->w_p_rnu)
2176 {
2177 // 'relativenumber' set: The text doesn't need to be drawn, but
2178 // the number column nearly always does.
2179 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
2180 }
2181
2182 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 row += wp->w_lines[idx++].wl_size;
2184 if (row > wp->w_height) /* past end of screen */
2185 break;
2186#ifdef FEAT_FOLDING
2187 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2188#else
2189 ++lnum;
2190#endif
2191#ifdef FEAT_SYN_HL
2192 did_update = DID_NONE;
2193#endif
2194 }
2195
2196 if (lnum > buf->b_ml.ml_line_count)
2197 {
2198 eof = TRUE;
2199 break;
2200 }
2201 }
2202 /*
2203 * End of loop over all window lines.
2204 */
2205
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002206#ifdef FEAT_VTP
2207 /* Rewrite the character at the end of the screen line. */
2208 if (use_vtp())
2209 {
2210 int i;
2211
2212 for (i = 0; i < Rows; ++i)
2213# ifdef FEAT_MBYTE
2214 if (enc_utf8)
2215 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2216 LineOffset[i] + screen_Columns) > 1)
2217 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2218 else
2219 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2220 else
2221# endif
2222 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2223 }
2224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
2226 if (idx > wp->w_lines_valid)
2227 wp->w_lines_valid = idx;
2228
2229#ifdef FEAT_SYN_HL
2230 /*
2231 * Let the syntax stuff know we stop parsing here.
2232 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002233 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 syntax_end_parsing(syntax_last_parsed + 1);
2235#endif
2236
2237 /*
2238 * If we didn't hit the end of the file, and we didn't finish the last
2239 * line we were working on, then the line didn't fit.
2240 */
2241 wp->w_empty_rows = 0;
2242#ifdef FEAT_DIFF
2243 wp->w_filler_rows = 0;
2244#endif
2245 if (!eof && !didline)
2246 {
2247 if (lnum == wp->w_topline)
2248 {
2249 /*
2250 * Single line that does not fit!
2251 * Don't overwrite it, it can be edited.
2252 */
2253 wp->w_botline = lnum + 1;
2254 }
2255#ifdef FEAT_DIFF
2256 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2257 {
2258 /* Window ends in filler lines. */
2259 wp->w_botline = lnum;
2260 wp->w_filler_rows = wp->w_height - srow;
2261 }
2262#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002263 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2264 {
2265 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2266
2267 /*
2268 * Last line isn't finished: Display "@@@" in the last screen line.
2269 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002270 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002271 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002272 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002273 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002274 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002275 set_empty_rows(wp, srow);
2276 wp->w_botline = lnum;
2277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2279 {
2280 /*
2281 * Last line isn't finished: Display "@@@" at the end.
2282 */
2283 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2284 W_WINROW(wp) + wp->w_height,
2285 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002286 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 set_empty_rows(wp, srow);
2288 wp->w_botline = lnum;
2289 }
2290 else
2291 {
2292 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2293 wp->w_botline = lnum;
2294 }
2295 }
2296 else
2297 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 if (eof) /* we hit the end of the file */
2300 {
2301 wp->w_botline = buf->b_ml.ml_line_count + 1;
2302#ifdef FEAT_DIFF
2303 j = diff_check_fill(wp, wp->w_botline);
2304 if (j > 0 && !wp->w_botfill)
2305 {
2306 /*
2307 * Display filler lines at the end of the file
2308 */
2309 if (char2cells(fill_diff) > 1)
2310 i = '-';
2311 else
2312 i = fill_diff;
2313 if (row + j > wp->w_height)
2314 j = wp->w_height - row;
2315 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2316 row += j;
2317 }
2318#endif
2319 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002320 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 wp->w_botline = lnum;
2322
2323 /* make sure the rest of the screen is blank */
2324 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002325 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
2327
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002328#ifdef SYN_TIME_LIMIT
2329 syn_set_timeout(NULL);
2330#endif
2331
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 /* Reset the type of redrawing required, the window has been updated. */
2333 wp->w_redr_type = 0;
2334#ifdef FEAT_DIFF
2335 wp->w_old_topfill = wp->w_topfill;
2336 wp->w_old_botfill = wp->w_botfill;
2337#endif
2338
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002339 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 {
2341 /*
2342 * There is a trick with w_botline. If we invalidate it on each
2343 * change that might modify it, this will cause a lot of expensive
2344 * calls to plines() in update_topline() each time. Therefore the
2345 * value of w_botline is often approximated, and this value is used to
2346 * compute the value of w_topline. If the value of w_botline was
2347 * wrong, check that the value of w_topline is correct (cursor is on
2348 * the visible part of the text). If it's not, we need to redraw
2349 * again. Mostly this just means scrolling up a few lines, so it
2350 * doesn't look too bad. Only do this for the current window (where
2351 * changes are relevant).
2352 */
2353 wp->w_valid |= VALID_BOTLINE;
2354 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2355 {
2356 recursive = TRUE;
2357 curwin->w_valid &= ~VALID_TOPLINE;
2358 update_topline(); /* may invalidate w_botline again */
2359 if (must_redraw != 0)
2360 {
2361 /* Don't update for changes in buffer again. */
2362 i = curbuf->b_mod_set;
2363 curbuf->b_mod_set = FALSE;
2364 win_update(curwin);
2365 must_redraw = 0;
2366 curbuf->b_mod_set = i;
2367 }
2368 recursive = FALSE;
2369 }
2370 }
2371
2372#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2373 /* restore got_int, unless CTRL-C was hit while redrawing */
2374 if (!got_int)
2375 got_int = save_got_int;
2376#endif
2377}
2378
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379/*
2380 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2381 * as the filler character.
2382 */
2383 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002384win_draw_end(
2385 win_T *wp,
2386 int c1,
2387 int c2,
2388 int row,
2389 int endrow,
2390 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391{
2392#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2393 int n = 0;
2394# define FDC_OFF n
2395#else
2396# define FDC_OFF 0
2397#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002398#ifdef FEAT_FOLDING
2399 int fdc = compute_foldcolumn(wp, 0);
2400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401
2402#ifdef FEAT_RIGHTLEFT
2403 if (wp->w_p_rl)
2404 {
2405 /* No check for cmdline window: should never be right-left. */
2406# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002407 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409 if (n > 0)
2410 {
2411 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002412 if (n > wp->w_width)
2413 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2415 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002416 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 }
2418# endif
2419# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002420 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
2422 int nn = n + 2;
2423
2424 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002425 if (nn > wp->w_width)
2426 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2428 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002429 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 n = nn;
2431 }
2432# endif
2433 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002434 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002435 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2437 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002438 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 }
2440 else
2441#endif
2442 {
2443#ifdef FEAT_CMDWIN
2444 if (cmdwin_type != 0 && wp == curwin)
2445 {
2446 /* draw the cmdline character in the leftmost column */
2447 n = 1;
2448 if (n > wp->w_width)
2449 n = wp->w_width;
2450 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002451 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002452 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 }
2454#endif
2455#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002456 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002458 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
2460 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002461 if (nn > wp->w_width)
2462 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002464 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002465 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 n = nn;
2467 }
2468#endif
2469#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002470 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 {
2472 int nn = n + 2;
2473
2474 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002475 if (nn > wp->w_width)
2476 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002478 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002479 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 n = nn;
2481 }
2482#endif
2483 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002484 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002485 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 }
2487 set_empty_rows(wp, row);
2488}
2489
Bram Moolenaar1a384422010-07-14 19:53:30 +02002490#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002491/*
2492 * Advance **color_cols and return TRUE when there are columns to draw.
2493 */
2494 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002495advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002496{
2497 while (**color_cols >= 0 && vcol > **color_cols)
2498 ++*color_cols;
2499 return (**color_cols >= 0);
2500}
2501#endif
2502
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002503#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2504/*
2505 * Copy "text" to ScreenLines using "attr".
2506 * Returns the next screen column.
2507 */
2508 static int
2509text_to_screenline(win_T *wp, char_u *text, int col)
2510{
2511 int off = (int)(current_ScreenLine - ScreenLines);
2512
2513#ifdef FEAT_MBYTE
2514 if (has_mbyte)
2515 {
2516 int cells;
2517 int u8c, u8cc[MAX_MCO];
2518 int i;
2519 int idx;
2520 int c_len;
2521 char_u *p;
2522# ifdef FEAT_ARABIC
2523 int prev_c = 0; /* previous Arabic character */
2524 int prev_c1 = 0; /* first composing char for prev_c */
2525# endif
2526
2527# ifdef FEAT_RIGHTLEFT
2528 if (wp->w_p_rl)
2529 idx = off;
2530 else
2531# endif
2532 idx = off + col;
2533
2534 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2535 for (p = text; *p != NUL; )
2536 {
2537 cells = (*mb_ptr2cells)(p);
2538 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002539 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002540# ifdef FEAT_RIGHTLEFT
2541 - (wp->w_p_rl ? col : 0)
2542# endif
2543 )
2544 break;
2545 ScreenLines[idx] = *p;
2546 if (enc_utf8)
2547 {
2548 u8c = utfc_ptr2char(p, u8cc);
2549 if (*p < 0x80 && u8cc[0] == 0)
2550 {
2551 ScreenLinesUC[idx] = 0;
2552#ifdef FEAT_ARABIC
2553 prev_c = u8c;
2554#endif
2555 }
2556 else
2557 {
2558#ifdef FEAT_ARABIC
2559 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2560 {
2561 /* Do Arabic shaping. */
2562 int pc, pc1, nc;
2563 int pcc[MAX_MCO];
2564 int firstbyte = *p;
2565
2566 /* The idea of what is the previous and next
2567 * character depends on 'rightleft'. */
2568 if (wp->w_p_rl)
2569 {
2570 pc = prev_c;
2571 pc1 = prev_c1;
2572 nc = utf_ptr2char(p + c_len);
2573 prev_c1 = u8cc[0];
2574 }
2575 else
2576 {
2577 pc = utfc_ptr2char(p + c_len, pcc);
2578 nc = prev_c;
2579 pc1 = pcc[0];
2580 }
2581 prev_c = u8c;
2582
2583 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2584 pc, pc1, nc);
2585 ScreenLines[idx] = firstbyte;
2586 }
2587 else
2588 prev_c = u8c;
2589#endif
2590 /* Non-BMP character: display as ? or fullwidth ?. */
2591#ifdef UNICODE16
2592 if (u8c >= 0x10000)
2593 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2594 else
2595#endif
2596 ScreenLinesUC[idx] = u8c;
2597 for (i = 0; i < Screen_mco; ++i)
2598 {
2599 ScreenLinesC[i][idx] = u8cc[i];
2600 if (u8cc[i] == 0)
2601 break;
2602 }
2603 }
2604 if (cells > 1)
2605 ScreenLines[idx + 1] = 0;
2606 }
2607 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2608 /* double-byte single width character */
2609 ScreenLines2[idx] = p[1];
2610 else if (cells > 1)
2611 /* double-width character */
2612 ScreenLines[idx + 1] = p[1];
2613 col += cells;
2614 idx += cells;
2615 p += c_len;
2616 }
2617 }
2618 else
2619#endif
2620 {
2621 int len = (int)STRLEN(text);
2622
Bram Moolenaar02631462017-09-22 15:20:32 +02002623 if (len > wp->w_width - col)
2624 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002625 if (len > 0)
2626 {
2627#ifdef FEAT_RIGHTLEFT
2628 if (wp->w_p_rl)
2629 STRNCPY(current_ScreenLine, text, len);
2630 else
2631#endif
2632 STRNCPY(current_ScreenLine + col, text, len);
2633 col += len;
2634 }
2635 }
2636 return col;
2637}
2638#endif
2639
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640#ifdef FEAT_FOLDING
2641/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002642 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2643 * space is available for window "wp", minus "col".
2644 */
2645 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002646compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002647{
2648 int fdc = wp->w_p_fdc;
2649 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002650 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002651
2652 if (fdc > wwidth - (col + wmw))
2653 fdc = wwidth - (col + wmw);
2654 return fdc;
2655}
2656
2657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 * Display one folded line.
2659 */
2660 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002661fold_line(
2662 win_T *wp,
2663 long fold_count,
2664 foldinfo_T *foldinfo,
2665 linenr_T lnum,
2666 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002668 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 pos_T *top, *bot;
2670 linenr_T lnume = lnum + fold_count - 1;
2671 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002672 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 int col;
2675 int txtcol;
2676 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002677 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
2679 /* Build the fold line:
2680 * 1. Add the cmdwin_type for the command-line window
2681 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002682 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 * 4. Compose the text
2684 * 5. Add the text
2685 * 6. set highlighting for the Visual area an other text
2686 */
2687 col = 0;
2688
2689 /*
2690 * 1. Add the cmdwin_type for the command-line window
2691 * Ignores 'rightleft', this window is never right-left.
2692 */
2693#ifdef FEAT_CMDWIN
2694 if (cmdwin_type != 0 && wp == curwin)
2695 {
2696 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002697 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698#ifdef FEAT_MBYTE
2699 if (enc_utf8)
2700 ScreenLinesUC[off] = 0;
2701#endif
2702 ++col;
2703 }
2704#endif
2705
2706 /*
2707 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002708 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002710 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 if (fdc > 0)
2712 {
2713 fill_foldcolumn(buf, wp, TRUE, lnum);
2714#ifdef FEAT_RIGHTLEFT
2715 if (wp->w_p_rl)
2716 {
2717 int i;
2718
Bram Moolenaar02631462017-09-22 15:20:32 +02002719 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002720 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 /* reverse the fold column */
2722 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002723 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725 else
2726#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002727 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 col += fdc;
2729 }
2730
2731#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002732# define RL_MEMSET(p, v, l) \
2733 do { \
2734 if (wp->w_p_rl) \
2735 for (ri = 0; ri < l; ++ri) \
2736 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2737 else \
2738 for (ri = 0; ri < l; ++ri) \
2739 ScreenAttrs[off + (p) + ri] = v; \
2740 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002742# define RL_MEMSET(p, v, l) \
2743 do { \
2744 for (ri = 0; ri < l; ++ri) \
2745 ScreenAttrs[off + (p) + ri] = v; \
2746 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747#endif
2748
Bram Moolenaar64486672010-05-16 15:46:46 +02002749 /* Set all attributes of the 'number' or 'relativenumber' column and the
2750 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002751 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
2753#ifdef FEAT_SIGNS
2754 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002755 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002757 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 if (len > 0)
2759 {
2760 if (len > 2)
2761 len = 2;
2762# ifdef FEAT_RIGHTLEFT
2763 if (wp->w_p_rl)
2764 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002765 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002766 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 else
2768# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002769 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 col += len;
2771 }
2772 }
2773#endif
2774
2775 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002776 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002778 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002780 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 if (len > 0)
2782 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002783 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002784 long num;
2785 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002786
2787 if (len > w + 1)
2788 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002789
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002790 if (wp->w_p_nu && !wp->w_p_rnu)
2791 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002792 num = (long)lnum;
2793 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002794 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002795 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002796 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002797 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002798 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002799 /* 'number' + 'relativenumber': cursor line shows absolute
2800 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002801 num = lnum;
2802 fmt = "%-*ld ";
2803 }
2804 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002805
Bram Moolenaar700e7342013-01-30 12:31:36 +01002806 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807#ifdef FEAT_RIGHTLEFT
2808 if (wp->w_p_rl)
2809 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002810 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002811 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 else
2813#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002814 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 col += len;
2816 }
2817 }
2818
2819 /*
2820 * 4. Compose the folded-line string with 'foldtext', if set.
2821 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002822 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823
2824 txtcol = col; /* remember where text starts */
2825
2826 /*
2827 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2828 * Right-left text is put in columns 0 - number-col, normal text is put
2829 * in columns number-col - window-width.
2830 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002831 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
2833 /* Fill the rest of the line with the fold filler */
2834#ifdef FEAT_RIGHTLEFT
2835 if (wp->w_p_rl)
2836 col -= txtcol;
2837#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002838 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839#ifdef FEAT_RIGHTLEFT
2840 - (wp->w_p_rl ? txtcol : 0)
2841#endif
2842 )
2843 {
2844#ifdef FEAT_MBYTE
2845 if (enc_utf8)
2846 {
2847 if (fill_fold >= 0x80)
2848 {
2849 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002850 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002851 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 }
2853 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002854 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002856 ScreenLines[off + col] = fill_fold;
2857 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002858 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002860 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002862 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
2864
2865 if (text != buf)
2866 vim_free(text);
2867
2868 /*
2869 * 6. set highlighting for the Visual area an other text.
2870 * If all folded lines are in the Visual area, highlight the line.
2871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2873 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002874 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 {
2876 /* Visual is after curwin->w_cursor */
2877 top = &curwin->w_cursor;
2878 bot = &VIsual;
2879 }
2880 else
2881 {
2882 /* Visual is before curwin->w_cursor */
2883 top = &VIsual;
2884 bot = &curwin->w_cursor;
2885 }
2886 if (lnum >= top->lnum
2887 && lnume <= bot->lnum
2888 && (VIsual_mode != 'v'
2889 || ((lnum > top->lnum
2890 || (lnum == top->lnum
2891 && top->col == 0))
2892 && (lnume < bot->lnum
2893 || (lnume == bot->lnum
2894 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002895 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 {
2897 if (VIsual_mode == Ctrl_V)
2898 {
2899 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002900 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002902 if (wp->w_old_cursor_lcol != MAXCOL
2903 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002904 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 len = wp->w_old_cursor_lcol;
2906 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002907 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002908 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002909 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 }
2911 }
2912 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002915 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
2918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002920#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002921 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2922 if (wp->w_p_cc_cols)
2923 {
2924 int i = 0;
2925 int j = wp->w_p_cc_cols[i];
2926 int old_txtcol = txtcol;
2927
2928 while (j > -1)
2929 {
2930 txtcol += j;
2931 if (wp->w_p_wrap)
2932 txtcol -= wp->w_skipcol;
2933 else
2934 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002935 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002936 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002937 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002938 txtcol = old_txtcol;
2939 j = wp->w_p_cc_cols[++i];
2940 }
2941 }
2942
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002943 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002944 if (wp->w_p_cuc)
2945 {
2946 txtcol += wp->w_virtcol;
2947 if (wp->w_p_wrap)
2948 txtcol -= wp->w_skipcol;
2949 else
2950 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002951 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002952 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002953 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002954 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
Bram Moolenaar02631462017-09-22 15:20:32 +02002957 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2958 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
2960 /*
2961 * Update w_cline_height and w_cline_folded if the cursor line was
2962 * updated (saves a call to plines() later).
2963 */
2964 if (wp == curwin
2965 && lnum <= curwin->w_cursor.lnum
2966 && lnume >= curwin->w_cursor.lnum)
2967 {
2968 curwin->w_cline_row = row;
2969 curwin->w_cline_height = 1;
2970 curwin->w_cline_folded = TRUE;
2971 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2972 }
2973}
2974
2975/*
2976 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2977 */
2978 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002979copy_text_attr(
2980 int off,
2981 char_u *buf,
2982 int len,
2983 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002985 int i;
2986
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 mch_memmove(ScreenLines + off, buf, (size_t)len);
2988# ifdef FEAT_MBYTE
2989 if (enc_utf8)
2990 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2991# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002992 for (i = 0; i < len; ++i)
2993 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994}
2995
2996/*
2997 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002998 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 */
3000 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003001fill_foldcolumn(
3002 char_u *p,
3003 win_T *wp,
3004 int closed, /* TRUE of FALSE */
3005 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006{
3007 int i = 0;
3008 int level;
3009 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003010 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003011 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012
3013 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003014 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015
3016 level = win_foldinfo.fi_level;
3017 if (level > 0)
3018 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003019 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003020 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003021
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 /* If the column is too narrow, we start at the lowest level that
3023 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003024 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 if (first_level < 1)
3026 first_level = 1;
3027
Bram Moolenaar1c934292015-01-27 16:39:29 +01003028 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 {
3030 if (win_foldinfo.fi_lnum == lnum
3031 && first_level + i >= win_foldinfo.fi_low_level)
3032 p[i] = '-';
3033 else if (first_level == 1)
3034 p[i] = '|';
3035 else if (first_level + i <= 9)
3036 p[i] = '0' + first_level + i;
3037 else
3038 p[i] = '>';
3039 if (first_level + i == level)
3040 break;
3041 }
3042 }
3043 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003044 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045}
3046#endif /* FEAT_FOLDING */
3047
3048/*
3049 * Display line "lnum" of window 'wp' on the screen.
3050 * Start at row "startrow", stop when "endrow" is reached.
3051 * wp->w_virtcol needs to be valid.
3052 *
3053 * Return the number of last row the line occupies.
3054 */
3055 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003056win_line(
3057 win_T *wp,
3058 linenr_T lnum,
3059 int startrow,
3060 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003061 int nochange UNUSED, // not updating for changed text
3062 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003064 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3066 int c = 0; /* init for GCC */
3067 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003068#ifdef FEAT_LINEBREAK
3069 long vcol_sbr = -1; /* virtual column after showbreak */
3070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 long vcol_prev = -1; /* "vcol" of previous character */
3072 char_u *line; /* current line */
3073 char_u *ptr; /* current position in "line" */
3074 int row; /* row in the window, excl w_winrow */
3075 int screen_row; /* row on the screen, incl w_winrow */
3076
3077 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3078 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003079 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003080 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 int c_extra = NUL; /* extra chars, all the same */
3082 int extra_attr = 0; /* attributes when n_extra != 0 */
3083 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3084 displaying lcs_eol at end-of-line */
3085 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3086 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3087
3088 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3089 int saved_n_extra = 0;
3090 char_u *saved_p_extra = NULL;
3091 int saved_c_extra = 0;
3092 int saved_char_attr = 0;
3093
3094 int n_attr = 0; /* chars with special attr */
3095 int saved_attr2 = 0; /* char_attr saved for n_attr */
3096 int n_attr3 = 0; /* chars with overruling special attr */
3097 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3098
3099 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3100
3101 int fromcol, tocol; /* start/end of inverting */
3102 int fromcol_prev = -2; /* start of inverting after cursor */
3103 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003105 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 pos_T pos;
3107 long v;
3108
3109 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003110 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3112 in this line */
3113 int attr = 0; /* attributes for area highlighting */
3114 int area_attr = 0; /* attributes desired by highlighting */
3115 int search_attr = 0; /* attributes desired by 'hlsearch' */
3116#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003117 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 int syntax_attr = 0; /* attributes desired by syntax */
3119 int has_syntax = FALSE; /* this buffer has syntax highl. */
3120 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003121 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003122 int draw_color_col = FALSE; /* highlight colorcolumn */
3123 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003124#endif
3125#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003126 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003127# define SPWORDLEN 150
3128 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003129 int nextlinecol = 0; /* column where nextline[] starts */
3130 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003131 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003132 int spell_attr = 0; /* attributes desired by spelling */
3133 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003134 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3135 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003136 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003137 static int cap_col = -1; /* column to check for Cap word */
3138 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003139 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140#endif
Bram Moolenaarc7875392018-09-13 14:57:41 +02003141 int extra_check = 0; // has syntax or linebreak
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142#ifdef FEAT_MBYTE
3143 int multi_attr = 0; /* attributes desired by multibyte */
3144 int mb_l = 1; /* multi-byte byte length */
3145 int mb_c = 0; /* decoded multi-byte character */
3146 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003147 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148#endif
3149#ifdef FEAT_DIFF
3150 int filler_lines; /* nr of filler lines to be drawn */
3151 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003152 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 int change_start = MAXCOL; /* first col of changed area */
3154 int change_end = -1; /* last col of changed area */
3155#endif
3156 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3157#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003158 int need_showbreak = FALSE; /* overlong line, skipping first x
3159 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003161#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003162 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003164 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165#endif
3166#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003167 matchitem_T *cur; /* points to the match list */
3168 match_T *shl; /* points to search_hl or a match */
3169 int shl_flag; /* flag to indicate whether search_hl
3170 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003171 int pos_inprogress; /* marks that position match search is
3172 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003173 int prevcol_hl_flag; /* flag to indicate whether prevcol
3174 equals startcol of search_hl or one
3175 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176#endif
3177#ifdef FEAT_ARABIC
3178 int prev_c = 0; /* previous Arabic character */
3179 int prev_c1 = 0; /* first composing char for prev_c */
3180#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003181#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003182 int did_line_attr = 0;
3183#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003184#ifdef FEAT_TERMINAL
3185 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003186 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189 /* draw_state: items that are drawn in sequence: */
3190#define WL_START 0 /* nothing done yet */
3191#ifdef FEAT_CMDWIN
3192# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3193#else
3194# define WL_CMDLINE WL_START
3195#endif
3196#ifdef FEAT_FOLDING
3197# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3198#else
3199# define WL_FOLD WL_CMDLINE
3200#endif
3201#ifdef FEAT_SIGNS
3202# define WL_SIGN WL_FOLD + 1 /* column for signs */
3203#else
3204# define WL_SIGN WL_FOLD /* column for signs */
3205#endif
3206#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003207#ifdef FEAT_LINEBREAK
3208# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003210# define WL_BRI WL_NR
3211#endif
3212#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3213# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3214#else
3215# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216#endif
3217#define WL_LINE WL_SBR + 1 /* text in the line */
3218 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003219#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 int feedback_col = 0;
3221 int feedback_old_attr = -1;
3222#endif
3223
Bram Moolenaar860cae12010-06-05 23:22:07 +02003224#ifdef FEAT_CONCEAL
3225 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003226 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003227 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003228 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003229 int is_concealing = FALSE;
3230 int boguscols = 0; /* nonexistent columns added to force
3231 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003232 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003233 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003234 int match_conc = 0; /* cchar for match functions */
3235 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003236 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003237# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003238# define FIX_FOR_BOGUSCOLS \
3239 { \
3240 n_extra += vcol_off; \
3241 vcol -= vcol_off; \
3242 vcol_off = 0; \
3243 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003244 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003245 boguscols = 0; \
3246 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003247#else
3248# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250
3251 if (startrow > endrow) /* past the end already! */
3252 return startrow;
3253
3254 row = startrow;
3255 screen_row = row + W_WINROW(wp);
3256
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003257 if (!number_only)
3258 {
3259 /*
3260 * To speed up the loop below, set extra_check when there is linebreak,
3261 * trailing white space and/or syntax processing to be done.
3262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003264 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265#endif
3266#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003267 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003268# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003269 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003270# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003271 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003273 /* Prepare for syntax highlighting in this line. When there is an
3274 * error, stop syntax highlighting. */
3275 save_did_emsg = did_emsg;
3276 did_emsg = FALSE;
3277 syntax_start(wp, lnum);
3278 if (did_emsg)
3279 wp->w_s->b_syn_error = TRUE;
3280 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003281 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003282 did_emsg = save_did_emsg;
3283#ifdef SYN_TIME_LIMIT
3284 if (!wp->w_s->b_syn_slow)
3285#endif
3286 {
3287 has_syntax = TRUE;
3288 extra_check = TRUE;
3289 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003292
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003293 /* Check for columns to display for 'colorcolumn'. */
3294 color_cols = wp->w_p_cc_cols;
3295 if (color_cols != NULL)
3296 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003297#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003298
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003299#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003300 if (term_show_buffer(wp->w_buffer))
3301 {
3302 extra_check = TRUE;
3303 get_term_attr = TRUE;
3304 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3305 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003306#endif
3307
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003308#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003309 if (wp->w_p_spell
3310 && *wp->w_s->b_p_spl != NUL
3311 && wp->w_s->b_langp.ga_len > 0
3312 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003313 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003314 /* Prepare for spell checking. */
3315 has_spell = TRUE;
3316 extra_check = TRUE;
3317
3318 /* Get the start of the next line, so that words that wrap to the next
3319 * line are found too: "et<line-break>al.".
3320 * Trick: skip a few chars for C/shell/Vim comments */
3321 nextline[SPWORDLEN] = NUL;
3322 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3323 {
3324 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3325 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3326 }
3327
3328 /* When a word wrapped from the previous line the start of the current
3329 * line is valid. */
3330 if (lnum == checked_lnum)
3331 cur_checked_col = checked_col;
3332 checked_lnum = 0;
3333
3334 /* When there was a sentence end in the previous line may require a
3335 * word starting with capital in this line. In line 1 always check
3336 * the first word. */
3337 if (lnum != capcol_lnum)
3338 cap_col = -1;
3339 if (lnum == 1)
3340 cap_col = 0;
3341 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343#endif
3344
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003345 /*
3346 * handle visual active in this window
3347 */
3348 fromcol = -10;
3349 tocol = MAXCOL;
3350 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003352 /* Visual is after curwin->w_cursor */
3353 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003355 top = &curwin->w_cursor;
3356 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003358 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003360 top = &VIsual;
3361 bot = &curwin->w_cursor;
3362 }
3363 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3364 if (VIsual_mode == Ctrl_V) /* block mode */
3365 {
3366 if (lnum_in_visual_area)
3367 {
3368 fromcol = wp->w_old_cursor_fcol;
3369 tocol = wp->w_old_cursor_lcol;
3370 }
3371 }
3372 else /* non-block mode */
3373 {
3374 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003376 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003378 if (VIsual_mode == 'V') /* linewise */
3379 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 else
3381 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003382 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3383 if (gchar_pos(top) == NUL)
3384 tocol = fromcol + 1;
3385 }
3386 }
3387 if (VIsual_mode != 'V' && lnum == bot->lnum)
3388 {
3389 if (*p_sel == 'e' && bot->col == 0
3390#ifdef FEAT_VIRTUALEDIT
3391 && bot->coladd == 0
3392#endif
3393 )
3394 {
3395 fromcol = -10;
3396 tocol = MAXCOL;
3397 }
3398 else if (bot->col == MAXCOL)
3399 tocol = MAXCOL;
3400 else
3401 {
3402 pos = *bot;
3403 if (*p_sel == 'e')
3404 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3405 else
3406 {
3407 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3408 ++tocol;
3409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 }
3411 }
3412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003414 /* Check if the character under the cursor should not be inverted */
3415 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003416#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003417 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003418#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003419 )
3420 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003422 /* if inverting in this line set area_highlighting */
3423 if (fromcol >= 0)
3424 {
3425 area_highlighting = TRUE;
3426 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003428 if ((clip_star.available && !clip_star.owned
3429 && clip_isautosel_star())
3430 || (clip_plus.available && !clip_plus.owned
3431 && clip_isautosel_plus()))
3432 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003437 /*
3438 * handle 'incsearch' and ":s///c" highlighting
3439 */
3440 else if (highlight_match
3441 && wp == curwin
3442 && lnum >= curwin->w_cursor.lnum
3443 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003445 if (lnum == curwin->w_cursor.lnum)
3446 getvcol(curwin, &(curwin->w_cursor),
3447 (colnr_T *)&fromcol, NULL, NULL);
3448 else
3449 fromcol = 0;
3450 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3451 {
3452 pos.lnum = lnum;
3453 pos.col = search_match_endcol;
3454 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3455 }
3456 else
3457 tocol = MAXCOL;
3458 /* do at least one character; happens when past end of line */
3459 if (fromcol == tocol)
3460 tocol = fromcol + 1;
3461 area_highlighting = TRUE;
3462 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 }
3465
3466#ifdef FEAT_DIFF
3467 filler_lines = diff_check(wp, lnum);
3468 if (filler_lines < 0)
3469 {
3470 if (filler_lines == -1)
3471 {
3472 if (diff_find_change(wp, lnum, &change_start, &change_end))
3473 diff_hlf = HLF_ADD; /* added line */
3474 else if (change_start == 0)
3475 diff_hlf = HLF_TXD; /* changed text */
3476 else
3477 diff_hlf = HLF_CHD; /* changed line */
3478 }
3479 else
3480 diff_hlf = HLF_ADD; /* added line */
3481 filler_lines = 0;
3482 area_highlighting = TRUE;
3483 }
3484 if (lnum == wp->w_topline)
3485 filler_lines = wp->w_topfill;
3486 filler_todo = filler_lines;
3487#endif
3488
3489#ifdef LINE_ATTR
3490# ifdef FEAT_SIGNS
3491 /* If this line has a sign with line highlighting set line_attr. */
3492 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3493 if (v != 0)
3494 line_attr = sign_get_attr((int)v, TRUE);
3495# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003496# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003498 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003499 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500# endif
3501 if (line_attr != 0)
3502 area_highlighting = TRUE;
3503#endif
3504
3505 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3506 ptr = line;
3507
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003508#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003509 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003510 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003511 /* For checking first word with a capital skip white space. */
3512 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003513 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003514
Bram Moolenaar30abd282005-06-22 22:35:10 +00003515 /* To be able to spell-check over line boundaries copy the end of the
3516 * current line into nextline[]. Above the start of the next line was
3517 * copied to nextline[SPWORDLEN]. */
3518 if (nextline[SPWORDLEN] == NUL)
3519 {
3520 /* No next line or it is empty. */
3521 nextlinecol = MAXCOL;
3522 nextline_idx = 0;
3523 }
3524 else
3525 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003526 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003527 if (v < SPWORDLEN)
3528 {
3529 /* Short line, use it completely and append the start of the
3530 * next line. */
3531 nextlinecol = 0;
3532 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003533 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003534 nextline_idx = v + 1;
3535 }
3536 else
3537 {
3538 /* Long line, use only the last SPWORDLEN bytes. */
3539 nextlinecol = v - SPWORDLEN;
3540 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3541 nextline_idx = SPWORDLEN + 1;
3542 }
3543 }
3544 }
3545#endif
3546
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003547 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003549 if (lcs_space || lcs_trail)
3550 extra_check = TRUE;
3551 /* find start of trailing whitespace */
3552 if (lcs_trail)
3553 {
3554 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003555 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003556 --trailcol;
3557 trailcol += (colnr_T) (ptr - line);
3558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
3560
3561 /*
3562 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3563 * first character to be displayed.
3564 */
3565 if (wp->w_p_wrap)
3566 v = wp->w_skipcol;
3567 else
3568 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003569 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
3571#ifdef FEAT_MBYTE
3572 char_u *prev_ptr = ptr;
3573#endif
3574 while (vcol < v && *ptr != NUL)
3575 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003576 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 vcol += c;
3578#ifdef FEAT_MBYTE
3579 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003581 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 }
3583
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003584 /* When:
3585 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003586 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003587 * - 'virtualedit' is set, or
3588 * - the visual mode is active,
3589 * the end of the line may be before the start of the displayed part.
3590 */
3591 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003592#ifdef FEAT_SYN_HL
3593 wp->w_p_cuc || draw_color_col ||
3594#endif
3595#ifdef FEAT_VIRTUALEDIT
3596 virtual_active() ||
3597#endif
3598 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003599 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602
3603 /* Handle a character that's not completely on the screen: Put ptr at
3604 * that character but skip the first few screen characters. */
3605 if (vcol > v)
3606 {
3607 vcol -= c;
3608#ifdef FEAT_MBYTE
3609 ptr = prev_ptr;
3610#else
3611 --ptr;
3612#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003613 /* If the character fits on the screen, don't need to skip it.
3614 * Except for a TAB. */
3615 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003616#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003617 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003618#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003619 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003620 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
3622
3623 /*
3624 * Adjust for when the inverted text is before the screen,
3625 * and when the start of the inverted text is before the screen.
3626 */
3627 if (tocol <= vcol)
3628 fromcol = 0;
3629 else if (fromcol >= 0 && fromcol < vcol)
3630 fromcol = vcol;
3631
3632#ifdef FEAT_LINEBREAK
3633 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3634 if (wp->w_p_wrap)
3635 need_showbreak = TRUE;
3636#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003637#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003638 /* When spell checking a word we need to figure out the start of the
3639 * word and if it's badly spelled or not. */
3640 if (has_spell)
3641 {
3642 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003643 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003644 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003645
3646 pos = wp->w_cursor;
3647 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003648 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003649 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003650
3651 /* spell_move_to() may call ml_get() and make "line" invalid */
3652 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3653 ptr = line + linecol;
3654
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003655 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003656 {
3657 /* no bad word found at line start, don't check until end of a
3658 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003659 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003660 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003661 }
3662 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003663 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003664 /* bad word found, use attributes until end of word */
3665 word_end = wp->w_cursor.col + len + 1;
3666
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003667 /* Turn index into actual attributes. */
3668 if (spell_hlf != HLF_COUNT)
3669 spell_attr = highlight_attr[spell_hlf];
3670 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003671 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003672
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003673# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003674 /* Need to restart syntax highlighting for this line. */
3675 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003676 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003677# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003678 }
3679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681
3682 /*
3683 * Correct highlighting for cursor that can't be disabled.
3684 * Avoids having to check this for each character.
3685 */
3686 if (fromcol >= 0)
3687 {
3688 if (noinvcur)
3689 {
3690 if ((colnr_T)fromcol == wp->w_virtcol)
3691 {
3692 /* highlighting starts at cursor, let it start just after the
3693 * cursor */
3694 fromcol_prev = fromcol;
3695 fromcol = -1;
3696 }
3697 else if ((colnr_T)fromcol < wp->w_virtcol)
3698 /* restart highlighting after the cursor */
3699 fromcol_prev = wp->w_virtcol;
3700 }
3701 if (fromcol >= tocol)
3702 fromcol = -1;
3703 }
3704
3705#ifdef FEAT_SEARCH_EXTRA
3706 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003707 * Handle highlighting the last used search pattern and matches.
3708 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003710 cur = wp->w_match_head;
3711 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003712 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003714 if (shl_flag == FALSE)
3715 {
3716 shl = &search_hl;
3717 shl_flag = TRUE;
3718 }
3719 else
3720 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003721 shl->startcol = MAXCOL;
3722 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003724 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003725 v = (long)(ptr - line);
3726 if (cur != NULL)
3727 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003728 next_search_hl(wp, shl, lnum, (colnr_T)v,
3729 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003730
3731 /* Need to get the line again, a multi-line regexp may have made it
3732 * invalid. */
3733 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3734 ptr = line + v;
3735
3736 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003738 if (shl->lnum == lnum)
3739 shl->startcol = shl->rm.startpos[0].col;
3740 else
3741 shl->startcol = 0;
3742 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3743 - shl->rm.startpos[0].lnum)
3744 shl->endcol = shl->rm.endpos[0].col;
3745 else
3746 shl->endcol = MAXCOL;
3747 /* Highlight one character for an empty match. */
3748 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003751 if (has_mbyte && line[shl->endcol] != NUL)
3752 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3753 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003755 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003757 if ((long)shl->startcol < v) /* match at leftcol */
3758 {
3759 shl->attr_cur = shl->attr;
3760 search_attr = shl->attr;
3761 }
3762 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003764 if (shl != &search_hl && cur != NULL)
3765 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 }
3767#endif
3768
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003769#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003770 /* Cursor line highlighting for 'cursorline' in the current window. Not
3771 * when Visual mode is active, because it's not clear what is selected
3772 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003773 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003774 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003775 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003776 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003777 area_highlighting = TRUE;
3778 }
3779#endif
3780
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003781 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 col = 0;
3783#ifdef FEAT_RIGHTLEFT
3784 if (wp->w_p_rl)
3785 {
3786 /* Rightleft window: process the text in the normal direction, but put
3787 * it in current_ScreenLine[] from right to left. Start at the
3788 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003789 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 off += col;
3791 }
3792#endif
3793
3794 /*
3795 * Repeat for the whole displayed line.
3796 */
3797 for (;;)
3798 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003799#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003800 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 /* Skip this quickly when working on the text. */
3803 if (draw_state != WL_LINE)
3804 {
3805#ifdef FEAT_CMDWIN
3806 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3807 {
3808 draw_state = WL_CMDLINE;
3809 if (cmdwin_type != 0 && wp == curwin)
3810 {
3811 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003813 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003814 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816 }
3817#endif
3818
3819#ifdef FEAT_FOLDING
3820 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3821 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003822 int fdc = compute_foldcolumn(wp, 0);
3823
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003825 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003827 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003828 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003829 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003830 p_extra_free = alloc(12 + 1);
3831
3832 if (p_extra_free != NULL)
3833 {
3834 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3835 n_extra = fdc;
3836 p_extra_free[n_extra] = NUL;
3837 p_extra = p_extra_free;
3838 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003839 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842 }
3843#endif
3844
3845#ifdef FEAT_SIGNS
3846 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3847 {
3848 draw_state = WL_SIGN;
3849 /* Show the sign column when there are any signs in this
3850 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003851 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003853 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003855 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856# endif
3857
3858 /* Draw two cells with the sign value or blank. */
3859 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003860 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 n_extra = 2;
3862
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003863 if (row == startrow
3864#ifdef FEAT_DIFF
3865 + filler_lines && filler_todo <= 0
3866#endif
3867 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
3869 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3870 SIGN_TEXT);
3871# ifdef FEAT_SIGN_ICONS
3872 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3873 SIGN_ICON);
3874 if (gui.in_use && icon_sign != 0)
3875 {
3876 /* Use the image in this position. */
3877 c_extra = SIGN_BYTE;
3878# ifdef FEAT_NETBEANS_INTG
3879 if (buf_signcount(wp->w_buffer, lnum) > 1)
3880 c_extra = MULTISIGN_BYTE;
3881# endif
3882 char_attr = icon_sign;
3883 }
3884 else
3885# endif
3886 if (text_sign != 0)
3887 {
3888 p_extra = sign_get_text(text_sign);
3889 if (p_extra != NULL)
3890 {
3891 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003892 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 }
3894 char_attr = sign_get_attr(text_sign, FALSE);
3895 }
3896 }
3897 }
3898 }
3899#endif
3900
3901 if (draw_state == WL_NR - 1 && n_extra == 0)
3902 {
3903 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003904 /* Display the absolute or relative line number. After the
3905 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3906 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 && (row == startrow
3908#ifdef FEAT_DIFF
3909 + filler_lines
3910#endif
3911 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3912 {
3913 /* Draw the line number (empty space after wrapping). */
3914 if (row == startrow
3915#ifdef FEAT_DIFF
3916 + filler_lines
3917#endif
3918 )
3919 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003920 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003921 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003922
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003923 if (wp->w_p_nu && !wp->w_p_rnu)
3924 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003925 num = (long)lnum;
3926 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003927 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003928 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003929 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003930 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003931 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003932 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003933 num = lnum;
3934 fmt = "%-*ld ";
3935 }
3936 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003937
Bram Moolenaar700e7342013-01-30 12:31:36 +01003938 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003939 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 if (wp->w_skipcol > 0)
3941 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3942 *p_extra = '-';
3943#ifdef FEAT_RIGHTLEFT
3944 if (wp->w_p_rl) /* reverse line numbers */
3945 rl_mirror(extra);
3946#endif
3947 p_extra = extra;
3948 c_extra = NUL;
3949 }
3950 else
3951 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003952 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003953 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003954#ifdef FEAT_SYN_HL
3955 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003956 * the current line differently.
3957 * TODO: Can we use CursorLine instead of CursorLineNr
3958 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003959 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003960 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003961 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
3964 }
3965
Bram Moolenaar597a4222014-06-25 14:39:50 +02003966#ifdef FEAT_LINEBREAK
3967 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3968 && n_extra == 0 && *p_sbr != NUL)
3969 /* draw indent after showbreak value */
3970 draw_state = WL_BRI;
3971 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3972 /* After the showbreak, draw the breakindent */
3973 draw_state = WL_BRI - 1;
3974
3975 /* draw 'breakindent': indent wrapped text accordingly */
3976 if (draw_state == WL_BRI - 1 && n_extra == 0)
3977 {
3978 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003979 /* if need_showbreak is set, breakindent also applies */
3980 if (wp->w_p_bri && n_extra == 0
3981 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003982# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003983 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003984# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003985 )
3986 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003987 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003988# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003989 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003990 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003991 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003992# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003993 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3994 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003995 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003996# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003997 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003998# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003999 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004000 c_extra = ' ';
4001 n_extra = get_breakindent_win(wp,
4002 ml_get_buf(wp->w_buffer, lnum, FALSE));
4003 /* Correct end of highlighted area for 'breakindent',
4004 * required when 'linebreak' is also set. */
4005 if (tocol == vcol)
4006 tocol += n_extra;
4007 }
4008 }
4009#endif
4010
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4012 if (draw_state == WL_SBR - 1 && n_extra == 0)
4013 {
4014 draw_state = WL_SBR;
4015# ifdef FEAT_DIFF
4016 if (filler_todo > 0)
4017 {
4018 /* Draw "deleted" diff line(s). */
4019 if (char2cells(fill_diff) > 1)
4020 c_extra = '-';
4021 else
4022 c_extra = fill_diff;
4023# ifdef FEAT_RIGHTLEFT
4024 if (wp->w_p_rl)
4025 n_extra = col + 1;
4026 else
4027# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004028 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004029 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
4031# endif
4032# ifdef FEAT_LINEBREAK
4033 if (*p_sbr != NUL && need_showbreak)
4034 {
4035 /* Draw 'showbreak' at the start of each broken line. */
4036 p_extra = p_sbr;
4037 c_extra = NUL;
4038 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004039 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004041 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 /* Correct end of highlighted area for 'showbreak',
4043 * required when 'linebreak' is also set. */
4044 if (tocol == vcol)
4045 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004046#ifdef FEAT_SYN_HL
4047 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004048 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004049 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004050 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
4053# endif
4054 }
4055#endif
4056
4057 if (draw_state == WL_LINE - 1 && n_extra == 0)
4058 {
4059 draw_state = WL_LINE;
4060 if (saved_n_extra)
4061 {
4062 /* Continue item from end of wrapped line. */
4063 n_extra = saved_n_extra;
4064 c_extra = saved_c_extra;
4065 p_extra = saved_p_extra;
4066 char_attr = saved_char_attr;
4067 }
4068 else
4069 char_attr = 0;
4070 }
4071 }
4072
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004073 // When still displaying '$' of change command, stop at cursor.
4074 // When only displaying the (relative) line number and that's done,
4075 // stop here.
4076 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004077 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078#ifdef FEAT_DIFF
4079 && filler_todo <= 0
4080#endif
4081 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004082 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004084 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004085 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004086 /* Pretend we have finished updating the window. Except when
4087 * 'cursorcolumn' is set. */
4088#ifdef FEAT_SYN_HL
4089 if (wp->w_p_cuc)
4090 row = wp->w_cline_row + wp->w_cline_height;
4091 else
4092#endif
4093 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 break;
4095 }
4096
4097 if (draw_state == WL_LINE && area_highlighting)
4098 {
4099 /* handle Visual or match highlighting in this line */
4100 if (vcol == fromcol
4101#ifdef FEAT_MBYTE
4102 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4103 && (*mb_ptr2cells)(ptr) > 1)
4104#endif
4105 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004106 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 && vcol < tocol))
4108 area_attr = attr; /* start highlighting */
4109 else if (area_attr != 0
4110 && (vcol == tocol
4111 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113
4114#ifdef FEAT_SEARCH_EXTRA
4115 if (!n_extra)
4116 {
4117 /*
4118 * Check for start/end of search pattern match.
4119 * After end, check for start/end of next match.
4120 * When another match, have to check for start again.
4121 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004122 * Do this for 'search_hl' and the match list (ordered by
4123 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004125 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004126 cur = wp->w_match_head;
4127 shl_flag = FALSE;
4128 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004130 if (shl_flag == FALSE
4131 && ((cur != NULL
4132 && cur->priority > SEARCH_HL_PRIORITY)
4133 || cur == NULL))
4134 {
4135 shl = &search_hl;
4136 shl_flag = TRUE;
4137 }
4138 else
4139 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004140 if (cur != NULL)
4141 cur->pos.cur = 0;
4142 pos_inprogress = TRUE;
4143 while (shl->rm.regprog != NULL
4144 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004146 if (shl->startcol != MAXCOL
4147 && v >= (long)shl->startcol
4148 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004150#ifdef FEAT_MBYTE
4151 int tmp_col = v + MB_PTR2LEN(ptr);
4152
4153 if (shl->endcol < tmp_col)
4154 shl->endcol = tmp_col;
4155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004157#ifdef FEAT_CONCEAL
4158 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4159 == cur->hlg_id)
4160 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004161 has_match_conc =
4162 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004163 match_conc = cur->conceal_char;
4164 }
4165 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004166 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004169 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 {
4171 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004172 next_search_hl(wp, shl, lnum, (colnr_T)v,
4173 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004174 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004175 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
4177 /* Need to get the line again, a multi-line regexp
4178 * may have made it invalid. */
4179 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4180 ptr = line + v;
4181
4182 if (shl->lnum == lnum)
4183 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004184 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004186 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004188 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004190 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 {
4192 /* highlight empty match, try again after
4193 * it */
4194#ifdef FEAT_MBYTE
4195 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004196 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004197 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 else
4199#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004200 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
4202
4203 /* Loop to check if the match starts at the
4204 * current position */
4205 continue;
4206 }
4207 }
4208 break;
4209 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004210 if (shl != &search_hl && cur != NULL)
4211 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004213
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004214 /* Use attributes from match with highest priority among
4215 * 'search_hl' and the match list. */
4216 search_attr = search_hl.attr_cur;
4217 cur = wp->w_match_head;
4218 shl_flag = FALSE;
4219 while (cur != NULL || shl_flag == FALSE)
4220 {
4221 if (shl_flag == FALSE
4222 && ((cur != NULL
4223 && cur->priority > SEARCH_HL_PRIORITY)
4224 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004225 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004226 shl = &search_hl;
4227 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004228 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004229 else
4230 shl = &cur->hl;
4231 if (shl->attr_cur != 0)
4232 search_attr = shl->attr_cur;
4233 if (shl != &search_hl && cur != NULL)
4234 cur = cur->next;
4235 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004236 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004237 if (*ptr == NUL && (did_line_attr >= 1
4238 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004239 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
4241#endif
4242
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004244 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004246 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4247 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004249 if (diff_hlf == HLF_TXD && ptr - line > change_end
4250 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004252 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004253 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004254 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 }
4256#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004257
4258 /* Decide which of the highlight attributes to use. */
4259 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004260#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004261 if (area_attr != 0)
4262 char_attr = hl_combine_attr(line_attr, area_attr);
4263 else if (search_attr != 0)
4264 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004265 /* Use line_attr when not in the Visual or 'incsearch' area
4266 * (area_attr may be 0 when "noinvcur" is set). */
4267 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004268 || vcol < fromcol || vcol_prev < fromcol_prev
4269 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004270 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004271#else
4272 if (area_attr != 0)
4273 char_attr = area_attr;
4274 else if (search_attr != 0)
4275 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004276#endif
4277 else
4278 {
4279 attr_pri = FALSE;
4280#ifdef FEAT_SYN_HL
4281 if (has_syntax)
4282 char_attr = syntax_attr;
4283 else
4284#endif
4285 char_attr = 0;
4286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288
4289 /*
4290 * Get the next character to put on the screen.
4291 */
4292 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004293 * The "p_extra" points to the extra stuff that is inserted to
4294 * represent special characters (non-printable stuff) and other
4295 * things. When all characters are the same, c_extra is used.
4296 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4297 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4299 */
4300 if (n_extra > 0)
4301 {
4302 if (c_extra != NUL)
4303 {
4304 c = c_extra;
4305#ifdef FEAT_MBYTE
4306 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004307 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 {
4309 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004310 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004311 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 }
4313 else
4314 mb_utf8 = FALSE;
4315#endif
4316 }
4317 else
4318 {
4319 c = *p_extra;
4320#ifdef FEAT_MBYTE
4321 if (has_mbyte)
4322 {
4323 mb_c = c;
4324 if (enc_utf8)
4325 {
4326 /* If the UTF-8 character is more than one byte:
4327 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004328 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 mb_utf8 = FALSE;
4330 if (mb_l > n_extra)
4331 mb_l = 1;
4332 else if (mb_l > 1)
4333 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004334 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004336 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 }
4338 }
4339 else
4340 {
4341 /* if this is a DBCS character, put it in "mb_c" */
4342 mb_l = MB_BYTE2LEN(c);
4343 if (mb_l >= n_extra)
4344 mb_l = 1;
4345 else if (mb_l > 1)
4346 mb_c = (c << 8) + p_extra[1];
4347 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004348 if (mb_l == 0) /* at the NUL at end-of-line */
4349 mb_l = 1;
4350
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 /* If a double-width char doesn't fit display a '>' in the
4352 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004353 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354# ifdef FEAT_RIGHTLEFT
4355 wp->w_p_rl ? (col <= 0) :
4356# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004357 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 && (*mb_char2cells)(mb_c) == 2)
4359 {
4360 c = '>';
4361 mb_c = c;
4362 mb_l = 1;
4363 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004364 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 /* put the pointer back to output the double-width
4366 * character at the start of the next line. */
4367 ++n_extra;
4368 --p_extra;
4369 }
4370 else
4371 {
4372 n_extra -= mb_l - 1;
4373 p_extra += mb_l - 1;
4374 }
4375 }
4376#endif
4377 ++p_extra;
4378 }
4379 --n_extra;
4380 }
4381 else
4382 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004383#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004384 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004385#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004386
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004387 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004388 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 /*
4390 * Get a character from the line itself.
4391 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004392 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004393#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004394 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396#ifdef FEAT_MBYTE
4397 if (has_mbyte)
4398 {
4399 mb_c = c;
4400 if (enc_utf8)
4401 {
4402 /* If the UTF-8 character is more than one byte: Decode it
4403 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004404 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 mb_utf8 = FALSE;
4406 if (mb_l > 1)
4407 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004408 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 /* Overlong encoded ASCII or ASCII with composing char
4410 * is displayed normally, except a NUL. */
4411 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004412 {
4413 c = mb_c;
4414# ifdef FEAT_LINEBREAK
4415 c0 = mb_c;
4416# endif
4417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004419
4420 /* At start of the line we can have a composing char.
4421 * Draw it as a space with a composing char. */
4422 if (utf_iscomposing(mb_c))
4423 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004424 int i;
4425
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004426 for (i = Screen_mco - 1; i > 0; --i)
4427 u8cc[i] = u8cc[i - 1];
4428 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004429 mb_c = ' ';
4430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 }
4432
4433 if ((mb_l == 1 && c >= 0x80)
4434 || (mb_l >= 1 && mb_c == 0)
4435 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004436# ifdef UNICODE16
4437 || mb_c >= 0x10000
4438# endif
4439 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 {
4441 /*
4442 * Illegal UTF-8 byte: display as <xx>.
4443 * Non-BMP character : display as ? or fullwidth ?.
4444 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004445# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004447# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 {
4449 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004450# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 if (wp->w_p_rl) /* reverse */
4452 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004455# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 else if (utf_char2cells(mb_c) != 2)
4457 STRCPY(extra, "?");
4458 else
4459 /* 0xff1f in UTF-8: full-width '?' */
4460 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004461# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
4463 p_extra = extra;
4464 c = *p_extra;
4465 mb_c = mb_ptr2char_adv(&p_extra);
4466 mb_utf8 = (c >= 0x80);
4467 n_extra = (int)STRLEN(p_extra);
4468 c_extra = NUL;
4469 if (area_attr == 0 && search_attr == 0)
4470 {
4471 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004472 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 saved_attr2 = char_attr; /* save current attr */
4474 }
4475 }
4476 else if (mb_l == 0) /* at the NUL at end-of-line */
4477 mb_l = 1;
4478#ifdef FEAT_ARABIC
4479 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4480 {
4481 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004482 int pc, pc1, nc;
4483 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
4485 /* The idea of what is the previous and next
4486 * character depends on 'rightleft'. */
4487 if (wp->w_p_rl)
4488 {
4489 pc = prev_c;
4490 pc1 = prev_c1;
4491 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004492 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494 else
4495 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004496 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004498 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
4500 prev_c = mb_c;
4501
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004502 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 }
4504 else
4505 prev_c = mb_c;
4506#endif
4507 }
4508 else /* enc_dbcs */
4509 {
4510 mb_l = MB_BYTE2LEN(c);
4511 if (mb_l == 0) /* at the NUL at end-of-line */
4512 mb_l = 1;
4513 else if (mb_l > 1)
4514 {
4515 /* We assume a second byte below 32 is illegal.
4516 * Hopefully this is OK for all double-byte encodings!
4517 */
4518 if (ptr[1] >= 32)
4519 mb_c = (c << 8) + ptr[1];
4520 else
4521 {
4522 if (ptr[1] == NUL)
4523 {
4524 /* head byte at end of line */
4525 mb_l = 1;
4526 transchar_nonprint(extra, c);
4527 }
4528 else
4529 {
4530 /* illegal tail byte */
4531 mb_l = 2;
4532 STRCPY(extra, "XX");
4533 }
4534 p_extra = extra;
4535 n_extra = (int)STRLEN(extra) - 1;
4536 c_extra = NUL;
4537 c = *p_extra++;
4538 if (area_attr == 0 && search_attr == 0)
4539 {
4540 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004541 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 saved_attr2 = char_attr; /* save current attr */
4543 }
4544 mb_c = c;
4545 }
4546 }
4547 }
4548 /* If a double-width char doesn't fit display a '>' in the
4549 * last column; the character is displayed at the start of the
4550 * next line. */
4551 if ((
4552# ifdef FEAT_RIGHTLEFT
4553 wp->w_p_rl ? (col <= 0) :
4554# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004555 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 && (*mb_char2cells)(mb_c) == 2)
4557 {
4558 c = '>';
4559 mb_c = c;
4560 mb_utf8 = FALSE;
4561 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004562 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 /* Put pointer back so that the character will be
4564 * displayed at the start of the next line. */
4565 --ptr;
4566 }
4567 else if (*ptr != NUL)
4568 ptr += mb_l - 1;
4569
4570 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004571 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004572 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004573 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004576 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 c = ' ';
4578 if (area_attr == 0 && search_attr == 0)
4579 {
4580 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004581 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 saved_attr2 = char_attr; /* save current attr */
4583 }
4584 mb_c = c;
4585 mb_utf8 = FALSE;
4586 mb_l = 1;
4587 }
4588
4589 }
4590#endif
4591 ++ptr;
4592
4593 if (extra_check)
4594 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004595#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004596 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004597#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004598
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004599#ifdef FEAT_TERMINAL
4600 if (get_term_attr)
4601 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004602 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004603
4604 if (!attr_pri)
4605 char_attr = syntax_attr;
4606 else
4607 char_attr = hl_combine_attr(syntax_attr, char_attr);
4608 }
4609#endif
4610
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004611#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 /* Get syntax attribute, unless still at the start of the line
4613 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004614 v = (long)(ptr - line);
4615 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
4617 /* Get the syntax attribute for the character. If there
4618 * is an error, disable syntax highlighting. */
4619 save_did_emsg = did_emsg;
4620 did_emsg = FALSE;
4621
Bram Moolenaar217ad922005-03-20 22:37:15 +00004622 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004623# ifdef FEAT_SPELL
4624 has_spell ? &can_spell :
4625# endif
4626 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627
4628 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004629 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004630 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004631 has_syntax = FALSE;
4632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 else
4634 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004635#ifdef SYN_TIME_LIMIT
4636 if (wp->w_s->b_syn_slow)
4637 has_syntax = FALSE;
4638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639
4640 /* Need to get the line again, a multi-line regexp may
4641 * have made it invalid. */
4642 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4643 ptr = line + v;
4644
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004645 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004647 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004648 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004649# ifdef FEAT_CONCEAL
4650 /* no concealing past the end of the line, it interferes
4651 * with line highlighting */
4652 if (c == NUL)
4653 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004654 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004655 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004656# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004658#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004659
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004660#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004661 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004662 * Only do this when there is no syntax highlighting, the
4663 * @Spell cluster is not used or the current syntax item
4664 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004665 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004666 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004667 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004668# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004669 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004670 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004671# endif
4672 if (c != 0 && (
4673# ifdef FEAT_SYN_HL
4674 !has_syntax ||
4675# endif
4676 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004677 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004678 char_u *prev_ptr, *p;
4679 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004680 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004681# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004682 if (has_mbyte)
4683 {
4684 prev_ptr = ptr - mb_l;
4685 v -= mb_l - 1;
4686 }
4687 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004688# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004689 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004690
4691 /* Use nextline[] if possible, it has the start of the
4692 * next line concatenated. */
4693 if ((prev_ptr - line) - nextlinecol >= 0)
4694 p = nextline + (prev_ptr - line) - nextlinecol;
4695 else
4696 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004697 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004698 len = spell_check(wp, p, &spell_hlf, &cap_col,
4699 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004700 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004701
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004702 /* In Insert mode only highlight a word that
4703 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004704 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004705 && (State & INSERT) != 0
4706 && wp->w_cursor.lnum == lnum
4707 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004708 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004709 && wp->w_cursor.col < (colnr_T)word_end)
4710 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004711 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004712 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004713 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004714
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004715 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004716 && (p - nextline) + len > nextline_idx)
4717 {
4718 /* Remember that the good word continues at the
4719 * start of the next line. */
4720 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004721 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004722 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004723
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004724 /* Turn index into actual attributes. */
4725 if (spell_hlf != HLF_COUNT)
4726 spell_attr = highlight_attr[spell_hlf];
4727
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004728 if (cap_col > 0)
4729 {
4730 if (p != prev_ptr
4731 && (p - nextline) + cap_col >= nextline_idx)
4732 {
4733 /* Remember that the word in the next line
4734 * must start with a capital. */
4735 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004736 cap_col = (int)((p - nextline) + cap_col
4737 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004738 }
4739 else
4740 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004741 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004742 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004743 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004744 }
4745 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004746 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004747 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004748 char_attr = hl_combine_attr(char_attr, spell_attr);
4749 else
4750 char_attr = hl_combine_attr(spell_attr, char_attr);
4751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752#endif
4753#ifdef FEAT_LINEBREAK
4754 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004755 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004757 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004758 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004760# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004761 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004762# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004763 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004765 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004767 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004768
Bram Moolenaar597a4222014-06-25 14:39:50 +02004769 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004770 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004771 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004772 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004773# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004774 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004775 wp->w_buffer->b_p_vts_array) - 1;
4776# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004777 n_extra = (int)wp->w_buffer->b_p_ts
4778 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004779# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004780
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004781# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004782 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004783# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004785# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004786 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004787 {
4788#ifdef FEAT_CONCEAL
4789 if (c == TAB)
4790 /* See "Tab alignment" below. */
4791 FIX_FOR_BOGUSCOLS;
4792#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004793 if (!wp->w_p_list)
4794 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
4797#endif
4798
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004799 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4800 */
4801 if (wp->w_p_list
4802 && (((c == 160
4803#ifdef FEAT_MBYTE
4804 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4805#endif
4806 ) && lcs_nbsp)
4807 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4808 {
4809 c = (c == ' ') ? lcs_space : lcs_nbsp;
4810 if (area_attr == 0 && search_attr == 0)
4811 {
4812 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004813 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004814 saved_attr2 = char_attr; /* save current attr */
4815 }
4816#ifdef FEAT_MBYTE
4817 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004818 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004819 {
4820 mb_utf8 = TRUE;
4821 u8cc[0] = 0;
4822 c = 0xc0;
4823 }
4824 else
4825 mb_utf8 = FALSE;
4826#endif
4827 }
4828
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4830 {
4831 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004832 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 {
4834 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004835 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 saved_attr2 = char_attr; /* save current attr */
4837 }
4838#ifdef FEAT_MBYTE
4839 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004840 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
4842 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004843 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004844 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 }
4846 else
4847 mb_utf8 = FALSE;
4848#endif
4849 }
4850 }
4851
4852 /*
4853 * Handling of non-printable characters.
4854 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004855 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 {
4857 /*
4858 * when getting a character from the file, we may have to
4859 * turn it into something else on the way to putting it
4860 * into "ScreenLines".
4861 */
4862 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4863 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004864 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004865 long vcol_adjusted = vcol; /* removed showbreak length */
4866#ifdef FEAT_LINEBREAK
4867 /* only adjust the tab_len, when at the first column
4868 * after the showbreak value was drawn */
4869 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4870 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004873#ifdef FEAT_VARTABS
4874 tab_len = tabstop_padding(vcol_adjusted,
4875 wp->w_buffer->b_p_ts,
4876 wp->w_buffer->b_p_vts_array) - 1;
4877#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004878 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004879 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4880#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004881
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004882#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004883 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004884#endif
4885 /* tab amount depends on current column */
4886 n_extra = tab_len;
4887#ifdef FEAT_LINEBREAK
4888 else
4889 {
4890 char_u *p;
4891 int len = n_extra;
4892 int i;
4893 int saved_nextra = n_extra;
4894
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004895#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004896 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004897 /* there are characters to conceal */
4898 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004899 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4900 */
4901 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4902 && n_extra > tab_len)
4903 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004904#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004905
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004906 /* if n_extra > 0, it gives the number of chars, to
4907 * use for a tab, else we need to calculate the width
4908 * for a tab */
4909#ifdef FEAT_MBYTE
4910 len = (tab_len * mb_char2len(lcs_tab2));
4911 if (n_extra > 0)
4912 len += n_extra - tab_len;
4913#endif
4914 c = lcs_tab1;
4915 p = alloc((unsigned)(len + 1));
4916 vim_memset(p, ' ', len);
4917 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004918 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004919 p_extra_free = p;
4920 for (i = 0; i < tab_len; i++)
4921 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004922 if (*p == NUL)
4923 {
4924 tab_len = i;
4925 break;
4926 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004927#ifdef FEAT_MBYTE
4928 mb_char2bytes(lcs_tab2, p);
4929 p += mb_char2len(lcs_tab2);
4930 n_extra += mb_char2len(lcs_tab2)
4931 - (saved_nextra > 0 ? 1 : 0);
4932#else
4933 p[i] = lcs_tab2;
4934#endif
4935 }
4936 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004937#ifdef FEAT_CONCEAL
4938 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4939 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004940 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004941 n_extra -= vcol_off;
4942#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004943 }
4944#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004945#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004946 {
4947 int vc_saved = vcol_off;
4948
4949 /* Tab alignment should be identical regardless of
4950 * 'conceallevel' value. So tab compensates of all
4951 * previous concealed characters, and thus resets
4952 * vcol_off and boguscols accumulated so far in the
4953 * line. Note that the tab can be longer than
4954 * 'tabstop' when there are concealed characters. */
4955 FIX_FOR_BOGUSCOLS;
4956
4957 /* Make sure, the highlighting for the tab char will be
4958 * correctly set further below (effectively reverts the
4959 * FIX_FOR_BOGSUCOLS macro */
4960 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004961 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004962 tab_len += vc_saved;
4963 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965#ifdef FEAT_MBYTE
4966 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4967#endif
4968 if (wp->w_p_list)
4969 {
4970 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004971#ifdef FEAT_LINEBREAK
4972 if (wp->w_p_lbr)
4973 c_extra = NUL; /* using p_extra from above */
4974 else
4975#endif
4976 c_extra = lcs_tab2;
4977 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004978 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 saved_attr2 = char_attr; /* save current attr */
4980#ifdef FEAT_MBYTE
4981 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004982 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
4984 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004985 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004986 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988#endif
4989 }
4990 else
4991 {
4992 c_extra = ' ';
4993 c = ' ';
4994 }
4995 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004996 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004997 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004998 || ((fromcol >= 0 || fromcol_prev >= 0)
4999 && tocol > vcol
5000 && VIsual_mode != Ctrl_V
5001 && (
5002# ifdef FEAT_RIGHTLEFT
5003 wp->w_p_rl ? (col >= 0) :
5004# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005005 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005006 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005007 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005008 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005009 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005011 /* Display a '$' after the line or highlight an extra
5012 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5014 /* For a diff line the highlighting continues after the
5015 * "$". */
5016 if (
5017# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005018 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019# ifdef LINE_ATTR
5020 &&
5021# endif
5022# endif
5023# ifdef LINE_ATTR
5024 line_attr == 0
5025# endif
5026 )
5027#endif
5028 {
5029#ifdef FEAT_VIRTUALEDIT
5030 /* In virtualedit, visual selections may extend
5031 * beyond end of line. */
5032 if (area_highlighting && virtual_active()
5033 && tocol != MAXCOL && vcol < tocol)
5034 n_extra = 0;
5035 else
5036#endif
5037 {
5038 p_extra = at_end_str;
5039 n_extra = 1;
5040 c_extra = NUL;
5041 }
5042 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005043 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005044 c = lcs_eol;
5045 else
5046 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 lcs_eol_one = -1;
5048 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005049 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005051 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 n_attr = 1;
5053 }
5054#ifdef FEAT_MBYTE
5055 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005056 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 {
5058 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005059 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005060 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 }
5062 else
5063 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5064#endif
5065 }
5066 else if (c != NUL)
5067 {
5068 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005069 if (n_extra == 0)
5070 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071#ifdef FEAT_RIGHTLEFT
5072 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5073 rl_mirror(p_extra); /* reverse "<12>" */
5074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005076#ifdef FEAT_LINEBREAK
5077 if (wp->w_p_lbr)
5078 {
5079 char_u *p;
5080
5081 c = *p_extra;
5082 p = alloc((unsigned)n_extra + 1);
5083 vim_memset(p, ' ', n_extra);
5084 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5085 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005086 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005087 p_extra_free = p_extra = p;
5088 }
5089 else
5090#endif
5091 {
5092 n_extra = byte2cells(c) - 1;
5093 c = *p_extra++;
5094 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005095 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
5097 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005098 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 saved_attr2 = char_attr; /* save current attr */
5100 }
5101#ifdef FEAT_MBYTE
5102 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5103#endif
5104 }
5105#ifdef FEAT_VIRTUALEDIT
5106 else if (VIsual_active
5107 && (VIsual_mode == Ctrl_V
5108 || VIsual_mode == 'v')
5109 && virtual_active()
5110 && tocol != MAXCOL
5111 && vcol < tocol
5112 && (
5113# ifdef FEAT_RIGHTLEFT
5114 wp->w_p_rl ? (col >= 0) :
5115# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005116 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
5118 c = ' ';
5119 --ptr; /* put it back at the NUL */
5120 }
5121#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005122#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 else if ((
5124# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005125 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005127# ifdef FEAT_TERMINAL
5128 term_attr != 0 ||
5129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 ) && (
5132# ifdef FEAT_RIGHTLEFT
5133 wp->w_p_rl ? (col >= 0) :
5134# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005135 (col
5136# ifdef FEAT_CONCEAL
5137 - boguscols
5138# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005139 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
5141 /* Highlight until the right side of the window */
5142 c = ' ';
5143 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005144
5145 /* Remember we do the char for line highlighting. */
5146 ++did_line_attr;
5147
5148 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005149 if (line_attr != 0 && char_attr == search_attr
5150 && (did_line_attr > 1
5151 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005152 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153# ifdef FEAT_DIFF
5154 if (diff_hlf == HLF_TXD)
5155 {
5156 diff_hlf = HLF_CHD;
5157 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005158 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005159 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005160 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5161 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005162 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005166# ifdef FEAT_TERMINAL
5167 if (term_attr != 0)
5168 {
5169 char_attr = term_attr;
5170 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5171 char_attr = hl_combine_attr(char_attr,
5172 HL_ATTR(HLF_CUL));
5173 }
5174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
5176#endif
5177 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005178
5179#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005180 if ( wp->w_p_cole > 0
5181 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005182 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005183 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005184 && !(lnum_in_visual_area
5185 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005186 {
5187 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005188 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005189 && (syn_get_sub_char() != NUL || match_conc
5190 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005191 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005192 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005193 /* First time at this concealed item: display one
5194 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005195 if (match_conc)
5196 c = match_conc;
5197 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005198 c = syn_get_sub_char();
5199 else if (lcs_conceal != NUL)
5200 c = lcs_conceal;
5201 else
5202 c = ' ';
5203
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005204 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005205
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005206 if (n_extra > 0)
5207 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005208 vcol += n_extra;
5209 if (wp->w_p_wrap && n_extra > 0)
5210 {
5211# ifdef FEAT_RIGHTLEFT
5212 if (wp->w_p_rl)
5213 {
5214 col -= n_extra;
5215 boguscols -= n_extra;
5216 }
5217 else
5218# endif
5219 {
5220 boguscols += n_extra;
5221 col += n_extra;
5222 }
5223 }
5224 n_extra = 0;
5225 n_attr = 0;
5226 }
5227 else if (n_skip == 0)
5228 {
5229 is_concealing = TRUE;
5230 n_skip = 1;
5231 }
5232# ifdef FEAT_MBYTE
5233 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005234 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005235 {
5236 mb_utf8 = TRUE;
5237 u8cc[0] = 0;
5238 c = 0xc0;
5239 }
5240 else
5241 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5242# endif
5243 }
5244 else
5245 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005246 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005247 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005248 }
5249#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 }
5251
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005252#ifdef FEAT_CONCEAL
5253 /* In the cursor line and we may be concealing characters: correct
5254 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005255 if (!did_wcol && draw_state == WL_LINE
5256 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005257 && conceal_cursor_line(wp)
5258 && (int)wp->w_virtcol <= vcol + n_skip)
5259 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005260# ifdef FEAT_RIGHTLEFT
5261 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005262 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005263 else
5264# endif
5265 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005266 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005267 did_wcol = TRUE;
5268 }
5269#endif
5270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 /* Don't override visual selection highlighting. */
5272 if (n_attr > 0
5273 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005274 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 char_attr = extra_attr;
5276
Bram Moolenaar81695252004-12-29 20:58:21 +00005277#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 /* XIM don't send preedit_start and preedit_end, but they send
5279 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5280 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005281 if (p_imst == IM_ON_THE_SPOT
5282 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005283 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 && (State & INSERT)
5285 && !p_imdisable
5286 && im_is_preediting()
5287 && draw_state == WL_LINE)
5288 {
5289 colnr_T tcol;
5290
5291 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005292 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 else
5294 tcol = preedit_end_col;
5295 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5296 {
5297 if (feedback_old_attr < 0)
5298 {
5299 feedback_col = 0;
5300 feedback_old_attr = char_attr;
5301 }
5302 char_attr = im_get_feedback_attr(feedback_col);
5303 if (char_attr < 0)
5304 char_attr = feedback_old_attr;
5305 feedback_col++;
5306 }
5307 else if (feedback_old_attr >= 0)
5308 {
5309 char_attr = feedback_old_attr;
5310 feedback_old_attr = -1;
5311 feedback_col = 0;
5312 }
5313 }
5314#endif
5315 /*
5316 * Handle the case where we are in column 0 but not on the first
5317 * character of the line and the user wants us to show us a
5318 * special character (via 'listchars' option "precedes:<char>".
5319 */
5320 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005321 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5323#ifdef FEAT_DIFF
5324 && filler_todo <= 0
5325#endif
5326 && draw_state > WL_NR
5327 && c != NUL)
5328 {
5329 c = lcs_prec;
5330 lcs_prec_todo = NUL;
5331#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005332 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5333 {
5334 /* Double-width character being overwritten by the "precedes"
5335 * character, need to fill up half the character. */
5336 c_extra = MB_FILLER_CHAR;
5337 n_extra = 1;
5338 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005339 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005342 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 {
5344 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005345 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005346 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 }
5348 else
5349 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5350#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005351 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 {
5353 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005354 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 n_attr3 = 1;
5356 }
5357 }
5358
5359 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005360 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005362 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005363#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005364 || did_line_attr == 1
5365#endif
5366 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005368#ifdef FEAT_SEARCH_EXTRA
5369 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005370
5371 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005372 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005373 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005374#endif
5375
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005376 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 * highlight match at end of line. If it's beyond the last
5378 * char on the screen, just overwrite that one (tricky!) Not
5379 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005380#ifdef FEAT_SEARCH_EXTRA
5381 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005382 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005383 prevcol_hl_flag = TRUE;
5384 else
5385 {
5386 cur = wp->w_match_head;
5387 while (cur != NULL)
5388 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005389 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005390 {
5391 prevcol_hl_flag = TRUE;
5392 break;
5393 }
5394 cur = cur->next;
5395 }
5396 }
5397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005399 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005400 && (VIsual_mode != Ctrl_V
5401 || lnum == VIsual.lnum
5402 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005403 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404#ifdef FEAT_SEARCH_EXTRA
5405 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005406 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005407# ifdef FEAT_SYN_HL
5408 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5409 && !(wp == curwin && VIsual_active))
5410# endif
5411# ifdef FEAT_DIFF
5412 && diff_hlf == (hlf_T)0
5413# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005414# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005415 && did_line_attr <= 1
5416# endif
5417 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418#endif
5419 ))
5420 {
5421 int n = 0;
5422
5423#ifdef FEAT_RIGHTLEFT
5424 if (wp->w_p_rl)
5425 {
5426 if (col < 0)
5427 n = 1;
5428 }
5429 else
5430#endif
5431 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005432 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 n = -1;
5434 }
5435 if (n != 0)
5436 {
5437 /* At the window boundary, highlight the last character
5438 * instead (better than nothing). */
5439 off += n;
5440 col += n;
5441 }
5442 else
5443 {
5444 /* Add a blank character to highlight. */
5445 ScreenLines[off] = ' ';
5446#ifdef FEAT_MBYTE
5447 if (enc_utf8)
5448 ScreenLinesUC[off] = 0;
5449#endif
5450 }
5451#ifdef FEAT_SEARCH_EXTRA
5452 if (area_attr == 0)
5453 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005454 /* Use attributes from match with highest priority among
5455 * 'search_hl' and the match list. */
5456 char_attr = search_hl.attr;
5457 cur = wp->w_match_head;
5458 shl_flag = FALSE;
5459 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005460 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005461 if (shl_flag == FALSE
5462 && ((cur != NULL
5463 && cur->priority > SEARCH_HL_PRIORITY)
5464 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005465 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005466 shl = &search_hl;
5467 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005468 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005469 else
5470 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005471 if ((ptr - line) - 1 == (long)shl->startcol
5472 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005473 char_attr = shl->attr;
5474 if (shl != &search_hl && cur != NULL)
5475 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 }
5478#endif
5479 ScreenAttrs[off] = char_attr;
5480#ifdef FEAT_RIGHTLEFT
5481 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005484 --off;
5485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 else
5487#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005488 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005490 ++off;
5491 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005492 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005493#ifdef FEAT_SYN_HL
5494 eol_hl_off = 1;
5495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498
Bram Moolenaar91170f82006-05-05 21:15:17 +00005499 /*
5500 * At end of the text line.
5501 */
5502 if (c == NUL)
5503 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005504#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005505 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005506 if (wp->w_p_wrap)
5507 v = wp->w_skipcol;
5508 else
5509 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005510
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005511 /* check if line ends before left margin */
5512 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005513 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005514#ifdef FEAT_CONCEAL
5515 /* Get rid of the boguscols now, we want to draw until the right
5516 * edge for 'cursorcolumn'. */
5517 col -= boguscols;
5518 boguscols = 0;
5519#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005520
5521 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005522 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005523
5524 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005525 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5526 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005527 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005528 && lnum != wp->w_cursor.lnum)
5529 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005530# ifdef FEAT_RIGHTLEFT
5531 && !wp->w_p_rl
5532# endif
5533 )
5534 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005535 int rightmost_vcol = 0;
5536 int i;
5537
5538 if (wp->w_p_cuc)
5539 rightmost_vcol = wp->w_virtcol;
5540 if (draw_color_col)
5541 /* determine rightmost colorcolumn to possibly draw */
5542 for (i = 0; color_cols[i] >= 0; ++i)
5543 if (rightmost_vcol < color_cols[i])
5544 rightmost_vcol = color_cols[i];
5545
Bram Moolenaar02631462017-09-22 15:20:32 +02005546 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005547 {
5548 ScreenLines[off] = ' ';
5549#ifdef FEAT_MBYTE
5550 if (enc_utf8)
5551 ScreenLinesUC[off] = 0;
5552#endif
5553 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005554 if (draw_color_col)
5555 draw_color_col = advance_color_col(VCOL_HLC,
5556 &color_cols);
5557
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005558 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005559 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005560 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005561 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005562 else
5563 ScreenAttrs[off++] = 0;
5564
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005565 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005566 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005567
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005568 ++vcol;
5569 }
5570 }
5571#endif
5572
Bram Moolenaar53f81742017-09-22 14:35:51 +02005573 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005574 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 row++;
5576
5577 /*
5578 * Update w_cline_height and w_cline_folded if the cursor line was
5579 * updated (saves a call to plines() later).
5580 */
5581 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5582 {
5583 curwin->w_cline_row = startrow;
5584 curwin->w_cline_height = row - startrow;
5585#ifdef FEAT_FOLDING
5586 curwin->w_cline_folded = FALSE;
5587#endif
5588 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5589 }
5590
5591 break;
5592 }
5593
5594 /* line continues beyond line end */
5595 if (lcs_ext
5596 && !wp->w_p_wrap
5597#ifdef FEAT_DIFF
5598 && filler_todo <= 0
5599#endif
5600 && (
5601#ifdef FEAT_RIGHTLEFT
5602 wp->w_p_rl ? col == 0 :
5603#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005604 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005606 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5608 {
5609 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005610 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611#ifdef FEAT_MBYTE
5612 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005613 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 {
5615 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005616 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005617 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
5619 else
5620 mb_utf8 = FALSE;
5621#endif
5622 }
5623
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005624#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005625 /* advance to the next 'colorcolumn' */
5626 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005627 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005628
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005629 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005630 * highlight the cursor position itself.
5631 * Also highlight the 'colorcolumn' if it is different than
5632 * 'cursorcolumn' */
5633 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005634 if (draw_state == WL_LINE && !lnum_in_visual_area
5635 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005636 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005637 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005638 && lnum != wp->w_cursor.lnum)
5639 {
5640 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005641 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005642 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005643 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005644 {
5645 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005646 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005647 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005648 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005649#endif
5650
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 /*
5652 * Store character to be displayed.
5653 * Skip characters that are left of the screen for 'nowrap'.
5654 */
5655 vcol_prev = vcol;
5656 if (draw_state < WL_LINE || n_skip <= 0)
5657 {
5658 /*
5659 * Store the character.
5660 */
5661#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5662 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5663 {
5664 /* A double-wide character is: put first halve in left cell. */
5665 --off;
5666 --col;
5667 }
5668#endif
5669 ScreenLines[off] = c;
5670#ifdef FEAT_MBYTE
5671 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005672 {
5673 if ((mb_c & 0xff00) == 0x8e00)
5674 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 else if (enc_utf8)
5678 {
5679 if (mb_utf8)
5680 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005681 int i;
5682
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005684 if ((c & 0xff) == 0)
5685 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005686 for (i = 0; i < Screen_mco; ++i)
5687 {
5688 ScreenLinesC[i][off] = u8cc[i];
5689 if (u8cc[i] == 0)
5690 break;
5691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 }
5693 else
5694 ScreenLinesUC[off] = 0;
5695 }
5696 if (multi_attr)
5697 {
5698 ScreenAttrs[off] = multi_attr;
5699 multi_attr = 0;
5700 }
5701 else
5702#endif
5703 ScreenAttrs[off] = char_attr;
5704
5705#ifdef FEAT_MBYTE
5706 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5707 {
5708 /* Need to fill two screen columns. */
5709 ++off;
5710 ++col;
5711 if (enc_utf8)
5712 /* UTF-8: Put a 0 in the second screen char. */
5713 ScreenLines[off] = 0;
5714 else
5715 /* DBCS: Put second byte in the second screen char. */
5716 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005717 if (draw_state > WL_NR
5718#ifdef FEAT_DIFF
5719 && filler_todo <= 0
5720#endif
5721 )
5722 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 /* When "tocol" is halfway a character, set it to the end of
5724 * the character, otherwise highlighting won't stop. */
5725 if (tocol == vcol)
5726 ++tocol;
5727#ifdef FEAT_RIGHTLEFT
5728 if (wp->w_p_rl)
5729 {
5730 /* now it's time to backup one cell */
5731 --off;
5732 --col;
5733 }
5734#endif
5735 }
5736#endif
5737#ifdef FEAT_RIGHTLEFT
5738 if (wp->w_p_rl)
5739 {
5740 --off;
5741 --col;
5742 }
5743 else
5744#endif
5745 {
5746 ++off;
5747 ++col;
5748 }
5749 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005750#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005751 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005752 {
5753 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005754 ++vcol_off;
5755 if (n_extra > 0)
5756 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005757 if (wp->w_p_wrap)
5758 {
5759 /*
5760 * Special voodoo required if 'wrap' is on.
5761 *
5762 * Advance the column indicator to force the line
5763 * drawing to wrap early. This will make the line
5764 * take up the same screen space when parts are concealed,
5765 * so that cursor line computations aren't messed up.
5766 *
5767 * To avoid the fictitious advance of 'col' causing
5768 * trailing junk to be written out of the screen line
5769 * we are building, 'boguscols' keeps track of the number
5770 * of bad columns we have advanced.
5771 */
5772 if (n_extra > 0)
5773 {
5774 vcol += n_extra;
5775# ifdef FEAT_RIGHTLEFT
5776 if (wp->w_p_rl)
5777 {
5778 col -= n_extra;
5779 boguscols -= n_extra;
5780 }
5781 else
5782# endif
5783 {
5784 col += n_extra;
5785 boguscols += n_extra;
5786 }
5787 n_extra = 0;
5788 n_attr = 0;
5789 }
5790
5791
5792# ifdef FEAT_MBYTE
5793 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5794 {
5795 /* Need to fill two screen columns. */
5796# ifdef FEAT_RIGHTLEFT
5797 if (wp->w_p_rl)
5798 {
5799 --boguscols;
5800 --col;
5801 }
5802 else
5803# endif
5804 {
5805 ++boguscols;
5806 ++col;
5807 }
5808 }
5809# endif
5810
5811# ifdef FEAT_RIGHTLEFT
5812 if (wp->w_p_rl)
5813 {
5814 --boguscols;
5815 --col;
5816 }
5817 else
5818# endif
5819 {
5820 ++boguscols;
5821 ++col;
5822 }
5823 }
5824 else
5825 {
5826 if (n_extra > 0)
5827 {
5828 vcol += n_extra;
5829 n_extra = 0;
5830 n_attr = 0;
5831 }
5832 }
5833
5834 }
5835#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 else
5837 --n_skip;
5838
Bram Moolenaar64486672010-05-16 15:46:46 +02005839 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5840 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005841 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842#ifdef FEAT_DIFF
5843 && filler_todo <= 0
5844#endif
5845 )
5846 ++vcol;
5847
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005848#ifdef FEAT_SYN_HL
5849 if (vcol_save_attr >= 0)
5850 char_attr = vcol_save_attr;
5851#endif
5852
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 /* restore attributes after "predeces" in 'listchars' */
5854 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5855 char_attr = saved_attr3;
5856
5857 /* restore attributes after last 'listchars' or 'number' char */
5858 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5859 char_attr = saved_attr2;
5860
5861 /*
5862 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005863 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 */
5865 if ((
5866#ifdef FEAT_RIGHTLEFT
5867 wp->w_p_rl ? (col < 0) :
5868#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005869 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 && (*ptr != NUL
5871#ifdef FEAT_DIFF
5872 || filler_todo > 0
5873#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005874 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5876 )
5877 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005878#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005879 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005880 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005881 boguscols = 0;
5882#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005883 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005884 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 ++row;
5887 ++screen_row;
5888
5889 /* When not wrapping and finished diff lines, or when displayed
5890 * '$' and highlighting until last column, break here. */
5891 if ((!wp->w_p_wrap
5892#ifdef FEAT_DIFF
5893 && filler_todo <= 0
5894#endif
5895 ) || lcs_eol_one == -1)
5896 break;
5897
5898 /* When the window is too narrow draw all "@" lines. */
5899 if (draw_state != WL_LINE
5900#ifdef FEAT_DIFF
5901 && filler_todo <= 0
5902#endif
5903 )
5904 {
5905 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 row = endrow;
5908 }
5909
5910 /* When line got too long for screen break here. */
5911 if (row == endrow)
5912 {
5913 ++row;
5914 break;
5915 }
5916
5917 if (screen_cur_row == screen_row - 1
5918#ifdef FEAT_DIFF
5919 && filler_todo <= 0
5920#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005921 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 {
5923 /* Remember that the line wraps, used for modeless copy. */
5924 LineWraps[screen_row - 1] = TRUE;
5925
5926 /*
5927 * Special trick to make copy/paste of wrapped lines work with
5928 * xterm/screen: write an extra character beyond the end of
5929 * the line. This will work with all terminal types
5930 * (regardless of the xn,am settings).
5931 * Only do this on a fast tty.
5932 * Only do this if the cursor is on the current line
5933 * (something has been written in it).
5934 * Don't do this for the GUI.
5935 * Don't do this for double-width characters.
5936 * Don't do this for a window not at the right screen border.
5937 */
5938 if (p_tf
5939#ifdef FEAT_GUI
5940 && !gui.in_use
5941#endif
5942#ifdef FEAT_MBYTE
5943 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005944 && ((*mb_off2cells)(LineOffset[screen_row],
5945 LineOffset[screen_row] + screen_Columns)
5946 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005948 + (int)Columns - 2,
5949 LineOffset[screen_row] + screen_Columns)
5950 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951#endif
5952 )
5953 {
5954 /* First make sure we are at the end of the screen line,
5955 * then output the same character again to let the
5956 * terminal know about the wrap. If the terminal doesn't
5957 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005958 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 screen_char(LineOffset[screen_row - 1]
5960 + (unsigned)Columns - 1,
5961 screen_row - 1, (int)(Columns - 1));
5962
5963#ifdef FEAT_MBYTE
5964 /* When there is a multi-byte character, just output a
5965 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005966 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5967 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 out_char(' ');
5969 else
5970#endif
5971 out_char(ScreenLines[LineOffset[screen_row - 1]
5972 + (Columns - 1)]);
5973 /* force a redraw of the first char on the next line */
5974 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5975 screen_start(); /* don't know where cursor is now */
5976 }
5977 }
5978
5979 col = 0;
5980 off = (unsigned)(current_ScreenLine - ScreenLines);
5981#ifdef FEAT_RIGHTLEFT
5982 if (wp->w_p_rl)
5983 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005984 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 off += col;
5986 }
5987#endif
5988
5989 /* reset the drawing state for the start of a wrapped line */
5990 draw_state = WL_START;
5991 saved_n_extra = n_extra;
5992 saved_p_extra = p_extra;
5993 saved_c_extra = c_extra;
5994 saved_char_attr = char_attr;
5995 n_extra = 0;
5996 lcs_prec_todo = lcs_prec;
5997#ifdef FEAT_LINEBREAK
5998# ifdef FEAT_DIFF
5999 if (filler_todo <= 0)
6000# endif
6001 need_showbreak = TRUE;
6002#endif
6003#ifdef FEAT_DIFF
6004 --filler_todo;
6005 /* When the filler lines are actually below the last line of the
6006 * file, don't draw the line itself, break here. */
6007 if (filler_todo == 0 && wp->w_botfill)
6008 break;
6009#endif
6010 }
6011
6012 } /* for every character in the line */
6013
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006014#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006015 /* After an empty line check first word for capital. */
6016 if (*skipwhite(line) == NUL)
6017 {
6018 capcol_lnum = lnum + 1;
6019 cap_col = 0;
6020 }
6021#endif
6022
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006023 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 return row;
6025}
6026
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006027#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006028/*
6029 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006030 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006031 */
6032 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006033comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006034{
6035 int i;
6036
6037 for (i = 0; i < Screen_mco; ++i)
6038 {
6039 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6040 return TRUE;
6041 if (ScreenLinesC[i][off_from] == 0)
6042 break;
6043 }
6044 return FALSE;
6045}
6046#endif
6047
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048/*
6049 * Check whether the given character needs redrawing:
6050 * - the (first byte of the) character is different
6051 * - the attributes are different
6052 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006053 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 */
6055 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006056char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057{
6058 if (cols > 0
6059 && ((ScreenLines[off_from] != ScreenLines[off_to]
6060 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6061
6062#ifdef FEAT_MBYTE
6063 || (enc_dbcs != 0
6064 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6065 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6066 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6067 : (cols > 1 && ScreenLines[off_from + 1]
6068 != ScreenLines[off_to + 1])))
6069 || (enc_utf8
6070 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6071 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006072 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006073 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6074 && ScreenLines[off_from + 1]
6075 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076#endif
6077 ))
6078 return TRUE;
6079 return FALSE;
6080}
6081
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006082#if defined(FEAT_TERMINAL) || defined(PROTO)
6083/*
6084 * Return the index in ScreenLines[] for the current screen line.
6085 */
6086 int
6087screen_get_current_line_off()
6088{
6089 return (int)(current_ScreenLine - ScreenLines);
6090}
6091#endif
6092
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093/*
6094 * Move one "cooked" screen line to the screen, but only the characters that
6095 * have actually changed. Handle insert/delete character.
6096 * "coloff" gives the first column on the screen for this line.
6097 * "endcol" gives the columns where valid characters are.
6098 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6099 * needs to be cleared, negative otherwise.
6100 * "rlflag" is TRUE in a rightleft window:
6101 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6102 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6103 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006104 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006105screen_line(
6106 int row,
6107 int coloff,
6108 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006109 int clear_width,
6110 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111{
6112 unsigned off_from;
6113 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006114#ifdef FEAT_MBYTE
6115 unsigned max_off_from;
6116 unsigned max_off_to;
6117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 int force = FALSE; /* force update rest of the line */
6121 int redraw_this /* bool: does character need redraw? */
6122#ifdef FEAT_GUI
6123 = TRUE /* For GUI when while-loop empty */
6124#endif
6125 ;
6126 int redraw_next; /* redraw_this for next character */
6127#ifdef FEAT_MBYTE
6128 int clear_next = FALSE;
6129 int char_cells; /* 1: normal char */
6130 /* 2: occupies two display cells */
6131# define CHAR_CELLS char_cells
6132#else
6133# define CHAR_CELLS 1
6134#endif
6135
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#ifdef FEAT_MBYTE
6149 max_off_from = off_from + screen_Columns;
6150 max_off_to = LineOffset[row] + screen_Columns;
6151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152
6153#ifdef FEAT_RIGHTLEFT
6154 if (rlflag)
6155 {
6156 /* Clear rest first, because it's left of the text. */
6157 if (clear_width > 0)
6158 {
6159 while (col <= endcol && ScreenLines[off_to] == ' '
6160 && ScreenAttrs[off_to] == 0
6161# ifdef FEAT_MBYTE
6162 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6163# endif
6164 )
6165 {
6166 ++off_to;
6167 ++col;
6168 }
6169 if (col <= endcol)
6170 screen_fill(row, row + 1, col + coloff,
6171 endcol + coloff + 1, ' ', ' ', 0);
6172 }
6173 col = endcol + 1;
6174 off_to = LineOffset[row] + col + coloff;
6175 off_from += col;
6176 endcol = (clear_width > 0 ? clear_width : -clear_width);
6177 }
6178#endif /* FEAT_RIGHTLEFT */
6179
6180 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6181
6182 while (col < endcol)
6183 {
6184#ifdef FEAT_MBYTE
6185 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006186 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 else
6188 char_cells = 1;
6189#endif
6190
6191 redraw_this = redraw_next;
6192 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6193 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6194
6195#ifdef FEAT_GUI
6196 /* If the next character was bold, then redraw the current character to
6197 * remove any pixels that might have spilt over into us. This only
6198 * happens in the GUI.
6199 */
6200 if (redraw_next && gui.in_use)
6201 {
6202 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006203 if (hl > HL_ALL)
6204 hl = syn_attr2attr(hl);
6205 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 redraw_this = TRUE;
6207 }
6208#endif
6209
6210 if (redraw_this)
6211 {
6212 /*
6213 * Special handling when 'xs' termcap flag set (hpterm):
6214 * Attributes for characters are stored at the position where the
6215 * cursor is when writing the highlighting code. The
6216 * start-highlighting code must be written with the cursor on the
6217 * first highlighted character. The stop-highlighting code must
6218 * be written with the cursor just after the last highlighted
6219 * character.
6220 * Overwriting a character doesn't remove it's highlighting. Need
6221 * to clear the rest of the line, and force redrawing it
6222 * completely.
6223 */
6224 if ( p_wiv
6225 && !force
6226#ifdef FEAT_GUI
6227 && !gui.in_use
6228#endif
6229 && ScreenAttrs[off_to] != 0
6230 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6231 {
6232 /*
6233 * Need to remove highlighting attributes here.
6234 */
6235 windgoto(row, col + coloff);
6236 out_str(T_CE); /* clear rest of this screen line */
6237 screen_start(); /* don't know where cursor is now */
6238 force = TRUE; /* force redraw of rest of the line */
6239 redraw_next = TRUE; /* or else next char would miss out */
6240
6241 /*
6242 * If the previous character was highlighted, need to stop
6243 * highlighting at this character.
6244 */
6245 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6246 {
6247 screen_attr = ScreenAttrs[off_to - 1];
6248 term_windgoto(row, col + coloff);
6249 screen_stop_highlight();
6250 }
6251 else
6252 screen_attr = 0; /* highlighting has stopped */
6253 }
6254#ifdef FEAT_MBYTE
6255 if (enc_dbcs != 0)
6256 {
6257 /* Check if overwriting a double-byte with a single-byte or
6258 * the other way around requires another character to be
6259 * redrawn. For UTF-8 this isn't needed, because comparing
6260 * ScreenLinesUC[] is sufficient. */
6261 if (char_cells == 1
6262 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006263 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 {
6265 /* Writing a single-cell character over a double-cell
6266 * character: need to redraw the next cell. */
6267 ScreenLines[off_to + 1] = 0;
6268 redraw_next = TRUE;
6269 }
6270 else if (char_cells == 2
6271 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006272 && (*mb_off2cells)(off_to, max_off_to) == 1
6273 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 {
6275 /* Writing the second half of a double-cell character over
6276 * a double-cell character: need to redraw the second
6277 * cell. */
6278 ScreenLines[off_to + 2] = 0;
6279 redraw_next = TRUE;
6280 }
6281
6282 if (enc_dbcs == DBCS_JPNU)
6283 ScreenLines2[off_to] = ScreenLines2[off_from];
6284 }
6285 /* When writing a single-width character over a double-width
6286 * character and at the end of the redrawn text, need to clear out
6287 * the right halve of the old character.
6288 * Also required when writing the right halve of a double-width
6289 * char over the left halve of an existing one. */
6290 if (has_mbyte && col + char_cells == endcol
6291 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006292 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006294 && (*mb_off2cells)(off_to, max_off_to) == 1
6295 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 clear_next = TRUE;
6297#endif
6298
6299 ScreenLines[off_to] = ScreenLines[off_from];
6300#ifdef FEAT_MBYTE
6301 if (enc_utf8)
6302 {
6303 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6304 if (ScreenLinesUC[off_from] != 0)
6305 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006306 int i;
6307
6308 for (i = 0; i < Screen_mco; ++i)
6309 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 }
6311 }
6312 if (char_cells == 2)
6313 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6314#endif
6315
6316#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006317 /* The bold trick makes a single column of pixels appear in the
6318 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 * character should be redrawn too. This happens for our own GUI
6320 * and for some xterms. */
6321 if (
6322# ifdef FEAT_GUI
6323 gui.in_use
6324# endif
6325# if defined(FEAT_GUI) && defined(UNIX)
6326 ||
6327# endif
6328# ifdef UNIX
6329 term_is_xterm
6330# endif
6331 )
6332 {
6333 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006334 if (hl > HL_ALL)
6335 hl = syn_attr2attr(hl);
6336 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 redraw_next = TRUE;
6338 }
6339#endif
6340 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6341#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006342 /* For simplicity set the attributes of second half of a
6343 * double-wide character equal to the first half. */
6344 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006346
6347 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 else
6350#endif
6351 screen_char(off_to, row, col + coloff);
6352 }
6353 else if ( p_wiv
6354#ifdef FEAT_GUI
6355 && !gui.in_use
6356#endif
6357 && col + coloff > 0)
6358 {
6359 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6360 {
6361 /*
6362 * Don't output stop-highlight when moving the cursor, it will
6363 * stop the highlighting when it should continue.
6364 */
6365 screen_attr = 0;
6366 }
6367 else if (screen_attr != 0)
6368 screen_stop_highlight();
6369 }
6370
6371 off_to += CHAR_CELLS;
6372 off_from += CHAR_CELLS;
6373 col += CHAR_CELLS;
6374 }
6375
6376#ifdef FEAT_MBYTE
6377 if (clear_next)
6378 {
6379 /* Clear the second half of a double-wide character of which the left
6380 * half was overwritten with a single-wide character. */
6381 ScreenLines[off_to] = ' ';
6382 if (enc_utf8)
6383 ScreenLinesUC[off_to] = 0;
6384 screen_char(off_to, row, col + coloff);
6385 }
6386#endif
6387
6388 if (clear_width > 0
6389#ifdef FEAT_RIGHTLEFT
6390 && !rlflag
6391#endif
6392 )
6393 {
6394#ifdef FEAT_GUI
6395 int startCol = col;
6396#endif
6397
6398 /* blank out the rest of the line */
6399 while (col < clear_width && ScreenLines[off_to] == ' '
6400 && ScreenAttrs[off_to] == 0
6401#ifdef FEAT_MBYTE
6402 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6403#endif
6404 )
6405 {
6406 ++off_to;
6407 ++col;
6408 }
6409 if (col < clear_width)
6410 {
6411#ifdef FEAT_GUI
6412 /*
6413 * In the GUI, clearing the rest of the line may leave pixels
6414 * behind if the first character cleared was bold. Some bold
6415 * fonts spill over the left. In this case we redraw the previous
6416 * character too. If we didn't skip any blanks above, then we
6417 * only redraw if the character wasn't already redrawn anyway.
6418 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006419 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 {
6421 hl = ScreenAttrs[off_to];
6422 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006423 {
6424 int prev_cells = 1;
6425# ifdef FEAT_MBYTE
6426 if (enc_utf8)
6427 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6428 * that its width is 2. */
6429 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6430 else if (enc_dbcs != 0)
6431 {
6432 /* find previous character by counting from first
6433 * column and get its width. */
6434 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006435 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006436
6437 while (off < off_to)
6438 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006439 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006440 off += prev_cells;
6441 }
6442 }
6443
6444 if (enc_dbcs != 0 && prev_cells > 1)
6445 screen_char_2(off_to - prev_cells, row,
6446 col + coloff - prev_cells);
6447 else
6448# endif
6449 screen_char(off_to - prev_cells, row,
6450 col + coloff - prev_cells);
6451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 }
6453#endif
6454 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6455 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 off_to += clear_width - col;
6457 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 }
6459 }
6460
6461 if (clear_width > 0)
6462 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 /* For a window that's left of another, draw the separator char. */
6464 if (col + coloff < Columns)
6465 {
6466 int c;
6467
6468 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006469 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006470#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006471 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6472 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 || ScreenAttrs[off_to] != hl)
6475 {
6476 ScreenLines[off_to] = c;
6477 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006478#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 if (enc_utf8)
6480 {
6481 if (c >= 0x80)
6482 {
6483 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006484 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 }
6486 else
6487 ScreenLinesUC[off_to] = 0;
6488 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 screen_char(off_to, row, col + coloff);
6491 }
6492 }
6493 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 LineWraps[row] = FALSE;
6495 }
6496}
6497
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006498#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006500 * Mirror text "str" for right-left displaying.
6501 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006503 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006504rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505{
6506 char_u *p1, *p2;
6507 int t;
6508
6509 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6510 {
6511 t = *p1;
6512 *p1 = *p2;
6513 *p2 = t;
6514 }
6515}
6516#endif
6517
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518/*
6519 * mark all status lines for redraw; used after first :cd
6520 */
6521 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006522status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523{
6524 win_T *wp;
6525
Bram Moolenaar29323592016-07-24 22:04:11 +02006526 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 if (wp->w_status_height)
6528 {
6529 wp->w_redr_status = TRUE;
6530 redraw_later(VALID);
6531 }
6532}
6533
6534/*
6535 * mark all status lines of the current buffer for redraw
6536 */
6537 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006538status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539{
6540 win_T *wp;
6541
Bram Moolenaar29323592016-07-24 22:04:11 +02006542 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6544 {
6545 wp->w_redr_status = TRUE;
6546 redraw_later(VALID);
6547 }
6548}
6549
6550/*
6551 * Redraw all status lines that need to be redrawn.
6552 */
6553 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006554redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555{
6556 win_T *wp;
6557
Bram Moolenaar29323592016-07-24 22:04:11 +02006558 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006560 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006561 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006562 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564
Bram Moolenaar4033c552017-09-16 20:54:51 +02006565#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566/*
6567 * Redraw all status lines at the bottom of frame "frp".
6568 */
6569 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006570win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571{
6572 if (frp->fr_layout == FR_LEAF)
6573 frp->fr_win->w_redr_status = TRUE;
6574 else if (frp->fr_layout == FR_ROW)
6575 {
6576 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6577 win_redraw_last_status(frp);
6578 }
6579 else /* frp->fr_layout == FR_COL */
6580 {
6581 frp = frp->fr_child;
6582 while (frp->fr_next != NULL)
6583 frp = frp->fr_next;
6584 win_redraw_last_status(frp);
6585 }
6586}
6587#endif
6588
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589/*
6590 * Draw the verticap separator right of window "wp" starting with line "row".
6591 */
6592 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006593draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594{
6595 int hl;
6596 int c;
6597
6598 if (wp->w_vsep_width)
6599 {
6600 /* draw the vertical separator right of this window */
6601 c = fillchar_vsep(&hl);
6602 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6603 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6604 c, ' ', hl);
6605 }
6606}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607
6608#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006609static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610
6611/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006612 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 */
6614 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006615status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616{
6617 int len = 0;
6618
6619#ifdef FEAT_MENU
6620 int emenu = (xp->xp_context == EXPAND_MENUS
6621 || xp->xp_context == EXPAND_MENUNAMES);
6622
6623 /* Check for menu separators - replace with '|'. */
6624 if (emenu && menu_is_separator(s))
6625 return 1;
6626#endif
6627
6628 while (*s != NUL)
6629 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006630 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006631 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006632 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 }
6634
6635 return len;
6636}
6637
6638/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006639 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006640 * These are backslashes used for escaping. Do show backslashes in help tags.
6641 */
6642 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006643skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006644{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006645 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006646#ifdef FEAT_MENU
6647 || ((xp->xp_context == EXPAND_MENUS
6648 || xp->xp_context == EXPAND_MENUNAMES)
6649 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6650#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006651 )
6652 {
6653#ifndef BACKSLASH_IN_FILENAME
6654 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6655 return 2;
6656#endif
6657 return 1;
6658 }
6659 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006660}
6661
6662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 * Show wildchar matches in the status line.
6664 * Show at least the "match" item.
6665 * We start at item 'first_match' in the list and show all matches that fit.
6666 *
6667 * If inversion is possible we use it. Else '=' characters are used.
6668 */
6669 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006670win_redr_status_matches(
6671 expand_T *xp,
6672 int num_matches,
6673 char_u **matches, /* list of matches */
6674 int match,
6675 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676{
6677#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6678 int row;
6679 char_u *buf;
6680 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006681 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 int fillchar;
6683 int attr;
6684 int i;
6685 int highlight = TRUE;
6686 char_u *selstart = NULL;
6687 int selstart_col = 0;
6688 char_u *selend = NULL;
6689 static int first_match = 0;
6690 int add_left = FALSE;
6691 char_u *s;
6692#ifdef FEAT_MENU
6693 int emenu;
6694#endif
6695#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6696 int l;
6697#endif
6698
6699 if (matches == NULL) /* interrupted completion? */
6700 return;
6701
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006702#ifdef FEAT_MBYTE
6703 if (has_mbyte)
6704 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6705 else
6706#endif
6707 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 if (buf == NULL)
6709 return;
6710
6711 if (match == -1) /* don't show match but original text */
6712 {
6713 match = 0;
6714 highlight = FALSE;
6715 }
6716 /* count 1 for the ending ">" */
6717 clen = status_match_len(xp, L_MATCH(match)) + 3;
6718 if (match == 0)
6719 first_match = 0;
6720 else if (match < first_match)
6721 {
6722 /* jumping left, as far as we can go */
6723 first_match = match;
6724 add_left = TRUE;
6725 }
6726 else
6727 {
6728 /* check if match fits on the screen */
6729 for (i = first_match; i < match; ++i)
6730 clen += status_match_len(xp, L_MATCH(i)) + 2;
6731 if (first_match > 0)
6732 clen += 2;
6733 /* jumping right, put match at the left */
6734 if ((long)clen > Columns)
6735 {
6736 first_match = match;
6737 /* if showing the last match, we can add some on the left */
6738 clen = 2;
6739 for (i = match; i < num_matches; ++i)
6740 {
6741 clen += status_match_len(xp, L_MATCH(i)) + 2;
6742 if ((long)clen >= Columns)
6743 break;
6744 }
6745 if (i == num_matches)
6746 add_left = TRUE;
6747 }
6748 }
6749 if (add_left)
6750 while (first_match > 0)
6751 {
6752 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6753 if ((long)clen >= Columns)
6754 break;
6755 --first_match;
6756 }
6757
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006758 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759
6760 if (first_match == 0)
6761 {
6762 *buf = NUL;
6763 len = 0;
6764 }
6765 else
6766 {
6767 STRCPY(buf, "< ");
6768 len = 2;
6769 }
6770 clen = len;
6771
6772 i = first_match;
6773 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6774 {
6775 if (i == match)
6776 {
6777 selstart = buf + len;
6778 selstart_col = clen;
6779 }
6780
6781 s = L_MATCH(i);
6782 /* Check for menu separators - replace with '|' */
6783#ifdef FEAT_MENU
6784 emenu = (xp->xp_context == EXPAND_MENUS
6785 || xp->xp_context == EXPAND_MENUNAMES);
6786 if (emenu && menu_is_separator(s))
6787 {
6788 STRCPY(buf + len, transchar('|'));
6789 l = (int)STRLEN(buf + len);
6790 len += l;
6791 clen += l;
6792 }
6793 else
6794#endif
6795 for ( ; *s != NUL; ++s)
6796 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006797 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 clen += ptr2cells(s);
6799#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006800 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {
6802 STRNCPY(buf + len, s, l);
6803 s += l - 1;
6804 len += l;
6805 }
6806 else
6807#endif
6808 {
6809 STRCPY(buf + len, transchar_byte(*s));
6810 len += (int)STRLEN(buf + len);
6811 }
6812 }
6813 if (i == match)
6814 selend = buf + len;
6815
6816 *(buf + len++) = ' ';
6817 *(buf + len++) = ' ';
6818 clen += 2;
6819 if (++i == num_matches)
6820 break;
6821 }
6822
6823 if (i != num_matches)
6824 {
6825 *(buf + len++) = '>';
6826 ++clen;
6827 }
6828
6829 buf[len] = NUL;
6830
6831 row = cmdline_row - 1;
6832 if (row >= 0)
6833 {
6834 if (wild_menu_showing == 0)
6835 {
6836 if (msg_scrolled > 0)
6837 {
6838 /* Put the wildmenu just above the command line. If there is
6839 * no room, scroll the screen one line up. */
6840 if (cmdline_row == Rows - 1)
6841 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006842 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 ++msg_scrolled;
6844 }
6845 else
6846 {
6847 ++cmdline_row;
6848 ++row;
6849 }
6850 wild_menu_showing = WM_SCROLLED;
6851 }
6852 else
6853 {
6854 /* Create status line if needed by setting 'laststatus' to 2.
6855 * Set 'winminheight' to zero to avoid that the window is
6856 * resized. */
6857 if (lastwin->w_status_height == 0)
6858 {
6859 save_p_ls = p_ls;
6860 save_p_wmh = p_wmh;
6861 p_ls = 2;
6862 p_wmh = 0;
6863 last_status(FALSE);
6864 }
6865 wild_menu_showing = WM_SHOWN;
6866 }
6867 }
6868
6869 screen_puts(buf, row, 0, attr);
6870 if (selstart != NULL && highlight)
6871 {
6872 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006873 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 }
6875
6876 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6877 }
6878
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 vim_free(buf);
6881}
6882#endif
6883
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884/*
6885 * Redraw the status line of window wp.
6886 *
6887 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006888 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6889 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006891 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006892win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893{
6894 int row;
6895 char_u *p;
6896 int len;
6897 int fillchar;
6898 int attr;
6899 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006900 static int busy = FALSE;
6901
6902 /* It's possible to get here recursively when 'statusline' (indirectly)
6903 * invokes ":redrawstatus". Simply ignore the call then. */
6904 if (busy)
6905 return;
6906 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907
6908 wp->w_redr_status = FALSE;
6909 if (wp->w_status_height == 0)
6910 {
6911 /* no status line, can only be last window */
6912 redraw_cmdline = TRUE;
6913 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006914 else if (!redrawing()
6915#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006916 // don't update status line when popup menu is visible and may be
6917 // drawn over it, unless it will be redrawn later
6918 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006919#endif
6920 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 {
6922 /* Don't redraw right now, do it later. */
6923 wp->w_redr_status = TRUE;
6924 }
6925#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006926 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 {
6928 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006929 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 }
6931#endif
6932 else
6933 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006934 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006936 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 p = NameBuff;
6938 len = (int)STRLEN(p);
6939
Bram Moolenaard85f2712017-07-28 21:51:57 +02006940 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941#ifdef FEAT_QUICKFIX
6942 || wp->w_p_pvw
6943#endif
6944 || bufIsChanged(wp->w_buffer)
6945 || wp->w_buffer->b_p_ro)
6946 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006947 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006949 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 len += (int)STRLEN(p + len);
6951 }
6952#ifdef FEAT_QUICKFIX
6953 if (wp->w_p_pvw)
6954 {
6955 STRCPY(p + len, _("[Preview]"));
6956 len += (int)STRLEN(p + len);
6957 }
6958#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006959 if (bufIsChanged(wp->w_buffer)
6960#ifdef FEAT_TERMINAL
6961 && !bt_terminal(wp->w_buffer)
6962#endif
6963 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {
6965 STRCPY(p + len, "[+]");
6966 len += 3;
6967 }
6968 if (wp->w_buffer->b_p_ro)
6969 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006970 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006971 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 }
6973
Bram Moolenaar02631462017-09-22 15:20:32 +02006974 this_ru_col = ru_col - (Columns - wp->w_width);
6975 if (this_ru_col < (wp->w_width + 1) / 2)
6976 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 if (this_ru_col <= 1)
6978 {
6979 p = (char_u *)"<"; /* No room for file name! */
6980 len = 1;
6981 }
6982 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983#ifdef FEAT_MBYTE
6984 if (has_mbyte)
6985 {
6986 int clen = 0, i;
6987
6988 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006989 clen = mb_string2cells(p, -1);
6990
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 /* Find first character that will fit.
6992 * Going from start to end is much faster for DBCS. */
6993 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006994 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 clen -= (*mb_ptr2cells)(p + i);
6996 len = clen;
6997 if (i > 0)
6998 {
6999 p = p + i - 1;
7000 *p = '<';
7001 ++len;
7002 }
7003
7004 }
7005 else
7006#endif
7007 if (len > this_ru_col - 1)
7008 {
7009 p += len - (this_ru_col - 1);
7010 *p = '<';
7011 len = this_ru_col - 1;
7012 }
7013
7014 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007015 screen_puts(p, row, wp->w_wincol, attr);
7016 screen_fill(row, row + 1, len + wp->w_wincol,
7017 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007019 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7021 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007022 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023
7024#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007025 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026#endif
7027 }
7028
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 /*
7030 * May need to draw the character below the vertical separator.
7031 */
7032 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7033 {
7034 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007035 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 else
7037 fillchar = fillchar_vsep(&attr);
7038 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7039 attr);
7040 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007041 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042}
7043
Bram Moolenaar238a5642006-02-21 22:12:05 +00007044#ifdef FEAT_STL_OPT
7045/*
7046 * Redraw the status line according to 'statusline' and take care of any
7047 * errors encountered.
7048 */
7049 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007050redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007051{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007052 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007053 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007054
7055 /* When called recursively return. This can happen when the statusline
7056 * contains an expression that triggers a redraw. */
7057 if (entered)
7058 return;
7059 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007060
Bram Moolenaara742e082016-04-05 21:10:38 +02007061 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007062 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007063 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007064 {
7065 /* When there is an error disable the statusline, otherwise the
7066 * display is messed up with errors and a redraw triggers the problem
7067 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007068 set_string_option_direct((char_u *)"statusline", -1,
7069 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007070 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007071 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007072 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007073 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007074}
7075#endif
7076
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077/*
7078 * Return TRUE if the status line of window "wp" is connected to the status
7079 * line of the window right of it. If not, then it's a vertical separator.
7080 * Only call if (wp->w_vsep_width != 0).
7081 */
7082 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007083stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084{
7085 frame_T *fr;
7086
7087 fr = wp->w_frame;
7088 while (fr->fr_parent != NULL)
7089 {
7090 if (fr->fr_parent->fr_layout == FR_COL)
7091 {
7092 if (fr->fr_next != NULL)
7093 break;
7094 }
7095 else
7096 {
7097 if (fr->fr_next != NULL)
7098 return TRUE;
7099 }
7100 fr = fr->fr_parent;
7101 }
7102 return FALSE;
7103}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106/*
7107 * Get the value to show for the language mappings, active 'keymap'.
7108 */
7109 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007110get_keymap_str(
7111 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007112 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007113 char_u *buf, /* buffer for the result */
7114 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115{
7116 char_u *p;
7117
7118 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7119 return FALSE;
7120
7121 {
7122#ifdef FEAT_EVAL
7123 buf_T *old_curbuf = curbuf;
7124 win_T *old_curwin = curwin;
7125 char_u *s;
7126
7127 curbuf = wp->w_buffer;
7128 curwin = wp;
7129 STRCPY(buf, "b:keymap_name"); /* must be writable */
7130 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007131 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 --emsg_skip;
7133 curbuf = old_curbuf;
7134 curwin = old_curwin;
7135 if (p == NULL || *p == NUL)
7136#endif
7137 {
7138#ifdef FEAT_KEYMAP
7139 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7140 p = wp->w_buffer->b_p_keymap;
7141 else
7142#endif
7143 p = (char_u *)"lang";
7144 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007145 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 buf[0] = NUL;
7147#ifdef FEAT_EVAL
7148 vim_free(s);
7149#endif
7150 }
7151 return buf[0] != NUL;
7152}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
7154#if defined(FEAT_STL_OPT) || defined(PROTO)
7155/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007156 * Redraw the status line or ruler of window "wp".
7157 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 */
7159 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007160win_redr_custom(
7161 win_T *wp,
7162 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007164 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 int attr;
7166 int curattr;
7167 int row;
7168 int col = 0;
7169 int maxwidth;
7170 int width;
7171 int n;
7172 int len;
7173 int fillchar;
7174 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007175 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007177 struct stl_hlrec hltab[STL_MAX_ITEM];
7178 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007179 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007180 win_T *ewp;
7181 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182
Bram Moolenaar1d633412013-12-11 15:52:01 +01007183 /* There is a tiny chance that this gets called recursively: When
7184 * redrawing a status line triggers redrawing the ruler or tabline.
7185 * Avoid trouble by not allowing recursion. */
7186 if (entered)
7187 return;
7188 entered = TRUE;
7189
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007191 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007193 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007194 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007195 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007196 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007197 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007198 maxwidth = Columns;
7199# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007200 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007201# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007203 else
7204 {
7205 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007206 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007207 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007208
7209 if (draw_ruler)
7210 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007211 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007212 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007213 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007214 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007215 if (*++stl == '-')
7216 stl++;
7217 if (atoi((char *)stl))
7218 while (VIM_ISDIGIT(*stl))
7219 stl++;
7220 if (*stl++ != '(')
7221 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007222 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007223 col = ru_col - (Columns - wp->w_width);
7224 if (col < (wp->w_width + 1) / 2)
7225 col = (wp->w_width + 1) / 2;
7226 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007227 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007228 {
7229 row = Rows - 1;
7230 --maxwidth; /* writing in last column may cause scrolling */
7231 fillchar = ' ';
7232 attr = 0;
7233 }
7234
7235# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007236 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007237# endif
7238 }
7239 else
7240 {
7241 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007242 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007243 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007244 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007245# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007246 use_sandbox = was_set_insecurely((char_u *)"statusline",
7247 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007248# endif
7249 }
7250
Bram Moolenaar53f81742017-09-22 14:35:51 +02007251 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007252 }
7253
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007255 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256
Bram Moolenaar61452852011-02-01 18:01:11 +01007257 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7258 * the cursor away and back. */
7259 ewp = wp == NULL ? curwin : wp;
7260 p_crb_save = ewp->w_p_crb;
7261 ewp->w_p_crb = FALSE;
7262
Bram Moolenaar362f3562009-11-03 16:20:34 +00007263 /* Make a copy, because the statusline may include a function call that
7264 * might change the option value and free the memory. */
7265 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007266 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007267 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007268 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007269 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007270 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007272 /* Make all characters printable. */
7273 p = transstr(buf);
7274 if (p != NULL)
7275 {
7276 vim_strncpy(buf, p, sizeof(buf) - 1);
7277 vim_free(p);
7278 }
7279
7280 /* fill up with "fillchar" */
7281 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007282 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 {
7284#ifdef FEAT_MBYTE
7285 len += (*mb_char2bytes)(fillchar, buf + len);
7286#else
7287 buf[len++] = fillchar;
7288#endif
7289 ++width;
7290 }
7291 buf[len] = NUL;
7292
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007293 /*
7294 * Draw each snippet with the specified highlighting.
7295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 curattr = attr;
7297 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007298 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007300 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 screen_puts_len(p, len, row, col, curattr);
7302 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007303 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007305 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007307 else if (hltab[n].userhl < 0)
7308 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007309#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007310 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7311 && wp->w_status_height != 0)
7312 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007313 else if (wp != NULL && bt_terminal(wp->w_buffer)
7314 && wp->w_status_height != 0)
7315 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007316#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007317 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007318 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007320 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 }
7322 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007323
7324 if (wp == NULL)
7325 {
7326 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7327 col = 0;
7328 len = 0;
7329 p = buf;
7330 fillchar = 0;
7331 for (n = 0; tabtab[n].start != NULL; n++)
7332 {
7333 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7334 while (col < len)
7335 TabPageIdxs[col++] = fillchar;
7336 p = tabtab[n].start;
7337 fillchar = tabtab[n].userhl;
7338 }
7339 while (col < Columns)
7340 TabPageIdxs[col++] = fillchar;
7341 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007342
7343theend:
7344 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345}
7346
7347#endif /* FEAT_STL_OPT */
7348
7349/*
7350 * Output a single character directly to the screen and update ScreenLines.
7351 */
7352 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007353screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 char_u buf[MB_MAXBYTES + 1];
7356
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007357#ifdef FEAT_MBYTE
7358 if (has_mbyte)
7359 buf[(*mb_char2bytes)(c, buf)] = NUL;
7360 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007362 {
7363 buf[0] = c;
7364 buf[1] = NUL;
7365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 screen_puts(buf, row, col, attr);
7367}
7368
7369/*
7370 * Get a single character directly from ScreenLines into "bytes[]".
7371 * Also return its attribute in *attrp;
7372 */
7373 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007374screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375{
7376 unsigned off;
7377
7378 /* safety check */
7379 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7380 {
7381 off = LineOffset[row] + col;
7382 *attrp = ScreenAttrs[off];
7383 bytes[0] = ScreenLines[off];
7384 bytes[1] = NUL;
7385
7386#ifdef FEAT_MBYTE
7387 if (enc_utf8 && ScreenLinesUC[off] != 0)
7388 bytes[utfc_char2bytes(off, bytes)] = NUL;
7389 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7390 {
7391 bytes[0] = ScreenLines[off];
7392 bytes[1] = ScreenLines2[off];
7393 bytes[2] = NUL;
7394 }
7395 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7396 {
7397 bytes[1] = ScreenLines[off + 1];
7398 bytes[2] = NUL;
7399 }
7400#endif
7401 }
7402}
7403
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007404#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007405/*
7406 * Return TRUE if composing characters for screen posn "off" differs from
7407 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007408 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007409 */
7410 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007411screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007412{
7413 int i;
7414
7415 for (i = 0; i < Screen_mco; ++i)
7416 {
7417 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7418 return TRUE;
7419 if (u8cc[i] == 0)
7420 break;
7421 }
7422 return FALSE;
7423}
7424#endif
7425
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426/*
7427 * Put string '*text' on the screen at position 'row' and 'col', with
7428 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7429 * Note: only outputs within one row, message is truncated at screen boundary!
7430 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7431 */
7432 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007433screen_puts(
7434 char_u *text,
7435 int row,
7436 int col,
7437 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438{
7439 screen_puts_len(text, -1, row, col, attr);
7440}
7441
7442/*
7443 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7444 * a NUL.
7445 */
7446 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007447screen_puts_len(
7448 char_u *text,
7449 int textlen,
7450 int row,
7451 int col,
7452 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453{
7454 unsigned off;
7455 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007456 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 int c;
7458#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007459 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 int mbyte_blen = 1;
7461 int mbyte_cells = 1;
7462 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007463 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 int clear_next_cell = FALSE;
7465# ifdef FEAT_ARABIC
7466 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007467 int pc, nc, nc1;
7468 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469# endif
7470#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007471#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7472 int force_redraw_this;
7473 int force_redraw_next = FALSE;
7474#endif
7475 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476
7477 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7478 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007479 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480
Bram Moolenaarc236c162008-07-13 17:41:49 +00007481#ifdef FEAT_MBYTE
7482 /* When drawing over the right halve of a double-wide char clear out the
7483 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007484 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007485# ifdef FEAT_GUI
7486 && !gui.in_use
7487# endif
7488 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007489 {
7490 ScreenLines[off - 1] = ' ';
7491 ScreenAttrs[off - 1] = 0;
7492 if (enc_utf8)
7493 {
7494 ScreenLinesUC[off - 1] = 0;
7495 ScreenLinesC[0][off - 1] = 0;
7496 }
7497 /* redraw the previous cell, make it empty */
7498 screen_char(off - 1, row, col - 1);
7499 /* force the cell at "col" to be redrawn */
7500 force_redraw_next = TRUE;
7501 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007502#endif
7503
Bram Moolenaar367329b2007-08-30 11:53:22 +00007504#ifdef FEAT_MBYTE
7505 max_off = LineOffset[row] + screen_Columns;
7506#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007507 while (col < screen_Columns
7508 && (len < 0 || (int)(ptr - text) < len)
7509 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 {
7511 c = *ptr;
7512#ifdef FEAT_MBYTE
7513 /* check if this is the first byte of a multibyte */
7514 if (has_mbyte)
7515 {
7516 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007517 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007519 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7521 mbyte_cells = 1;
7522 else if (enc_dbcs != 0)
7523 mbyte_cells = mbyte_blen;
7524 else /* enc_utf8 */
7525 {
7526 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007527 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 (int)((text + len) - ptr));
7529 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007530 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007532# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 /* Non-BMP character: display as ? or fullwidth ?. */
7534 if (u8c >= 0x10000)
7535 {
7536 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7537 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007538 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007540# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541# ifdef FEAT_ARABIC
7542 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7543 {
7544 /* Do Arabic shaping. */
7545 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7546 {
7547 /* Past end of string to be displayed. */
7548 nc = NUL;
7549 nc1 = NUL;
7550 }
7551 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007552 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007553 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7554 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007555 nc1 = pcc[0];
7556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 pc = prev_c;
7558 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007559 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 }
7561 else
7562 prev_c = u8c;
7563# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007564 if (col + mbyte_cells > screen_Columns)
7565 {
7566 /* Only 1 cell left, but character requires 2 cells:
7567 * display a '>' in the last column to avoid wrapping. */
7568 c = '>';
7569 mbyte_cells = 1;
7570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 }
7572 }
7573#endif
7574
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007575#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7576 force_redraw_this = force_redraw_next;
7577 force_redraw_next = FALSE;
7578#endif
7579
7580 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581#ifdef FEAT_MBYTE
7582 || (mbyte_cells == 2
7583 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7584 || (enc_dbcs == DBCS_JPNU
7585 && c == 0x8e
7586 && ScreenLines2[off] != ptr[1])
7587 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007588 && (ScreenLinesUC[off] !=
7589 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7590 || (ScreenLinesUC[off] != 0
7591 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592#endif
7593 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007594 || exmode_active;
7595
7596 if (need_redraw
7597#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7598 || force_redraw_this
7599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 )
7601 {
7602#if defined(FEAT_GUI) || defined(UNIX)
7603 /* The bold trick makes a single row of pixels appear in the next
7604 * character. When a bold character is removed, the next
7605 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007606 * and for some xterms. */
7607 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608# ifdef FEAT_GUI
7609 gui.in_use
7610# endif
7611# if defined(FEAT_GUI) && defined(UNIX)
7612 ||
7613# endif
7614# ifdef UNIX
7615 term_is_xterm
7616# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007617 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007619 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007621 if (n > HL_ALL)
7622 n = syn_attr2attr(n);
7623 if (n & HL_BOLD)
7624 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 }
7626#endif
7627#ifdef FEAT_MBYTE
7628 /* When at the end of the text and overwriting a two-cell
7629 * character with a one-cell character, need to clear the next
7630 * cell. Also when overwriting the left halve of a two-cell char
7631 * with the right halve of a two-cell char. Do this only once
7632 * (mb_off2cells() may return 2 on the right halve). */
7633 if (clear_next_cell)
7634 clear_next_cell = FALSE;
7635 else if (has_mbyte
7636 && (len < 0 ? ptr[mbyte_blen] == NUL
7637 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007638 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007640 && (*mb_off2cells)(off, max_off) == 1
7641 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 clear_next_cell = TRUE;
7643
7644 /* Make sure we never leave a second byte of a double-byte behind,
7645 * it confuses mb_off2cells(). */
7646 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007647 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007649 && (*mb_off2cells)(off, max_off) == 1
7650 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 ScreenLines[off + mbyte_blen] = 0;
7652#endif
7653 ScreenLines[off] = c;
7654 ScreenAttrs[off] = attr;
7655#ifdef FEAT_MBYTE
7656 if (enc_utf8)
7657 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007658 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 ScreenLinesUC[off] = 0;
7660 else
7661 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007662 int i;
7663
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007665 for (i = 0; i < Screen_mco; ++i)
7666 {
7667 ScreenLinesC[i][off] = u8cc[i];
7668 if (u8cc[i] == 0)
7669 break;
7670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 }
7672 if (mbyte_cells == 2)
7673 {
7674 ScreenLines[off + 1] = 0;
7675 ScreenAttrs[off + 1] = attr;
7676 }
7677 screen_char(off, row, col);
7678 }
7679 else if (mbyte_cells == 2)
7680 {
7681 ScreenLines[off + 1] = ptr[1];
7682 ScreenAttrs[off + 1] = attr;
7683 screen_char_2(off, row, col);
7684 }
7685 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7686 {
7687 ScreenLines2[off] = ptr[1];
7688 screen_char(off, row, col);
7689 }
7690 else
7691#endif
7692 screen_char(off, row, col);
7693 }
7694#ifdef FEAT_MBYTE
7695 if (has_mbyte)
7696 {
7697 off += mbyte_cells;
7698 col += mbyte_cells;
7699 ptr += mbyte_blen;
7700 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007701 {
7702 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007704 len = -1;
7705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 }
7707 else
7708#endif
7709 {
7710 ++off;
7711 ++col;
7712 ++ptr;
7713 }
7714 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007715
7716#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7717 /* If we detected the next character needs to be redrawn, but the text
7718 * doesn't extend up to there, update the character here. */
7719 if (force_redraw_next && col < screen_Columns)
7720 {
7721# ifdef FEAT_MBYTE
7722 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7723 screen_char_2(off, row, col);
7724 else
7725# endif
7726 screen_char(off, row, col);
7727 }
7728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729}
7730
7731#ifdef FEAT_SEARCH_EXTRA
7732/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007733 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 */
7735 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007736start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737{
7738 if (p_hls && !no_hlsearch)
7739 {
7740 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007741 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007742# ifdef FEAT_RELTIME
7743 /* Set the time limit to 'redrawtime'. */
7744 profile_setlimit(p_rdt, &search_hl.tm);
7745# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 }
7747}
7748
7749/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007750 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 */
7752 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007753end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754{
7755 if (search_hl.rm.regprog != NULL)
7756 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007757 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 search_hl.rm.regprog = NULL;
7759 }
7760}
7761
7762/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007763 * Init for calling prepare_search_hl().
7764 */
7765 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007766init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007767{
7768 matchitem_T *cur;
7769
7770 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7771 * match */
7772 cur = wp->w_match_head;
7773 while (cur != NULL)
7774 {
7775 cur->hl.rm = cur->match;
7776 if (cur->hlg_id == 0)
7777 cur->hl.attr = 0;
7778 else
7779 cur->hl.attr = syn_id2attr(cur->hlg_id);
7780 cur->hl.buf = wp->w_buffer;
7781 cur->hl.lnum = 0;
7782 cur->hl.first_lnum = 0;
7783# ifdef FEAT_RELTIME
7784 /* Set the time limit to 'redrawtime'. */
7785 profile_setlimit(p_rdt, &(cur->hl.tm));
7786# endif
7787 cur = cur->next;
7788 }
7789 search_hl.buf = wp->w_buffer;
7790 search_hl.lnum = 0;
7791 search_hl.first_lnum = 0;
7792 /* time limit is set at the toplevel, for all windows */
7793}
7794
7795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 * Advance to the match in window "wp" line "lnum" or past it.
7797 */
7798 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007799prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007801 matchitem_T *cur; /* points to the match list */
7802 match_T *shl; /* points to search_hl or a match */
7803 int shl_flag; /* flag to indicate whether search_hl
7804 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007805 int pos_inprogress; /* marks that position match search is
7806 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 int n;
7808
7809 /*
7810 * When using a multi-line pattern, start searching at the top
7811 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007812 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007814 cur = wp->w_match_head;
7815 shl_flag = FALSE;
7816 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007818 if (shl_flag == FALSE)
7819 {
7820 shl = &search_hl;
7821 shl_flag = TRUE;
7822 }
7823 else
7824 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 if (shl->rm.regprog != NULL
7826 && shl->lnum == 0
7827 && re_multiline(shl->rm.regprog))
7828 {
7829 if (shl->first_lnum == 0)
7830 {
7831# ifdef FEAT_FOLDING
7832 for (shl->first_lnum = lnum;
7833 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7834 if (hasFoldingWin(wp, shl->first_lnum - 1,
7835 NULL, NULL, TRUE, NULL))
7836 break;
7837# else
7838 shl->first_lnum = wp->w_topline;
7839# endif
7840 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007841 if (cur != NULL)
7842 cur->pos.cur = 0;
7843 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007845 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7846 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007848 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7849 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007850 pos_inprogress = cur == NULL || cur->pos.cur == 0
7851 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 if (shl->lnum != 0)
7853 {
7854 shl->first_lnum = shl->lnum
7855 + shl->rm.endpos[0].lnum
7856 - shl->rm.startpos[0].lnum;
7857 n = shl->rm.endpos[0].col;
7858 }
7859 else
7860 {
7861 ++shl->first_lnum;
7862 n = 0;
7863 }
7864 }
7865 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007866 if (shl != &search_hl && cur != NULL)
7867 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 }
7869}
7870
7871/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007872 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 * Uses shl->buf.
7874 * Sets shl->lnum and shl->rm contents.
7875 * Note: Assumes a previous match is always before "lnum", unless
7876 * shl->lnum is zero.
7877 * Careful: Any pointers for buffer lines will become invalid.
7878 */
7879 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007880next_search_hl(
7881 win_T *win,
7882 match_T *shl, /* points to search_hl or a match */
7883 linenr_T lnum,
7884 colnr_T mincol, /* minimal column for a match */
7885 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886{
7887 linenr_T l;
7888 colnr_T matchcol;
7889 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007890 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007892 // for :{range}s/pat only highlight inside the range
7893 if (lnum < search_first_line || lnum > search_last_line)
7894 {
7895 shl->lnum = 0;
7896 return;
7897 }
7898
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 if (shl->lnum != 0)
7900 {
7901 /* Check for three situations:
7902 * 1. If the "lnum" is below a previous match, start a new search.
7903 * 2. If the previous match includes "mincol", use it.
7904 * 3. Continue after the previous match.
7905 */
7906 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7907 if (lnum > l)
7908 shl->lnum = 0;
7909 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7910 return;
7911 }
7912
7913 /*
7914 * Repeat searching for a match until one is found that includes "mincol"
7915 * or none is found in this line.
7916 */
7917 called_emsg = FALSE;
7918 for (;;)
7919 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007920#ifdef FEAT_RELTIME
7921 /* Stop searching after passing the time limit. */
7922 if (profile_passed_limit(&(shl->tm)))
7923 {
7924 shl->lnum = 0; /* no match found in time */
7925 break;
7926 }
7927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 /* Three situations:
7929 * 1. No useful previous match: search from start of line.
7930 * 2. Not Vi compatible or empty match: continue at next character.
7931 * Break the loop if this is beyond the end of the line.
7932 * 3. Vi compatible searching: continue at end of previous match.
7933 */
7934 if (shl->lnum == 0)
7935 matchcol = 0;
7936 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7937 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007938 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007940 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007941
7942 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007943 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007944 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007946 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 shl->lnum = 0;
7948 break;
7949 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007950#ifdef FEAT_MBYTE
7951 if (has_mbyte)
7952 matchcol += mb_ptr2len(ml);
7953 else
7954#endif
7955 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 }
7957 else
7958 matchcol = shl->rm.endpos[0].col;
7959
7960 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007961 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007963 /* Remember whether shl->rm is using a copy of the regprog in
7964 * cur->match. */
7965 int regprog_is_copy = (shl != &search_hl && cur != NULL
7966 && shl == &cur->hl
7967 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007968 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007969
Bram Moolenaarb3414592014-06-17 17:48:32 +02007970 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7971 matchcol,
7972#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007973 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007974#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007975 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007976#endif
7977 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007978 /* Copy the regprog, in case it got freed and recompiled. */
7979 if (regprog_is_copy)
7980 cur->match.regprog = cur->hl.rm.regprog;
7981
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007982 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007983 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007984 /* Error while handling regexp: stop using this regexp. */
7985 if (shl == &search_hl)
7986 {
7987 /* don't free regprog in the match list, it's a copy */
7988 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02007989 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007990 }
7991 shl->rm.regprog = NULL;
7992 shl->lnum = 0;
7993 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7994 message */
7995 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007996 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007997 }
7998 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007999 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008000 else
8001 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 if (nmatched == 0)
8003 {
8004 shl->lnum = 0; /* no match found */
8005 break;
8006 }
8007 if (shl->rm.startpos[0].lnum > 0
8008 || shl->rm.startpos[0].col >= mincol
8009 || nmatched > 1
8010 || shl->rm.endpos[0].col > mincol)
8011 {
8012 shl->lnum += shl->rm.startpos[0].lnum;
8013 break; /* useful match found */
8014 }
8015 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008016
8017 // Restore called_emsg for assert_fails().
8018 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020
Bram Moolenaar85077472016-10-16 14:35:48 +02008021/*
8022 * If there is a match fill "shl" and return one.
8023 * Return zero otherwise.
8024 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008025 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008026next_search_hl_pos(
8027 match_T *shl, /* points to a match */
8028 linenr_T lnum,
8029 posmatch_T *posmatch, /* match positions */
8030 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008031{
8032 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008033 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008034
Bram Moolenaarb3414592014-06-17 17:48:32 +02008035 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8036 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008037 llpos_T *pos = &posmatch->pos[i];
8038
8039 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008040 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008041 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008042 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008043 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008044 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008045 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008046 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008047 /* if this match comes before the one at "found" then swap
8048 * them */
8049 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008050 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008051 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008052
Bram Moolenaar85077472016-10-16 14:35:48 +02008053 *pos = posmatch->pos[found];
8054 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008055 }
8056 }
8057 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008058 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008059 }
8060 }
8061 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008062 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008063 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008064 colnr_T start = posmatch->pos[found].col == 0
8065 ? 0 : posmatch->pos[found].col - 1;
8066 colnr_T end = posmatch->pos[found].col == 0
8067 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008068
Bram Moolenaar85077472016-10-16 14:35:48 +02008069 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008070 shl->rm.startpos[0].lnum = 0;
8071 shl->rm.startpos[0].col = start;
8072 shl->rm.endpos[0].lnum = 0;
8073 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008074 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008075 posmatch->cur = found + 1;
8076 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008077 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008078 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008079}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008080#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008081
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008083screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085 attrentry_T *aep = NULL;
8086
8087 screen_attr = attr;
8088 if (full_screen
8089#ifdef WIN3264
8090 && termcap_active
8091#endif
8092 )
8093 {
8094#ifdef FEAT_GUI
8095 if (gui.in_use)
8096 {
8097 char buf[20];
8098
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008099 /* The GUI handles this internally. */
8100 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 OUT_STR(buf);
8102 }
8103 else
8104#endif
8105 {
8106 if (attr > HL_ALL) /* special HL attr. */
8107 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008108 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 aep = syn_cterm_attr2entry(attr);
8110 else
8111 aep = syn_term_attr2entry(attr);
8112 if (aep == NULL) /* did ":syntax clear" */
8113 attr = 0;
8114 else
8115 attr = aep->ae_attr;
8116 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008117 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008119 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008120#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008121 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8122 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8123 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008124#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008125 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008126 /* If the Normal FG color has BOLD attribute and the new HL
8127 * has a FG color defined, clear BOLD. */
8128 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008129 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008131 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008132 out_str(T_UCS);
8133 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008134 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8135 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008137 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008139 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008141 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008142 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143
8144 /*
8145 * Output the color or start string after bold etc., in case the
8146 * bold etc. override the color setting.
8147 */
8148 if (aep != NULL)
8149 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008150#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008151 /* When 'termguicolors' is set but fg or bg is unset,
8152 * fall back to the cterm colors. This helps for SpellBad,
8153 * where the GUI uses a red undercurl. */
8154 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008156 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008157 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008158 }
8159 else
8160#endif
8161 if (t_colors > 1)
8162 {
8163 if (aep->ae_u.cterm.fg_color)
8164 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8165 }
8166#ifdef FEAT_TERMGUICOLORS
8167 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8168 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008169 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008170 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 }
8172 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008173#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008174 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008176 if (aep->ae_u.cterm.bg_color)
8177 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8178 }
8179
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008180 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008181 {
8182 if (aep->ae_u.term.start != NULL)
8183 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 }
8185 }
8186 }
8187 }
8188}
8189
8190 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008191screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192{
8193 int do_ME = FALSE; /* output T_ME code */
8194
8195 if (screen_attr != 0
8196#ifdef WIN3264
8197 && termcap_active
8198#endif
8199 )
8200 {
8201#ifdef FEAT_GUI
8202 if (gui.in_use)
8203 {
8204 char buf[20];
8205
8206 /* use internal GUI code */
8207 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8208 OUT_STR(buf);
8209 }
8210 else
8211#endif
8212 {
8213 if (screen_attr > HL_ALL) /* special HL attr. */
8214 {
8215 attrentry_T *aep;
8216
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008217 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {
8219 /*
8220 * Assume that t_me restores the original colors!
8221 */
8222 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008223 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008224#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008225 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8226 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8227 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008228#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008229 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008230#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008231 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8232 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8233 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008234#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008235 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 do_ME = TRUE;
8237 }
8238 else
8239 {
8240 aep = syn_term_attr2entry(screen_attr);
8241 if (aep != NULL && aep->ae_u.term.stop != NULL)
8242 {
8243 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8244 do_ME = TRUE;
8245 else
8246 out_str(aep->ae_u.term.stop);
8247 }
8248 }
8249 if (aep == NULL) /* did ":syntax clear" */
8250 screen_attr = 0;
8251 else
8252 screen_attr = aep->ae_attr;
8253 }
8254
8255 /*
8256 * Often all ending-codes are equal to T_ME. Avoid outputting the
8257 * same sequence several times.
8258 */
8259 if (screen_attr & HL_STANDOUT)
8260 {
8261 if (STRCMP(T_SE, T_ME) == 0)
8262 do_ME = TRUE;
8263 else
8264 out_str(T_SE);
8265 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008266 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008267 {
8268 if (STRCMP(T_UCE, T_ME) == 0)
8269 do_ME = TRUE;
8270 else
8271 out_str(T_UCE);
8272 }
8273 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008274 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {
8276 if (STRCMP(T_UE, T_ME) == 0)
8277 do_ME = TRUE;
8278 else
8279 out_str(T_UE);
8280 }
8281 if (screen_attr & HL_ITALIC)
8282 {
8283 if (STRCMP(T_CZR, T_ME) == 0)
8284 do_ME = TRUE;
8285 else
8286 out_str(T_CZR);
8287 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008288 if (screen_attr & HL_STRIKETHROUGH)
8289 {
8290 if (STRCMP(T_STE, T_ME) == 0)
8291 do_ME = TRUE;
8292 else
8293 out_str(T_STE);
8294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8296 out_str(T_ME);
8297
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008298#ifdef FEAT_TERMGUICOLORS
8299 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008301 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008302 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008303 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008304 term_bg_rgb_color(cterm_normal_bg_gui_color);
8305 }
8306 else
8307#endif
8308 {
8309 if (t_colors > 1)
8310 {
8311 /* set Normal cterm colors */
8312 if (cterm_normal_fg_color != 0)
8313 term_fg_color(cterm_normal_fg_color - 1);
8314 if (cterm_normal_bg_color != 0)
8315 term_bg_color(cterm_normal_bg_color - 1);
8316 if (cterm_normal_fg_bold)
8317 out_str(T_MD);
8318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 }
8320 }
8321 }
8322 screen_attr = 0;
8323}
8324
8325/*
8326 * Reset the colors for a cterm. Used when leaving Vim.
8327 * The machine specific code may override this again.
8328 */
8329 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008330reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008332 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {
8334 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008335#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008336 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8337 || cterm_normal_bg_gui_color != INVALCOLOR)
8338 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008339#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 {
8343 out_str(T_OP);
8344 screen_attr = -1;
8345 }
8346 if (cterm_normal_fg_bold)
8347 {
8348 out_str(T_ME);
8349 screen_attr = -1;
8350 }
8351 }
8352}
8353
8354/*
8355 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8356 * using the attributes from ScreenAttrs["off"].
8357 */
8358 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008359screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360{
8361 int attr;
8362
8363 /* Check for illegal values, just in case (could happen just after
8364 * resizing). */
8365 if (row >= screen_Rows || col >= screen_Columns)
8366 return;
8367
Bram Moolenaar494838a2015-02-10 19:20:37 +01008368 /* Outputting a character in the last cell on the screen may scroll the
8369 * screen up. Only do it when the "xn" termcap property is set, otherwise
8370 * mark the character invalid (update it when scrolled up). */
8371 if (*T_XN == NUL
8372 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373#ifdef FEAT_RIGHTLEFT
8374 /* account for first command-line character in rightleft mode */
8375 && !cmdmsg_rl
8376#endif
8377 )
8378 {
8379 ScreenAttrs[off] = (sattr_T)-1;
8380 return;
8381 }
8382
8383 /*
8384 * Stop highlighting first, so it's easier to move the cursor.
8385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 if (screen_char_attr != 0)
8387 attr = screen_char_attr;
8388 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 attr = ScreenAttrs[off];
8390 if (screen_attr != attr)
8391 screen_stop_highlight();
8392
8393 windgoto(row, col);
8394
8395 if (screen_attr != attr)
8396 screen_start_highlight(attr);
8397
8398#ifdef FEAT_MBYTE
8399 if (enc_utf8 && ScreenLinesUC[off] != 0)
8400 {
8401 char_u buf[MB_MAXBYTES + 1];
8402
Bram Moolenaarcb070082016-04-02 22:14:51 +02008403 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008404 {
8405 if (*p_ambw == 'd'
8406# ifdef FEAT_GUI
8407 && !gui.in_use
8408# endif
8409 )
8410 {
8411 /* Clear the two screen cells. If the character is actually
8412 * single width it won't change the second cell. */
8413 out_str((char_u *)" ");
8414 term_windgoto(row, col);
8415 }
8416 /* not sure where the cursor is after drawing the ambiguous width
8417 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008418 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008419 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008420 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008422
8423 /* Convert the UTF-8 character to bytes and write it. */
8424 buf[utfc_char2bytes(off, buf)] = NUL;
8425 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 }
8427 else
8428#endif
8429 {
8430#ifdef FEAT_MBYTE
8431 out_flush_check();
8432#endif
8433 out_char(ScreenLines[off]);
8434#ifdef FEAT_MBYTE
8435 /* double-byte character in single-width cell */
8436 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8437 out_char(ScreenLines2[off]);
8438#endif
8439 }
8440
8441 screen_cur_col++;
8442}
8443
8444#ifdef FEAT_MBYTE
8445
8446/*
8447 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8448 * on the screen at position 'row' and 'col'.
8449 * The attributes of the first byte is used for all. This is required to
8450 * output the two bytes of a double-byte character with nothing in between.
8451 */
8452 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008453screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 /* Check for illegal values (could be wrong when screen was resized). */
8456 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8457 return;
8458
8459 /* Outputting the last character on the screen may scrollup the screen.
8460 * Don't to it! Mark the character invalid (update it when scrolled up) */
8461 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8462 {
8463 ScreenAttrs[off] = (sattr_T)-1;
8464 return;
8465 }
8466
8467 /* Output the first byte normally (positions the cursor), then write the
8468 * second byte directly. */
8469 screen_char(off, row, col);
8470 out_char(ScreenLines[off + 1]);
8471 ++screen_cur_col;
8472}
8473#endif
8474
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475/*
8476 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8477 * This uses the contents of ScreenLines[] and doesn't change it.
8478 */
8479 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008480screen_draw_rectangle(
8481 int row,
8482 int col,
8483 int height,
8484 int width,
8485 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486{
8487 int r, c;
8488 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008489#ifdef FEAT_MBYTE
8490 int max_off;
8491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008493 /* Can't use ScreenLines unless initialized */
8494 if (ScreenLines == NULL)
8495 return;
8496
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 if (invert)
8498 screen_char_attr = HL_INVERSE;
8499 for (r = row; r < row + height; ++r)
8500 {
8501 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008502#ifdef FEAT_MBYTE
8503 max_off = off + screen_Columns;
8504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 for (c = col; c < col + width; ++c)
8506 {
8507#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008508 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 {
8510 screen_char_2(off + c, r, c);
8511 ++c;
8512 }
8513 else
8514#endif
8515 {
8516 screen_char(off + c, r, c);
8517#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008518 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 ++c;
8520#endif
8521 }
8522 }
8523 }
8524 screen_char_attr = 0;
8525}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527/*
8528 * Redraw the characters for a vertically split window.
8529 */
8530 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008531redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532{
8533 int col;
8534 int width;
8535
8536# ifdef FEAT_CLIPBOARD
8537 clip_may_clear_selection(row, end - 1);
8538# endif
8539
8540 if (wp == NULL)
8541 {
8542 col = 0;
8543 width = Columns;
8544 }
8545 else
8546 {
8547 col = wp->w_wincol;
8548 width = wp->w_width;
8549 }
8550 screen_draw_rectangle(row, col, end - row, width, FALSE);
8551}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008553 static void
8554space_to_screenline(int off, int attr)
8555{
8556 ScreenLines[off] = ' ';
8557 ScreenAttrs[off] = attr;
8558# ifdef FEAT_MBYTE
8559 if (enc_utf8)
8560 ScreenLinesUC[off] = 0;
8561# endif
8562}
8563
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564/*
8565 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8566 * with character 'c1' in first column followed by 'c2' in the other columns.
8567 * Use attributes 'attr'.
8568 */
8569 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008570screen_fill(
8571 int start_row,
8572 int end_row,
8573 int start_col,
8574 int end_col,
8575 int c1,
8576 int c2,
8577 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 int row;
8580 int col;
8581 int off;
8582 int end_off;
8583 int did_delete;
8584 int c;
8585 int norm_term;
8586#if defined(FEAT_GUI) || defined(UNIX)
8587 int force_next = FALSE;
8588#endif
8589
8590 if (end_row > screen_Rows) /* safety check */
8591 end_row = screen_Rows;
8592 if (end_col > screen_Columns) /* safety check */
8593 end_col = screen_Columns;
8594 if (ScreenLines == NULL
8595 || start_row >= end_row
8596 || start_col >= end_col) /* nothing to do */
8597 return;
8598
8599 /* it's a "normal" terminal when not in a GUI or cterm */
8600 norm_term = (
8601#ifdef FEAT_GUI
8602 !gui.in_use &&
8603#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008604 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 for (row = start_row; row < end_row; ++row)
8606 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008607#ifdef FEAT_MBYTE
8608 if (has_mbyte
8609# ifdef FEAT_GUI
8610 && !gui.in_use
8611# endif
8612 )
8613 {
8614 /* When drawing over the right halve of a double-wide char clear
8615 * out the left halve. When drawing over the left halve of a
8616 * double wide-char clear out the right halve. Only needed in a
8617 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008618 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008619 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008620 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008621 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008622 }
8623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 /*
8625 * Try to use delete-line termcap code, when no attributes or in a
8626 * "normal" terminal, where a bold/italic space is just a
8627 * space.
8628 */
8629 did_delete = FALSE;
8630 if (c2 == ' '
8631 && end_col == Columns
8632 && can_clear(T_CE)
8633 && (attr == 0
8634 || (norm_term
8635 && attr <= HL_ALL
8636 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8637 {
8638 /*
8639 * check if we really need to clear something
8640 */
8641 col = start_col;
8642 if (c1 != ' ') /* don't clear first char */
8643 ++col;
8644
8645 off = LineOffset[row] + col;
8646 end_off = LineOffset[row] + end_col;
8647
8648 /* skip blanks (used often, keep it fast!) */
8649#ifdef FEAT_MBYTE
8650 if (enc_utf8)
8651 while (off < end_off && ScreenLines[off] == ' '
8652 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8653 ++off;
8654 else
8655#endif
8656 while (off < end_off && ScreenLines[off] == ' '
8657 && ScreenAttrs[off] == 0)
8658 ++off;
8659 if (off < end_off) /* something to be cleared */
8660 {
8661 col = off - LineOffset[row];
8662 screen_stop_highlight();
8663 term_windgoto(row, col);/* clear rest of this screen line */
8664 out_str(T_CE);
8665 screen_start(); /* don't know where cursor is now */
8666 col = end_col - col;
8667 while (col--) /* clear chars in ScreenLines */
8668 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008669 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 ++off;
8671 }
8672 }
8673 did_delete = TRUE; /* the chars are cleared now */
8674 }
8675
8676 off = LineOffset[row] + start_col;
8677 c = c1;
8678 for (col = start_col; col < end_col; ++col)
8679 {
8680 if (ScreenLines[off] != c
8681#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008682 || (enc_utf8 && (int)ScreenLinesUC[off]
8683 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684#endif
8685 || ScreenAttrs[off] != attr
8686#if defined(FEAT_GUI) || defined(UNIX)
8687 || force_next
8688#endif
8689 )
8690 {
8691#if defined(FEAT_GUI) || defined(UNIX)
8692 /* The bold trick may make a single row of pixels appear in
8693 * the next character. When a bold character is removed, the
8694 * next character should be redrawn too. This happens for our
8695 * own GUI and for some xterms. */
8696 if (
8697# ifdef FEAT_GUI
8698 gui.in_use
8699# endif
8700# if defined(FEAT_GUI) && defined(UNIX)
8701 ||
8702# endif
8703# ifdef UNIX
8704 term_is_xterm
8705# endif
8706 )
8707 {
8708 if (ScreenLines[off] != ' '
8709 && (ScreenAttrs[off] > HL_ALL
8710 || ScreenAttrs[off] & HL_BOLD))
8711 force_next = TRUE;
8712 else
8713 force_next = FALSE;
8714 }
8715#endif
8716 ScreenLines[off] = c;
8717#ifdef FEAT_MBYTE
8718 if (enc_utf8)
8719 {
8720 if (c >= 0x80)
8721 {
8722 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008723 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 }
8725 else
8726 ScreenLinesUC[off] = 0;
8727 }
8728#endif
8729 ScreenAttrs[off] = attr;
8730 if (!did_delete || c != ' ')
8731 screen_char(off, row, col);
8732 }
8733 ++off;
8734 if (col == start_col)
8735 {
8736 if (did_delete)
8737 break;
8738 c = c2;
8739 }
8740 }
8741 if (end_col == Columns)
8742 LineWraps[row] = FALSE;
8743 if (row == Rows - 1) /* overwritten the command line */
8744 {
8745 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008746 if (start_col == 0 && end_col == Columns
8747 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008749 if (start_col == 0)
8750 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 }
8752 }
8753}
8754
8755/*
8756 * Check if there should be a delay. Used before clearing or redrawing the
8757 * screen or the command line.
8758 */
8759 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008760check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761{
8762 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8763 && !did_wait_return
8764 && emsg_silent == 0)
8765 {
8766 out_flush();
8767 ui_delay(1000L, TRUE);
8768 emsg_on_display = FALSE;
8769 if (check_msg_scroll)
8770 msg_scroll = FALSE;
8771 }
8772}
8773
8774/*
8775 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008776 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 * Returns TRUE if there is a valid screen to write to.
8778 * Returns FALSE when starting up and screen not initialized yet.
8779 */
8780 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008781screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008783 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 return (ScreenLines != NULL);
8785}
8786
8787/*
8788 * Resize the shell to Rows and Columns.
8789 * Allocate ScreenLines[] and associated items.
8790 *
8791 * There may be some time between setting Rows and Columns and (re)allocating
8792 * ScreenLines[]. This happens when starting up and when (manually) changing
8793 * the shell size. Always use screen_Rows and screen_Columns to access items
8794 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8795 * final size of the shell is needed.
8796 */
8797 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008798screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799{
8800 int new_row, old_row;
8801#ifdef FEAT_GUI
8802 int old_Rows;
8803#endif
8804 win_T *wp;
8805 int outofmem = FALSE;
8806 int len;
8807 schar_T *new_ScreenLines;
8808#ifdef FEAT_MBYTE
8809 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008810 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008812 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813#endif
8814 sattr_T *new_ScreenAttrs;
8815 unsigned *new_LineOffset;
8816 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008817 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008818 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008820 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008821 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008823retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 /*
8825 * Allocation of the screen buffers is done only when the size changes and
8826 * when Rows and Columns have been set and we have started doing full
8827 * screen stuff.
8828 */
8829 if ((ScreenLines != NULL
8830 && Rows == screen_Rows
8831 && Columns == screen_Columns
8832#ifdef FEAT_MBYTE
8833 && enc_utf8 == (ScreenLinesUC != NULL)
8834 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008835 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836#endif
8837 )
8838 || Rows == 0
8839 || Columns == 0
8840 || (!full_screen && ScreenLines == NULL))
8841 return;
8842
8843 /*
8844 * It's possible that we produce an out-of-memory message below, which
8845 * will cause this function to be called again. To break the loop, just
8846 * return here.
8847 */
8848 if (entered)
8849 return;
8850 entered = TRUE;
8851
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008852 /*
8853 * Note that the window sizes are updated before reallocating the arrays,
8854 * thus we must not redraw here!
8855 */
8856 ++RedrawingDisabled;
8857
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 win_new_shellsize(); /* fit the windows in the new sized shell */
8859
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 comp_col(); /* recompute columns for shown command and ruler */
8861
8862 /*
8863 * We're changing the size of the screen.
8864 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8865 * - Move lines from the old arrays into the new arrays, clear extra
8866 * lines (unless the screen is going to be cleared).
8867 * - Free the old arrays.
8868 *
8869 * If anything fails, make ScreenLines NULL, so we don't do anything!
8870 * Continuing with the old ScreenLines may result in a crash, because the
8871 * size is wrong.
8872 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008873 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008875 if (aucmd_win != NULL)
8876 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877
8878 new_ScreenLines = (schar_T *)lalloc((long_u)(
8879 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8880#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008881 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 if (enc_utf8)
8883 {
8884 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8885 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008886 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008887 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8889 }
8890 if (enc_dbcs == DBCS_JPNU)
8891 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8892 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8893#endif
8894 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8895 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8896 new_LineOffset = (unsigned *)lalloc((long_u)(
8897 Rows * sizeof(unsigned)), FALSE);
8898 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008899 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008901 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 {
8903 if (win_alloc_lines(wp) == FAIL)
8904 {
8905 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008906 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 }
8908 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008909 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8910 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008911 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008912give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008914#ifdef FEAT_MBYTE
8915 for (i = 0; i < p_mco; ++i)
8916 if (new_ScreenLinesC[i] == NULL)
8917 break;
8918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 if (new_ScreenLines == NULL
8920#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008921 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8923#endif
8924 || new_ScreenAttrs == NULL
8925 || new_LineOffset == NULL
8926 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008927 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 || outofmem)
8929 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008930 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008931 {
8932 /* guess the size */
8933 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8934
8935 /* Remember we did this to avoid getting outofmem messages over
8936 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008937 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008938 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008939 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01008941 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008942 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008943 VIM_CLEAR(new_ScreenLinesC[i]);
8944 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01008946 VIM_CLEAR(new_ScreenAttrs);
8947 VIM_CLEAR(new_LineOffset);
8948 VIM_CLEAR(new_LineWraps);
8949 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 }
8951 else
8952 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008953 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008954
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 for (new_row = 0; new_row < Rows; ++new_row)
8956 {
8957 new_LineOffset[new_row] = new_row * Columns;
8958 new_LineWraps[new_row] = FALSE;
8959
8960 /*
8961 * If the screen is not going to be cleared, copy as much as
8962 * possible from the old screen to the new one and clear the rest
8963 * (used when resizing the window at the "--more--" prompt or when
8964 * executing an external command, for the GUI).
8965 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008966 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 {
8968 (void)vim_memset(new_ScreenLines + new_row * Columns,
8969 ' ', (size_t)Columns * sizeof(schar_T));
8970#ifdef FEAT_MBYTE
8971 if (enc_utf8)
8972 {
8973 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8974 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008975 for (i = 0; i < p_mco; ++i)
8976 (void)vim_memset(new_ScreenLinesC[i]
8977 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 0, (size_t)Columns * sizeof(u8char_T));
8979 }
8980 if (enc_dbcs == DBCS_JPNU)
8981 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8982 0, (size_t)Columns * sizeof(schar_T));
8983#endif
8984 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8985 0, (size_t)Columns * sizeof(sattr_T));
8986 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008987 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 {
8989 if (screen_Columns < Columns)
8990 len = screen_Columns;
8991 else
8992 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008993#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008994 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008995 * may be invalid now. Also when p_mco changes. */
8996 if (!(enc_utf8 && ScreenLinesUC == NULL)
8997 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008998#endif
8999 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9000 ScreenLines + LineOffset[old_row],
9001 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009003 if (enc_utf8 && ScreenLinesUC != NULL
9004 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 {
9006 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9007 ScreenLinesUC + LineOffset[old_row],
9008 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009009 for (i = 0; i < p_mco; ++i)
9010 mch_memmove(new_ScreenLinesC[i]
9011 + new_LineOffset[new_row],
9012 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 (size_t)len * sizeof(u8char_T));
9014 }
9015 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9016 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9017 ScreenLines2 + LineOffset[old_row],
9018 (size_t)len * sizeof(schar_T));
9019#endif
9020 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9021 ScreenAttrs + LineOffset[old_row],
9022 (size_t)len * sizeof(sattr_T));
9023 }
9024 }
9025 }
9026 /* Use the last line of the screen for the current line. */
9027 current_ScreenLine = new_ScreenLines + Rows * Columns;
9028 }
9029
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009030 free_screenlines();
9031
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 ScreenLines = new_ScreenLines;
9033#ifdef FEAT_MBYTE
9034 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009035 for (i = 0; i < p_mco; ++i)
9036 ScreenLinesC[i] = new_ScreenLinesC[i];
9037 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 ScreenLines2 = new_ScreenLines2;
9039#endif
9040 ScreenAttrs = new_ScreenAttrs;
9041 LineOffset = new_LineOffset;
9042 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009043 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044
9045 /* It's important that screen_Rows and screen_Columns reflect the actual
9046 * size of ScreenLines[]. Set them before calling anything. */
9047#ifdef FEAT_GUI
9048 old_Rows = screen_Rows;
9049#endif
9050 screen_Rows = Rows;
9051 screen_Columns = Columns;
9052
9053 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009054 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 screenclear2();
9056
9057#ifdef FEAT_GUI
9058 else if (gui.in_use
9059 && !gui.starting
9060 && ScreenLines != NULL
9061 && old_Rows != Rows)
9062 {
9063 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9064 /*
9065 * Adjust the position of the cursor, for when executing an external
9066 * command.
9067 */
9068 if (msg_row >= Rows) /* Rows got smaller */
9069 msg_row = Rows - 1; /* put cursor at last row */
9070 else if (Rows > old_Rows) /* Rows got bigger */
9071 msg_row += Rows - old_Rows; /* put cursor in same place */
9072 if (msg_col >= Columns) /* Columns got smaller */
9073 msg_col = Columns - 1; /* put cursor at last column */
9074 }
9075#endif
9076
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009078 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009079
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009080 /*
9081 * Do not apply autocommands more than 3 times to avoid an endless loop
9082 * in case applying autocommands always changes Rows or Columns.
9083 */
9084 if (starting == 0 && ++retry_count <= 3)
9085 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009086 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009087 /* In rare cases, autocommands may have altered Rows or Columns,
9088 * jump back to check if we need to allocate the screen again. */
9089 goto retry;
9090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091}
9092
9093 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009094free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009095{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009096#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009097 int i;
9098
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009099 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009100 for (i = 0; i < Screen_mco; ++i)
9101 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009102 vim_free(ScreenLines2);
9103#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009104 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009105 vim_free(ScreenAttrs);
9106 vim_free(LineOffset);
9107 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009108 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009109}
9110
9111 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009112screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113{
9114 check_for_delay(FALSE);
9115 screenalloc(FALSE); /* allocate screen buffers if size changed */
9116 screenclear2(); /* clear the screen */
9117}
9118
9119 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009120screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121{
9122 int i;
9123
9124 if (starting == NO_SCREEN || ScreenLines == NULL
9125#ifdef FEAT_GUI
9126 || (gui.in_use && gui.starting)
9127#endif
9128 )
9129 return;
9130
9131#ifdef FEAT_GUI
9132 if (!gui.in_use)
9133#endif
9134 screen_attr = -1; /* force setting the Normal colors */
9135 screen_stop_highlight(); /* don't want highlighting here */
9136
9137#ifdef FEAT_CLIPBOARD
9138 /* disable selection without redrawing it */
9139 clip_scroll_selection(9999);
9140#endif
9141
9142 /* blank out ScreenLines */
9143 for (i = 0; i < Rows; ++i)
9144 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009145 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 LineWraps[i] = FALSE;
9147 }
9148
9149 if (can_clear(T_CL))
9150 {
9151 out_str(T_CL); /* clear the display */
9152 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009153 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 }
9155 else
9156 {
9157 /* can't clear the screen, mark all chars with invalid attributes */
9158 for (i = 0; i < Rows; ++i)
9159 lineinvalid(LineOffset[i], (int)Columns);
9160 clear_cmdline = TRUE;
9161 }
9162
9163 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9164
9165 win_rest_invalid(firstwin);
9166 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009167 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 if (must_redraw == CLEAR) /* no need to clear again */
9169 must_redraw = NOT_VALID;
9170 compute_cmdrow();
9171 msg_row = cmdline_row; /* put cursor on last line for messages */
9172 msg_col = 0;
9173 screen_start(); /* don't know where cursor is now */
9174 msg_scrolled = 0; /* can't scroll back */
9175 msg_didany = FALSE;
9176 msg_didout = FALSE;
9177}
9178
9179/*
9180 * Clear one line in ScreenLines.
9181 */
9182 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009183lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184{
9185 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9186#ifdef FEAT_MBYTE
9187 if (enc_utf8)
9188 (void)vim_memset(ScreenLinesUC + off, 0,
9189 (size_t)width * sizeof(u8char_T));
9190#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009191 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192}
9193
9194/*
9195 * Mark one line in ScreenLines invalid by setting the attributes to an
9196 * invalid value.
9197 */
9198 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009199lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
9201 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9202}
9203
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204/*
9205 * Copy part of a Screenline for vertically split window "wp".
9206 */
9207 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009208linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209{
9210 unsigned off_to = LineOffset[to] + wp->w_wincol;
9211 unsigned off_from = LineOffset[from] + wp->w_wincol;
9212
9213 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9214 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009215#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 if (enc_utf8)
9217 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009218 int i;
9219
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9221 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009222 for (i = 0; i < p_mco; ++i)
9223 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9224 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 }
9226 if (enc_dbcs == DBCS_JPNU)
9227 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9228 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9231 wp->w_width * sizeof(sattr_T));
9232}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233
9234/*
9235 * Return TRUE if clearing with term string "p" would work.
9236 * It can't work when the string is empty or it won't set the right background.
9237 */
9238 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009239can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240{
9241 return (*p != NUL && (t_colors <= 1
9242#ifdef FEAT_GUI
9243 || gui.in_use
9244#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009245#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009246 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009247 || (!p_tgc && cterm_normal_bg_color == 0)
9248#else
9249 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009250#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009251 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252}
9253
9254/*
9255 * Reset cursor position. Use whenever cursor was moved because of outputting
9256 * something directly to the screen (shell commands) or a terminal control
9257 * code.
9258 */
9259 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009260screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261{
9262 screen_cur_row = screen_cur_col = 9999;
9263}
9264
9265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 * Move the cursor to position "row","col" in the screen.
9267 * This tries to find the most efficient way to move, minimizing the number of
9268 * characters sent to the terminal.
9269 */
9270 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009271windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009273 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 int i;
9275 int plan;
9276 int cost;
9277 int wouldbe_col;
9278 int noinvcurs;
9279 char_u *bs;
9280 int goto_cost;
9281 int attr;
9282
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009283#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9285
9286#define PLAN_LE 1
9287#define PLAN_CR 2
9288#define PLAN_NL 3
9289#define PLAN_WRITE 4
9290 /* Can't use ScreenLines unless initialized */
9291 if (ScreenLines == NULL)
9292 return;
9293
9294 if (col != screen_cur_col || row != screen_cur_row)
9295 {
9296 /* Check for valid position. */
9297 if (row < 0) /* window without text lines? */
9298 row = 0;
9299 if (row >= screen_Rows)
9300 row = screen_Rows - 1;
9301 if (col >= screen_Columns)
9302 col = screen_Columns - 1;
9303
9304 /* check if no cursor movement is allowed in highlight mode */
9305 if (screen_attr && *T_MS == NUL)
9306 noinvcurs = HIGHL_COST;
9307 else
9308 noinvcurs = 0;
9309 goto_cost = GOTO_COST + noinvcurs;
9310
9311 /*
9312 * Plan how to do the positioning:
9313 * 1. Use CR to move it to column 0, same row.
9314 * 2. Use T_LE to move it a few columns to the left.
9315 * 3. Use NL to move a few lines down, column 0.
9316 * 4. Move a few columns to the right with T_ND or by writing chars.
9317 *
9318 * Don't do this if the cursor went beyond the last column, the cursor
9319 * position is unknown then (some terminals wrap, some don't )
9320 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009321 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 * characters to move the cursor to the right.
9323 */
9324 if (row >= screen_cur_row && screen_cur_col < Columns)
9325 {
9326 /*
9327 * If the cursor is in the same row, bigger col, we can use CR
9328 * or T_LE.
9329 */
9330 bs = NULL; /* init for GCC */
9331 attr = screen_attr;
9332 if (row == screen_cur_row && col < screen_cur_col)
9333 {
9334 /* "le" is preferred over "bc", because "bc" is obsolete */
9335 if (*T_LE)
9336 bs = T_LE; /* "cursor left" */
9337 else
9338 bs = T_BC; /* "backspace character (old) */
9339 if (*bs)
9340 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9341 else
9342 cost = 999;
9343 if (col + 1 < cost) /* using CR is less characters */
9344 {
9345 plan = PLAN_CR;
9346 wouldbe_col = 0;
9347 cost = 1; /* CR is just one character */
9348 }
9349 else
9350 {
9351 plan = PLAN_LE;
9352 wouldbe_col = col;
9353 }
9354 if (noinvcurs) /* will stop highlighting */
9355 {
9356 cost += noinvcurs;
9357 attr = 0;
9358 }
9359 }
9360
9361 /*
9362 * If the cursor is above where we want to be, we can use CR LF.
9363 */
9364 else if (row > screen_cur_row)
9365 {
9366 plan = PLAN_NL;
9367 wouldbe_col = 0;
9368 cost = (row - screen_cur_row) * 2; /* CR LF */
9369 if (noinvcurs) /* will stop highlighting */
9370 {
9371 cost += noinvcurs;
9372 attr = 0;
9373 }
9374 }
9375
9376 /*
9377 * If the cursor is in the same row, smaller col, just use write.
9378 */
9379 else
9380 {
9381 plan = PLAN_WRITE;
9382 wouldbe_col = screen_cur_col;
9383 cost = 0;
9384 }
9385
9386 /*
9387 * Check if any characters that need to be written have the
9388 * correct attributes. Also avoid UTF-8 characters.
9389 */
9390 i = col - wouldbe_col;
9391 if (i > 0)
9392 cost += i;
9393 if (cost < goto_cost && i > 0)
9394 {
9395 /*
9396 * Check if the attributes are correct without additionally
9397 * stopping highlighting.
9398 */
9399 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9400 while (i && *p++ == attr)
9401 --i;
9402 if (i != 0)
9403 {
9404 /*
9405 * Try if it works when highlighting is stopped here.
9406 */
9407 if (*--p == 0)
9408 {
9409 cost += noinvcurs;
9410 while (i && *p++ == 0)
9411 --i;
9412 }
9413 if (i != 0)
9414 cost = 999; /* different attributes, don't do it */
9415 }
9416#ifdef FEAT_MBYTE
9417 if (enc_utf8)
9418 {
9419 /* Don't use an UTF-8 char for positioning, it's slow. */
9420 for (i = wouldbe_col; i < col; ++i)
9421 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9422 {
9423 cost = 999;
9424 break;
9425 }
9426 }
9427#endif
9428 }
9429
9430 /*
9431 * We can do it without term_windgoto()!
9432 */
9433 if (cost < goto_cost)
9434 {
9435 if (plan == PLAN_LE)
9436 {
9437 if (noinvcurs)
9438 screen_stop_highlight();
9439 while (screen_cur_col > col)
9440 {
9441 out_str(bs);
9442 --screen_cur_col;
9443 }
9444 }
9445 else if (plan == PLAN_CR)
9446 {
9447 if (noinvcurs)
9448 screen_stop_highlight();
9449 out_char('\r');
9450 screen_cur_col = 0;
9451 }
9452 else if (plan == PLAN_NL)
9453 {
9454 if (noinvcurs)
9455 screen_stop_highlight();
9456 while (screen_cur_row < row)
9457 {
9458 out_char('\n');
9459 ++screen_cur_row;
9460 }
9461 screen_cur_col = 0;
9462 }
9463
9464 i = col - screen_cur_col;
9465 if (i > 0)
9466 {
9467 /*
9468 * Use cursor-right if it's one character only. Avoids
9469 * removing a line of pixels from the last bold char, when
9470 * using the bold trick in the GUI.
9471 */
9472 if (T_ND[0] != NUL && T_ND[1] == NUL)
9473 {
9474 while (i-- > 0)
9475 out_char(*T_ND);
9476 }
9477 else
9478 {
9479 int off;
9480
9481 off = LineOffset[row] + screen_cur_col;
9482 while (i-- > 0)
9483 {
9484 if (ScreenAttrs[off] != screen_attr)
9485 screen_stop_highlight();
9486#ifdef FEAT_MBYTE
9487 out_flush_check();
9488#endif
9489 out_char(ScreenLines[off]);
9490#ifdef FEAT_MBYTE
9491 if (enc_dbcs == DBCS_JPNU
9492 && ScreenLines[off] == 0x8e)
9493 out_char(ScreenLines2[off]);
9494#endif
9495 ++off;
9496 }
9497 }
9498 }
9499 }
9500 }
9501 else
9502 cost = 999;
9503
9504 if (cost >= goto_cost)
9505 {
9506 if (noinvcurs)
9507 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009508 if (row == screen_cur_row && (col > screen_cur_col)
9509 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 term_cursor_right(col - screen_cur_col);
9511 else
9512 term_windgoto(row, col);
9513 }
9514 screen_cur_row = row;
9515 screen_cur_col = col;
9516 }
9517}
9518
9519/*
9520 * Set cursor to its position in the current window.
9521 */
9522 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009523setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009525 setcursor_mayforce(FALSE);
9526}
9527
9528/*
9529 * Set cursor to its position in the current window.
9530 * When "force" is TRUE also when not redrawing.
9531 */
9532 void
9533setcursor_mayforce(int force)
9534{
9535 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 {
9537 validate_cursor();
9538 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009539 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009541 /* With 'rightleft' set and the cursor on a double-wide
9542 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009543 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009545 (has_mbyte
9546 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9547 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548# endif
9549 1)) :
9550#endif
9551 curwin->w_wcol));
9552 }
9553}
9554
9555
9556/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009557 * Insert 'line_count' lines at 'row' in window 'wp'.
9558 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9559 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 * scrolling.
9561 * Returns FAIL if the lines are not inserted, OK for success.
9562 */
9563 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009564win_ins_lines(
9565 win_T *wp,
9566 int row,
9567 int line_count,
9568 int invalid,
9569 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570{
9571 int did_delete;
9572 int nextrow;
9573 int lastrow;
9574 int retval;
9575
9576 if (invalid)
9577 wp->w_lines_valid = 0;
9578
9579 if (wp->w_height < 5)
9580 return FAIL;
9581
9582 if (line_count > wp->w_height - row)
9583 line_count = wp->w_height - row;
9584
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009585 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 if (retval != MAYBE)
9587 return retval;
9588
9589 /*
9590 * If there is a next window or a status line, we first try to delete the
9591 * lines at the bottom to avoid messing what is after the window.
9592 * If this fails and there are following windows, don't do anything to avoid
9593 * messing up those windows, better just redraw.
9594 */
9595 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 if (wp->w_next != NULL || wp->w_status_height)
9597 {
9598 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009599 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 did_delete = TRUE;
9601 else if (wp->w_next)
9602 return FAIL;
9603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 /*
9605 * if no lines deleted, blank the lines that will end up below the window
9606 */
9607 if (!did_delete)
9608 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009611 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 lastrow = nextrow + line_count;
9613 if (lastrow > Rows)
9614 lastrow = Rows;
9615 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009616 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 ' ', ' ', 0);
9618 }
9619
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009620 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 == FAIL)
9622 {
9623 /* deletion will have messed up other windows */
9624 if (did_delete)
9625 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 win_rest_invalid(W_NEXT(wp));
9628 }
9629 return FAIL;
9630 }
9631
9632 return OK;
9633}
9634
9635/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009636 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9638 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9639 * scrolling
9640 * Return OK for success, FAIL if the lines are not deleted.
9641 */
9642 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009643win_del_lines(
9644 win_T *wp,
9645 int row,
9646 int line_count,
9647 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009648 int mayclear,
9649 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650{
9651 int retval;
9652
9653 if (invalid)
9654 wp->w_lines_valid = 0;
9655
9656 if (line_count > wp->w_height - row)
9657 line_count = wp->w_height - row;
9658
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009659 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 if (retval != MAYBE)
9661 return retval;
9662
9663 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009664 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 return FAIL;
9666
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 /*
9668 * If there are windows or status lines below, try to put them at the
9669 * correct place. If we can't do that, they have to be redrawn.
9670 */
9671 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9672 {
9673 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009674 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 {
9676 wp->w_redr_status = TRUE;
9677 win_rest_invalid(wp->w_next);
9678 }
9679 }
9680 /*
9681 * If this is the last window and there is no status line, redraw the
9682 * command line later.
9683 */
9684 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 redraw_cmdline = TRUE;
9686 return OK;
9687}
9688
9689/*
9690 * Common code for win_ins_lines() and win_del_lines().
9691 * Returns OK or FAIL when the work has been done.
9692 * Returns MAYBE when not finished yet.
9693 */
9694 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009695win_do_lines(
9696 win_T *wp,
9697 int row,
9698 int line_count,
9699 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009700 int del,
9701 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702{
9703 int retval;
9704
9705 if (!redrawing() || line_count <= 0)
9706 return FAIL;
9707
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009708 /* When inserting lines would result in loss of command output, just redraw
9709 * the lines. */
9710 if (no_win_do_lines_ins && !del)
9711 return FAIL;
9712
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009714 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009716 if (!no_win_do_lines_ins)
9717 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 return FAIL;
9719 }
9720
9721 /*
9722 * Delete all remaining lines
9723 */
9724 if (row + line_count >= wp->w_height)
9725 {
9726 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009727 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 ' ', ' ', 0);
9729 return OK;
9730 }
9731
9732 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009733 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009735 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009737 if (!no_win_do_lines_ins)
9738 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739
9740 /*
9741 * If the terminal can set a scroll region, use that.
9742 * Always do this in a vertically split window. This will redraw from
9743 * ScreenLines[] when t_CV isn't defined. That's faster than using
9744 * win_line().
9745 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009746 * a character in the lower right corner of the scroll region may cause a
9747 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009749 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 scroll_region_set(wp, row);
9753 if (del)
9754 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009755 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 else
9757 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009758 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 scroll_region_reset();
9761 return retval;
9762 }
9763
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9765 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766
9767 return MAYBE;
9768}
9769
9770/*
9771 * window 'wp' and everything after it is messed up, mark it for redraw
9772 */
9773 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009774win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 {
9778 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 wp->w_redr_status = TRUE;
9780 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 }
9782 redraw_cmdline = TRUE;
9783}
9784
9785/*
9786 * The rest of the routines in this file perform screen manipulations. The
9787 * given operation is performed physically on the screen. The corresponding
9788 * change is also made to the internal screen image. In this way, the editor
9789 * anticipates the effect of editing changes on the appearance of the screen.
9790 * That way, when we call screenupdate a complete redraw isn't usually
9791 * necessary. Another advantage is that we can keep adding code to anticipate
9792 * screen changes, and in the meantime, everything still works.
9793 */
9794
9795/*
9796 * types for inserting or deleting lines
9797 */
9798#define USE_T_CAL 1
9799#define USE_T_CDL 2
9800#define USE_T_AL 3
9801#define USE_T_CE 4
9802#define USE_T_DL 5
9803#define USE_T_SR 6
9804#define USE_NL 7
9805#define USE_T_CD 8
9806#define USE_REDRAW 9
9807
9808/*
9809 * insert lines on the screen and update ScreenLines[]
9810 * 'end' is the line after the scrolled part. Normally it is Rows.
9811 * When scrolling region used 'off' is the offset from the top for the region.
9812 * 'row' and 'end' are relative to the start of the region.
9813 *
9814 * return FAIL for failure, OK for success.
9815 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009816 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009817screen_ins_lines(
9818 int off,
9819 int row,
9820 int line_count,
9821 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009822 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009823 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824{
9825 int i;
9826 int j;
9827 unsigned temp;
9828 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009829 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 int type;
9831 int result_empty;
9832 int can_ce = can_clear(T_CE);
9833
9834 /*
9835 * FAIL if
9836 * - there is no valid screen
9837 * - the screen has to be redrawn completely
9838 * - the line count is less than one
9839 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009840 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009842 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9843#ifdef FEAT_CLIPBOARD
9844 || (clip_star.state != SELECT_CLEARED
9845 && redrawing_for_callback > 0)
9846#endif
9847 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 return FAIL;
9849
9850 /*
9851 * There are seven ways to insert lines:
9852 * 0. When in a vertically split window and t_CV isn't set, redraw the
9853 * characters from ScreenLines[].
9854 * 1. Use T_CD (clear to end of display) if it exists and the result of
9855 * the insert is just empty lines
9856 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9857 * present or line_count > 1. It looks better if we do all the inserts
9858 * at once.
9859 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9860 * insert is just empty lines and T_CE is not present or line_count >
9861 * 1.
9862 * 4. Use T_AL (insert line) if it exists.
9863 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9864 * just empty lines.
9865 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9866 * just empty lines.
9867 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9868 * the 'da' flag is not set or we have clear line capability.
9869 * 8. redraw the characters from ScreenLines[].
9870 *
9871 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9872 * the scrollbar for the window. It does have insert line, use that if it
9873 * exists.
9874 */
9875 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9877 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009878 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 type = USE_T_CD;
9880 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9881 type = USE_T_CAL;
9882 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9883 type = USE_T_CDL;
9884 else if (*T_AL != NUL)
9885 type = USE_T_AL;
9886 else if (can_ce && result_empty)
9887 type = USE_T_CE;
9888 else if (*T_DL != NUL && result_empty)
9889 type = USE_T_DL;
9890 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9891 type = USE_T_SR;
9892 else
9893 return FAIL;
9894
9895 /*
9896 * For clearing the lines screen_del_lines() is used. This will also take
9897 * care of t_db if necessary.
9898 */
9899 if (type == USE_T_CD || type == USE_T_CDL ||
9900 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009901 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902
9903 /*
9904 * If text is retained below the screen, first clear or delete as many
9905 * lines at the bottom of the window as are about to be inserted so that
9906 * the deleted lines won't later surface during a screen_del_lines.
9907 */
9908 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009909 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910
9911#ifdef FEAT_CLIPBOARD
9912 /* Remove a modeless selection when inserting lines halfway the screen
9913 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009914 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009915 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 else
9917 clip_scroll_selection(-line_count);
9918#endif
9919
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920#ifdef FEAT_GUI
9921 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9922 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009923 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924#endif
9925
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009926 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9927 cursor_col = wp->w_wincol;
9928
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 if (*T_CCS != NUL) /* cursor relative to region */
9930 cursor_row = row;
9931 else
9932 cursor_row = row + off;
9933
9934 /*
9935 * Shift LineOffset[] line_count down to reflect the inserted lines.
9936 * Clear the inserted lines in ScreenLines[].
9937 */
9938 row += off;
9939 end += off;
9940 for (i = 0; i < line_count; ++i)
9941 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 if (wp != NULL && wp->w_width != Columns)
9943 {
9944 /* need to copy part of a line */
9945 j = end - 1 - i;
9946 while ((j -= line_count) >= row)
9947 linecopy(j + line_count, j, wp);
9948 j += line_count;
9949 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009950 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9951 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 else
9953 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9954 LineWraps[j] = FALSE;
9955 }
9956 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 {
9958 j = end - 1 - i;
9959 temp = LineOffset[j];
9960 while ((j -= line_count) >= row)
9961 {
9962 LineOffset[j + line_count] = LineOffset[j];
9963 LineWraps[j + line_count] = LineWraps[j];
9964 }
9965 LineOffset[j + line_count] = temp;
9966 LineWraps[j + line_count] = FALSE;
9967 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009968 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 else
9970 lineinvalid(temp, (int)Columns);
9971 }
9972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973
9974 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009975 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009976 if (clear_attr != 0)
9977 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 /* redraw the characters */
9980 if (type == USE_REDRAW)
9981 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009982 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 {
9984 term_append_lines(line_count);
9985 screen_start(); /* don't know where cursor is now */
9986 }
9987 else
9988 {
9989 for (i = 0; i < line_count; i++)
9990 {
9991 if (type == USE_T_AL)
9992 {
9993 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009994 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 out_str(T_AL);
9996 }
9997 else /* type == USE_T_SR */
9998 out_str(T_SR);
9999 screen_start(); /* don't know where cursor is now */
10000 }
10001 }
10002
10003 /*
10004 * With scroll-reverse and 'da' flag set we need to clear the lines that
10005 * have been scrolled down into the region.
10006 */
10007 if (type == USE_T_SR && *T_DA)
10008 {
10009 for (i = 0; i < line_count; ++i)
10010 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010011 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 out_str(T_CE);
10013 screen_start(); /* don't know where cursor is now */
10014 }
10015 }
10016
10017#ifdef FEAT_GUI
10018 gui_can_update_cursor();
10019 if (gui.in_use)
10020 out_flush(); /* always flush after a scroll */
10021#endif
10022 return OK;
10023}
10024
10025/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010026 * Delete lines on the screen and update ScreenLines[].
10027 * "end" is the line after the scrolled part. Normally it is Rows.
10028 * When scrolling region used "off" is the offset from the top for the region.
10029 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 *
10031 * Return OK for success, FAIL if the lines are not deleted.
10032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010034screen_del_lines(
10035 int off,
10036 int row,
10037 int line_count,
10038 int end,
10039 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010040 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010041 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042{
10043 int j;
10044 int i;
10045 unsigned temp;
10046 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010047 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 int cursor_end;
10049 int result_empty; /* result is empty until end of region */
10050 int can_delete; /* deleting line codes can be used */
10051 int type;
10052
10053 /*
10054 * FAIL if
10055 * - there is no valid screen
10056 * - the screen has to be redrawn completely
10057 * - the line count is less than one
10058 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010059 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010061 if (!screen_valid(TRUE) || line_count <= 0
10062 || (!force && line_count > p_ttyscroll)
10063#ifdef FEAT_CLIPBOARD
10064 || (clip_star.state != SELECT_CLEARED
10065 && redrawing_for_callback > 0)
10066#endif
10067 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 return FAIL;
10069
10070 /*
10071 * Check if the rest of the current region will become empty.
10072 */
10073 result_empty = row + line_count >= end;
10074
10075 /*
10076 * We can delete lines only when 'db' flag not set or when 'ce' option
10077 * available.
10078 */
10079 can_delete = (*T_DB == NUL || can_clear(T_CE));
10080
10081 /*
10082 * There are six ways to delete lines:
10083 * 0. When in a vertically split window and t_CV isn't set, redraw the
10084 * characters from ScreenLines[].
10085 * 1. Use T_CD if it exists and the result is empty.
10086 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10087 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10088 * none of the other ways work.
10089 * 4. Use T_CE (erase line) if the result is empty.
10090 * 5. Use T_DL (delete line) if it exists.
10091 * 6. redraw the characters from ScreenLines[].
10092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10094 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010095 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 type = USE_T_CD;
10097#if defined(__BEOS__) && defined(BEOS_DR8)
10098 /*
10099 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10100 * its internal termcap... this works okay for tests which test *T_DB !=
10101 * NUL. It has the disadvantage that the user cannot use any :set t_*
10102 * command to get T_DB (back) to empty_option, only :set term=... will do
10103 * the trick...
10104 * Anyway, this hack will hopefully go away with the next OS release.
10105 * (Olaf Seibert)
10106 */
10107 else if (row == 0 && T_DB == empty_option
10108 && (line_count == 1 || *T_CDL == NUL))
10109#else
10110 else if (row == 0 && (
10111#ifndef AMIGA
10112 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10113 * up, so use delete-line command */
10114 line_count == 1 ||
10115#endif
10116 *T_CDL == NUL))
10117#endif
10118 type = USE_NL;
10119 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10120 type = USE_T_CDL;
10121 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010122 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 type = USE_T_CE;
10124 else if (*T_DL != NUL && can_delete)
10125 type = USE_T_DL;
10126 else if (*T_CDL != NUL && can_delete)
10127 type = USE_T_CDL;
10128 else
10129 return FAIL;
10130
10131#ifdef FEAT_CLIPBOARD
10132 /* Remove a modeless selection when deleting lines halfway the screen or
10133 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010134 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010135 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 else
10137 clip_scroll_selection(line_count);
10138#endif
10139
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140#ifdef FEAT_GUI
10141 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10142 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010143 gui_dont_update_cursor(gui.cursor_row >= row + off
10144 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145#endif
10146
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010147 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10148 cursor_col = wp->w_wincol;
10149
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 if (*T_CCS != NUL) /* cursor relative to region */
10151 {
10152 cursor_row = row;
10153 cursor_end = end;
10154 }
10155 else
10156 {
10157 cursor_row = row + off;
10158 cursor_end = end + off;
10159 }
10160
10161 /*
10162 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10163 * Clear the inserted lines in ScreenLines[].
10164 */
10165 row += off;
10166 end += off;
10167 for (i = 0; i < line_count; ++i)
10168 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 if (wp != NULL && wp->w_width != Columns)
10170 {
10171 /* need to copy part of a line */
10172 j = row + i;
10173 while ((j += line_count) <= end - 1)
10174 linecopy(j - line_count, j, wp);
10175 j -= line_count;
10176 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010177 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10178 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 else
10180 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10181 LineWraps[j] = FALSE;
10182 }
10183 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 {
10185 /* whole width, moving the line pointers is faster */
10186 j = row + i;
10187 temp = LineOffset[j];
10188 while ((j += line_count) <= end - 1)
10189 {
10190 LineOffset[j - line_count] = LineOffset[j];
10191 LineWraps[j - line_count] = LineWraps[j];
10192 }
10193 LineOffset[j - line_count] = temp;
10194 LineWraps[j - line_count] = FALSE;
10195 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010196 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 else
10198 lineinvalid(temp, (int)Columns);
10199 }
10200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010202 if (screen_attr != clear_attr)
10203 screen_stop_highlight();
10204 if (clear_attr != 0)
10205 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 /* redraw the characters */
10208 if (type == USE_REDRAW)
10209 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010210 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010212 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 out_str(T_CD);
10214 screen_start(); /* don't know where cursor is now */
10215 }
10216 else if (type == USE_T_CDL)
10217 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010218 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 term_delete_lines(line_count);
10220 screen_start(); /* don't know where cursor is now */
10221 }
10222 /*
10223 * Deleting lines at top of the screen or scroll region: Just scroll
10224 * the whole screen (scroll region) up by outputting newlines on the
10225 * last line.
10226 */
10227 else if (type == USE_NL)
10228 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010229 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 for (i = line_count; --i >= 0; )
10231 out_char('\n'); /* cursor will remain on same line */
10232 }
10233 else
10234 {
10235 for (i = line_count; --i >= 0; )
10236 {
10237 if (type == USE_T_DL)
10238 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010239 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 out_str(T_DL); /* delete a line */
10241 }
10242 else /* type == USE_T_CE */
10243 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010244 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 out_str(T_CE); /* erase a line */
10246 }
10247 screen_start(); /* don't know where cursor is now */
10248 }
10249 }
10250
10251 /*
10252 * If the 'db' flag is set, we need to clear the lines that have been
10253 * scrolled up at the bottom of the region.
10254 */
10255 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10256 {
10257 for (i = line_count; i > 0; --i)
10258 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010259 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 out_str(T_CE); /* erase a line */
10261 screen_start(); /* don't know where cursor is now */
10262 }
10263 }
10264
10265#ifdef FEAT_GUI
10266 gui_can_update_cursor();
10267 if (gui.in_use)
10268 out_flush(); /* always flush after a scroll */
10269#endif
10270
10271 return OK;
10272}
10273
10274/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010275 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 *
10277 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10278 * If clear_cmdline is FALSE there may be a message there that needs to be
10279 * cleared only if a mode is shown.
10280 * Return the length of the message (0 if no message).
10281 */
10282 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010283showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284{
10285 int need_clear;
10286 int length = 0;
10287 int do_mode;
10288 int attr;
10289 int nwr_save;
10290#ifdef FEAT_INS_EXPAND
10291 int sub_attr;
10292#endif
10293
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010294 do_mode = ((p_smd && msg_silent == 0)
10295 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010296 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010297 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010298 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 {
10300 /*
10301 * Don't show mode right now, when not redrawing or inside a mapping.
10302 * Call char_avail() only when we are going to show something, because
10303 * it takes a bit of time.
10304 */
10305 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10306 {
10307 redraw_cmdline = TRUE; /* show mode later */
10308 return 0;
10309 }
10310
10311 nwr_save = need_wait_return;
10312
10313 /* wait a bit before overwriting an important message */
10314 check_for_delay(FALSE);
10315
10316 /* if the cmdline is more than one line high, erase top lines */
10317 need_clear = clear_cmdline;
10318 if (clear_cmdline && cmdline_row < Rows - 1)
10319 msg_clr_cmdline(); /* will reset clear_cmdline */
10320
10321 /* Position on the last line in the window, column 0 */
10322 msg_pos_mode();
10323 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010324 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 if (do_mode)
10326 {
10327 MSG_PUTS_ATTR("--", attr);
10328#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010329 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010330# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010331 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010332# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010333 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010334# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010335 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010336# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 MSG_PUTS_ATTR(" IM", attr);
10338# else
10339 MSG_PUTS_ATTR(" XIM", attr);
10340# endif
10341#endif
10342#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10343 if (gui.in_use)
10344 {
10345 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010346 {
10347 /* HANGUL */
10348 if (enc_utf8)
10349 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10350 else
10351 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 }
10354#endif
10355#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010356 /* CTRL-X in Insert mode */
10357 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 {
10359 /* These messages can get long, avoid a wrap in a narrow
10360 * window. Prefer showing edit_submode_extra. */
10361 length = (Rows - msg_row) * Columns - 3;
10362 if (edit_submode_extra != NULL)
10363 length -= vim_strsize(edit_submode_extra);
10364 if (length > 0)
10365 {
10366 if (edit_submode_pre != NULL)
10367 length -= vim_strsize(edit_submode_pre);
10368 if (length - vim_strsize(edit_submode) > 0)
10369 {
10370 if (edit_submode_pre != NULL)
10371 msg_puts_attr(edit_submode_pre, attr);
10372 msg_puts_attr(edit_submode, attr);
10373 }
10374 if (edit_submode_extra != NULL)
10375 {
10376 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10377 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010378 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 else
10380 sub_attr = attr;
10381 msg_puts_attr(edit_submode_extra, sub_attr);
10382 }
10383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 }
10385 else
10386#endif
10387 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 if (State & VREPLACE_FLAG)
10389 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010390 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10392 else if (State & INSERT)
10393 {
10394#ifdef FEAT_RIGHTLEFT
10395 if (p_ri)
10396 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10397#endif
10398 MSG_PUTS_ATTR(_(" INSERT"), attr);
10399 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010400 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 MSG_PUTS_ATTR(_(" (insert)"), attr);
10402 else if (restart_edit == 'R')
10403 MSG_PUTS_ATTR(_(" (replace)"), attr);
10404 else if (restart_edit == 'V')
10405 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10406#ifdef FEAT_RIGHTLEFT
10407 if (p_hkmap)
10408 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10409# ifdef FEAT_FKMAP
10410 if (p_fkmap)
10411 MSG_PUTS_ATTR(farsi_text_5, attr);
10412# endif
10413#endif
10414#ifdef FEAT_KEYMAP
10415 if (State & LANGMAP)
10416 {
10417# ifdef FEAT_ARABIC
10418 if (curwin->w_p_arab)
10419 MSG_PUTS_ATTR(_(" Arabic"), attr);
10420 else
10421# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010422 if (get_keymap_str(curwin, (char_u *)" (%s)",
10423 NameBuff, MAXPATHL))
10424 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 }
10426#endif
10427 if ((State & INSERT) && p_paste)
10428 MSG_PUTS_ATTR(_(" (paste)"), attr);
10429
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 if (VIsual_active)
10431 {
10432 char *p;
10433
10434 /* Don't concatenate separate words to avoid translation
10435 * problems. */
10436 switch ((VIsual_select ? 4 : 0)
10437 + (VIsual_mode == Ctrl_V) * 2
10438 + (VIsual_mode == 'V'))
10439 {
10440 case 0: p = N_(" VISUAL"); break;
10441 case 1: p = N_(" VISUAL LINE"); break;
10442 case 2: p = N_(" VISUAL BLOCK"); break;
10443 case 4: p = N_(" SELECT"); break;
10444 case 5: p = N_(" SELECT LINE"); break;
10445 default: p = N_(" SELECT BLOCK"); break;
10446 }
10447 MSG_PUTS_ATTR(_(p), attr);
10448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 MSG_PUTS_ATTR(" --", attr);
10450 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010451
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 need_clear = TRUE;
10453 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010454 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455#ifdef FEAT_INS_EXPAND
10456 && edit_submode == NULL /* otherwise it gets too long */
10457#endif
10458 )
10459 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010460 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 need_clear = TRUE;
10462 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010463
10464 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 if (need_clear || clear_cmdline)
10466 msg_clr_eos();
10467 msg_didout = FALSE; /* overwrite this message */
10468 length = msg_col;
10469 msg_col = 0;
10470 need_wait_return = nwr_save; /* never ask for hit-return for this */
10471 }
10472 else if (clear_cmdline && msg_silent == 0)
10473 /* Clear the whole command line. Will reset "clear_cmdline". */
10474 msg_clr_cmdline();
10475
10476#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 /* In Visual mode the size of the selected area must be redrawn. */
10478 if (VIsual_active)
10479 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480
10481 /* If the last window has no status line, the ruler is after the mode
10482 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010483 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010484 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485#endif
10486 redraw_cmdline = FALSE;
10487 clear_cmdline = FALSE;
10488
10489 return length;
10490}
10491
10492/*
10493 * Position for a mode message.
10494 */
10495 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010496msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497{
10498 msg_col = 0;
10499 msg_row = Rows - 1;
10500}
10501
10502/*
10503 * Delete mode message. Used when ESC is typed which is expected to end
10504 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010505 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506 */
10507 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010508unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509{
10510 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010511 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 */
10513 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10514 redraw_cmdline = TRUE; /* delete mode later */
10515 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010516 clearmode();
10517}
10518
10519/*
10520 * Clear the mode message.
10521 */
10522 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010523clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010524{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010525 int save_msg_row = msg_row;
10526 int save_msg_col = msg_col;
10527
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010528 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010529 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010530 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010531 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010532
10533 msg_col = save_msg_col;
10534 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535}
10536
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010537 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010538recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010539{
10540 MSG_PUTS_ATTR(_("recording"), attr);
10541 if (!shortmess(SHM_RECORDING))
10542 {
10543 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010544 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010545 MSG_PUTS_ATTR(s, attr);
10546 }
10547}
10548
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010549/*
10550 * Draw the tab pages line at the top of the Vim window.
10551 */
10552 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010553draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010554{
10555 int tabcount = 0;
10556 tabpage_T *tp;
10557 int tabwidth;
10558 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010559 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010560 int attr;
10561 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010562 win_T *cwp;
10563 int wincount;
10564 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010565 int c;
10566 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010567 int attr_sel = HL_ATTR(HLF_TPS);
10568 int attr_nosel = HL_ATTR(HLF_TP);
10569 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010570 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010571 int room;
10572 int use_sep_chars = (t_colors < 8
10573#ifdef FEAT_GUI
10574 && !gui.in_use
10575#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010576#ifdef FEAT_TERMGUICOLORS
10577 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010578#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010579 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010580
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010581 if (ScreenLines == NULL)
10582 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010583 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010584
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010585#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010586 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010587 if (gui_use_tabline())
10588 {
10589 gui_update_tabline();
10590 return;
10591 }
10592#endif
10593
10594 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010595 return;
10596
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010597#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010598
10599 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10600 for (scol = 0; scol < Columns; ++scol)
10601 TabPageIdxs[scol] = 0;
10602
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010603 /* Use the 'tabline' option if it's set. */
10604 if (*p_tal != NUL)
10605 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010606 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010607
10608 /* Check for an error. If there is one we would loop in redrawing the
10609 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010610 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010611 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010612 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010613 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010614 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010615 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010616 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010617 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010618#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010619 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010620 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010621 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010622
Bram Moolenaar238a5642006-02-21 22:12:05 +000010623 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10624 if (tabwidth < 6)
10625 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010626
Bram Moolenaar238a5642006-02-21 22:12:05 +000010627 attr = attr_nosel;
10628 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010629 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010630 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10631 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010632 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010633 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010634
Bram Moolenaar238a5642006-02-21 22:12:05 +000010635 if (tp->tp_topframe == topframe)
10636 attr = attr_sel;
10637 if (use_sep_chars && col > 0)
10638 screen_putchar('|', 0, col++, attr);
10639
10640 if (tp->tp_topframe != topframe)
10641 attr = attr_nosel;
10642
10643 screen_putchar(' ', 0, col++, attr);
10644
10645 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010646 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010647 cwp = curwin;
10648 wp = firstwin;
10649 }
10650 else
10651 {
10652 cwp = tp->tp_curwin;
10653 wp = tp->tp_firstwin;
10654 }
10655
10656 modified = FALSE;
10657 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10658 if (bufIsChanged(wp->w_buffer))
10659 modified = TRUE;
10660 if (modified || wincount > 1)
10661 {
10662 if (wincount > 1)
10663 {
10664 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010665 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010666 if (col + len >= Columns - 3)
10667 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010668 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010669#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010670 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010671#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010672 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010673#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010674 );
10675 col += len;
10676 }
10677 if (modified)
10678 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10679 screen_putchar(' ', 0, col++, attr);
10680 }
10681
10682 room = scol - col + tabwidth - 1;
10683 if (room > 0)
10684 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010685 /* Get buffer name in NameBuff[] */
10686 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010687 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010688 len = vim_strsize(NameBuff);
10689 p = NameBuff;
10690#ifdef FEAT_MBYTE
10691 if (has_mbyte)
10692 while (len > room)
10693 {
10694 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010695 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010696 }
10697 else
10698#endif
10699 if (len > room)
10700 {
10701 p += len - room;
10702 len = room;
10703 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010704 if (len > Columns - col - 1)
10705 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010706
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010707 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010708 col += len;
10709 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010710 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010711
10712 /* Store the tab page number in TabPageIdxs[], so that
10713 * jump_to_mouse() knows where each one is. */
10714 ++tabcount;
10715 while (scol < col)
10716 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010717 }
10718
Bram Moolenaar238a5642006-02-21 22:12:05 +000010719 if (use_sep_chars)
10720 c = '_';
10721 else
10722 c = ' ';
10723 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010724
10725 /* Put an "X" for closing the current tab if there are several. */
10726 if (first_tabpage->tp_next != NULL)
10727 {
10728 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10729 TabPageIdxs[Columns - 1] = -999;
10730 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010731 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010732
10733 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10734 * set. */
10735 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010736}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010737
10738/*
10739 * Get buffer name for "buf" into NameBuff[].
10740 * Takes care of special buffer names and translates special characters.
10741 */
10742 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010743get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010744{
10745 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010746 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010747 else
10748 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10749 trans_characters(NameBuff, MAXPATHL);
10750}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010751
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752/*
10753 * Get the character to use in a status line. Get its attributes in "*attr".
10754 */
10755 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010756fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757{
10758 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010759
10760#ifdef FEAT_TERMINAL
10761 if (bt_terminal(wp->w_buffer))
10762 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010763 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010764 {
10765 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010766 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010767 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010768 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010769 {
10770 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010771 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010772 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010773 }
10774 else
10775#endif
10776 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010778 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 fill = fill_stl;
10780 }
10781 else
10782 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010783 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784 fill = fill_stlnc;
10785 }
10786 /* Use fill when there is highlighting, and highlighting of current
10787 * window differs, or the fillchars differ, or this is not the
10788 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010789 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010790 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 || (fill_stl != fill_stlnc)))
10792 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010793 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 return '^';
10795 return '=';
10796}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798/*
10799 * Get the character to use in a separator between vertically split windows.
10800 * Get its attributes in "*attr".
10801 */
10802 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010803fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010805 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 if (*attr == 0 && fill_vert == ' ')
10807 return '|';
10808 else
10809 return fill_vert;
10810}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811
10812/*
10813 * Return TRUE if redrawing should currently be done.
10814 */
10815 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010816redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010818#ifdef FEAT_EVAL
10819 if (disable_redraw_for_testing)
10820 return 0;
10821 else
10822#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010823 return ((!RedrawingDisabled
10824#ifdef FEAT_EVAL
10825 || ignore_redraw_flag_for_testing
10826#endif
10827 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828}
10829
10830/*
10831 * Return TRUE if printing messages should currently be done.
10832 */
10833 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010834messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835{
10836 return (!(p_lz && char_avail() && !KeyTyped));
10837}
10838
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010839#ifdef FEAT_MENU
10840/*
10841 * Draw the window toolbar.
10842 */
10843 static void
10844redraw_win_toolbar(win_T *wp)
10845{
10846 vimmenu_T *menu;
10847 int item_idx = 0;
10848 int item_count = 0;
10849 int col = 0;
10850 int next_col;
10851 int off = (int)(current_ScreenLine - ScreenLines);
10852 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10853 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10854
10855 vim_free(wp->w_winbar_items);
10856 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10857 ++item_count;
10858 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10859 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10860
10861 /* TODO: use fewer spaces if there is not enough room */
10862 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010863 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010864 {
10865 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010866 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010867 break;
10868 if (col > 1)
10869 {
10870 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010871 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010872 break;
10873 }
10874
10875 wp->w_winbar_items[item_idx].wb_startcol = col;
10876 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010877 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010878 break;
10879
10880 next_col = text_to_screenline(wp, menu->name, col);
10881 while (col < next_col)
10882 {
10883 ScreenAttrs[off + col] = button_attr;
10884 ++col;
10885 }
10886 wp->w_winbar_items[item_idx].wb_endcol = col;
10887 wp->w_winbar_items[item_idx].wb_menu = menu;
10888 ++item_idx;
10889
Bram Moolenaar02631462017-09-22 15:20:32 +020010890 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010891 break;
10892 space_to_screenline(off + col, button_attr);
10893 ++col;
10894 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010895 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010896 {
10897 space_to_screenline(off + col, fill_attr);
10898 ++col;
10899 }
10900 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10901
Bram Moolenaar02631462017-09-22 15:20:32 +020010902 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10903 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010904}
10905#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010906
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907/*
10908 * Show current status info in ruler and various other places
10909 * If always is FALSE, only show ruler if position has changed.
10910 */
10911 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010912showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913{
10914 if (!always && !redrawing())
10915 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010916#ifdef FEAT_INS_EXPAND
10917 if (pum_visible())
10918 {
10919 /* Don't redraw right now, do it later. */
10920 curwin->w_redr_status = TRUE;
10921 return;
10922 }
10923#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010924#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010925 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010926 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010927 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 else
10930#endif
10931#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010932 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933#endif
10934
10935#ifdef FEAT_TITLE
10936 if (need_maketitle
10937# ifdef FEAT_STL_OPT
10938 || (p_icon && (stl_syntax & STL_IN_ICON))
10939 || (p_title && (stl_syntax & STL_IN_TITLE))
10940# endif
10941 )
10942 maketitle();
10943#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010944 /* Redraw the tab pages line if needed. */
10945 if (redraw_tabline)
10946 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947}
10948
10949#ifdef FEAT_CMDL_INFO
10950 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010951win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010953#define RULER_BUF_LEN 70
10954 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 int row;
10956 int fillchar;
10957 int attr;
10958 int empty_line = FALSE;
10959 colnr_T virtcol;
10960 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010961 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 int this_ru_col;
10964 int off = 0;
10965 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966
10967 /* If 'ruler' off or redrawing disabled, don't do anything */
10968 if (!p_ru)
10969 return;
10970
10971 /*
10972 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10973 * after deleting lines, before cursor.lnum is corrected.
10974 */
10975 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10976 return;
10977
10978#ifdef FEAT_INS_EXPAND
10979 /* Don't draw the ruler while doing insert-completion, it might overwrite
10980 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 if (edit_submode != NULL)
10983 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010984 // Don't draw the ruler when the popup menu is visible, it may overlap.
10985 // Except when the popup menu will be redrawn anyway.
10986 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010987 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988#endif
10989
10990#ifdef FEAT_STL_OPT
10991 if (*p_ruf)
10992 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010993 int save_called_emsg = called_emsg;
10994
10995 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010997 if (called_emsg)
10998 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010999 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011000 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 return;
11002 }
11003#endif
11004
11005 /*
11006 * Check if not in Insert mode and the line is empty (will show "0-1").
11007 */
11008 if (!(State & INSERT)
11009 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11010 empty_line = TRUE;
11011
11012 /*
11013 * Only draw the ruler when something changed.
11014 */
11015 validate_virtcol_win(wp);
11016 if ( redraw_cmdline
11017 || always
11018 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11019 || wp->w_cursor.col != wp->w_ru_cursor.col
11020 || wp->w_virtcol != wp->w_ru_virtcol
11021#ifdef FEAT_VIRTUALEDIT
11022 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11023#endif
11024 || wp->w_topline != wp->w_ru_topline
11025 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11026#ifdef FEAT_DIFF
11027 || wp->w_topfill != wp->w_ru_topfill
11028#endif
11029 || empty_line != wp->w_ru_empty)
11030 {
11031 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 if (wp->w_status_height)
11033 {
11034 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011035 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011036 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011037 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 }
11039 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 {
11041 row = Rows - 1;
11042 fillchar = ' ';
11043 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 width = Columns;
11045 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 }
11047
11048 /* In list mode virtcol needs to be recomputed */
11049 virtcol = wp->w_virtcol;
11050 if (wp->w_p_list && lcs_tab1 == NUL)
11051 {
11052 wp->w_p_list = FALSE;
11053 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11054 wp->w_p_list = TRUE;
11055 }
11056
11057 /*
11058 * Some sprintfs return the length, some return a pointer.
11059 * To avoid portability problems we use strlen() here.
11060 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011061 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11063 ? 0L
11064 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011065 len = STRLEN(buffer);
11066 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11068 (int)virtcol + 1);
11069
11070 /*
11071 * Add a "50%" if there is room for it.
11072 * On the last line, don't print in the last column (scrolls the
11073 * screen up on some terminals).
11074 */
11075 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011076 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 this_ru_col = ru_col - (Columns - width);
11081 if (this_ru_col < 0)
11082 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 /* Never use more than half the window/screen width, leave the other
11084 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011085 if (this_ru_col < (width + 1) / 2)
11086 this_ru_col = (width + 1) / 2;
11087 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011089 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011090 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 {
11092#ifdef FEAT_MBYTE
11093 if (has_mbyte)
11094 i += (*mb_char2bytes)(fillchar, buffer + i);
11095 else
11096#endif
11097 buffer[i++] = fillchar;
11098 ++o;
11099 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011100 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 }
11102 /* Truncate at window boundary. */
11103#ifdef FEAT_MBYTE
11104 if (has_mbyte)
11105 {
11106 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011107 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 {
11109 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011110 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 {
11112 buffer[i] = NUL;
11113 break;
11114 }
11115 }
11116 }
11117 else
11118#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011119 if (this_ru_col + (int)STRLEN(buffer) > width)
11120 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121
Bram Moolenaar4033c552017-09-16 20:54:51 +020011122 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 i = redraw_cmdline;
11124 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011125 this_ru_col + off + (int)STRLEN(buffer),
11126 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127 fillchar, fillchar, attr);
11128 /* don't redraw the cmdline because of showing the ruler */
11129 redraw_cmdline = i;
11130 wp->w_ru_cursor = wp->w_cursor;
11131 wp->w_ru_virtcol = wp->w_virtcol;
11132 wp->w_ru_empty = empty_line;
11133 wp->w_ru_topline = wp->w_topline;
11134 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11135#ifdef FEAT_DIFF
11136 wp->w_ru_topfill = wp->w_topfill;
11137#endif
11138 }
11139}
11140#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011141
11142#if defined(FEAT_LINEBREAK) || defined(PROTO)
11143/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011144 * Return the width of the 'number' and 'relativenumber' column.
11145 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011146 * Otherwise it depends on 'numberwidth' and the line count.
11147 */
11148 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011149number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011150{
11151 int n;
11152 linenr_T lnum;
11153
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011154 if (wp->w_p_rnu && !wp->w_p_nu)
11155 /* cursor line shows "0" */
11156 lnum = wp->w_height;
11157 else
11158 /* cursor line shows absolute line number */
11159 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011160
Bram Moolenaar6b314672015-03-20 15:42:10 +010011161 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011162 return wp->w_nrwidth_width;
11163 wp->w_nrwidth_line_count = lnum;
11164
11165 n = 0;
11166 do
11167 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011168 lnum /= 10;
11169 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011170 } while (lnum > 0);
11171
11172 /* 'numberwidth' gives the minimal width plus one */
11173 if (n < wp->w_p_nuw - 1)
11174 n = wp->w_p_nuw - 1;
11175
11176 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011177 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011178 return n;
11179}
11180#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011181
11182/*
11183 * Return the current cursor column. This is the actual position on the
11184 * screen. First column is 0.
11185 */
11186 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011187screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011188{
11189 return screen_cur_col;
11190}
11191
11192/*
11193 * Return the current cursor row. This is the actual position on the screen.
11194 * First row is 0.
11195 */
11196 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011197screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011198{
11199 return screen_cur_row;
11200}