blob: 01428c4da6ff0ed7c847d11657b65d28006ccdd8 [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.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002179 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2180 if (fold_count != 0)
2181 {
2182 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2183 --fold_count;
2184 }
2185 else
2186 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002187 }
2188
2189 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 row += wp->w_lines[idx++].wl_size;
2191 if (row > wp->w_height) /* past end of screen */
2192 break;
2193#ifdef FEAT_FOLDING
2194 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2195#else
2196 ++lnum;
2197#endif
2198#ifdef FEAT_SYN_HL
2199 did_update = DID_NONE;
2200#endif
2201 }
2202
2203 if (lnum > buf->b_ml.ml_line_count)
2204 {
2205 eof = TRUE;
2206 break;
2207 }
2208 }
2209 /*
2210 * End of loop over all window lines.
2211 */
2212
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002213#ifdef FEAT_VTP
2214 /* Rewrite the character at the end of the screen line. */
2215 if (use_vtp())
2216 {
2217 int i;
2218
2219 for (i = 0; i < Rows; ++i)
2220# ifdef FEAT_MBYTE
2221 if (enc_utf8)
2222 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2223 LineOffset[i] + screen_Columns) > 1)
2224 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2225 else
2226 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2227 else
2228# endif
2229 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2230 }
2231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
2233 if (idx > wp->w_lines_valid)
2234 wp->w_lines_valid = idx;
2235
2236#ifdef FEAT_SYN_HL
2237 /*
2238 * Let the syntax stuff know we stop parsing here.
2239 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002240 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 syntax_end_parsing(syntax_last_parsed + 1);
2242#endif
2243
2244 /*
2245 * If we didn't hit the end of the file, and we didn't finish the last
2246 * line we were working on, then the line didn't fit.
2247 */
2248 wp->w_empty_rows = 0;
2249#ifdef FEAT_DIFF
2250 wp->w_filler_rows = 0;
2251#endif
2252 if (!eof && !didline)
2253 {
2254 if (lnum == wp->w_topline)
2255 {
2256 /*
2257 * Single line that does not fit!
2258 * Don't overwrite it, it can be edited.
2259 */
2260 wp->w_botline = lnum + 1;
2261 }
2262#ifdef FEAT_DIFF
2263 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2264 {
2265 /* Window ends in filler lines. */
2266 wp->w_botline = lnum;
2267 wp->w_filler_rows = wp->w_height - srow;
2268 }
2269#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002270 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2271 {
2272 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2273
2274 /*
2275 * Last line isn't finished: Display "@@@" in the last screen line.
2276 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002277 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002278 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002279 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002280 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002281 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002282 set_empty_rows(wp, srow);
2283 wp->w_botline = lnum;
2284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2286 {
2287 /*
2288 * Last line isn't finished: Display "@@@" at the end.
2289 */
2290 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2291 W_WINROW(wp) + wp->w_height,
2292 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002293 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 set_empty_rows(wp, srow);
2295 wp->w_botline = lnum;
2296 }
2297 else
2298 {
2299 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2300 wp->w_botline = lnum;
2301 }
2302 }
2303 else
2304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 if (eof) /* we hit the end of the file */
2307 {
2308 wp->w_botline = buf->b_ml.ml_line_count + 1;
2309#ifdef FEAT_DIFF
2310 j = diff_check_fill(wp, wp->w_botline);
2311 if (j > 0 && !wp->w_botfill)
2312 {
2313 /*
2314 * Display filler lines at the end of the file
2315 */
2316 if (char2cells(fill_diff) > 1)
2317 i = '-';
2318 else
2319 i = fill_diff;
2320 if (row + j > wp->w_height)
2321 j = wp->w_height - row;
2322 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2323 row += j;
2324 }
2325#endif
2326 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002327 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 wp->w_botline = lnum;
2329
2330 /* make sure the rest of the screen is blank */
2331 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002332 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 }
2334
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002335#ifdef SYN_TIME_LIMIT
2336 syn_set_timeout(NULL);
2337#endif
2338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 /* Reset the type of redrawing required, the window has been updated. */
2340 wp->w_redr_type = 0;
2341#ifdef FEAT_DIFF
2342 wp->w_old_topfill = wp->w_topfill;
2343 wp->w_old_botfill = wp->w_botfill;
2344#endif
2345
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002346 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
2348 /*
2349 * There is a trick with w_botline. If we invalidate it on each
2350 * change that might modify it, this will cause a lot of expensive
2351 * calls to plines() in update_topline() each time. Therefore the
2352 * value of w_botline is often approximated, and this value is used to
2353 * compute the value of w_topline. If the value of w_botline was
2354 * wrong, check that the value of w_topline is correct (cursor is on
2355 * the visible part of the text). If it's not, we need to redraw
2356 * again. Mostly this just means scrolling up a few lines, so it
2357 * doesn't look too bad. Only do this for the current window (where
2358 * changes are relevant).
2359 */
2360 wp->w_valid |= VALID_BOTLINE;
2361 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2362 {
2363 recursive = TRUE;
2364 curwin->w_valid &= ~VALID_TOPLINE;
2365 update_topline(); /* may invalidate w_botline again */
2366 if (must_redraw != 0)
2367 {
2368 /* Don't update for changes in buffer again. */
2369 i = curbuf->b_mod_set;
2370 curbuf->b_mod_set = FALSE;
2371 win_update(curwin);
2372 must_redraw = 0;
2373 curbuf->b_mod_set = i;
2374 }
2375 recursive = FALSE;
2376 }
2377 }
2378
2379#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2380 /* restore got_int, unless CTRL-C was hit while redrawing */
2381 if (!got_int)
2382 got_int = save_got_int;
2383#endif
2384}
2385
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386/*
2387 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2388 * as the filler character.
2389 */
2390 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002391win_draw_end(
2392 win_T *wp,
2393 int c1,
2394 int c2,
2395 int row,
2396 int endrow,
2397 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398{
2399#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2400 int n = 0;
2401# define FDC_OFF n
2402#else
2403# define FDC_OFF 0
2404#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002405#ifdef FEAT_FOLDING
2406 int fdc = compute_foldcolumn(wp, 0);
2407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409#ifdef FEAT_RIGHTLEFT
2410 if (wp->w_p_rl)
2411 {
2412 /* No check for cmdline window: should never be right-left. */
2413# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002414 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
2416 if (n > 0)
2417 {
2418 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002419 if (n > wp->w_width)
2420 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2422 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002423 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425# endif
2426# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002427 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 {
2429 int nn = n + 2;
2430
2431 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002432 if (nn > wp->w_width)
2433 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2435 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002436 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 n = nn;
2438 }
2439# endif
2440 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002441 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002442 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2444 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002445 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 }
2447 else
2448#endif
2449 {
2450#ifdef FEAT_CMDWIN
2451 if (cmdwin_type != 0 && wp == curwin)
2452 {
2453 /* draw the cmdline character in the leftmost column */
2454 n = 1;
2455 if (n > wp->w_width)
2456 n = wp->w_width;
2457 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002458 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002459 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
2461#endif
2462#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002463 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002465 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002468 if (nn > wp->w_width)
2469 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002471 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002472 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 n = nn;
2474 }
2475#endif
2476#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002477 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
2479 int nn = n + 2;
2480
2481 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002482 if (nn > wp->w_width)
2483 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002485 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002486 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 n = nn;
2488 }
2489#endif
2490 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002491 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002492 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 }
2494 set_empty_rows(wp, row);
2495}
2496
Bram Moolenaar1a384422010-07-14 19:53:30 +02002497#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002498/*
2499 * Advance **color_cols and return TRUE when there are columns to draw.
2500 */
2501 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002502advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002503{
2504 while (**color_cols >= 0 && vcol > **color_cols)
2505 ++*color_cols;
2506 return (**color_cols >= 0);
2507}
2508#endif
2509
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002510#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2511/*
2512 * Copy "text" to ScreenLines using "attr".
2513 * Returns the next screen column.
2514 */
2515 static int
2516text_to_screenline(win_T *wp, char_u *text, int col)
2517{
2518 int off = (int)(current_ScreenLine - ScreenLines);
2519
2520#ifdef FEAT_MBYTE
2521 if (has_mbyte)
2522 {
2523 int cells;
2524 int u8c, u8cc[MAX_MCO];
2525 int i;
2526 int idx;
2527 int c_len;
2528 char_u *p;
2529# ifdef FEAT_ARABIC
2530 int prev_c = 0; /* previous Arabic character */
2531 int prev_c1 = 0; /* first composing char for prev_c */
2532# endif
2533
2534# ifdef FEAT_RIGHTLEFT
2535 if (wp->w_p_rl)
2536 idx = off;
2537 else
2538# endif
2539 idx = off + col;
2540
2541 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2542 for (p = text; *p != NUL; )
2543 {
2544 cells = (*mb_ptr2cells)(p);
2545 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002546 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002547# ifdef FEAT_RIGHTLEFT
2548 - (wp->w_p_rl ? col : 0)
2549# endif
2550 )
2551 break;
2552 ScreenLines[idx] = *p;
2553 if (enc_utf8)
2554 {
2555 u8c = utfc_ptr2char(p, u8cc);
2556 if (*p < 0x80 && u8cc[0] == 0)
2557 {
2558 ScreenLinesUC[idx] = 0;
2559#ifdef FEAT_ARABIC
2560 prev_c = u8c;
2561#endif
2562 }
2563 else
2564 {
2565#ifdef FEAT_ARABIC
2566 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2567 {
2568 /* Do Arabic shaping. */
2569 int pc, pc1, nc;
2570 int pcc[MAX_MCO];
2571 int firstbyte = *p;
2572
2573 /* The idea of what is the previous and next
2574 * character depends on 'rightleft'. */
2575 if (wp->w_p_rl)
2576 {
2577 pc = prev_c;
2578 pc1 = prev_c1;
2579 nc = utf_ptr2char(p + c_len);
2580 prev_c1 = u8cc[0];
2581 }
2582 else
2583 {
2584 pc = utfc_ptr2char(p + c_len, pcc);
2585 nc = prev_c;
2586 pc1 = pcc[0];
2587 }
2588 prev_c = u8c;
2589
2590 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2591 pc, pc1, nc);
2592 ScreenLines[idx] = firstbyte;
2593 }
2594 else
2595 prev_c = u8c;
2596#endif
2597 /* Non-BMP character: display as ? or fullwidth ?. */
2598#ifdef UNICODE16
2599 if (u8c >= 0x10000)
2600 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2601 else
2602#endif
2603 ScreenLinesUC[idx] = u8c;
2604 for (i = 0; i < Screen_mco; ++i)
2605 {
2606 ScreenLinesC[i][idx] = u8cc[i];
2607 if (u8cc[i] == 0)
2608 break;
2609 }
2610 }
2611 if (cells > 1)
2612 ScreenLines[idx + 1] = 0;
2613 }
2614 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2615 /* double-byte single width character */
2616 ScreenLines2[idx] = p[1];
2617 else if (cells > 1)
2618 /* double-width character */
2619 ScreenLines[idx + 1] = p[1];
2620 col += cells;
2621 idx += cells;
2622 p += c_len;
2623 }
2624 }
2625 else
2626#endif
2627 {
2628 int len = (int)STRLEN(text);
2629
Bram Moolenaar02631462017-09-22 15:20:32 +02002630 if (len > wp->w_width - col)
2631 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002632 if (len > 0)
2633 {
2634#ifdef FEAT_RIGHTLEFT
2635 if (wp->w_p_rl)
2636 STRNCPY(current_ScreenLine, text, len);
2637 else
2638#endif
2639 STRNCPY(current_ScreenLine + col, text, len);
2640 col += len;
2641 }
2642 }
2643 return col;
2644}
2645#endif
2646
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647#ifdef FEAT_FOLDING
2648/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002649 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2650 * space is available for window "wp", minus "col".
2651 */
2652 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002653compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002654{
2655 int fdc = wp->w_p_fdc;
2656 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002657 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002658
2659 if (fdc > wwidth - (col + wmw))
2660 fdc = wwidth - (col + wmw);
2661 return fdc;
2662}
2663
2664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 * Display one folded line.
2666 */
2667 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002668fold_line(
2669 win_T *wp,
2670 long fold_count,
2671 foldinfo_T *foldinfo,
2672 linenr_T lnum,
2673 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002675 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 pos_T *top, *bot;
2677 linenr_T lnume = lnum + fold_count - 1;
2678 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002679 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 int col;
2682 int txtcol;
2683 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002684 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685
2686 /* Build the fold line:
2687 * 1. Add the cmdwin_type for the command-line window
2688 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002689 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 * 4. Compose the text
2691 * 5. Add the text
2692 * 6. set highlighting for the Visual area an other text
2693 */
2694 col = 0;
2695
2696 /*
2697 * 1. Add the cmdwin_type for the command-line window
2698 * Ignores 'rightleft', this window is never right-left.
2699 */
2700#ifdef FEAT_CMDWIN
2701 if (cmdwin_type != 0 && wp == curwin)
2702 {
2703 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002704 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705#ifdef FEAT_MBYTE
2706 if (enc_utf8)
2707 ScreenLinesUC[off] = 0;
2708#endif
2709 ++col;
2710 }
2711#endif
2712
2713 /*
2714 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002715 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002717 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 if (fdc > 0)
2719 {
2720 fill_foldcolumn(buf, wp, TRUE, lnum);
2721#ifdef FEAT_RIGHTLEFT
2722 if (wp->w_p_rl)
2723 {
2724 int i;
2725
Bram Moolenaar02631462017-09-22 15:20:32 +02002726 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002727 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 /* reverse the fold column */
2729 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002730 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 }
2732 else
2733#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002734 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 col += fdc;
2736 }
2737
2738#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002739# define RL_MEMSET(p, v, l) \
2740 do { \
2741 if (wp->w_p_rl) \
2742 for (ri = 0; ri < l; ++ri) \
2743 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2744 else \
2745 for (ri = 0; ri < l; ++ri) \
2746 ScreenAttrs[off + (p) + ri] = v; \
2747 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002749# define RL_MEMSET(p, v, l) \
2750 do { \
2751 for (ri = 0; ri < l; ++ri) \
2752 ScreenAttrs[off + (p) + ri] = v; \
2753 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754#endif
2755
Bram Moolenaar64486672010-05-16 15:46:46 +02002756 /* Set all attributes of the 'number' or 'relativenumber' column and the
2757 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002758 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
2760#ifdef FEAT_SIGNS
2761 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002762 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002764 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 if (len > 0)
2766 {
2767 if (len > 2)
2768 len = 2;
2769# ifdef FEAT_RIGHTLEFT
2770 if (wp->w_p_rl)
2771 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002772 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002773 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 else
2775# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002776 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 col += len;
2778 }
2779 }
2780#endif
2781
2782 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002783 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002785 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002787 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 if (len > 0)
2789 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002790 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002791 long num;
2792 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002793
2794 if (len > w + 1)
2795 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002796
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002797 if (wp->w_p_nu && !wp->w_p_rnu)
2798 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002799 num = (long)lnum;
2800 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002801 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002802 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002803 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002804 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002805 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002806 /* 'number' + 'relativenumber': cursor line shows absolute
2807 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002808 num = lnum;
2809 fmt = "%-*ld ";
2810 }
2811 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002812
Bram Moolenaar700e7342013-01-30 12:31:36 +01002813 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814#ifdef FEAT_RIGHTLEFT
2815 if (wp->w_p_rl)
2816 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002817 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002818 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 else
2820#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002821 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 col += len;
2823 }
2824 }
2825
2826 /*
2827 * 4. Compose the folded-line string with 'foldtext', if set.
2828 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002829 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
2831 txtcol = col; /* remember where text starts */
2832
2833 /*
2834 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2835 * Right-left text is put in columns 0 - number-col, normal text is put
2836 * in columns number-col - window-width.
2837 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002838 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839
2840 /* Fill the rest of the line with the fold filler */
2841#ifdef FEAT_RIGHTLEFT
2842 if (wp->w_p_rl)
2843 col -= txtcol;
2844#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002845 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846#ifdef FEAT_RIGHTLEFT
2847 - (wp->w_p_rl ? txtcol : 0)
2848#endif
2849 )
2850 {
2851#ifdef FEAT_MBYTE
2852 if (enc_utf8)
2853 {
2854 if (fill_fold >= 0x80)
2855 {
2856 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002857 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002858 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 }
2860 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002861 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002863 ScreenLines[off + col] = fill_fold;
2864 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002865 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002867 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002869 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 }
2871
2872 if (text != buf)
2873 vim_free(text);
2874
2875 /*
2876 * 6. set highlighting for the Visual area an other text.
2877 * If all folded lines are in the Visual area, highlight the line.
2878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2880 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002881 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 {
2883 /* Visual is after curwin->w_cursor */
2884 top = &curwin->w_cursor;
2885 bot = &VIsual;
2886 }
2887 else
2888 {
2889 /* Visual is before curwin->w_cursor */
2890 top = &VIsual;
2891 bot = &curwin->w_cursor;
2892 }
2893 if (lnum >= top->lnum
2894 && lnume <= bot->lnum
2895 && (VIsual_mode != 'v'
2896 || ((lnum > top->lnum
2897 || (lnum == top->lnum
2898 && top->col == 0))
2899 && (lnume < bot->lnum
2900 || (lnume == bot->lnum
2901 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002902 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 {
2904 if (VIsual_mode == Ctrl_V)
2905 {
2906 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002907 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002909 if (wp->w_old_cursor_lcol != MAXCOL
2910 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002911 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 len = wp->w_old_cursor_lcol;
2913 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002914 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002915 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002916 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
2918 }
2919 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002922 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 }
2925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002927#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002928 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2929 if (wp->w_p_cc_cols)
2930 {
2931 int i = 0;
2932 int j = wp->w_p_cc_cols[i];
2933 int old_txtcol = txtcol;
2934
2935 while (j > -1)
2936 {
2937 txtcol += j;
2938 if (wp->w_p_wrap)
2939 txtcol -= wp->w_skipcol;
2940 else
2941 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002942 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002943 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002944 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002945 txtcol = old_txtcol;
2946 j = wp->w_p_cc_cols[++i];
2947 }
2948 }
2949
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002950 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002951 if (wp->w_p_cuc)
2952 {
2953 txtcol += wp->w_virtcol;
2954 if (wp->w_p_wrap)
2955 txtcol -= wp->w_skipcol;
2956 else
2957 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002958 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002959 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002960 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002961 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963
Bram Moolenaar02631462017-09-22 15:20:32 +02002964 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2965 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966
2967 /*
2968 * Update w_cline_height and w_cline_folded if the cursor line was
2969 * updated (saves a call to plines() later).
2970 */
2971 if (wp == curwin
2972 && lnum <= curwin->w_cursor.lnum
2973 && lnume >= curwin->w_cursor.lnum)
2974 {
2975 curwin->w_cline_row = row;
2976 curwin->w_cline_height = 1;
2977 curwin->w_cline_folded = TRUE;
2978 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2979 }
2980}
2981
2982/*
2983 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2984 */
2985 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002986copy_text_attr(
2987 int off,
2988 char_u *buf,
2989 int len,
2990 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002992 int i;
2993
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 mch_memmove(ScreenLines + off, buf, (size_t)len);
2995# ifdef FEAT_MBYTE
2996 if (enc_utf8)
2997 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2998# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002999 for (i = 0; i < len; ++i)
3000 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001}
3002
3003/*
3004 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003005 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 */
3007 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003008fill_foldcolumn(
3009 char_u *p,
3010 win_T *wp,
3011 int closed, /* TRUE of FALSE */
3012 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
3014 int i = 0;
3015 int level;
3016 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003017 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003018 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019
3020 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003021 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022
3023 level = win_foldinfo.fi_level;
3024 if (level > 0)
3025 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003026 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003027 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003028
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 /* If the column is too narrow, we start at the lowest level that
3030 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003031 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 if (first_level < 1)
3033 first_level = 1;
3034
Bram Moolenaar1c934292015-01-27 16:39:29 +01003035 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 {
3037 if (win_foldinfo.fi_lnum == lnum
3038 && first_level + i >= win_foldinfo.fi_low_level)
3039 p[i] = '-';
3040 else if (first_level == 1)
3041 p[i] = '|';
3042 else if (first_level + i <= 9)
3043 p[i] = '0' + first_level + i;
3044 else
3045 p[i] = '>';
3046 if (first_level + i == level)
3047 break;
3048 }
3049 }
3050 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003051 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052}
3053#endif /* FEAT_FOLDING */
3054
3055/*
3056 * Display line "lnum" of window 'wp' on the screen.
3057 * Start at row "startrow", stop when "endrow" is reached.
3058 * wp->w_virtcol needs to be valid.
3059 *
3060 * Return the number of last row the line occupies.
3061 */
3062 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003063win_line(
3064 win_T *wp,
3065 linenr_T lnum,
3066 int startrow,
3067 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003068 int nochange UNUSED, // not updating for changed text
3069 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003071 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3073 int c = 0; /* init for GCC */
3074 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003075#ifdef FEAT_LINEBREAK
3076 long vcol_sbr = -1; /* virtual column after showbreak */
3077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 long vcol_prev = -1; /* "vcol" of previous character */
3079 char_u *line; /* current line */
3080 char_u *ptr; /* current position in "line" */
3081 int row; /* row in the window, excl w_winrow */
3082 int screen_row; /* row on the screen, incl w_winrow */
3083
3084 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3085 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003086 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003087 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 int c_extra = NUL; /* extra chars, all the same */
3089 int extra_attr = 0; /* attributes when n_extra != 0 */
3090 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3091 displaying lcs_eol at end-of-line */
3092 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3093 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3094
3095 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3096 int saved_n_extra = 0;
3097 char_u *saved_p_extra = NULL;
3098 int saved_c_extra = 0;
3099 int saved_char_attr = 0;
3100
3101 int n_attr = 0; /* chars with special attr */
3102 int saved_attr2 = 0; /* char_attr saved for n_attr */
3103 int n_attr3 = 0; /* chars with overruling special attr */
3104 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3105
3106 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3107
3108 int fromcol, tocol; /* start/end of inverting */
3109 int fromcol_prev = -2; /* start of inverting after cursor */
3110 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003112 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 pos_T pos;
3114 long v;
3115
3116 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003117 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3119 in this line */
3120 int attr = 0; /* attributes for area highlighting */
3121 int area_attr = 0; /* attributes desired by highlighting */
3122 int search_attr = 0; /* attributes desired by 'hlsearch' */
3123#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003124 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 int syntax_attr = 0; /* attributes desired by syntax */
3126 int has_syntax = FALSE; /* this buffer has syntax highl. */
3127 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003128 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003129 int draw_color_col = FALSE; /* highlight colorcolumn */
3130 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003131#endif
3132#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003133 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003134# define SPWORDLEN 150
3135 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003136 int nextlinecol = 0; /* column where nextline[] starts */
3137 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003138 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003139 int spell_attr = 0; /* attributes desired by spelling */
3140 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003141 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3142 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003143 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003144 static int cap_col = -1; /* column to check for Cap word */
3145 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003146 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147#endif
Bram Moolenaarc7875392018-09-13 14:57:41 +02003148 int extra_check = 0; // has syntax or linebreak
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149#ifdef FEAT_MBYTE
3150 int multi_attr = 0; /* attributes desired by multibyte */
3151 int mb_l = 1; /* multi-byte byte length */
3152 int mb_c = 0; /* decoded multi-byte character */
3153 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003154 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#endif
3156#ifdef FEAT_DIFF
3157 int filler_lines; /* nr of filler lines to be drawn */
3158 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003159 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 int change_start = MAXCOL; /* first col of changed area */
3161 int change_end = -1; /* last col of changed area */
3162#endif
3163 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3164#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003165 int need_showbreak = FALSE; /* overlong line, skipping first x
3166 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003168#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003169 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003171 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172#endif
3173#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003174 matchitem_T *cur; /* points to the match list */
3175 match_T *shl; /* points to search_hl or a match */
3176 int shl_flag; /* flag to indicate whether search_hl
3177 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003178 int pos_inprogress; /* marks that position match search is
3179 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003180 int prevcol_hl_flag; /* flag to indicate whether prevcol
3181 equals startcol of search_hl or one
3182 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183#endif
3184#ifdef FEAT_ARABIC
3185 int prev_c = 0; /* previous Arabic character */
3186 int prev_c1 = 0; /* first composing char for prev_c */
3187#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003188#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003189 int did_line_attr = 0;
3190#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003191#ifdef FEAT_TERMINAL
3192 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003193 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
3196 /* draw_state: items that are drawn in sequence: */
3197#define WL_START 0 /* nothing done yet */
3198#ifdef FEAT_CMDWIN
3199# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3200#else
3201# define WL_CMDLINE WL_START
3202#endif
3203#ifdef FEAT_FOLDING
3204# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3205#else
3206# define WL_FOLD WL_CMDLINE
3207#endif
3208#ifdef FEAT_SIGNS
3209# define WL_SIGN WL_FOLD + 1 /* column for signs */
3210#else
3211# define WL_SIGN WL_FOLD /* column for signs */
3212#endif
3213#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003214#ifdef FEAT_LINEBREAK
3215# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003217# define WL_BRI WL_NR
3218#endif
3219#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3220# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3221#else
3222# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223#endif
3224#define WL_LINE WL_SBR + 1 /* text in the line */
3225 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003226#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 int feedback_col = 0;
3228 int feedback_old_attr = -1;
3229#endif
3230
Bram Moolenaar860cae12010-06-05 23:22:07 +02003231#ifdef FEAT_CONCEAL
3232 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003233 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003234 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003235 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003236 int is_concealing = FALSE;
3237 int boguscols = 0; /* nonexistent columns added to force
3238 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003239 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003240 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003241 int match_conc = 0; /* cchar for match functions */
3242 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003243 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003244# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003245# define FIX_FOR_BOGUSCOLS \
3246 { \
3247 n_extra += vcol_off; \
3248 vcol -= vcol_off; \
3249 vcol_off = 0; \
3250 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003251 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003252 boguscols = 0; \
3253 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003254#else
3255# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
3258 if (startrow > endrow) /* past the end already! */
3259 return startrow;
3260
3261 row = startrow;
3262 screen_row = row + W_WINROW(wp);
3263
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003264 if (!number_only)
3265 {
3266 /*
3267 * To speed up the loop below, set extra_check when there is linebreak,
3268 * trailing white space and/or syntax processing to be done.
3269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003271 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272#endif
3273#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003274 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003275# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003276 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003277# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003278 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003280 /* Prepare for syntax highlighting in this line. When there is an
3281 * error, stop syntax highlighting. */
3282 save_did_emsg = did_emsg;
3283 did_emsg = FALSE;
3284 syntax_start(wp, lnum);
3285 if (did_emsg)
3286 wp->w_s->b_syn_error = TRUE;
3287 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003288 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003289 did_emsg = save_did_emsg;
3290#ifdef SYN_TIME_LIMIT
3291 if (!wp->w_s->b_syn_slow)
3292#endif
3293 {
3294 has_syntax = TRUE;
3295 extra_check = TRUE;
3296 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003299
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003300 /* Check for columns to display for 'colorcolumn'. */
3301 color_cols = wp->w_p_cc_cols;
3302 if (color_cols != NULL)
3303 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003304#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003305
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003306#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003307 if (term_show_buffer(wp->w_buffer))
3308 {
3309 extra_check = TRUE;
3310 get_term_attr = TRUE;
3311 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3312 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003313#endif
3314
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003315#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003316 if (wp->w_p_spell
3317 && *wp->w_s->b_p_spl != NUL
3318 && wp->w_s->b_langp.ga_len > 0
3319 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003320 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003321 /* Prepare for spell checking. */
3322 has_spell = TRUE;
3323 extra_check = TRUE;
3324
Bram Moolenaar7701f302018-10-02 21:20:32 +02003325 /* Get the start of the next line, so that words that wrap to the
3326 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003327 * Trick: skip a few chars for C/shell/Vim comments */
3328 nextline[SPWORDLEN] = NUL;
3329 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3330 {
3331 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3332 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3333 }
3334
Bram Moolenaar7701f302018-10-02 21:20:32 +02003335 /* When a word wrapped from the previous line the start of the
3336 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003337 if (lnum == checked_lnum)
3338 cur_checked_col = checked_col;
3339 checked_lnum = 0;
3340
3341 /* When there was a sentence end in the previous line may require a
3342 * word starting with capital in this line. In line 1 always check
3343 * the first word. */
3344 if (lnum != capcol_lnum)
3345 cap_col = -1;
3346 if (lnum == 1)
3347 cap_col = 0;
3348 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350#endif
3351
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003352 /*
3353 * handle visual active in this window
3354 */
3355 fromcol = -10;
3356 tocol = MAXCOL;
3357 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003359 /* Visual is after curwin->w_cursor */
3360 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003362 top = &curwin->w_cursor;
3363 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003365 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003367 top = &VIsual;
3368 bot = &curwin->w_cursor;
3369 }
3370 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3371 if (VIsual_mode == Ctrl_V) /* block mode */
3372 {
3373 if (lnum_in_visual_area)
3374 {
3375 fromcol = wp->w_old_cursor_fcol;
3376 tocol = wp->w_old_cursor_lcol;
3377 }
3378 }
3379 else /* non-block mode */
3380 {
3381 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003383 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003385 if (VIsual_mode == 'V') /* linewise */
3386 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 else
3388 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003389 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3390 if (gchar_pos(top) == NUL)
3391 tocol = fromcol + 1;
3392 }
3393 }
3394 if (VIsual_mode != 'V' && lnum == bot->lnum)
3395 {
3396 if (*p_sel == 'e' && bot->col == 0
3397#ifdef FEAT_VIRTUALEDIT
3398 && bot->coladd == 0
3399#endif
3400 )
3401 {
3402 fromcol = -10;
3403 tocol = MAXCOL;
3404 }
3405 else if (bot->col == MAXCOL)
3406 tocol = MAXCOL;
3407 else
3408 {
3409 pos = *bot;
3410 if (*p_sel == 'e')
3411 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3412 else
3413 {
3414 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3415 ++tocol;
3416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 }
3418 }
3419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003421 /* Check if the character under the cursor should not be inverted */
3422 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003423#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003424 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003425#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003426 )
3427 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003429 /* if inverting in this line set area_highlighting */
3430 if (fromcol >= 0)
3431 {
3432 area_highlighting = TRUE;
3433 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003435 if ((clip_star.available && !clip_star.owned
3436 && clip_isautosel_star())
3437 || (clip_plus.available && !clip_plus.owned
3438 && clip_isautosel_plus()))
3439 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003444 /*
3445 * handle 'incsearch' and ":s///c" highlighting
3446 */
3447 else if (highlight_match
3448 && wp == curwin
3449 && lnum >= curwin->w_cursor.lnum
3450 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003452 if (lnum == curwin->w_cursor.lnum)
3453 getvcol(curwin, &(curwin->w_cursor),
3454 (colnr_T *)&fromcol, NULL, NULL);
3455 else
3456 fromcol = 0;
3457 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3458 {
3459 pos.lnum = lnum;
3460 pos.col = search_match_endcol;
3461 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3462 }
3463 else
3464 tocol = MAXCOL;
3465 /* do at least one character; happens when past end of line */
3466 if (fromcol == tocol)
3467 tocol = fromcol + 1;
3468 area_highlighting = TRUE;
3469 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 }
3472
3473#ifdef FEAT_DIFF
3474 filler_lines = diff_check(wp, lnum);
3475 if (filler_lines < 0)
3476 {
3477 if (filler_lines == -1)
3478 {
3479 if (diff_find_change(wp, lnum, &change_start, &change_end))
3480 diff_hlf = HLF_ADD; /* added line */
3481 else if (change_start == 0)
3482 diff_hlf = HLF_TXD; /* changed text */
3483 else
3484 diff_hlf = HLF_CHD; /* changed line */
3485 }
3486 else
3487 diff_hlf = HLF_ADD; /* added line */
3488 filler_lines = 0;
3489 area_highlighting = TRUE;
3490 }
3491 if (lnum == wp->w_topline)
3492 filler_lines = wp->w_topfill;
3493 filler_todo = filler_lines;
3494#endif
3495
3496#ifdef LINE_ATTR
3497# ifdef FEAT_SIGNS
3498 /* If this line has a sign with line highlighting set line_attr. */
3499 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3500 if (v != 0)
3501 line_attr = sign_get_attr((int)v, TRUE);
3502# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003503# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003505 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003506 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507# endif
3508 if (line_attr != 0)
3509 area_highlighting = TRUE;
3510#endif
3511
3512 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3513 ptr = line;
3514
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003515#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003516 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003517 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003518 /* For checking first word with a capital skip white space. */
3519 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003520 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003521
Bram Moolenaar30abd282005-06-22 22:35:10 +00003522 /* To be able to spell-check over line boundaries copy the end of the
3523 * current line into nextline[]. Above the start of the next line was
3524 * copied to nextline[SPWORDLEN]. */
3525 if (nextline[SPWORDLEN] == NUL)
3526 {
3527 /* No next line or it is empty. */
3528 nextlinecol = MAXCOL;
3529 nextline_idx = 0;
3530 }
3531 else
3532 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003533 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003534 if (v < SPWORDLEN)
3535 {
3536 /* Short line, use it completely and append the start of the
3537 * next line. */
3538 nextlinecol = 0;
3539 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003540 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003541 nextline_idx = v + 1;
3542 }
3543 else
3544 {
3545 /* Long line, use only the last SPWORDLEN bytes. */
3546 nextlinecol = v - SPWORDLEN;
3547 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3548 nextline_idx = SPWORDLEN + 1;
3549 }
3550 }
3551 }
3552#endif
3553
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003554 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003556 if (lcs_space || lcs_trail)
3557 extra_check = TRUE;
3558 /* find start of trailing whitespace */
3559 if (lcs_trail)
3560 {
3561 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003562 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003563 --trailcol;
3564 trailcol += (colnr_T) (ptr - line);
3565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
3567
3568 /*
3569 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3570 * first character to be displayed.
3571 */
3572 if (wp->w_p_wrap)
3573 v = wp->w_skipcol;
3574 else
3575 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003576 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 {
3578#ifdef FEAT_MBYTE
3579 char_u *prev_ptr = ptr;
3580#endif
3581 while (vcol < v && *ptr != NUL)
3582 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003583 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 vcol += c;
3585#ifdef FEAT_MBYTE
3586 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003588 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
3590
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003591 /* When:
3592 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003593 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003594 * - 'virtualedit' is set, or
3595 * - the visual mode is active,
3596 * the end of the line may be before the start of the displayed part.
3597 */
3598 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003599#ifdef FEAT_SYN_HL
3600 wp->w_p_cuc || draw_color_col ||
3601#endif
3602#ifdef FEAT_VIRTUALEDIT
3603 virtual_active() ||
3604#endif
3605 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003606 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609
3610 /* Handle a character that's not completely on the screen: Put ptr at
3611 * that character but skip the first few screen characters. */
3612 if (vcol > v)
3613 {
3614 vcol -= c;
3615#ifdef FEAT_MBYTE
3616 ptr = prev_ptr;
3617#else
3618 --ptr;
3619#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003620 /* If the character fits on the screen, don't need to skip it.
3621 * Except for a TAB. */
3622 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003623#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003624 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003625#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003626 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003627 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
3629
3630 /*
3631 * Adjust for when the inverted text is before the screen,
3632 * and when the start of the inverted text is before the screen.
3633 */
3634 if (tocol <= vcol)
3635 fromcol = 0;
3636 else if (fromcol >= 0 && fromcol < vcol)
3637 fromcol = vcol;
3638
3639#ifdef FEAT_LINEBREAK
3640 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3641 if (wp->w_p_wrap)
3642 need_showbreak = TRUE;
3643#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003644#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003645 /* When spell checking a word we need to figure out the start of the
3646 * word and if it's badly spelled or not. */
3647 if (has_spell)
3648 {
3649 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003650 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003651 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003652
3653 pos = wp->w_cursor;
3654 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003655 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003656 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003657
3658 /* spell_move_to() may call ml_get() and make "line" invalid */
3659 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3660 ptr = line + linecol;
3661
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003662 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003663 {
3664 /* no bad word found at line start, don't check until end of a
3665 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003666 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003667 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003668 }
3669 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003670 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003671 /* bad word found, use attributes until end of word */
3672 word_end = wp->w_cursor.col + len + 1;
3673
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003674 /* Turn index into actual attributes. */
3675 if (spell_hlf != HLF_COUNT)
3676 spell_attr = highlight_attr[spell_hlf];
3677 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003678 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003679
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003680# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003681 /* Need to restart syntax highlighting for this line. */
3682 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003683 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003684# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003685 }
3686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 }
3688
3689 /*
3690 * Correct highlighting for cursor that can't be disabled.
3691 * Avoids having to check this for each character.
3692 */
3693 if (fromcol >= 0)
3694 {
3695 if (noinvcur)
3696 {
3697 if ((colnr_T)fromcol == wp->w_virtcol)
3698 {
3699 /* highlighting starts at cursor, let it start just after the
3700 * cursor */
3701 fromcol_prev = fromcol;
3702 fromcol = -1;
3703 }
3704 else if ((colnr_T)fromcol < wp->w_virtcol)
3705 /* restart highlighting after the cursor */
3706 fromcol_prev = wp->w_virtcol;
3707 }
3708 if (fromcol >= tocol)
3709 fromcol = -1;
3710 }
3711
3712#ifdef FEAT_SEARCH_EXTRA
3713 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003714 * Handle highlighting the last used search pattern and matches.
3715 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003717 cur = wp->w_match_head;
3718 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003719 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003721 if (shl_flag == FALSE)
3722 {
3723 shl = &search_hl;
3724 shl_flag = TRUE;
3725 }
3726 else
3727 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003728 shl->startcol = MAXCOL;
3729 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003731 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003732 v = (long)(ptr - line);
3733 if (cur != NULL)
3734 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003735 next_search_hl(wp, shl, lnum, (colnr_T)v,
3736 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003737
3738 /* Need to get the line again, a multi-line regexp may have made it
3739 * invalid. */
3740 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3741 ptr = line + v;
3742
3743 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003745 if (shl->lnum == lnum)
3746 shl->startcol = shl->rm.startpos[0].col;
3747 else
3748 shl->startcol = 0;
3749 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3750 - shl->rm.startpos[0].lnum)
3751 shl->endcol = shl->rm.endpos[0].col;
3752 else
3753 shl->endcol = MAXCOL;
3754 /* Highlight one character for an empty match. */
3755 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003758 if (has_mbyte && line[shl->endcol] != NUL)
3759 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3760 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003762 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003764 if ((long)shl->startcol < v) /* match at leftcol */
3765 {
3766 shl->attr_cur = shl->attr;
3767 search_attr = shl->attr;
3768 }
3769 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003771 if (shl != &search_hl && cur != NULL)
3772 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 }
3774#endif
3775
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003776#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003777 /* Cursor line highlighting for 'cursorline' in the current window. Not
3778 * when Visual mode is active, because it's not clear what is selected
3779 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003780 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003781 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003782 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003783 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003784 area_highlighting = TRUE;
3785 }
3786#endif
3787
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003788 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 col = 0;
3790#ifdef FEAT_RIGHTLEFT
3791 if (wp->w_p_rl)
3792 {
3793 /* Rightleft window: process the text in the normal direction, but put
3794 * it in current_ScreenLine[] from right to left. Start at the
3795 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003796 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 off += col;
3798 }
3799#endif
3800
3801 /*
3802 * Repeat for the whole displayed line.
3803 */
3804 for (;;)
3805 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003806#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003807 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 /* Skip this quickly when working on the text. */
3810 if (draw_state != WL_LINE)
3811 {
3812#ifdef FEAT_CMDWIN
3813 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3814 {
3815 draw_state = WL_CMDLINE;
3816 if (cmdwin_type != 0 && wp == curwin)
3817 {
3818 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003820 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003821 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
3823 }
3824#endif
3825
3826#ifdef FEAT_FOLDING
3827 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3828 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003829 int fdc = compute_foldcolumn(wp, 0);
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003832 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003834 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003835 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003836 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003837 p_extra_free = alloc(12 + 1);
3838
3839 if (p_extra_free != NULL)
3840 {
3841 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3842 n_extra = fdc;
3843 p_extra_free[n_extra] = NUL;
3844 p_extra = p_extra_free;
3845 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003846 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849 }
3850#endif
3851
3852#ifdef FEAT_SIGNS
3853 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3854 {
3855 draw_state = WL_SIGN;
3856 /* Show the sign column when there are any signs in this
3857 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003858 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003860 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003862 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863# endif
3864
3865 /* Draw two cells with the sign value or blank. */
3866 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003867 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 n_extra = 2;
3869
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003870 if (row == startrow
3871#ifdef FEAT_DIFF
3872 + filler_lines && filler_todo <= 0
3873#endif
3874 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 {
3876 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3877 SIGN_TEXT);
3878# ifdef FEAT_SIGN_ICONS
3879 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3880 SIGN_ICON);
3881 if (gui.in_use && icon_sign != 0)
3882 {
3883 /* Use the image in this position. */
3884 c_extra = SIGN_BYTE;
3885# ifdef FEAT_NETBEANS_INTG
3886 if (buf_signcount(wp->w_buffer, lnum) > 1)
3887 c_extra = MULTISIGN_BYTE;
3888# endif
3889 char_attr = icon_sign;
3890 }
3891 else
3892# endif
3893 if (text_sign != 0)
3894 {
3895 p_extra = sign_get_text(text_sign);
3896 if (p_extra != NULL)
3897 {
3898 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003899 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
3901 char_attr = sign_get_attr(text_sign, FALSE);
3902 }
3903 }
3904 }
3905 }
3906#endif
3907
3908 if (draw_state == WL_NR - 1 && n_extra == 0)
3909 {
3910 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003911 /* Display the absolute or relative line number. After the
3912 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3913 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 && (row == startrow
3915#ifdef FEAT_DIFF
3916 + filler_lines
3917#endif
3918 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3919 {
3920 /* Draw the line number (empty space after wrapping). */
3921 if (row == startrow
3922#ifdef FEAT_DIFF
3923 + filler_lines
3924#endif
3925 )
3926 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003927 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003928 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003929
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003930 if (wp->w_p_nu && !wp->w_p_rnu)
3931 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003932 num = (long)lnum;
3933 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003934 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003935 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003936 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003937 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003938 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003939 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003940 num = lnum;
3941 fmt = "%-*ld ";
3942 }
3943 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003944
Bram Moolenaar700e7342013-01-30 12:31:36 +01003945 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003946 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 if (wp->w_skipcol > 0)
3948 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3949 *p_extra = '-';
3950#ifdef FEAT_RIGHTLEFT
3951 if (wp->w_p_rl) /* reverse line numbers */
3952 rl_mirror(extra);
3953#endif
3954 p_extra = extra;
3955 c_extra = NUL;
3956 }
3957 else
3958 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003959 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003960 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003961#ifdef FEAT_SYN_HL
3962 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003963 * the current line differently.
3964 * TODO: Can we use CursorLine instead of CursorLineNr
3965 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003966 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003967 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003968 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971 }
3972
Bram Moolenaar597a4222014-06-25 14:39:50 +02003973#ifdef FEAT_LINEBREAK
3974 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3975 && n_extra == 0 && *p_sbr != NUL)
3976 /* draw indent after showbreak value */
3977 draw_state = WL_BRI;
3978 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3979 /* After the showbreak, draw the breakindent */
3980 draw_state = WL_BRI - 1;
3981
3982 /* draw 'breakindent': indent wrapped text accordingly */
3983 if (draw_state == WL_BRI - 1 && n_extra == 0)
3984 {
3985 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003986 /* if need_showbreak is set, breakindent also applies */
3987 if (wp->w_p_bri && n_extra == 0
3988 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003989# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003990 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003991# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003992 )
3993 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003994 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003995# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003996 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003997 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003998 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003999# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004000 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4001 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004002 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004003# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004004 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004005# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004006 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004007 c_extra = ' ';
4008 n_extra = get_breakindent_win(wp,
4009 ml_get_buf(wp->w_buffer, lnum, FALSE));
4010 /* Correct end of highlighted area for 'breakindent',
4011 * required when 'linebreak' is also set. */
4012 if (tocol == vcol)
4013 tocol += n_extra;
4014 }
4015 }
4016#endif
4017
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4019 if (draw_state == WL_SBR - 1 && n_extra == 0)
4020 {
4021 draw_state = WL_SBR;
4022# ifdef FEAT_DIFF
4023 if (filler_todo > 0)
4024 {
4025 /* Draw "deleted" diff line(s). */
4026 if (char2cells(fill_diff) > 1)
4027 c_extra = '-';
4028 else
4029 c_extra = fill_diff;
4030# ifdef FEAT_RIGHTLEFT
4031 if (wp->w_p_rl)
4032 n_extra = col + 1;
4033 else
4034# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004035 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004036 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
4038# endif
4039# ifdef FEAT_LINEBREAK
4040 if (*p_sbr != NUL && need_showbreak)
4041 {
4042 /* Draw 'showbreak' at the start of each broken line. */
4043 p_extra = p_sbr;
4044 c_extra = NUL;
4045 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004046 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004048 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 /* Correct end of highlighted area for 'showbreak',
4050 * required when 'linebreak' is also set. */
4051 if (tocol == vcol)
4052 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004053#ifdef FEAT_SYN_HL
4054 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004055 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004056 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004057 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060# endif
4061 }
4062#endif
4063
4064 if (draw_state == WL_LINE - 1 && n_extra == 0)
4065 {
4066 draw_state = WL_LINE;
4067 if (saved_n_extra)
4068 {
4069 /* Continue item from end of wrapped line. */
4070 n_extra = saved_n_extra;
4071 c_extra = saved_c_extra;
4072 p_extra = saved_p_extra;
4073 char_attr = saved_char_attr;
4074 }
4075 else
4076 char_attr = 0;
4077 }
4078 }
4079
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004080 // When still displaying '$' of change command, stop at cursor.
4081 // When only displaying the (relative) line number and that's done,
4082 // stop here.
4083 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004084 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085#ifdef FEAT_DIFF
4086 && filler_todo <= 0
4087#endif
4088 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004089 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004091 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004092 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004093 /* Pretend we have finished updating the window. Except when
4094 * 'cursorcolumn' is set. */
4095#ifdef FEAT_SYN_HL
4096 if (wp->w_p_cuc)
4097 row = wp->w_cline_row + wp->w_cline_height;
4098 else
4099#endif
4100 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 break;
4102 }
4103
4104 if (draw_state == WL_LINE && area_highlighting)
4105 {
4106 /* handle Visual or match highlighting in this line */
4107 if (vcol == fromcol
4108#ifdef FEAT_MBYTE
4109 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4110 && (*mb_ptr2cells)(ptr) > 1)
4111#endif
4112 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004113 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 && vcol < tocol))
4115 area_attr = attr; /* start highlighting */
4116 else if (area_attr != 0
4117 && (vcol == tocol
4118 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
4121#ifdef FEAT_SEARCH_EXTRA
4122 if (!n_extra)
4123 {
4124 /*
4125 * Check for start/end of search pattern match.
4126 * After end, check for start/end of next match.
4127 * When another match, have to check for start again.
4128 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004129 * Do this for 'search_hl' and the match list (ordered by
4130 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004132 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004133 cur = wp->w_match_head;
4134 shl_flag = FALSE;
4135 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004137 if (shl_flag == FALSE
4138 && ((cur != NULL
4139 && cur->priority > SEARCH_HL_PRIORITY)
4140 || cur == NULL))
4141 {
4142 shl = &search_hl;
4143 shl_flag = TRUE;
4144 }
4145 else
4146 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004147 if (cur != NULL)
4148 cur->pos.cur = 0;
4149 pos_inprogress = TRUE;
4150 while (shl->rm.regprog != NULL
4151 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004153 if (shl->startcol != MAXCOL
4154 && v >= (long)shl->startcol
4155 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004157#ifdef FEAT_MBYTE
4158 int tmp_col = v + MB_PTR2LEN(ptr);
4159
4160 if (shl->endcol < tmp_col)
4161 shl->endcol = tmp_col;
4162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004164#ifdef FEAT_CONCEAL
4165 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4166 == cur->hlg_id)
4167 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004168 has_match_conc =
4169 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004170 match_conc = cur->conceal_char;
4171 }
4172 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004173 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004176 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 {
4178 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004179 next_search_hl(wp, shl, lnum, (colnr_T)v,
4180 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004181 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004182 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
4184 /* Need to get the line again, a multi-line regexp
4185 * may have made it invalid. */
4186 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4187 ptr = line + v;
4188
4189 if (shl->lnum == lnum)
4190 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004191 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004193 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004195 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004197 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 {
4199 /* highlight empty match, try again after
4200 * it */
4201#ifdef FEAT_MBYTE
4202 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004203 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004204 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 else
4206#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004207 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 }
4209
4210 /* Loop to check if the match starts at the
4211 * current position */
4212 continue;
4213 }
4214 }
4215 break;
4216 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004217 if (shl != &search_hl && cur != NULL)
4218 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004220
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004221 /* Use attributes from match with highest priority among
4222 * 'search_hl' and the match list. */
4223 search_attr = search_hl.attr_cur;
4224 cur = wp->w_match_head;
4225 shl_flag = FALSE;
4226 while (cur != NULL || shl_flag == FALSE)
4227 {
4228 if (shl_flag == FALSE
4229 && ((cur != NULL
4230 && cur->priority > SEARCH_HL_PRIORITY)
4231 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004232 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004233 shl = &search_hl;
4234 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004235 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004236 else
4237 shl = &cur->hl;
4238 if (shl->attr_cur != 0)
4239 search_attr = shl->attr_cur;
4240 if (shl != &search_hl && cur != NULL)
4241 cur = cur->next;
4242 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004243 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004244 if (*ptr == NUL && (did_line_attr >= 1
4245 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004246 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 }
4248#endif
4249
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004251 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004253 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4254 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004256 if (diff_hlf == HLF_TXD && ptr - line > change_end
4257 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004259 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004260 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004261 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004264
4265 /* Decide which of the highlight attributes to use. */
4266 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004267#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004268 if (area_attr != 0)
4269 char_attr = hl_combine_attr(line_attr, area_attr);
4270 else if (search_attr != 0)
4271 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004272 /* Use line_attr when not in the Visual or 'incsearch' area
4273 * (area_attr may be 0 when "noinvcur" is set). */
4274 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004275 || vcol < fromcol || vcol_prev < fromcol_prev
4276 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004277 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004278#else
4279 if (area_attr != 0)
4280 char_attr = area_attr;
4281 else if (search_attr != 0)
4282 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004283#endif
4284 else
4285 {
4286 attr_pri = FALSE;
4287#ifdef FEAT_SYN_HL
4288 if (has_syntax)
4289 char_attr = syntax_attr;
4290 else
4291#endif
4292 char_attr = 0;
4293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
4295
4296 /*
4297 * Get the next character to put on the screen.
4298 */
4299 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004300 * The "p_extra" points to the extra stuff that is inserted to
4301 * represent special characters (non-printable stuff) and other
4302 * things. When all characters are the same, c_extra is used.
4303 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4304 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4306 */
4307 if (n_extra > 0)
4308 {
4309 if (c_extra != NUL)
4310 {
4311 c = c_extra;
4312#ifdef FEAT_MBYTE
4313 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004314 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 {
4316 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004317 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004318 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 }
4320 else
4321 mb_utf8 = FALSE;
4322#endif
4323 }
4324 else
4325 {
4326 c = *p_extra;
4327#ifdef FEAT_MBYTE
4328 if (has_mbyte)
4329 {
4330 mb_c = c;
4331 if (enc_utf8)
4332 {
4333 /* If the UTF-8 character is more than one byte:
4334 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004335 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 mb_utf8 = FALSE;
4337 if (mb_l > n_extra)
4338 mb_l = 1;
4339 else if (mb_l > 1)
4340 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004341 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004343 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345 }
4346 else
4347 {
4348 /* if this is a DBCS character, put it in "mb_c" */
4349 mb_l = MB_BYTE2LEN(c);
4350 if (mb_l >= n_extra)
4351 mb_l = 1;
4352 else if (mb_l > 1)
4353 mb_c = (c << 8) + p_extra[1];
4354 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004355 if (mb_l == 0) /* at the NUL at end-of-line */
4356 mb_l = 1;
4357
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 /* If a double-width char doesn't fit display a '>' in the
4359 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004360 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361# ifdef FEAT_RIGHTLEFT
4362 wp->w_p_rl ? (col <= 0) :
4363# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004364 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 && (*mb_char2cells)(mb_c) == 2)
4366 {
4367 c = '>';
4368 mb_c = c;
4369 mb_l = 1;
4370 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004371 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 /* put the pointer back to output the double-width
4373 * character at the start of the next line. */
4374 ++n_extra;
4375 --p_extra;
4376 }
4377 else
4378 {
4379 n_extra -= mb_l - 1;
4380 p_extra += mb_l - 1;
4381 }
4382 }
4383#endif
4384 ++p_extra;
4385 }
4386 --n_extra;
4387 }
4388 else
4389 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004390#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004391 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004392#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004393
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004394 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004395 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 /*
4397 * Get a character from the line itself.
4398 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004399 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004400#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004401 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403#ifdef FEAT_MBYTE
4404 if (has_mbyte)
4405 {
4406 mb_c = c;
4407 if (enc_utf8)
4408 {
4409 /* If the UTF-8 character is more than one byte: Decode it
4410 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004411 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 mb_utf8 = FALSE;
4413 if (mb_l > 1)
4414 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004415 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 /* Overlong encoded ASCII or ASCII with composing char
4417 * is displayed normally, except a NUL. */
4418 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004419 {
4420 c = mb_c;
4421# ifdef FEAT_LINEBREAK
4422 c0 = mb_c;
4423# endif
4424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004426
4427 /* At start of the line we can have a composing char.
4428 * Draw it as a space with a composing char. */
4429 if (utf_iscomposing(mb_c))
4430 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004431 int i;
4432
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004433 for (i = Screen_mco - 1; i > 0; --i)
4434 u8cc[i] = u8cc[i - 1];
4435 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004436 mb_c = ' ';
4437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
4439
4440 if ((mb_l == 1 && c >= 0x80)
4441 || (mb_l >= 1 && mb_c == 0)
4442 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004443# ifdef UNICODE16
4444 || mb_c >= 0x10000
4445# endif
4446 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 {
4448 /*
4449 * Illegal UTF-8 byte: display as <xx>.
4450 * Non-BMP character : display as ? or fullwidth ?.
4451 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004452# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 {
4456 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004457# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 if (wp->w_p_rl) /* reverse */
4459 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004460# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004462# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 else if (utf_char2cells(mb_c) != 2)
4464 STRCPY(extra, "?");
4465 else
4466 /* 0xff1f in UTF-8: full-width '?' */
4467 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004468# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469
4470 p_extra = extra;
4471 c = *p_extra;
4472 mb_c = mb_ptr2char_adv(&p_extra);
4473 mb_utf8 = (c >= 0x80);
4474 n_extra = (int)STRLEN(p_extra);
4475 c_extra = NUL;
4476 if (area_attr == 0 && search_attr == 0)
4477 {
4478 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004479 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 saved_attr2 = char_attr; /* save current attr */
4481 }
4482 }
4483 else if (mb_l == 0) /* at the NUL at end-of-line */
4484 mb_l = 1;
4485#ifdef FEAT_ARABIC
4486 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4487 {
4488 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004489 int pc, pc1, nc;
4490 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491
4492 /* The idea of what is the previous and next
4493 * character depends on 'rightleft'. */
4494 if (wp->w_p_rl)
4495 {
4496 pc = prev_c;
4497 pc1 = prev_c1;
4498 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004499 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501 else
4502 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004503 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004505 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 }
4507 prev_c = mb_c;
4508
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004509 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 }
4511 else
4512 prev_c = mb_c;
4513#endif
4514 }
4515 else /* enc_dbcs */
4516 {
4517 mb_l = MB_BYTE2LEN(c);
4518 if (mb_l == 0) /* at the NUL at end-of-line */
4519 mb_l = 1;
4520 else if (mb_l > 1)
4521 {
4522 /* We assume a second byte below 32 is illegal.
4523 * Hopefully this is OK for all double-byte encodings!
4524 */
4525 if (ptr[1] >= 32)
4526 mb_c = (c << 8) + ptr[1];
4527 else
4528 {
4529 if (ptr[1] == NUL)
4530 {
4531 /* head byte at end of line */
4532 mb_l = 1;
4533 transchar_nonprint(extra, c);
4534 }
4535 else
4536 {
4537 /* illegal tail byte */
4538 mb_l = 2;
4539 STRCPY(extra, "XX");
4540 }
4541 p_extra = extra;
4542 n_extra = (int)STRLEN(extra) - 1;
4543 c_extra = NUL;
4544 c = *p_extra++;
4545 if (area_attr == 0 && search_attr == 0)
4546 {
4547 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004548 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 saved_attr2 = char_attr; /* save current attr */
4550 }
4551 mb_c = c;
4552 }
4553 }
4554 }
4555 /* If a double-width char doesn't fit display a '>' in the
4556 * last column; the character is displayed at the start of the
4557 * next line. */
4558 if ((
4559# ifdef FEAT_RIGHTLEFT
4560 wp->w_p_rl ? (col <= 0) :
4561# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004562 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 && (*mb_char2cells)(mb_c) == 2)
4564 {
4565 c = '>';
4566 mb_c = c;
4567 mb_utf8 = FALSE;
4568 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004569 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 /* Put pointer back so that the character will be
4571 * displayed at the start of the next line. */
4572 --ptr;
4573 }
4574 else if (*ptr != NUL)
4575 ptr += mb_l - 1;
4576
4577 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004578 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004579 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004580 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004583 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 c = ' ';
4585 if (area_attr == 0 && search_attr == 0)
4586 {
4587 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004588 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 saved_attr2 = char_attr; /* save current attr */
4590 }
4591 mb_c = c;
4592 mb_utf8 = FALSE;
4593 mb_l = 1;
4594 }
4595
4596 }
4597#endif
4598 ++ptr;
4599
4600 if (extra_check)
4601 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004602#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004603 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004604#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004605
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004606#ifdef FEAT_TERMINAL
4607 if (get_term_attr)
4608 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004609 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004610
4611 if (!attr_pri)
4612 char_attr = syntax_attr;
4613 else
4614 char_attr = hl_combine_attr(syntax_attr, char_attr);
4615 }
4616#endif
4617
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004618#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 /* Get syntax attribute, unless still at the start of the line
4620 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004621 v = (long)(ptr - line);
4622 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
4624 /* Get the syntax attribute for the character. If there
4625 * is an error, disable syntax highlighting. */
4626 save_did_emsg = did_emsg;
4627 did_emsg = FALSE;
4628
Bram Moolenaar217ad922005-03-20 22:37:15 +00004629 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004630# ifdef FEAT_SPELL
4631 has_spell ? &can_spell :
4632# endif
4633 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634
4635 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004636 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004637 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004638 has_syntax = FALSE;
4639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 else
4641 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004642#ifdef SYN_TIME_LIMIT
4643 if (wp->w_s->b_syn_slow)
4644 has_syntax = FALSE;
4645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646
4647 /* Need to get the line again, a multi-line regexp may
4648 * have made it invalid. */
4649 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4650 ptr = line + v;
4651
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004652 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004654 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004655 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004656# ifdef FEAT_CONCEAL
4657 /* no concealing past the end of the line, it interferes
4658 * with line highlighting */
4659 if (c == NUL)
4660 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004661 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004662 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004663# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004665#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004666
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004667#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004668 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004669 * Only do this when there is no syntax highlighting, the
4670 * @Spell cluster is not used or the current syntax item
4671 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004672 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004673 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004674 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004675# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004676 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004677 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004678# endif
4679 if (c != 0 && (
4680# ifdef FEAT_SYN_HL
4681 !has_syntax ||
4682# endif
4683 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004684 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004685 char_u *prev_ptr, *p;
4686 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004687 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004688# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004689 if (has_mbyte)
4690 {
4691 prev_ptr = ptr - mb_l;
4692 v -= mb_l - 1;
4693 }
4694 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004695# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004696 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004697
4698 /* Use nextline[] if possible, it has the start of the
4699 * next line concatenated. */
4700 if ((prev_ptr - line) - nextlinecol >= 0)
4701 p = nextline + (prev_ptr - line) - nextlinecol;
4702 else
4703 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004704 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004705 len = spell_check(wp, p, &spell_hlf, &cap_col,
4706 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004707 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004708
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004709 /* In Insert mode only highlight a word that
4710 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004711 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004712 && (State & INSERT) != 0
4713 && wp->w_cursor.lnum == lnum
4714 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004715 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004716 && wp->w_cursor.col < (colnr_T)word_end)
4717 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004718 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004719 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004720 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004721
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004722 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004723 && (p - nextline) + len > nextline_idx)
4724 {
4725 /* Remember that the good word continues at the
4726 * start of the next line. */
4727 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004728 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004729 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004730
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004731 /* Turn index into actual attributes. */
4732 if (spell_hlf != HLF_COUNT)
4733 spell_attr = highlight_attr[spell_hlf];
4734
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004735 if (cap_col > 0)
4736 {
4737 if (p != prev_ptr
4738 && (p - nextline) + cap_col >= nextline_idx)
4739 {
4740 /* Remember that the word in the next line
4741 * must start with a capital. */
4742 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004743 cap_col = (int)((p - nextline) + cap_col
4744 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004745 }
4746 else
4747 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004748 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004749 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004750 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004751 }
4752 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004753 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004754 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004755 char_attr = hl_combine_attr(char_attr, spell_attr);
4756 else
4757 char_attr = hl_combine_attr(spell_attr, char_attr);
4758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759#endif
4760#ifdef FEAT_LINEBREAK
4761 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004762 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004764 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004765 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004767# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004768 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004769# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004770 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004772 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004774 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004775
Bram Moolenaar597a4222014-06-25 14:39:50 +02004776 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004777 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004778 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004779 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004780# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004781 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004782 wp->w_buffer->b_p_vts_array) - 1;
4783# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004784 n_extra = (int)wp->w_buffer->b_p_ts
4785 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004786# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004787
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004788# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004789 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004790# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004792# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004793 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004794 {
4795#ifdef FEAT_CONCEAL
4796 if (c == TAB)
4797 /* See "Tab alignment" below. */
4798 FIX_FOR_BOGUSCOLS;
4799#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004800 if (!wp->w_p_list)
4801 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 }
4804#endif
4805
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004806 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4807 */
4808 if (wp->w_p_list
4809 && (((c == 160
4810#ifdef FEAT_MBYTE
4811 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4812#endif
4813 ) && lcs_nbsp)
4814 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4815 {
4816 c = (c == ' ') ? lcs_space : lcs_nbsp;
4817 if (area_attr == 0 && search_attr == 0)
4818 {
4819 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004820 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004821 saved_attr2 = char_attr; /* save current attr */
4822 }
4823#ifdef FEAT_MBYTE
4824 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004825 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004826 {
4827 mb_utf8 = TRUE;
4828 u8cc[0] = 0;
4829 c = 0xc0;
4830 }
4831 else
4832 mb_utf8 = FALSE;
4833#endif
4834 }
4835
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4837 {
4838 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004839 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 {
4841 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004842 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 saved_attr2 = char_attr; /* save current attr */
4844 }
4845#ifdef FEAT_MBYTE
4846 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004847 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 {
4849 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004850 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004851 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
4853 else
4854 mb_utf8 = FALSE;
4855#endif
4856 }
4857 }
4858
4859 /*
4860 * Handling of non-printable characters.
4861 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004862 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 {
4864 /*
4865 * when getting a character from the file, we may have to
4866 * turn it into something else on the way to putting it
4867 * into "ScreenLines".
4868 */
4869 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4870 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004871 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004872 long vcol_adjusted = vcol; /* removed showbreak length */
4873#ifdef FEAT_LINEBREAK
4874 /* only adjust the tab_len, when at the first column
4875 * after the showbreak value was drawn */
4876 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4877 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004880#ifdef FEAT_VARTABS
4881 tab_len = tabstop_padding(vcol_adjusted,
4882 wp->w_buffer->b_p_ts,
4883 wp->w_buffer->b_p_vts_array) - 1;
4884#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004885 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004886 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4887#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004888
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004889#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004890 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004891#endif
4892 /* tab amount depends on current column */
4893 n_extra = tab_len;
4894#ifdef FEAT_LINEBREAK
4895 else
4896 {
4897 char_u *p;
4898 int len = n_extra;
4899 int i;
4900 int saved_nextra = n_extra;
4901
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004902#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004903 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004904 /* there are characters to conceal */
4905 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004906 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4907 */
4908 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4909 && n_extra > tab_len)
4910 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004911#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004912
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004913 /* if n_extra > 0, it gives the number of chars, to
4914 * use for a tab, else we need to calculate the width
4915 * for a tab */
4916#ifdef FEAT_MBYTE
4917 len = (tab_len * mb_char2len(lcs_tab2));
4918 if (n_extra > 0)
4919 len += n_extra - tab_len;
4920#endif
4921 c = lcs_tab1;
4922 p = alloc((unsigned)(len + 1));
4923 vim_memset(p, ' ', len);
4924 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004925 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004926 p_extra_free = p;
4927 for (i = 0; i < tab_len; i++)
4928 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004929 if (*p == NUL)
4930 {
4931 tab_len = i;
4932 break;
4933 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004934#ifdef FEAT_MBYTE
4935 mb_char2bytes(lcs_tab2, p);
4936 p += mb_char2len(lcs_tab2);
4937 n_extra += mb_char2len(lcs_tab2)
4938 - (saved_nextra > 0 ? 1 : 0);
4939#else
4940 p[i] = lcs_tab2;
4941#endif
4942 }
4943 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004944#ifdef FEAT_CONCEAL
4945 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4946 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004947 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004948 n_extra -= vcol_off;
4949#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004950 }
4951#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004952#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004953 {
4954 int vc_saved = vcol_off;
4955
4956 /* Tab alignment should be identical regardless of
4957 * 'conceallevel' value. So tab compensates of all
4958 * previous concealed characters, and thus resets
4959 * vcol_off and boguscols accumulated so far in the
4960 * line. Note that the tab can be longer than
4961 * 'tabstop' when there are concealed characters. */
4962 FIX_FOR_BOGUSCOLS;
4963
4964 /* Make sure, the highlighting for the tab char will be
4965 * correctly set further below (effectively reverts the
4966 * FIX_FOR_BOGSUCOLS macro */
4967 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004968 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004969 tab_len += vc_saved;
4970 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972#ifdef FEAT_MBYTE
4973 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4974#endif
4975 if (wp->w_p_list)
4976 {
4977 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004978#ifdef FEAT_LINEBREAK
4979 if (wp->w_p_lbr)
4980 c_extra = NUL; /* using p_extra from above */
4981 else
4982#endif
4983 c_extra = lcs_tab2;
4984 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004985 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 saved_attr2 = char_attr; /* save current attr */
4987#ifdef FEAT_MBYTE
4988 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004989 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 {
4991 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004992 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004993 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 }
4995#endif
4996 }
4997 else
4998 {
4999 c_extra = ' ';
5000 c = ' ';
5001 }
5002 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005003 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005004 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005005 || ((fromcol >= 0 || fromcol_prev >= 0)
5006 && tocol > vcol
5007 && VIsual_mode != Ctrl_V
5008 && (
5009# ifdef FEAT_RIGHTLEFT
5010 wp->w_p_rl ? (col >= 0) :
5011# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005012 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005013 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005014 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005015 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005016 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005018 /* Display a '$' after the line or highlight an extra
5019 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5021 /* For a diff line the highlighting continues after the
5022 * "$". */
5023 if (
5024# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005025 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026# ifdef LINE_ATTR
5027 &&
5028# endif
5029# endif
5030# ifdef LINE_ATTR
5031 line_attr == 0
5032# endif
5033 )
5034#endif
5035 {
5036#ifdef FEAT_VIRTUALEDIT
5037 /* In virtualedit, visual selections may extend
5038 * beyond end of line. */
5039 if (area_highlighting && virtual_active()
5040 && tocol != MAXCOL && vcol < tocol)
5041 n_extra = 0;
5042 else
5043#endif
5044 {
5045 p_extra = at_end_str;
5046 n_extra = 1;
5047 c_extra = NUL;
5048 }
5049 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005050 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005051 c = lcs_eol;
5052 else
5053 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 lcs_eol_one = -1;
5055 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005056 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005058 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 n_attr = 1;
5060 }
5061#ifdef FEAT_MBYTE
5062 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005063 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
5065 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005066 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005067 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
5069 else
5070 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5071#endif
5072 }
5073 else if (c != NUL)
5074 {
5075 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005076 if (n_extra == 0)
5077 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078#ifdef FEAT_RIGHTLEFT
5079 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5080 rl_mirror(p_extra); /* reverse "<12>" */
5081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005083#ifdef FEAT_LINEBREAK
5084 if (wp->w_p_lbr)
5085 {
5086 char_u *p;
5087
5088 c = *p_extra;
5089 p = alloc((unsigned)n_extra + 1);
5090 vim_memset(p, ' ', n_extra);
5091 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5092 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005093 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005094 p_extra_free = p_extra = p;
5095 }
5096 else
5097#endif
5098 {
5099 n_extra = byte2cells(c) - 1;
5100 c = *p_extra++;
5101 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005102 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 {
5104 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005105 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 saved_attr2 = char_attr; /* save current attr */
5107 }
5108#ifdef FEAT_MBYTE
5109 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5110#endif
5111 }
5112#ifdef FEAT_VIRTUALEDIT
5113 else if (VIsual_active
5114 && (VIsual_mode == Ctrl_V
5115 || VIsual_mode == 'v')
5116 && virtual_active()
5117 && tocol != MAXCOL
5118 && vcol < tocol
5119 && (
5120# ifdef FEAT_RIGHTLEFT
5121 wp->w_p_rl ? (col >= 0) :
5122# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005123 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
5125 c = ' ';
5126 --ptr; /* put it back at the NUL */
5127 }
5128#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005129#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 else if ((
5131# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005132 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005134# ifdef FEAT_TERMINAL
5135 term_attr != 0 ||
5136# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 ) && (
5139# ifdef FEAT_RIGHTLEFT
5140 wp->w_p_rl ? (col >= 0) :
5141# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005142 (col
5143# ifdef FEAT_CONCEAL
5144 - boguscols
5145# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005146 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
5148 /* Highlight until the right side of the window */
5149 c = ' ';
5150 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005151
5152 /* Remember we do the char for line highlighting. */
5153 ++did_line_attr;
5154
5155 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005156 if (line_attr != 0 && char_attr == search_attr
5157 && (did_line_attr > 1
5158 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005159 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160# ifdef FEAT_DIFF
5161 if (diff_hlf == HLF_TXD)
5162 {
5163 diff_hlf = HLF_CHD;
5164 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005165 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005166 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005167 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5168 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005169 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
5172# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005173# ifdef FEAT_TERMINAL
5174 if (term_attr != 0)
5175 {
5176 char_attr = term_attr;
5177 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5178 char_attr = hl_combine_attr(char_attr,
5179 HL_ATTR(HLF_CUL));
5180 }
5181# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
5183#endif
5184 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005185
5186#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005187 if ( wp->w_p_cole > 0
5188 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005189 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005190 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005191 && !(lnum_in_visual_area
5192 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005193 {
5194 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005195 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005196 && (syn_get_sub_char() != NUL || match_conc
5197 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005198 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005199 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005200 /* First time at this concealed item: display one
5201 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005202 if (match_conc)
5203 c = match_conc;
5204 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005205 c = syn_get_sub_char();
5206 else if (lcs_conceal != NUL)
5207 c = lcs_conceal;
5208 else
5209 c = ' ';
5210
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005211 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005212
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005213 if (n_extra > 0)
5214 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005215 vcol += n_extra;
5216 if (wp->w_p_wrap && n_extra > 0)
5217 {
5218# ifdef FEAT_RIGHTLEFT
5219 if (wp->w_p_rl)
5220 {
5221 col -= n_extra;
5222 boguscols -= n_extra;
5223 }
5224 else
5225# endif
5226 {
5227 boguscols += n_extra;
5228 col += n_extra;
5229 }
5230 }
5231 n_extra = 0;
5232 n_attr = 0;
5233 }
5234 else if (n_skip == 0)
5235 {
5236 is_concealing = TRUE;
5237 n_skip = 1;
5238 }
5239# ifdef FEAT_MBYTE
5240 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005241 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005242 {
5243 mb_utf8 = TRUE;
5244 u8cc[0] = 0;
5245 c = 0xc0;
5246 }
5247 else
5248 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5249# endif
5250 }
5251 else
5252 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005253 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005254 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005255 }
5256#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 }
5258
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005259#ifdef FEAT_CONCEAL
5260 /* In the cursor line and we may be concealing characters: correct
5261 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005262 if (!did_wcol && draw_state == WL_LINE
5263 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005264 && conceal_cursor_line(wp)
5265 && (int)wp->w_virtcol <= vcol + n_skip)
5266 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005267# ifdef FEAT_RIGHTLEFT
5268 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005269 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005270 else
5271# endif
5272 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005273 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005274 did_wcol = TRUE;
5275 }
5276#endif
5277
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 /* Don't override visual selection highlighting. */
5279 if (n_attr > 0
5280 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005281 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 char_attr = extra_attr;
5283
Bram Moolenaar81695252004-12-29 20:58:21 +00005284#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 /* XIM don't send preedit_start and preedit_end, but they send
5286 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5287 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005288 if (p_imst == IM_ON_THE_SPOT
5289 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005290 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 && (State & INSERT)
5292 && !p_imdisable
5293 && im_is_preediting()
5294 && draw_state == WL_LINE)
5295 {
5296 colnr_T tcol;
5297
5298 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005299 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 else
5301 tcol = preedit_end_col;
5302 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5303 {
5304 if (feedback_old_attr < 0)
5305 {
5306 feedback_col = 0;
5307 feedback_old_attr = char_attr;
5308 }
5309 char_attr = im_get_feedback_attr(feedback_col);
5310 if (char_attr < 0)
5311 char_attr = feedback_old_attr;
5312 feedback_col++;
5313 }
5314 else if (feedback_old_attr >= 0)
5315 {
5316 char_attr = feedback_old_attr;
5317 feedback_old_attr = -1;
5318 feedback_col = 0;
5319 }
5320 }
5321#endif
5322 /*
5323 * Handle the case where we are in column 0 but not on the first
5324 * character of the line and the user wants us to show us a
5325 * special character (via 'listchars' option "precedes:<char>".
5326 */
5327 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005328 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5330#ifdef FEAT_DIFF
5331 && filler_todo <= 0
5332#endif
5333 && draw_state > WL_NR
5334 && c != NUL)
5335 {
5336 c = lcs_prec;
5337 lcs_prec_todo = NUL;
5338#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005339 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5340 {
5341 /* Double-width character being overwritten by the "precedes"
5342 * character, need to fill up half the character. */
5343 c_extra = MB_FILLER_CHAR;
5344 n_extra = 1;
5345 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005346 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005349 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 {
5351 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005352 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005353 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 }
5355 else
5356 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5357#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005358 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 {
5360 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005361 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 n_attr3 = 1;
5363 }
5364 }
5365
5366 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005367 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005369 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005370#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005371 || did_line_attr == 1
5372#endif
5373 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005375#ifdef FEAT_SEARCH_EXTRA
5376 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005377
5378 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005379 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005380 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005381#endif
5382
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005383 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 * highlight match at end of line. If it's beyond the last
5385 * char on the screen, just overwrite that one (tricky!) Not
5386 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005387#ifdef FEAT_SEARCH_EXTRA
5388 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005389 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005390 prevcol_hl_flag = TRUE;
5391 else
5392 {
5393 cur = wp->w_match_head;
5394 while (cur != NULL)
5395 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005396 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005397 {
5398 prevcol_hl_flag = TRUE;
5399 break;
5400 }
5401 cur = cur->next;
5402 }
5403 }
5404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005406 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005407 && (VIsual_mode != Ctrl_V
5408 || lnum == VIsual.lnum
5409 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005410 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411#ifdef FEAT_SEARCH_EXTRA
5412 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005413 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005414# ifdef FEAT_SYN_HL
5415 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5416 && !(wp == curwin && VIsual_active))
5417# endif
5418# ifdef FEAT_DIFF
5419 && diff_hlf == (hlf_T)0
5420# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005421# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005422 && did_line_attr <= 1
5423# endif
5424 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425#endif
5426 ))
5427 {
5428 int n = 0;
5429
5430#ifdef FEAT_RIGHTLEFT
5431 if (wp->w_p_rl)
5432 {
5433 if (col < 0)
5434 n = 1;
5435 }
5436 else
5437#endif
5438 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005439 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 n = -1;
5441 }
5442 if (n != 0)
5443 {
5444 /* At the window boundary, highlight the last character
5445 * instead (better than nothing). */
5446 off += n;
5447 col += n;
5448 }
5449 else
5450 {
5451 /* Add a blank character to highlight. */
5452 ScreenLines[off] = ' ';
5453#ifdef FEAT_MBYTE
5454 if (enc_utf8)
5455 ScreenLinesUC[off] = 0;
5456#endif
5457 }
5458#ifdef FEAT_SEARCH_EXTRA
5459 if (area_attr == 0)
5460 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005461 /* Use attributes from match with highest priority among
5462 * 'search_hl' and the match list. */
5463 char_attr = search_hl.attr;
5464 cur = wp->w_match_head;
5465 shl_flag = FALSE;
5466 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005467 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005468 if (shl_flag == FALSE
5469 && ((cur != NULL
5470 && cur->priority > SEARCH_HL_PRIORITY)
5471 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005472 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005473 shl = &search_hl;
5474 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005475 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005476 else
5477 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005478 if ((ptr - line) - 1 == (long)shl->startcol
5479 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005480 char_attr = shl->attr;
5481 if (shl != &search_hl && cur != NULL)
5482 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 }
5485#endif
5486 ScreenAttrs[off] = char_attr;
5487#ifdef FEAT_RIGHTLEFT
5488 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005491 --off;
5492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 else
5494#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005497 ++off;
5498 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005499 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005500#ifdef FEAT_SYN_HL
5501 eol_hl_off = 1;
5502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
Bram Moolenaar91170f82006-05-05 21:15:17 +00005506 /*
5507 * At end of the text line.
5508 */
5509 if (c == NUL)
5510 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005511#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005512 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005513 if (wp->w_p_wrap)
5514 v = wp->w_skipcol;
5515 else
5516 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005517
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005518 /* check if line ends before left margin */
5519 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005520 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005521#ifdef FEAT_CONCEAL
5522 /* Get rid of the boguscols now, we want to draw until the right
5523 * edge for 'cursorcolumn'. */
5524 col -= boguscols;
5525 boguscols = 0;
5526#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005527
5528 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005529 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005530
5531 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005532 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5533 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005534 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005535 && lnum != wp->w_cursor.lnum)
5536 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005537# ifdef FEAT_RIGHTLEFT
5538 && !wp->w_p_rl
5539# endif
5540 )
5541 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005542 int rightmost_vcol = 0;
5543 int i;
5544
5545 if (wp->w_p_cuc)
5546 rightmost_vcol = wp->w_virtcol;
5547 if (draw_color_col)
5548 /* determine rightmost colorcolumn to possibly draw */
5549 for (i = 0; color_cols[i] >= 0; ++i)
5550 if (rightmost_vcol < color_cols[i])
5551 rightmost_vcol = color_cols[i];
5552
Bram Moolenaar02631462017-09-22 15:20:32 +02005553 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005554 {
5555 ScreenLines[off] = ' ';
5556#ifdef FEAT_MBYTE
5557 if (enc_utf8)
5558 ScreenLinesUC[off] = 0;
5559#endif
5560 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005561 if (draw_color_col)
5562 draw_color_col = advance_color_col(VCOL_HLC,
5563 &color_cols);
5564
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005565 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005566 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005567 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005568 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005569 else
5570 ScreenAttrs[off++] = 0;
5571
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005572 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005573 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005574
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005575 ++vcol;
5576 }
5577 }
5578#endif
5579
Bram Moolenaar53f81742017-09-22 14:35:51 +02005580 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005581 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 row++;
5583
5584 /*
5585 * Update w_cline_height and w_cline_folded if the cursor line was
5586 * updated (saves a call to plines() later).
5587 */
5588 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5589 {
5590 curwin->w_cline_row = startrow;
5591 curwin->w_cline_height = row - startrow;
5592#ifdef FEAT_FOLDING
5593 curwin->w_cline_folded = FALSE;
5594#endif
5595 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5596 }
5597
5598 break;
5599 }
5600
5601 /* line continues beyond line end */
5602 if (lcs_ext
5603 && !wp->w_p_wrap
5604#ifdef FEAT_DIFF
5605 && filler_todo <= 0
5606#endif
5607 && (
5608#ifdef FEAT_RIGHTLEFT
5609 wp->w_p_rl ? col == 0 :
5610#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005611 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005613 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5615 {
5616 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005617 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618#ifdef FEAT_MBYTE
5619 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005620 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 {
5622 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005623 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005624 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 }
5626 else
5627 mb_utf8 = FALSE;
5628#endif
5629 }
5630
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005631#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005632 /* advance to the next 'colorcolumn' */
5633 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005634 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005635
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005636 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005637 * highlight the cursor position itself.
5638 * Also highlight the 'colorcolumn' if it is different than
5639 * 'cursorcolumn' */
5640 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005641 if (draw_state == WL_LINE && !lnum_in_visual_area
5642 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005643 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005644 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005645 && lnum != wp->w_cursor.lnum)
5646 {
5647 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005648 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005649 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005650 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005651 {
5652 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005653 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005654 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005655 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005656#endif
5657
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 /*
5659 * Store character to be displayed.
5660 * Skip characters that are left of the screen for 'nowrap'.
5661 */
5662 vcol_prev = vcol;
5663 if (draw_state < WL_LINE || n_skip <= 0)
5664 {
5665 /*
5666 * Store the character.
5667 */
5668#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5669 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5670 {
5671 /* A double-wide character is: put first halve in left cell. */
5672 --off;
5673 --col;
5674 }
5675#endif
5676 ScreenLines[off] = c;
5677#ifdef FEAT_MBYTE
5678 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005679 {
5680 if ((mb_c & 0xff00) == 0x8e00)
5681 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 else if (enc_utf8)
5685 {
5686 if (mb_utf8)
5687 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005688 int i;
5689
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005691 if ((c & 0xff) == 0)
5692 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005693 for (i = 0; i < Screen_mco; ++i)
5694 {
5695 ScreenLinesC[i][off] = u8cc[i];
5696 if (u8cc[i] == 0)
5697 break;
5698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700 else
5701 ScreenLinesUC[off] = 0;
5702 }
5703 if (multi_attr)
5704 {
5705 ScreenAttrs[off] = multi_attr;
5706 multi_attr = 0;
5707 }
5708 else
5709#endif
5710 ScreenAttrs[off] = char_attr;
5711
5712#ifdef FEAT_MBYTE
5713 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5714 {
5715 /* Need to fill two screen columns. */
5716 ++off;
5717 ++col;
5718 if (enc_utf8)
5719 /* UTF-8: Put a 0 in the second screen char. */
5720 ScreenLines[off] = 0;
5721 else
5722 /* DBCS: Put second byte in the second screen char. */
5723 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005724 if (draw_state > WL_NR
5725#ifdef FEAT_DIFF
5726 && filler_todo <= 0
5727#endif
5728 )
5729 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 /* When "tocol" is halfway a character, set it to the end of
5731 * the character, otherwise highlighting won't stop. */
5732 if (tocol == vcol)
5733 ++tocol;
5734#ifdef FEAT_RIGHTLEFT
5735 if (wp->w_p_rl)
5736 {
5737 /* now it's time to backup one cell */
5738 --off;
5739 --col;
5740 }
5741#endif
5742 }
5743#endif
5744#ifdef FEAT_RIGHTLEFT
5745 if (wp->w_p_rl)
5746 {
5747 --off;
5748 --col;
5749 }
5750 else
5751#endif
5752 {
5753 ++off;
5754 ++col;
5755 }
5756 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005757#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005758 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005759 {
5760 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005761 ++vcol_off;
5762 if (n_extra > 0)
5763 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005764 if (wp->w_p_wrap)
5765 {
5766 /*
5767 * Special voodoo required if 'wrap' is on.
5768 *
5769 * Advance the column indicator to force the line
5770 * drawing to wrap early. This will make the line
5771 * take up the same screen space when parts are concealed,
5772 * so that cursor line computations aren't messed up.
5773 *
5774 * To avoid the fictitious advance of 'col' causing
5775 * trailing junk to be written out of the screen line
5776 * we are building, 'boguscols' keeps track of the number
5777 * of bad columns we have advanced.
5778 */
5779 if (n_extra > 0)
5780 {
5781 vcol += n_extra;
5782# ifdef FEAT_RIGHTLEFT
5783 if (wp->w_p_rl)
5784 {
5785 col -= n_extra;
5786 boguscols -= n_extra;
5787 }
5788 else
5789# endif
5790 {
5791 col += n_extra;
5792 boguscols += n_extra;
5793 }
5794 n_extra = 0;
5795 n_attr = 0;
5796 }
5797
5798
5799# ifdef FEAT_MBYTE
5800 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5801 {
5802 /* Need to fill two screen columns. */
5803# ifdef FEAT_RIGHTLEFT
5804 if (wp->w_p_rl)
5805 {
5806 --boguscols;
5807 --col;
5808 }
5809 else
5810# endif
5811 {
5812 ++boguscols;
5813 ++col;
5814 }
5815 }
5816# endif
5817
5818# ifdef FEAT_RIGHTLEFT
5819 if (wp->w_p_rl)
5820 {
5821 --boguscols;
5822 --col;
5823 }
5824 else
5825# endif
5826 {
5827 ++boguscols;
5828 ++col;
5829 }
5830 }
5831 else
5832 {
5833 if (n_extra > 0)
5834 {
5835 vcol += n_extra;
5836 n_extra = 0;
5837 n_attr = 0;
5838 }
5839 }
5840
5841 }
5842#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 else
5844 --n_skip;
5845
Bram Moolenaar64486672010-05-16 15:46:46 +02005846 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5847 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005848 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849#ifdef FEAT_DIFF
5850 && filler_todo <= 0
5851#endif
5852 )
5853 ++vcol;
5854
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005855#ifdef FEAT_SYN_HL
5856 if (vcol_save_attr >= 0)
5857 char_attr = vcol_save_attr;
5858#endif
5859
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 /* restore attributes after "predeces" in 'listchars' */
5861 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5862 char_attr = saved_attr3;
5863
5864 /* restore attributes after last 'listchars' or 'number' char */
5865 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5866 char_attr = saved_attr2;
5867
5868 /*
5869 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005870 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 */
5872 if ((
5873#ifdef FEAT_RIGHTLEFT
5874 wp->w_p_rl ? (col < 0) :
5875#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005876 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 && (*ptr != NUL
5878#ifdef FEAT_DIFF
5879 || filler_todo > 0
5880#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005881 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5883 )
5884 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005885#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005886 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005887 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005888 boguscols = 0;
5889#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005890 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005891 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 ++row;
5894 ++screen_row;
5895
5896 /* When not wrapping and finished diff lines, or when displayed
5897 * '$' and highlighting until last column, break here. */
5898 if ((!wp->w_p_wrap
5899#ifdef FEAT_DIFF
5900 && filler_todo <= 0
5901#endif
5902 ) || lcs_eol_one == -1)
5903 break;
5904
5905 /* When the window is too narrow draw all "@" lines. */
5906 if (draw_state != WL_LINE
5907#ifdef FEAT_DIFF
5908 && filler_todo <= 0
5909#endif
5910 )
5911 {
5912 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 row = endrow;
5915 }
5916
5917 /* When line got too long for screen break here. */
5918 if (row == endrow)
5919 {
5920 ++row;
5921 break;
5922 }
5923
5924 if (screen_cur_row == screen_row - 1
5925#ifdef FEAT_DIFF
5926 && filler_todo <= 0
5927#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005928 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 {
5930 /* Remember that the line wraps, used for modeless copy. */
5931 LineWraps[screen_row - 1] = TRUE;
5932
5933 /*
5934 * Special trick to make copy/paste of wrapped lines work with
5935 * xterm/screen: write an extra character beyond the end of
5936 * the line. This will work with all terminal types
5937 * (regardless of the xn,am settings).
5938 * Only do this on a fast tty.
5939 * Only do this if the cursor is on the current line
5940 * (something has been written in it).
5941 * Don't do this for the GUI.
5942 * Don't do this for double-width characters.
5943 * Don't do this for a window not at the right screen border.
5944 */
5945 if (p_tf
5946#ifdef FEAT_GUI
5947 && !gui.in_use
5948#endif
5949#ifdef FEAT_MBYTE
5950 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005951 && ((*mb_off2cells)(LineOffset[screen_row],
5952 LineOffset[screen_row] + screen_Columns)
5953 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005955 + (int)Columns - 2,
5956 LineOffset[screen_row] + screen_Columns)
5957 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958#endif
5959 )
5960 {
5961 /* First make sure we are at the end of the screen line,
5962 * then output the same character again to let the
5963 * terminal know about the wrap. If the terminal doesn't
5964 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005965 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 screen_char(LineOffset[screen_row - 1]
5967 + (unsigned)Columns - 1,
5968 screen_row - 1, (int)(Columns - 1));
5969
5970#ifdef FEAT_MBYTE
5971 /* When there is a multi-byte character, just output a
5972 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005973 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5974 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 out_char(' ');
5976 else
5977#endif
5978 out_char(ScreenLines[LineOffset[screen_row - 1]
5979 + (Columns - 1)]);
5980 /* force a redraw of the first char on the next line */
5981 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5982 screen_start(); /* don't know where cursor is now */
5983 }
5984 }
5985
5986 col = 0;
5987 off = (unsigned)(current_ScreenLine - ScreenLines);
5988#ifdef FEAT_RIGHTLEFT
5989 if (wp->w_p_rl)
5990 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005991 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 off += col;
5993 }
5994#endif
5995
5996 /* reset the drawing state for the start of a wrapped line */
5997 draw_state = WL_START;
5998 saved_n_extra = n_extra;
5999 saved_p_extra = p_extra;
6000 saved_c_extra = c_extra;
6001 saved_char_attr = char_attr;
6002 n_extra = 0;
6003 lcs_prec_todo = lcs_prec;
6004#ifdef FEAT_LINEBREAK
6005# ifdef FEAT_DIFF
6006 if (filler_todo <= 0)
6007# endif
6008 need_showbreak = TRUE;
6009#endif
6010#ifdef FEAT_DIFF
6011 --filler_todo;
6012 /* When the filler lines are actually below the last line of the
6013 * file, don't draw the line itself, break here. */
6014 if (filler_todo == 0 && wp->w_botfill)
6015 break;
6016#endif
6017 }
6018
6019 } /* for every character in the line */
6020
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006021#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006022 /* After an empty line check first word for capital. */
6023 if (*skipwhite(line) == NUL)
6024 {
6025 capcol_lnum = lnum + 1;
6026 cap_col = 0;
6027 }
6028#endif
6029
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006030 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 return row;
6032}
6033
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006034#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006035/*
6036 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006037 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006038 */
6039 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006040comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006041{
6042 int i;
6043
6044 for (i = 0; i < Screen_mco; ++i)
6045 {
6046 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6047 return TRUE;
6048 if (ScreenLinesC[i][off_from] == 0)
6049 break;
6050 }
6051 return FALSE;
6052}
6053#endif
6054
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055/*
6056 * Check whether the given character needs redrawing:
6057 * - the (first byte of the) character is different
6058 * - the attributes are different
6059 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006060 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 */
6062 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006063char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064{
6065 if (cols > 0
6066 && ((ScreenLines[off_from] != ScreenLines[off_to]
6067 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6068
6069#ifdef FEAT_MBYTE
6070 || (enc_dbcs != 0
6071 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6072 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6073 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6074 : (cols > 1 && ScreenLines[off_from + 1]
6075 != ScreenLines[off_to + 1])))
6076 || (enc_utf8
6077 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6078 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006079 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006080 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6081 && ScreenLines[off_from + 1]
6082 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083#endif
6084 ))
6085 return TRUE;
6086 return FALSE;
6087}
6088
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006089#if defined(FEAT_TERMINAL) || defined(PROTO)
6090/*
6091 * Return the index in ScreenLines[] for the current screen line.
6092 */
6093 int
6094screen_get_current_line_off()
6095{
6096 return (int)(current_ScreenLine - ScreenLines);
6097}
6098#endif
6099
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100/*
6101 * Move one "cooked" screen line to the screen, but only the characters that
6102 * have actually changed. Handle insert/delete character.
6103 * "coloff" gives the first column on the screen for this line.
6104 * "endcol" gives the columns where valid characters are.
6105 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6106 * needs to be cleared, negative otherwise.
6107 * "rlflag" is TRUE in a rightleft window:
6108 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6109 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6110 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006111 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006112screen_line(
6113 int row,
6114 int coloff,
6115 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006116 int clear_width,
6117 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118{
6119 unsigned off_from;
6120 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006121#ifdef FEAT_MBYTE
6122 unsigned max_off_from;
6123 unsigned max_off_to;
6124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 int force = FALSE; /* force update rest of the line */
6128 int redraw_this /* bool: does character need redraw? */
6129#ifdef FEAT_GUI
6130 = TRUE /* For GUI when while-loop empty */
6131#endif
6132 ;
6133 int redraw_next; /* redraw_this for next character */
6134#ifdef FEAT_MBYTE
6135 int clear_next = FALSE;
6136 int char_cells; /* 1: normal char */
6137 /* 2: occupies two display cells */
6138# define CHAR_CELLS char_cells
6139#else
6140# define CHAR_CELLS 1
6141#endif
6142
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006143 /* Check for illegal row and col, just in case. */
6144 if (row >= Rows)
6145 row = Rows - 1;
6146 if (endcol > Columns)
6147 endcol = Columns;
6148
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149# ifdef FEAT_CLIPBOARD
6150 clip_may_clear_selection(row, row);
6151# endif
6152
6153 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6154 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006155#ifdef FEAT_MBYTE
6156 max_off_from = off_from + screen_Columns;
6157 max_off_to = LineOffset[row] + screen_Columns;
6158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159
6160#ifdef FEAT_RIGHTLEFT
6161 if (rlflag)
6162 {
6163 /* Clear rest first, because it's left of the text. */
6164 if (clear_width > 0)
6165 {
6166 while (col <= endcol && ScreenLines[off_to] == ' '
6167 && ScreenAttrs[off_to] == 0
6168# ifdef FEAT_MBYTE
6169 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6170# endif
6171 )
6172 {
6173 ++off_to;
6174 ++col;
6175 }
6176 if (col <= endcol)
6177 screen_fill(row, row + 1, col + coloff,
6178 endcol + coloff + 1, ' ', ' ', 0);
6179 }
6180 col = endcol + 1;
6181 off_to = LineOffset[row] + col + coloff;
6182 off_from += col;
6183 endcol = (clear_width > 0 ? clear_width : -clear_width);
6184 }
6185#endif /* FEAT_RIGHTLEFT */
6186
6187 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6188
6189 while (col < endcol)
6190 {
6191#ifdef FEAT_MBYTE
6192 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006193 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 else
6195 char_cells = 1;
6196#endif
6197
6198 redraw_this = redraw_next;
6199 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6200 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6201
6202#ifdef FEAT_GUI
6203 /* If the next character was bold, then redraw the current character to
6204 * remove any pixels that might have spilt over into us. This only
6205 * happens in the GUI.
6206 */
6207 if (redraw_next && gui.in_use)
6208 {
6209 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006210 if (hl > HL_ALL)
6211 hl = syn_attr2attr(hl);
6212 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 redraw_this = TRUE;
6214 }
6215#endif
6216
6217 if (redraw_this)
6218 {
6219 /*
6220 * Special handling when 'xs' termcap flag set (hpterm):
6221 * Attributes for characters are stored at the position where the
6222 * cursor is when writing the highlighting code. The
6223 * start-highlighting code must be written with the cursor on the
6224 * first highlighted character. The stop-highlighting code must
6225 * be written with the cursor just after the last highlighted
6226 * character.
6227 * Overwriting a character doesn't remove it's highlighting. Need
6228 * to clear the rest of the line, and force redrawing it
6229 * completely.
6230 */
6231 if ( p_wiv
6232 && !force
6233#ifdef FEAT_GUI
6234 && !gui.in_use
6235#endif
6236 && ScreenAttrs[off_to] != 0
6237 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6238 {
6239 /*
6240 * Need to remove highlighting attributes here.
6241 */
6242 windgoto(row, col + coloff);
6243 out_str(T_CE); /* clear rest of this screen line */
6244 screen_start(); /* don't know where cursor is now */
6245 force = TRUE; /* force redraw of rest of the line */
6246 redraw_next = TRUE; /* or else next char would miss out */
6247
6248 /*
6249 * If the previous character was highlighted, need to stop
6250 * highlighting at this character.
6251 */
6252 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6253 {
6254 screen_attr = ScreenAttrs[off_to - 1];
6255 term_windgoto(row, col + coloff);
6256 screen_stop_highlight();
6257 }
6258 else
6259 screen_attr = 0; /* highlighting has stopped */
6260 }
6261#ifdef FEAT_MBYTE
6262 if (enc_dbcs != 0)
6263 {
6264 /* Check if overwriting a double-byte with a single-byte or
6265 * the other way around requires another character to be
6266 * redrawn. For UTF-8 this isn't needed, because comparing
6267 * ScreenLinesUC[] is sufficient. */
6268 if (char_cells == 1
6269 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006270 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 {
6272 /* Writing a single-cell character over a double-cell
6273 * character: need to redraw the next cell. */
6274 ScreenLines[off_to + 1] = 0;
6275 redraw_next = TRUE;
6276 }
6277 else if (char_cells == 2
6278 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006279 && (*mb_off2cells)(off_to, max_off_to) == 1
6280 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 {
6282 /* Writing the second half of a double-cell character over
6283 * a double-cell character: need to redraw the second
6284 * cell. */
6285 ScreenLines[off_to + 2] = 0;
6286 redraw_next = TRUE;
6287 }
6288
6289 if (enc_dbcs == DBCS_JPNU)
6290 ScreenLines2[off_to] = ScreenLines2[off_from];
6291 }
6292 /* When writing a single-width character over a double-width
6293 * character and at the end of the redrawn text, need to clear out
6294 * the right halve of the old character.
6295 * Also required when writing the right halve of a double-width
6296 * char over the left halve of an existing one. */
6297 if (has_mbyte && col + char_cells == endcol
6298 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006299 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006301 && (*mb_off2cells)(off_to, max_off_to) == 1
6302 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 clear_next = TRUE;
6304#endif
6305
6306 ScreenLines[off_to] = ScreenLines[off_from];
6307#ifdef FEAT_MBYTE
6308 if (enc_utf8)
6309 {
6310 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6311 if (ScreenLinesUC[off_from] != 0)
6312 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006313 int i;
6314
6315 for (i = 0; i < Screen_mco; ++i)
6316 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 }
6318 }
6319 if (char_cells == 2)
6320 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6321#endif
6322
6323#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006324 /* The bold trick makes a single column of pixels appear in the
6325 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 * character should be redrawn too. This happens for our own GUI
6327 * and for some xterms. */
6328 if (
6329# ifdef FEAT_GUI
6330 gui.in_use
6331# endif
6332# if defined(FEAT_GUI) && defined(UNIX)
6333 ||
6334# endif
6335# ifdef UNIX
6336 term_is_xterm
6337# endif
6338 )
6339 {
6340 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006341 if (hl > HL_ALL)
6342 hl = syn_attr2attr(hl);
6343 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 redraw_next = TRUE;
6345 }
6346#endif
6347 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6348#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006349 /* For simplicity set the attributes of second half of a
6350 * double-wide character equal to the first half. */
6351 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006353
6354 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 else
6357#endif
6358 screen_char(off_to, row, col + coloff);
6359 }
6360 else if ( p_wiv
6361#ifdef FEAT_GUI
6362 && !gui.in_use
6363#endif
6364 && col + coloff > 0)
6365 {
6366 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6367 {
6368 /*
6369 * Don't output stop-highlight when moving the cursor, it will
6370 * stop the highlighting when it should continue.
6371 */
6372 screen_attr = 0;
6373 }
6374 else if (screen_attr != 0)
6375 screen_stop_highlight();
6376 }
6377
6378 off_to += CHAR_CELLS;
6379 off_from += CHAR_CELLS;
6380 col += CHAR_CELLS;
6381 }
6382
6383#ifdef FEAT_MBYTE
6384 if (clear_next)
6385 {
6386 /* Clear the second half of a double-wide character of which the left
6387 * half was overwritten with a single-wide character. */
6388 ScreenLines[off_to] = ' ';
6389 if (enc_utf8)
6390 ScreenLinesUC[off_to] = 0;
6391 screen_char(off_to, row, col + coloff);
6392 }
6393#endif
6394
6395 if (clear_width > 0
6396#ifdef FEAT_RIGHTLEFT
6397 && !rlflag
6398#endif
6399 )
6400 {
6401#ifdef FEAT_GUI
6402 int startCol = col;
6403#endif
6404
6405 /* blank out the rest of the line */
6406 while (col < clear_width && ScreenLines[off_to] == ' '
6407 && ScreenAttrs[off_to] == 0
6408#ifdef FEAT_MBYTE
6409 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6410#endif
6411 )
6412 {
6413 ++off_to;
6414 ++col;
6415 }
6416 if (col < clear_width)
6417 {
6418#ifdef FEAT_GUI
6419 /*
6420 * In the GUI, clearing the rest of the line may leave pixels
6421 * behind if the first character cleared was bold. Some bold
6422 * fonts spill over the left. In this case we redraw the previous
6423 * character too. If we didn't skip any blanks above, then we
6424 * only redraw if the character wasn't already redrawn anyway.
6425 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006426 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 {
6428 hl = ScreenAttrs[off_to];
6429 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006430 {
6431 int prev_cells = 1;
6432# ifdef FEAT_MBYTE
6433 if (enc_utf8)
6434 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6435 * that its width is 2. */
6436 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6437 else if (enc_dbcs != 0)
6438 {
6439 /* find previous character by counting from first
6440 * column and get its width. */
6441 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006442 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006443
6444 while (off < off_to)
6445 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006446 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006447 off += prev_cells;
6448 }
6449 }
6450
6451 if (enc_dbcs != 0 && prev_cells > 1)
6452 screen_char_2(off_to - prev_cells, row,
6453 col + coloff - prev_cells);
6454 else
6455# endif
6456 screen_char(off_to - prev_cells, row,
6457 col + coloff - prev_cells);
6458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 }
6460#endif
6461 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6462 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 off_to += clear_width - col;
6464 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 }
6466 }
6467
6468 if (clear_width > 0)
6469 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 /* For a window that's left of another, draw the separator char. */
6471 if (col + coloff < Columns)
6472 {
6473 int c;
6474
6475 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006476 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006477#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006478 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6479 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 || ScreenAttrs[off_to] != hl)
6482 {
6483 ScreenLines[off_to] = c;
6484 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006485#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 if (enc_utf8)
6487 {
6488 if (c >= 0x80)
6489 {
6490 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006491 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 }
6493 else
6494 ScreenLinesUC[off_to] = 0;
6495 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 screen_char(off_to, row, col + coloff);
6498 }
6499 }
6500 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 LineWraps[row] = FALSE;
6502 }
6503}
6504
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006505#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006507 * Mirror text "str" for right-left displaying.
6508 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006510 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006511rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512{
6513 char_u *p1, *p2;
6514 int t;
6515
6516 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6517 {
6518 t = *p1;
6519 *p1 = *p2;
6520 *p2 = t;
6521 }
6522}
6523#endif
6524
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525/*
6526 * mark all status lines for redraw; used after first :cd
6527 */
6528 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006529status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530{
6531 win_T *wp;
6532
Bram Moolenaar29323592016-07-24 22:04:11 +02006533 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 if (wp->w_status_height)
6535 {
6536 wp->w_redr_status = TRUE;
6537 redraw_later(VALID);
6538 }
6539}
6540
6541/*
6542 * mark all status lines of the current buffer for redraw
6543 */
6544 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006545status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546{
6547 win_T *wp;
6548
Bram Moolenaar29323592016-07-24 22:04:11 +02006549 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6551 {
6552 wp->w_redr_status = TRUE;
6553 redraw_later(VALID);
6554 }
6555}
6556
6557/*
6558 * Redraw all status lines that need to be redrawn.
6559 */
6560 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006561redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562{
6563 win_T *wp;
6564
Bram Moolenaar29323592016-07-24 22:04:11 +02006565 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006567 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006568 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006569 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
Bram Moolenaar4033c552017-09-16 20:54:51 +02006572#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573/*
6574 * Redraw all status lines at the bottom of frame "frp".
6575 */
6576 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006577win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578{
6579 if (frp->fr_layout == FR_LEAF)
6580 frp->fr_win->w_redr_status = TRUE;
6581 else if (frp->fr_layout == FR_ROW)
6582 {
6583 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6584 win_redraw_last_status(frp);
6585 }
6586 else /* frp->fr_layout == FR_COL */
6587 {
6588 frp = frp->fr_child;
6589 while (frp->fr_next != NULL)
6590 frp = frp->fr_next;
6591 win_redraw_last_status(frp);
6592 }
6593}
6594#endif
6595
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596/*
6597 * Draw the verticap separator right of window "wp" starting with line "row".
6598 */
6599 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006600draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601{
6602 int hl;
6603 int c;
6604
6605 if (wp->w_vsep_width)
6606 {
6607 /* draw the vertical separator right of this window */
6608 c = fillchar_vsep(&hl);
6609 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6610 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6611 c, ' ', hl);
6612 }
6613}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614
6615#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006616static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617
6618/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006619 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 */
6621 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006622status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623{
6624 int len = 0;
6625
6626#ifdef FEAT_MENU
6627 int emenu = (xp->xp_context == EXPAND_MENUS
6628 || xp->xp_context == EXPAND_MENUNAMES);
6629
6630 /* Check for menu separators - replace with '|'. */
6631 if (emenu && menu_is_separator(s))
6632 return 1;
6633#endif
6634
6635 while (*s != NUL)
6636 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006637 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006638 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006639 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 }
6641
6642 return len;
6643}
6644
6645/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006646 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006647 * These are backslashes used for escaping. Do show backslashes in help tags.
6648 */
6649 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006650skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006651{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006652 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006653#ifdef FEAT_MENU
6654 || ((xp->xp_context == EXPAND_MENUS
6655 || xp->xp_context == EXPAND_MENUNAMES)
6656 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6657#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006658 )
6659 {
6660#ifndef BACKSLASH_IN_FILENAME
6661 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6662 return 2;
6663#endif
6664 return 1;
6665 }
6666 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006667}
6668
6669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 * Show wildchar matches in the status line.
6671 * Show at least the "match" item.
6672 * We start at item 'first_match' in the list and show all matches that fit.
6673 *
6674 * If inversion is possible we use it. Else '=' characters are used.
6675 */
6676 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006677win_redr_status_matches(
6678 expand_T *xp,
6679 int num_matches,
6680 char_u **matches, /* list of matches */
6681 int match,
6682 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683{
6684#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6685 int row;
6686 char_u *buf;
6687 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006688 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 int fillchar;
6690 int attr;
6691 int i;
6692 int highlight = TRUE;
6693 char_u *selstart = NULL;
6694 int selstart_col = 0;
6695 char_u *selend = NULL;
6696 static int first_match = 0;
6697 int add_left = FALSE;
6698 char_u *s;
6699#ifdef FEAT_MENU
6700 int emenu;
6701#endif
6702#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6703 int l;
6704#endif
6705
6706 if (matches == NULL) /* interrupted completion? */
6707 return;
6708
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006709#ifdef FEAT_MBYTE
6710 if (has_mbyte)
6711 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6712 else
6713#endif
6714 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 if (buf == NULL)
6716 return;
6717
6718 if (match == -1) /* don't show match but original text */
6719 {
6720 match = 0;
6721 highlight = FALSE;
6722 }
6723 /* count 1 for the ending ">" */
6724 clen = status_match_len(xp, L_MATCH(match)) + 3;
6725 if (match == 0)
6726 first_match = 0;
6727 else if (match < first_match)
6728 {
6729 /* jumping left, as far as we can go */
6730 first_match = match;
6731 add_left = TRUE;
6732 }
6733 else
6734 {
6735 /* check if match fits on the screen */
6736 for (i = first_match; i < match; ++i)
6737 clen += status_match_len(xp, L_MATCH(i)) + 2;
6738 if (first_match > 0)
6739 clen += 2;
6740 /* jumping right, put match at the left */
6741 if ((long)clen > Columns)
6742 {
6743 first_match = match;
6744 /* if showing the last match, we can add some on the left */
6745 clen = 2;
6746 for (i = match; i < num_matches; ++i)
6747 {
6748 clen += status_match_len(xp, L_MATCH(i)) + 2;
6749 if ((long)clen >= Columns)
6750 break;
6751 }
6752 if (i == num_matches)
6753 add_left = TRUE;
6754 }
6755 }
6756 if (add_left)
6757 while (first_match > 0)
6758 {
6759 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6760 if ((long)clen >= Columns)
6761 break;
6762 --first_match;
6763 }
6764
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006765 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766
6767 if (first_match == 0)
6768 {
6769 *buf = NUL;
6770 len = 0;
6771 }
6772 else
6773 {
6774 STRCPY(buf, "< ");
6775 len = 2;
6776 }
6777 clen = len;
6778
6779 i = first_match;
6780 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6781 {
6782 if (i == match)
6783 {
6784 selstart = buf + len;
6785 selstart_col = clen;
6786 }
6787
6788 s = L_MATCH(i);
6789 /* Check for menu separators - replace with '|' */
6790#ifdef FEAT_MENU
6791 emenu = (xp->xp_context == EXPAND_MENUS
6792 || xp->xp_context == EXPAND_MENUNAMES);
6793 if (emenu && menu_is_separator(s))
6794 {
6795 STRCPY(buf + len, transchar('|'));
6796 l = (int)STRLEN(buf + len);
6797 len += l;
6798 clen += l;
6799 }
6800 else
6801#endif
6802 for ( ; *s != NUL; ++s)
6803 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006804 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 clen += ptr2cells(s);
6806#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006807 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 {
6809 STRNCPY(buf + len, s, l);
6810 s += l - 1;
6811 len += l;
6812 }
6813 else
6814#endif
6815 {
6816 STRCPY(buf + len, transchar_byte(*s));
6817 len += (int)STRLEN(buf + len);
6818 }
6819 }
6820 if (i == match)
6821 selend = buf + len;
6822
6823 *(buf + len++) = ' ';
6824 *(buf + len++) = ' ';
6825 clen += 2;
6826 if (++i == num_matches)
6827 break;
6828 }
6829
6830 if (i != num_matches)
6831 {
6832 *(buf + len++) = '>';
6833 ++clen;
6834 }
6835
6836 buf[len] = NUL;
6837
6838 row = cmdline_row - 1;
6839 if (row >= 0)
6840 {
6841 if (wild_menu_showing == 0)
6842 {
6843 if (msg_scrolled > 0)
6844 {
6845 /* Put the wildmenu just above the command line. If there is
6846 * no room, scroll the screen one line up. */
6847 if (cmdline_row == Rows - 1)
6848 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006849 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 ++msg_scrolled;
6851 }
6852 else
6853 {
6854 ++cmdline_row;
6855 ++row;
6856 }
6857 wild_menu_showing = WM_SCROLLED;
6858 }
6859 else
6860 {
6861 /* Create status line if needed by setting 'laststatus' to 2.
6862 * Set 'winminheight' to zero to avoid that the window is
6863 * resized. */
6864 if (lastwin->w_status_height == 0)
6865 {
6866 save_p_ls = p_ls;
6867 save_p_wmh = p_wmh;
6868 p_ls = 2;
6869 p_wmh = 0;
6870 last_status(FALSE);
6871 }
6872 wild_menu_showing = WM_SHOWN;
6873 }
6874 }
6875
6876 screen_puts(buf, row, 0, attr);
6877 if (selstart != NULL && highlight)
6878 {
6879 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006880 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 }
6882
6883 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6884 }
6885
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 vim_free(buf);
6888}
6889#endif
6890
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891/*
6892 * Redraw the status line of window wp.
6893 *
6894 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006895 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6896 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006898 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006899win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900{
6901 int row;
6902 char_u *p;
6903 int len;
6904 int fillchar;
6905 int attr;
6906 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006907 static int busy = FALSE;
6908
6909 /* It's possible to get here recursively when 'statusline' (indirectly)
6910 * invokes ":redrawstatus". Simply ignore the call then. */
6911 if (busy)
6912 return;
6913 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914
6915 wp->w_redr_status = FALSE;
6916 if (wp->w_status_height == 0)
6917 {
6918 /* no status line, can only be last window */
6919 redraw_cmdline = TRUE;
6920 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006921 else if (!redrawing()
6922#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006923 // don't update status line when popup menu is visible and may be
6924 // drawn over it, unless it will be redrawn later
6925 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006926#endif
6927 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {
6929 /* Don't redraw right now, do it later. */
6930 wp->w_redr_status = TRUE;
6931 }
6932#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006933 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 {
6935 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006936 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 }
6938#endif
6939 else
6940 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006941 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006943 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 p = NameBuff;
6945 len = (int)STRLEN(p);
6946
Bram Moolenaard85f2712017-07-28 21:51:57 +02006947 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948#ifdef FEAT_QUICKFIX
6949 || wp->w_p_pvw
6950#endif
6951 || bufIsChanged(wp->w_buffer)
6952 || wp->w_buffer->b_p_ro)
6953 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006954 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006956 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 len += (int)STRLEN(p + len);
6958 }
6959#ifdef FEAT_QUICKFIX
6960 if (wp->w_p_pvw)
6961 {
6962 STRCPY(p + len, _("[Preview]"));
6963 len += (int)STRLEN(p + len);
6964 }
6965#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006966 if (bufIsChanged(wp->w_buffer)
6967#ifdef FEAT_TERMINAL
6968 && !bt_terminal(wp->w_buffer)
6969#endif
6970 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 {
6972 STRCPY(p + len, "[+]");
6973 len += 3;
6974 }
6975 if (wp->w_buffer->b_p_ro)
6976 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006977 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006978 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 }
6980
Bram Moolenaar02631462017-09-22 15:20:32 +02006981 this_ru_col = ru_col - (Columns - wp->w_width);
6982 if (this_ru_col < (wp->w_width + 1) / 2)
6983 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 if (this_ru_col <= 1)
6985 {
6986 p = (char_u *)"<"; /* No room for file name! */
6987 len = 1;
6988 }
6989 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990#ifdef FEAT_MBYTE
6991 if (has_mbyte)
6992 {
6993 int clen = 0, i;
6994
6995 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006996 clen = mb_string2cells(p, -1);
6997
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 /* Find first character that will fit.
6999 * Going from start to end is much faster for DBCS. */
7000 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007001 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 clen -= (*mb_ptr2cells)(p + i);
7003 len = clen;
7004 if (i > 0)
7005 {
7006 p = p + i - 1;
7007 *p = '<';
7008 ++len;
7009 }
7010
7011 }
7012 else
7013#endif
7014 if (len > this_ru_col - 1)
7015 {
7016 p += len - (this_ru_col - 1);
7017 *p = '<';
7018 len = this_ru_col - 1;
7019 }
7020
7021 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007022 screen_puts(p, row, wp->w_wincol, attr);
7023 screen_fill(row, row + 1, len + wp->w_wincol,
7024 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007026 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7028 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007029 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030
7031#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007032 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033#endif
7034 }
7035
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 /*
7037 * May need to draw the character below the vertical separator.
7038 */
7039 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7040 {
7041 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007042 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 else
7044 fillchar = fillchar_vsep(&attr);
7045 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7046 attr);
7047 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007048 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049}
7050
Bram Moolenaar238a5642006-02-21 22:12:05 +00007051#ifdef FEAT_STL_OPT
7052/*
7053 * Redraw the status line according to 'statusline' and take care of any
7054 * errors encountered.
7055 */
7056 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007057redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007058{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007059 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007060 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007061
7062 /* When called recursively return. This can happen when the statusline
7063 * contains an expression that triggers a redraw. */
7064 if (entered)
7065 return;
7066 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007067
Bram Moolenaara742e082016-04-05 21:10:38 +02007068 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007069 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007070 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007071 {
7072 /* When there is an error disable the statusline, otherwise the
7073 * display is messed up with errors and a redraw triggers the problem
7074 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007075 set_string_option_direct((char_u *)"statusline", -1,
7076 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007077 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007078 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007079 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007080 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007081}
7082#endif
7083
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084/*
7085 * Return TRUE if the status line of window "wp" is connected to the status
7086 * line of the window right of it. If not, then it's a vertical separator.
7087 * Only call if (wp->w_vsep_width != 0).
7088 */
7089 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007090stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091{
7092 frame_T *fr;
7093
7094 fr = wp->w_frame;
7095 while (fr->fr_parent != NULL)
7096 {
7097 if (fr->fr_parent->fr_layout == FR_COL)
7098 {
7099 if (fr->fr_next != NULL)
7100 break;
7101 }
7102 else
7103 {
7104 if (fr->fr_next != NULL)
7105 return TRUE;
7106 }
7107 fr = fr->fr_parent;
7108 }
7109 return FALSE;
7110}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113/*
7114 * Get the value to show for the language mappings, active 'keymap'.
7115 */
7116 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007117get_keymap_str(
7118 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007119 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007120 char_u *buf, /* buffer for the result */
7121 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122{
7123 char_u *p;
7124
7125 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7126 return FALSE;
7127
7128 {
7129#ifdef FEAT_EVAL
7130 buf_T *old_curbuf = curbuf;
7131 win_T *old_curwin = curwin;
7132 char_u *s;
7133
7134 curbuf = wp->w_buffer;
7135 curwin = wp;
7136 STRCPY(buf, "b:keymap_name"); /* must be writable */
7137 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007138 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 --emsg_skip;
7140 curbuf = old_curbuf;
7141 curwin = old_curwin;
7142 if (p == NULL || *p == NUL)
7143#endif
7144 {
7145#ifdef FEAT_KEYMAP
7146 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7147 p = wp->w_buffer->b_p_keymap;
7148 else
7149#endif
7150 p = (char_u *)"lang";
7151 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007152 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 buf[0] = NUL;
7154#ifdef FEAT_EVAL
7155 vim_free(s);
7156#endif
7157 }
7158 return buf[0] != NUL;
7159}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160
7161#if defined(FEAT_STL_OPT) || defined(PROTO)
7162/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007163 * Redraw the status line or ruler of window "wp".
7164 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 */
7166 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007167win_redr_custom(
7168 win_T *wp,
7169 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007171 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 int attr;
7173 int curattr;
7174 int row;
7175 int col = 0;
7176 int maxwidth;
7177 int width;
7178 int n;
7179 int len;
7180 int fillchar;
7181 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007182 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007184 struct stl_hlrec hltab[STL_MAX_ITEM];
7185 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007186 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007187 win_T *ewp;
7188 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189
Bram Moolenaar1d633412013-12-11 15:52:01 +01007190 /* There is a tiny chance that this gets called recursively: When
7191 * redrawing a status line triggers redrawing the ruler or tabline.
7192 * Avoid trouble by not allowing recursion. */
7193 if (entered)
7194 return;
7195 entered = TRUE;
7196
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007198 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007200 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007201 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007202 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007203 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007204 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007205 maxwidth = Columns;
7206# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007207 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007208# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007210 else
7211 {
7212 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007213 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007214 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007215
7216 if (draw_ruler)
7217 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007218 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007219 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007220 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007221 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007222 if (*++stl == '-')
7223 stl++;
7224 if (atoi((char *)stl))
7225 while (VIM_ISDIGIT(*stl))
7226 stl++;
7227 if (*stl++ != '(')
7228 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007229 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007230 col = ru_col - (Columns - wp->w_width);
7231 if (col < (wp->w_width + 1) / 2)
7232 col = (wp->w_width + 1) / 2;
7233 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007234 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007235 {
7236 row = Rows - 1;
7237 --maxwidth; /* writing in last column may cause scrolling */
7238 fillchar = ' ';
7239 attr = 0;
7240 }
7241
7242# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007243 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007244# endif
7245 }
7246 else
7247 {
7248 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007249 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007250 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007251 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007252# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007253 use_sandbox = was_set_insecurely((char_u *)"statusline",
7254 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007255# endif
7256 }
7257
Bram Moolenaar53f81742017-09-22 14:35:51 +02007258 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007259 }
7260
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007262 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263
Bram Moolenaar61452852011-02-01 18:01:11 +01007264 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7265 * the cursor away and back. */
7266 ewp = wp == NULL ? curwin : wp;
7267 p_crb_save = ewp->w_p_crb;
7268 ewp->w_p_crb = FALSE;
7269
Bram Moolenaar362f3562009-11-03 16:20:34 +00007270 /* Make a copy, because the statusline may include a function call that
7271 * might change the option value and free the memory. */
7272 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007273 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007274 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007275 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007276 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007277 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007279 /* Make all characters printable. */
7280 p = transstr(buf);
7281 if (p != NULL)
7282 {
7283 vim_strncpy(buf, p, sizeof(buf) - 1);
7284 vim_free(p);
7285 }
7286
7287 /* fill up with "fillchar" */
7288 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007289 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 {
7291#ifdef FEAT_MBYTE
7292 len += (*mb_char2bytes)(fillchar, buf + len);
7293#else
7294 buf[len++] = fillchar;
7295#endif
7296 ++width;
7297 }
7298 buf[len] = NUL;
7299
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007300 /*
7301 * Draw each snippet with the specified highlighting.
7302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 curattr = attr;
7304 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007305 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007307 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 screen_puts_len(p, len, row, col, curattr);
7309 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007310 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007312 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007314 else if (hltab[n].userhl < 0)
7315 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007316#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007317 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7318 && wp->w_status_height != 0)
7319 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007320 else if (wp != NULL && bt_terminal(wp->w_buffer)
7321 && wp->w_status_height != 0)
7322 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007323#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007324 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007325 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007327 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 }
7329 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007330
7331 if (wp == NULL)
7332 {
7333 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7334 col = 0;
7335 len = 0;
7336 p = buf;
7337 fillchar = 0;
7338 for (n = 0; tabtab[n].start != NULL; n++)
7339 {
7340 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7341 while (col < len)
7342 TabPageIdxs[col++] = fillchar;
7343 p = tabtab[n].start;
7344 fillchar = tabtab[n].userhl;
7345 }
7346 while (col < Columns)
7347 TabPageIdxs[col++] = fillchar;
7348 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007349
7350theend:
7351 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352}
7353
7354#endif /* FEAT_STL_OPT */
7355
7356/*
7357 * Output a single character directly to the screen and update ScreenLines.
7358 */
7359 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007360screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 char_u buf[MB_MAXBYTES + 1];
7363
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007364#ifdef FEAT_MBYTE
7365 if (has_mbyte)
7366 buf[(*mb_char2bytes)(c, buf)] = NUL;
7367 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007369 {
7370 buf[0] = c;
7371 buf[1] = NUL;
7372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 screen_puts(buf, row, col, attr);
7374}
7375
7376/*
7377 * Get a single character directly from ScreenLines into "bytes[]".
7378 * Also return its attribute in *attrp;
7379 */
7380 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007381screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382{
7383 unsigned off;
7384
7385 /* safety check */
7386 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7387 {
7388 off = LineOffset[row] + col;
7389 *attrp = ScreenAttrs[off];
7390 bytes[0] = ScreenLines[off];
7391 bytes[1] = NUL;
7392
7393#ifdef FEAT_MBYTE
7394 if (enc_utf8 && ScreenLinesUC[off] != 0)
7395 bytes[utfc_char2bytes(off, bytes)] = NUL;
7396 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7397 {
7398 bytes[0] = ScreenLines[off];
7399 bytes[1] = ScreenLines2[off];
7400 bytes[2] = NUL;
7401 }
7402 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7403 {
7404 bytes[1] = ScreenLines[off + 1];
7405 bytes[2] = NUL;
7406 }
7407#endif
7408 }
7409}
7410
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007411#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007412/*
7413 * Return TRUE if composing characters for screen posn "off" differs from
7414 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007415 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007416 */
7417 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007418screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007419{
7420 int i;
7421
7422 for (i = 0; i < Screen_mco; ++i)
7423 {
7424 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7425 return TRUE;
7426 if (u8cc[i] == 0)
7427 break;
7428 }
7429 return FALSE;
7430}
7431#endif
7432
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433/*
7434 * Put string '*text' on the screen at position 'row' and 'col', with
7435 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7436 * Note: only outputs within one row, message is truncated at screen boundary!
7437 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7438 */
7439 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007440screen_puts(
7441 char_u *text,
7442 int row,
7443 int col,
7444 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445{
7446 screen_puts_len(text, -1, row, col, attr);
7447}
7448
7449/*
7450 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7451 * a NUL.
7452 */
7453 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007454screen_puts_len(
7455 char_u *text,
7456 int textlen,
7457 int row,
7458 int col,
7459 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460{
7461 unsigned off;
7462 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007463 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 int c;
7465#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007466 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 int mbyte_blen = 1;
7468 int mbyte_cells = 1;
7469 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007470 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 int clear_next_cell = FALSE;
7472# ifdef FEAT_ARABIC
7473 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007474 int pc, nc, nc1;
7475 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476# endif
7477#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007478#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7479 int force_redraw_this;
7480 int force_redraw_next = FALSE;
7481#endif
7482 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483
7484 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7485 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007486 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487
Bram Moolenaarc236c162008-07-13 17:41:49 +00007488#ifdef FEAT_MBYTE
7489 /* When drawing over the right halve of a double-wide char clear out the
7490 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007491 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007492# ifdef FEAT_GUI
7493 && !gui.in_use
7494# endif
7495 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007496 {
7497 ScreenLines[off - 1] = ' ';
7498 ScreenAttrs[off - 1] = 0;
7499 if (enc_utf8)
7500 {
7501 ScreenLinesUC[off - 1] = 0;
7502 ScreenLinesC[0][off - 1] = 0;
7503 }
7504 /* redraw the previous cell, make it empty */
7505 screen_char(off - 1, row, col - 1);
7506 /* force the cell at "col" to be redrawn */
7507 force_redraw_next = TRUE;
7508 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007509#endif
7510
Bram Moolenaar367329b2007-08-30 11:53:22 +00007511#ifdef FEAT_MBYTE
7512 max_off = LineOffset[row] + screen_Columns;
7513#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007514 while (col < screen_Columns
7515 && (len < 0 || (int)(ptr - text) < len)
7516 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 {
7518 c = *ptr;
7519#ifdef FEAT_MBYTE
7520 /* check if this is the first byte of a multibyte */
7521 if (has_mbyte)
7522 {
7523 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007524 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007526 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7528 mbyte_cells = 1;
7529 else if (enc_dbcs != 0)
7530 mbyte_cells = mbyte_blen;
7531 else /* enc_utf8 */
7532 {
7533 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007534 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 (int)((text + len) - ptr));
7536 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007537 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007539# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 /* Non-BMP character: display as ? or fullwidth ?. */
7541 if (u8c >= 0x10000)
7542 {
7543 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7544 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007545 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007547# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548# ifdef FEAT_ARABIC
7549 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7550 {
7551 /* Do Arabic shaping. */
7552 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7553 {
7554 /* Past end of string to be displayed. */
7555 nc = NUL;
7556 nc1 = NUL;
7557 }
7558 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007559 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007560 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7561 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007562 nc1 = pcc[0];
7563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 pc = prev_c;
7565 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007566 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 }
7568 else
7569 prev_c = u8c;
7570# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007571 if (col + mbyte_cells > screen_Columns)
7572 {
7573 /* Only 1 cell left, but character requires 2 cells:
7574 * display a '>' in the last column to avoid wrapping. */
7575 c = '>';
7576 mbyte_cells = 1;
7577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 }
7579 }
7580#endif
7581
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007582#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7583 force_redraw_this = force_redraw_next;
7584 force_redraw_next = FALSE;
7585#endif
7586
7587 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588#ifdef FEAT_MBYTE
7589 || (mbyte_cells == 2
7590 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7591 || (enc_dbcs == DBCS_JPNU
7592 && c == 0x8e
7593 && ScreenLines2[off] != ptr[1])
7594 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007595 && (ScreenLinesUC[off] !=
7596 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7597 || (ScreenLinesUC[off] != 0
7598 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599#endif
7600 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007601 || exmode_active;
7602
7603 if (need_redraw
7604#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7605 || force_redraw_this
7606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 )
7608 {
7609#if defined(FEAT_GUI) || defined(UNIX)
7610 /* The bold trick makes a single row of pixels appear in the next
7611 * character. When a bold character is removed, the next
7612 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007613 * and for some xterms. */
7614 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615# ifdef FEAT_GUI
7616 gui.in_use
7617# endif
7618# if defined(FEAT_GUI) && defined(UNIX)
7619 ||
7620# endif
7621# ifdef UNIX
7622 term_is_xterm
7623# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007624 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007626 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007628 if (n > HL_ALL)
7629 n = syn_attr2attr(n);
7630 if (n & HL_BOLD)
7631 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 }
7633#endif
7634#ifdef FEAT_MBYTE
7635 /* When at the end of the text and overwriting a two-cell
7636 * character with a one-cell character, need to clear the next
7637 * cell. Also when overwriting the left halve of a two-cell char
7638 * with the right halve of a two-cell char. Do this only once
7639 * (mb_off2cells() may return 2 on the right halve). */
7640 if (clear_next_cell)
7641 clear_next_cell = FALSE;
7642 else if (has_mbyte
7643 && (len < 0 ? ptr[mbyte_blen] == NUL
7644 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007645 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007647 && (*mb_off2cells)(off, max_off) == 1
7648 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 clear_next_cell = TRUE;
7650
7651 /* Make sure we never leave a second byte of a double-byte behind,
7652 * it confuses mb_off2cells(). */
7653 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007654 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007656 && (*mb_off2cells)(off, max_off) == 1
7657 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 ScreenLines[off + mbyte_blen] = 0;
7659#endif
7660 ScreenLines[off] = c;
7661 ScreenAttrs[off] = attr;
7662#ifdef FEAT_MBYTE
7663 if (enc_utf8)
7664 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007665 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 ScreenLinesUC[off] = 0;
7667 else
7668 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007669 int i;
7670
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007672 for (i = 0; i < Screen_mco; ++i)
7673 {
7674 ScreenLinesC[i][off] = u8cc[i];
7675 if (u8cc[i] == 0)
7676 break;
7677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 }
7679 if (mbyte_cells == 2)
7680 {
7681 ScreenLines[off + 1] = 0;
7682 ScreenAttrs[off + 1] = attr;
7683 }
7684 screen_char(off, row, col);
7685 }
7686 else if (mbyte_cells == 2)
7687 {
7688 ScreenLines[off + 1] = ptr[1];
7689 ScreenAttrs[off + 1] = attr;
7690 screen_char_2(off, row, col);
7691 }
7692 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7693 {
7694 ScreenLines2[off] = ptr[1];
7695 screen_char(off, row, col);
7696 }
7697 else
7698#endif
7699 screen_char(off, row, col);
7700 }
7701#ifdef FEAT_MBYTE
7702 if (has_mbyte)
7703 {
7704 off += mbyte_cells;
7705 col += mbyte_cells;
7706 ptr += mbyte_blen;
7707 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007708 {
7709 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007711 len = -1;
7712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 }
7714 else
7715#endif
7716 {
7717 ++off;
7718 ++col;
7719 ++ptr;
7720 }
7721 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007722
7723#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7724 /* If we detected the next character needs to be redrawn, but the text
7725 * doesn't extend up to there, update the character here. */
7726 if (force_redraw_next && col < screen_Columns)
7727 {
7728# ifdef FEAT_MBYTE
7729 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7730 screen_char_2(off, row, col);
7731 else
7732# endif
7733 screen_char(off, row, col);
7734 }
7735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736}
7737
7738#ifdef FEAT_SEARCH_EXTRA
7739/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007740 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 */
7742 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007743start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744{
7745 if (p_hls && !no_hlsearch)
7746 {
7747 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007748 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007749# ifdef FEAT_RELTIME
7750 /* Set the time limit to 'redrawtime'. */
7751 profile_setlimit(p_rdt, &search_hl.tm);
7752# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 }
7754}
7755
7756/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007757 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 */
7759 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007760end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761{
7762 if (search_hl.rm.regprog != NULL)
7763 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007764 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 search_hl.rm.regprog = NULL;
7766 }
7767}
7768
7769/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007770 * Init for calling prepare_search_hl().
7771 */
7772 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007773init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007774{
7775 matchitem_T *cur;
7776
7777 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7778 * match */
7779 cur = wp->w_match_head;
7780 while (cur != NULL)
7781 {
7782 cur->hl.rm = cur->match;
7783 if (cur->hlg_id == 0)
7784 cur->hl.attr = 0;
7785 else
7786 cur->hl.attr = syn_id2attr(cur->hlg_id);
7787 cur->hl.buf = wp->w_buffer;
7788 cur->hl.lnum = 0;
7789 cur->hl.first_lnum = 0;
7790# ifdef FEAT_RELTIME
7791 /* Set the time limit to 'redrawtime'. */
7792 profile_setlimit(p_rdt, &(cur->hl.tm));
7793# endif
7794 cur = cur->next;
7795 }
7796 search_hl.buf = wp->w_buffer;
7797 search_hl.lnum = 0;
7798 search_hl.first_lnum = 0;
7799 /* time limit is set at the toplevel, for all windows */
7800}
7801
7802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 * Advance to the match in window "wp" line "lnum" or past it.
7804 */
7805 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007806prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007808 matchitem_T *cur; /* points to the match list */
7809 match_T *shl; /* points to search_hl or a match */
7810 int shl_flag; /* flag to indicate whether search_hl
7811 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007812 int pos_inprogress; /* marks that position match search is
7813 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 int n;
7815
7816 /*
7817 * When using a multi-line pattern, start searching at the top
7818 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007819 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007821 cur = wp->w_match_head;
7822 shl_flag = FALSE;
7823 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007825 if (shl_flag == FALSE)
7826 {
7827 shl = &search_hl;
7828 shl_flag = TRUE;
7829 }
7830 else
7831 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 if (shl->rm.regprog != NULL
7833 && shl->lnum == 0
7834 && re_multiline(shl->rm.regprog))
7835 {
7836 if (shl->first_lnum == 0)
7837 {
7838# ifdef FEAT_FOLDING
7839 for (shl->first_lnum = lnum;
7840 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7841 if (hasFoldingWin(wp, shl->first_lnum - 1,
7842 NULL, NULL, TRUE, NULL))
7843 break;
7844# else
7845 shl->first_lnum = wp->w_topline;
7846# endif
7847 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007848 if (cur != NULL)
7849 cur->pos.cur = 0;
7850 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007852 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7853 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007855 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7856 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007857 pos_inprogress = cur == NULL || cur->pos.cur == 0
7858 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 if (shl->lnum != 0)
7860 {
7861 shl->first_lnum = shl->lnum
7862 + shl->rm.endpos[0].lnum
7863 - shl->rm.startpos[0].lnum;
7864 n = shl->rm.endpos[0].col;
7865 }
7866 else
7867 {
7868 ++shl->first_lnum;
7869 n = 0;
7870 }
7871 }
7872 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007873 if (shl != &search_hl && cur != NULL)
7874 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 }
7876}
7877
7878/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007879 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 * Uses shl->buf.
7881 * Sets shl->lnum and shl->rm contents.
7882 * Note: Assumes a previous match is always before "lnum", unless
7883 * shl->lnum is zero.
7884 * Careful: Any pointers for buffer lines will become invalid.
7885 */
7886 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007887next_search_hl(
7888 win_T *win,
7889 match_T *shl, /* points to search_hl or a match */
7890 linenr_T lnum,
7891 colnr_T mincol, /* minimal column for a match */
7892 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893{
7894 linenr_T l;
7895 colnr_T matchcol;
7896 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007897 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007899 // for :{range}s/pat only highlight inside the range
7900 if (lnum < search_first_line || lnum > search_last_line)
7901 {
7902 shl->lnum = 0;
7903 return;
7904 }
7905
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 if (shl->lnum != 0)
7907 {
7908 /* Check for three situations:
7909 * 1. If the "lnum" is below a previous match, start a new search.
7910 * 2. If the previous match includes "mincol", use it.
7911 * 3. Continue after the previous match.
7912 */
7913 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7914 if (lnum > l)
7915 shl->lnum = 0;
7916 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7917 return;
7918 }
7919
7920 /*
7921 * Repeat searching for a match until one is found that includes "mincol"
7922 * or none is found in this line.
7923 */
7924 called_emsg = FALSE;
7925 for (;;)
7926 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007927#ifdef FEAT_RELTIME
7928 /* Stop searching after passing the time limit. */
7929 if (profile_passed_limit(&(shl->tm)))
7930 {
7931 shl->lnum = 0; /* no match found in time */
7932 break;
7933 }
7934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 /* Three situations:
7936 * 1. No useful previous match: search from start of line.
7937 * 2. Not Vi compatible or empty match: continue at next character.
7938 * Break the loop if this is beyond the end of the line.
7939 * 3. Vi compatible searching: continue at end of previous match.
7940 */
7941 if (shl->lnum == 0)
7942 matchcol = 0;
7943 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7944 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007945 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007947 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007948
7949 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007950 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007951 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007953 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 shl->lnum = 0;
7955 break;
7956 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007957#ifdef FEAT_MBYTE
7958 if (has_mbyte)
7959 matchcol += mb_ptr2len(ml);
7960 else
7961#endif
7962 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 }
7964 else
7965 matchcol = shl->rm.endpos[0].col;
7966
7967 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007968 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007970 /* Remember whether shl->rm is using a copy of the regprog in
7971 * cur->match. */
7972 int regprog_is_copy = (shl != &search_hl && cur != NULL
7973 && shl == &cur->hl
7974 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007975 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007976
Bram Moolenaarb3414592014-06-17 17:48:32 +02007977 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7978 matchcol,
7979#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007980 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007981#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007982 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007983#endif
7984 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007985 /* Copy the regprog, in case it got freed and recompiled. */
7986 if (regprog_is_copy)
7987 cur->match.regprog = cur->hl.rm.regprog;
7988
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007989 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007990 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007991 /* Error while handling regexp: stop using this regexp. */
7992 if (shl == &search_hl)
7993 {
7994 /* don't free regprog in the match list, it's a copy */
7995 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02007996 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007997 }
7998 shl->rm.regprog = NULL;
7999 shl->lnum = 0;
8000 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8001 message */
8002 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008003 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008004 }
8005 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008006 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008007 else
8008 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 if (nmatched == 0)
8010 {
8011 shl->lnum = 0; /* no match found */
8012 break;
8013 }
8014 if (shl->rm.startpos[0].lnum > 0
8015 || shl->rm.startpos[0].col >= mincol
8016 || nmatched > 1
8017 || shl->rm.endpos[0].col > mincol)
8018 {
8019 shl->lnum += shl->rm.startpos[0].lnum;
8020 break; /* useful match found */
8021 }
8022 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008023
8024 // Restore called_emsg for assert_fails().
8025 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027
Bram Moolenaar85077472016-10-16 14:35:48 +02008028/*
8029 * If there is a match fill "shl" and return one.
8030 * Return zero otherwise.
8031 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008032 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008033next_search_hl_pos(
8034 match_T *shl, /* points to a match */
8035 linenr_T lnum,
8036 posmatch_T *posmatch, /* match positions */
8037 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008038{
8039 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008040 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008041
Bram Moolenaarb3414592014-06-17 17:48:32 +02008042 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8043 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008044 llpos_T *pos = &posmatch->pos[i];
8045
8046 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008047 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008048 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008049 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008050 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008051 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008052 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008053 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008054 /* if this match comes before the one at "found" then swap
8055 * them */
8056 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008057 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008058 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008059
Bram Moolenaar85077472016-10-16 14:35:48 +02008060 *pos = posmatch->pos[found];
8061 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008062 }
8063 }
8064 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008065 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008066 }
8067 }
8068 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008069 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008070 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008071 colnr_T start = posmatch->pos[found].col == 0
8072 ? 0 : posmatch->pos[found].col - 1;
8073 colnr_T end = posmatch->pos[found].col == 0
8074 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008075
Bram Moolenaar85077472016-10-16 14:35:48 +02008076 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008077 shl->rm.startpos[0].lnum = 0;
8078 shl->rm.startpos[0].col = start;
8079 shl->rm.endpos[0].lnum = 0;
8080 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008081 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008082 posmatch->cur = found + 1;
8083 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008084 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008085 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008086}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008087#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008088
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008090screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091{
8092 attrentry_T *aep = NULL;
8093
8094 screen_attr = attr;
8095 if (full_screen
8096#ifdef WIN3264
8097 && termcap_active
8098#endif
8099 )
8100 {
8101#ifdef FEAT_GUI
8102 if (gui.in_use)
8103 {
8104 char buf[20];
8105
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008106 /* The GUI handles this internally. */
8107 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 OUT_STR(buf);
8109 }
8110 else
8111#endif
8112 {
8113 if (attr > HL_ALL) /* special HL attr. */
8114 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008115 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 aep = syn_cterm_attr2entry(attr);
8117 else
8118 aep = syn_term_attr2entry(attr);
8119 if (aep == NULL) /* did ":syntax clear" */
8120 attr = 0;
8121 else
8122 attr = aep->ae_attr;
8123 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008124 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008126 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008127#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008128 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8129 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8130 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008131#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008132 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008133 /* If the Normal FG color has BOLD attribute and the new HL
8134 * has a FG color defined, clear BOLD. */
8135 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008136 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008138 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008139 out_str(T_UCS);
8140 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008141 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8142 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008144 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008146 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008148 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008149 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150
8151 /*
8152 * Output the color or start string after bold etc., in case the
8153 * bold etc. override the color setting.
8154 */
8155 if (aep != NULL)
8156 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008157#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008158 /* When 'termguicolors' is set but fg or bg is unset,
8159 * fall back to the cterm colors. This helps for SpellBad,
8160 * where the GUI uses a red undercurl. */
8161 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008163 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008164 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008165 }
8166 else
8167#endif
8168 if (t_colors > 1)
8169 {
8170 if (aep->ae_u.cterm.fg_color)
8171 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8172 }
8173#ifdef FEAT_TERMGUICOLORS
8174 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8175 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008176 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008177 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 }
8179 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008180#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008181 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008183 if (aep->ae_u.cterm.bg_color)
8184 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8185 }
8186
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008187 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008188 {
8189 if (aep->ae_u.term.start != NULL)
8190 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 }
8192 }
8193 }
8194 }
8195}
8196
8197 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008198screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199{
8200 int do_ME = FALSE; /* output T_ME code */
8201
8202 if (screen_attr != 0
8203#ifdef WIN3264
8204 && termcap_active
8205#endif
8206 )
8207 {
8208#ifdef FEAT_GUI
8209 if (gui.in_use)
8210 {
8211 char buf[20];
8212
8213 /* use internal GUI code */
8214 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8215 OUT_STR(buf);
8216 }
8217 else
8218#endif
8219 {
8220 if (screen_attr > HL_ALL) /* special HL attr. */
8221 {
8222 attrentry_T *aep;
8223
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008224 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {
8226 /*
8227 * Assume that t_me restores the original colors!
8228 */
8229 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008230 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008231#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008232 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8233 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8234 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008235#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008236 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008237#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008238 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8239 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8240 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008241#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008242 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 do_ME = TRUE;
8244 }
8245 else
8246 {
8247 aep = syn_term_attr2entry(screen_attr);
8248 if (aep != NULL && aep->ae_u.term.stop != NULL)
8249 {
8250 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8251 do_ME = TRUE;
8252 else
8253 out_str(aep->ae_u.term.stop);
8254 }
8255 }
8256 if (aep == NULL) /* did ":syntax clear" */
8257 screen_attr = 0;
8258 else
8259 screen_attr = aep->ae_attr;
8260 }
8261
8262 /*
8263 * Often all ending-codes are equal to T_ME. Avoid outputting the
8264 * same sequence several times.
8265 */
8266 if (screen_attr & HL_STANDOUT)
8267 {
8268 if (STRCMP(T_SE, T_ME) == 0)
8269 do_ME = TRUE;
8270 else
8271 out_str(T_SE);
8272 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008273 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008274 {
8275 if (STRCMP(T_UCE, T_ME) == 0)
8276 do_ME = TRUE;
8277 else
8278 out_str(T_UCE);
8279 }
8280 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008281 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {
8283 if (STRCMP(T_UE, T_ME) == 0)
8284 do_ME = TRUE;
8285 else
8286 out_str(T_UE);
8287 }
8288 if (screen_attr & HL_ITALIC)
8289 {
8290 if (STRCMP(T_CZR, T_ME) == 0)
8291 do_ME = TRUE;
8292 else
8293 out_str(T_CZR);
8294 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008295 if (screen_attr & HL_STRIKETHROUGH)
8296 {
8297 if (STRCMP(T_STE, T_ME) == 0)
8298 do_ME = TRUE;
8299 else
8300 out_str(T_STE);
8301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8303 out_str(T_ME);
8304
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008305#ifdef FEAT_TERMGUICOLORS
8306 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008308 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008309 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008310 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008311 term_bg_rgb_color(cterm_normal_bg_gui_color);
8312 }
8313 else
8314#endif
8315 {
8316 if (t_colors > 1)
8317 {
8318 /* set Normal cterm colors */
8319 if (cterm_normal_fg_color != 0)
8320 term_fg_color(cterm_normal_fg_color - 1);
8321 if (cterm_normal_bg_color != 0)
8322 term_bg_color(cterm_normal_bg_color - 1);
8323 if (cterm_normal_fg_bold)
8324 out_str(T_MD);
8325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 }
8327 }
8328 }
8329 screen_attr = 0;
8330}
8331
8332/*
8333 * Reset the colors for a cterm. Used when leaving Vim.
8334 * The machine specific code may override this again.
8335 */
8336 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008337reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008339 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 {
8341 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008342#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008343 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8344 || cterm_normal_bg_gui_color != INVALCOLOR)
8345 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008346#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {
8350 out_str(T_OP);
8351 screen_attr = -1;
8352 }
8353 if (cterm_normal_fg_bold)
8354 {
8355 out_str(T_ME);
8356 screen_attr = -1;
8357 }
8358 }
8359}
8360
8361/*
8362 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8363 * using the attributes from ScreenAttrs["off"].
8364 */
8365 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008366screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367{
8368 int attr;
8369
8370 /* Check for illegal values, just in case (could happen just after
8371 * resizing). */
8372 if (row >= screen_Rows || col >= screen_Columns)
8373 return;
8374
Bram Moolenaar494838a2015-02-10 19:20:37 +01008375 /* Outputting a character in the last cell on the screen may scroll the
8376 * screen up. Only do it when the "xn" termcap property is set, otherwise
8377 * mark the character invalid (update it when scrolled up). */
8378 if (*T_XN == NUL
8379 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380#ifdef FEAT_RIGHTLEFT
8381 /* account for first command-line character in rightleft mode */
8382 && !cmdmsg_rl
8383#endif
8384 )
8385 {
8386 ScreenAttrs[off] = (sattr_T)-1;
8387 return;
8388 }
8389
8390 /*
8391 * Stop highlighting first, so it's easier to move the cursor.
8392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 if (screen_char_attr != 0)
8394 attr = screen_char_attr;
8395 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 attr = ScreenAttrs[off];
8397 if (screen_attr != attr)
8398 screen_stop_highlight();
8399
8400 windgoto(row, col);
8401
8402 if (screen_attr != attr)
8403 screen_start_highlight(attr);
8404
8405#ifdef FEAT_MBYTE
8406 if (enc_utf8 && ScreenLinesUC[off] != 0)
8407 {
8408 char_u buf[MB_MAXBYTES + 1];
8409
Bram Moolenaarcb070082016-04-02 22:14:51 +02008410 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008411 {
8412 if (*p_ambw == 'd'
8413# ifdef FEAT_GUI
8414 && !gui.in_use
8415# endif
8416 )
8417 {
8418 /* Clear the two screen cells. If the character is actually
8419 * single width it won't change the second cell. */
8420 out_str((char_u *)" ");
8421 term_windgoto(row, col);
8422 }
8423 /* not sure where the cursor is after drawing the ambiguous width
8424 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008425 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008426 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008427 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008429
8430 /* Convert the UTF-8 character to bytes and write it. */
8431 buf[utfc_char2bytes(off, buf)] = NUL;
8432 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 }
8434 else
8435#endif
8436 {
8437#ifdef FEAT_MBYTE
8438 out_flush_check();
8439#endif
8440 out_char(ScreenLines[off]);
8441#ifdef FEAT_MBYTE
8442 /* double-byte character in single-width cell */
8443 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8444 out_char(ScreenLines2[off]);
8445#endif
8446 }
8447
8448 screen_cur_col++;
8449}
8450
8451#ifdef FEAT_MBYTE
8452
8453/*
8454 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8455 * on the screen at position 'row' and 'col'.
8456 * The attributes of the first byte is used for all. This is required to
8457 * output the two bytes of a double-byte character with nothing in between.
8458 */
8459 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008460screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461{
8462 /* Check for illegal values (could be wrong when screen was resized). */
8463 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8464 return;
8465
8466 /* Outputting the last character on the screen may scrollup the screen.
8467 * Don't to it! Mark the character invalid (update it when scrolled up) */
8468 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8469 {
8470 ScreenAttrs[off] = (sattr_T)-1;
8471 return;
8472 }
8473
8474 /* Output the first byte normally (positions the cursor), then write the
8475 * second byte directly. */
8476 screen_char(off, row, col);
8477 out_char(ScreenLines[off + 1]);
8478 ++screen_cur_col;
8479}
8480#endif
8481
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482/*
8483 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8484 * This uses the contents of ScreenLines[] and doesn't change it.
8485 */
8486 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008487screen_draw_rectangle(
8488 int row,
8489 int col,
8490 int height,
8491 int width,
8492 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493{
8494 int r, c;
8495 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008496#ifdef FEAT_MBYTE
8497 int max_off;
8498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008500 /* Can't use ScreenLines unless initialized */
8501 if (ScreenLines == NULL)
8502 return;
8503
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 if (invert)
8505 screen_char_attr = HL_INVERSE;
8506 for (r = row; r < row + height; ++r)
8507 {
8508 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008509#ifdef FEAT_MBYTE
8510 max_off = off + screen_Columns;
8511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 for (c = col; c < col + width; ++c)
8513 {
8514#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008515 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 {
8517 screen_char_2(off + c, r, c);
8518 ++c;
8519 }
8520 else
8521#endif
8522 {
8523 screen_char(off + c, r, c);
8524#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008525 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 ++c;
8527#endif
8528 }
8529 }
8530 }
8531 screen_char_attr = 0;
8532}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534/*
8535 * Redraw the characters for a vertically split window.
8536 */
8537 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008538redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539{
8540 int col;
8541 int width;
8542
8543# ifdef FEAT_CLIPBOARD
8544 clip_may_clear_selection(row, end - 1);
8545# endif
8546
8547 if (wp == NULL)
8548 {
8549 col = 0;
8550 width = Columns;
8551 }
8552 else
8553 {
8554 col = wp->w_wincol;
8555 width = wp->w_width;
8556 }
8557 screen_draw_rectangle(row, col, end - row, width, FALSE);
8558}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008560 static void
8561space_to_screenline(int off, int attr)
8562{
8563 ScreenLines[off] = ' ';
8564 ScreenAttrs[off] = attr;
8565# ifdef FEAT_MBYTE
8566 if (enc_utf8)
8567 ScreenLinesUC[off] = 0;
8568# endif
8569}
8570
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571/*
8572 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8573 * with character 'c1' in first column followed by 'c2' in the other columns.
8574 * Use attributes 'attr'.
8575 */
8576 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008577screen_fill(
8578 int start_row,
8579 int end_row,
8580 int start_col,
8581 int end_col,
8582 int c1,
8583 int c2,
8584 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585{
8586 int row;
8587 int col;
8588 int off;
8589 int end_off;
8590 int did_delete;
8591 int c;
8592 int norm_term;
8593#if defined(FEAT_GUI) || defined(UNIX)
8594 int force_next = FALSE;
8595#endif
8596
8597 if (end_row > screen_Rows) /* safety check */
8598 end_row = screen_Rows;
8599 if (end_col > screen_Columns) /* safety check */
8600 end_col = screen_Columns;
8601 if (ScreenLines == NULL
8602 || start_row >= end_row
8603 || start_col >= end_col) /* nothing to do */
8604 return;
8605
8606 /* it's a "normal" terminal when not in a GUI or cterm */
8607 norm_term = (
8608#ifdef FEAT_GUI
8609 !gui.in_use &&
8610#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008611 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 for (row = start_row; row < end_row; ++row)
8613 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008614#ifdef FEAT_MBYTE
8615 if (has_mbyte
8616# ifdef FEAT_GUI
8617 && !gui.in_use
8618# endif
8619 )
8620 {
8621 /* When drawing over the right halve of a double-wide char clear
8622 * out the left halve. When drawing over the left halve of a
8623 * double wide-char clear out the right halve. Only needed in a
8624 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008625 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008626 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008627 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008628 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008629 }
8630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 /*
8632 * Try to use delete-line termcap code, when no attributes or in a
8633 * "normal" terminal, where a bold/italic space is just a
8634 * space.
8635 */
8636 did_delete = FALSE;
8637 if (c2 == ' '
8638 && end_col == Columns
8639 && can_clear(T_CE)
8640 && (attr == 0
8641 || (norm_term
8642 && attr <= HL_ALL
8643 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8644 {
8645 /*
8646 * check if we really need to clear something
8647 */
8648 col = start_col;
8649 if (c1 != ' ') /* don't clear first char */
8650 ++col;
8651
8652 off = LineOffset[row] + col;
8653 end_off = LineOffset[row] + end_col;
8654
8655 /* skip blanks (used often, keep it fast!) */
8656#ifdef FEAT_MBYTE
8657 if (enc_utf8)
8658 while (off < end_off && ScreenLines[off] == ' '
8659 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8660 ++off;
8661 else
8662#endif
8663 while (off < end_off && ScreenLines[off] == ' '
8664 && ScreenAttrs[off] == 0)
8665 ++off;
8666 if (off < end_off) /* something to be cleared */
8667 {
8668 col = off - LineOffset[row];
8669 screen_stop_highlight();
8670 term_windgoto(row, col);/* clear rest of this screen line */
8671 out_str(T_CE);
8672 screen_start(); /* don't know where cursor is now */
8673 col = end_col - col;
8674 while (col--) /* clear chars in ScreenLines */
8675 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008676 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 ++off;
8678 }
8679 }
8680 did_delete = TRUE; /* the chars are cleared now */
8681 }
8682
8683 off = LineOffset[row] + start_col;
8684 c = c1;
8685 for (col = start_col; col < end_col; ++col)
8686 {
8687 if (ScreenLines[off] != c
8688#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008689 || (enc_utf8 && (int)ScreenLinesUC[off]
8690 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691#endif
8692 || ScreenAttrs[off] != attr
8693#if defined(FEAT_GUI) || defined(UNIX)
8694 || force_next
8695#endif
8696 )
8697 {
8698#if defined(FEAT_GUI) || defined(UNIX)
8699 /* The bold trick may make a single row of pixels appear in
8700 * the next character. When a bold character is removed, the
8701 * next character should be redrawn too. This happens for our
8702 * own GUI and for some xterms. */
8703 if (
8704# ifdef FEAT_GUI
8705 gui.in_use
8706# endif
8707# if defined(FEAT_GUI) && defined(UNIX)
8708 ||
8709# endif
8710# ifdef UNIX
8711 term_is_xterm
8712# endif
8713 )
8714 {
8715 if (ScreenLines[off] != ' '
8716 && (ScreenAttrs[off] > HL_ALL
8717 || ScreenAttrs[off] & HL_BOLD))
8718 force_next = TRUE;
8719 else
8720 force_next = FALSE;
8721 }
8722#endif
8723 ScreenLines[off] = c;
8724#ifdef FEAT_MBYTE
8725 if (enc_utf8)
8726 {
8727 if (c >= 0x80)
8728 {
8729 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008730 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 }
8732 else
8733 ScreenLinesUC[off] = 0;
8734 }
8735#endif
8736 ScreenAttrs[off] = attr;
8737 if (!did_delete || c != ' ')
8738 screen_char(off, row, col);
8739 }
8740 ++off;
8741 if (col == start_col)
8742 {
8743 if (did_delete)
8744 break;
8745 c = c2;
8746 }
8747 }
8748 if (end_col == Columns)
8749 LineWraps[row] = FALSE;
8750 if (row == Rows - 1) /* overwritten the command line */
8751 {
8752 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008753 if (start_col == 0 && end_col == Columns
8754 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008756 if (start_col == 0)
8757 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 }
8759 }
8760}
8761
8762/*
8763 * Check if there should be a delay. Used before clearing or redrawing the
8764 * screen or the command line.
8765 */
8766 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008767check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768{
8769 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8770 && !did_wait_return
8771 && emsg_silent == 0)
8772 {
8773 out_flush();
8774 ui_delay(1000L, TRUE);
8775 emsg_on_display = FALSE;
8776 if (check_msg_scroll)
8777 msg_scroll = FALSE;
8778 }
8779}
8780
8781/*
8782 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008783 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 * Returns TRUE if there is a valid screen to write to.
8785 * Returns FALSE when starting up and screen not initialized yet.
8786 */
8787 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008788screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008790 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 return (ScreenLines != NULL);
8792}
8793
8794/*
8795 * Resize the shell to Rows and Columns.
8796 * Allocate ScreenLines[] and associated items.
8797 *
8798 * There may be some time between setting Rows and Columns and (re)allocating
8799 * ScreenLines[]. This happens when starting up and when (manually) changing
8800 * the shell size. Always use screen_Rows and screen_Columns to access items
8801 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8802 * final size of the shell is needed.
8803 */
8804 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008805screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806{
8807 int new_row, old_row;
8808#ifdef FEAT_GUI
8809 int old_Rows;
8810#endif
8811 win_T *wp;
8812 int outofmem = FALSE;
8813 int len;
8814 schar_T *new_ScreenLines;
8815#ifdef FEAT_MBYTE
8816 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008817 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008819 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820#endif
8821 sattr_T *new_ScreenAttrs;
8822 unsigned *new_LineOffset;
8823 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008824 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008825 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008827 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008828 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008830retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 /*
8832 * Allocation of the screen buffers is done only when the size changes and
8833 * when Rows and Columns have been set and we have started doing full
8834 * screen stuff.
8835 */
8836 if ((ScreenLines != NULL
8837 && Rows == screen_Rows
8838 && Columns == screen_Columns
8839#ifdef FEAT_MBYTE
8840 && enc_utf8 == (ScreenLinesUC != NULL)
8841 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008842 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843#endif
8844 )
8845 || Rows == 0
8846 || Columns == 0
8847 || (!full_screen && ScreenLines == NULL))
8848 return;
8849
8850 /*
8851 * It's possible that we produce an out-of-memory message below, which
8852 * will cause this function to be called again. To break the loop, just
8853 * return here.
8854 */
8855 if (entered)
8856 return;
8857 entered = TRUE;
8858
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008859 /*
8860 * Note that the window sizes are updated before reallocating the arrays,
8861 * thus we must not redraw here!
8862 */
8863 ++RedrawingDisabled;
8864
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 win_new_shellsize(); /* fit the windows in the new sized shell */
8866
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 comp_col(); /* recompute columns for shown command and ruler */
8868
8869 /*
8870 * We're changing the size of the screen.
8871 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8872 * - Move lines from the old arrays into the new arrays, clear extra
8873 * lines (unless the screen is going to be cleared).
8874 * - Free the old arrays.
8875 *
8876 * If anything fails, make ScreenLines NULL, so we don't do anything!
8877 * Continuing with the old ScreenLines may result in a crash, because the
8878 * size is wrong.
8879 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008880 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008882 if (aucmd_win != NULL)
8883 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884
8885 new_ScreenLines = (schar_T *)lalloc((long_u)(
8886 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8887#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008888 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 if (enc_utf8)
8890 {
8891 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8892 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008893 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008894 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8896 }
8897 if (enc_dbcs == DBCS_JPNU)
8898 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8899 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8900#endif
8901 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8902 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8903 new_LineOffset = (unsigned *)lalloc((long_u)(
8904 Rows * sizeof(unsigned)), FALSE);
8905 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008906 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008908 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 {
8910 if (win_alloc_lines(wp) == FAIL)
8911 {
8912 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008913 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 }
8915 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008916 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8917 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008918 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008919give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008921#ifdef FEAT_MBYTE
8922 for (i = 0; i < p_mco; ++i)
8923 if (new_ScreenLinesC[i] == NULL)
8924 break;
8925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 if (new_ScreenLines == NULL
8927#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008928 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8930#endif
8931 || new_ScreenAttrs == NULL
8932 || new_LineOffset == NULL
8933 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008934 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 || outofmem)
8936 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008937 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008938 {
8939 /* guess the size */
8940 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8941
8942 /* Remember we did this to avoid getting outofmem messages over
8943 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008944 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008945 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008946 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01008948 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008949 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008950 VIM_CLEAR(new_ScreenLinesC[i]);
8951 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01008953 VIM_CLEAR(new_ScreenAttrs);
8954 VIM_CLEAR(new_LineOffset);
8955 VIM_CLEAR(new_LineWraps);
8956 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 }
8958 else
8959 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008960 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008961
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 for (new_row = 0; new_row < Rows; ++new_row)
8963 {
8964 new_LineOffset[new_row] = new_row * Columns;
8965 new_LineWraps[new_row] = FALSE;
8966
8967 /*
8968 * If the screen is not going to be cleared, copy as much as
8969 * possible from the old screen to the new one and clear the rest
8970 * (used when resizing the window at the "--more--" prompt or when
8971 * executing an external command, for the GUI).
8972 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008973 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 {
8975 (void)vim_memset(new_ScreenLines + new_row * Columns,
8976 ' ', (size_t)Columns * sizeof(schar_T));
8977#ifdef FEAT_MBYTE
8978 if (enc_utf8)
8979 {
8980 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8981 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008982 for (i = 0; i < p_mco; ++i)
8983 (void)vim_memset(new_ScreenLinesC[i]
8984 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 0, (size_t)Columns * sizeof(u8char_T));
8986 }
8987 if (enc_dbcs == DBCS_JPNU)
8988 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8989 0, (size_t)Columns * sizeof(schar_T));
8990#endif
8991 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8992 0, (size_t)Columns * sizeof(sattr_T));
8993 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008994 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 {
8996 if (screen_Columns < Columns)
8997 len = screen_Columns;
8998 else
8999 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009000#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009001 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009002 * may be invalid now. Also when p_mco changes. */
9003 if (!(enc_utf8 && ScreenLinesUC == NULL)
9004 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009005#endif
9006 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9007 ScreenLines + LineOffset[old_row],
9008 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009010 if (enc_utf8 && ScreenLinesUC != NULL
9011 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 {
9013 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9014 ScreenLinesUC + LineOffset[old_row],
9015 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009016 for (i = 0; i < p_mco; ++i)
9017 mch_memmove(new_ScreenLinesC[i]
9018 + new_LineOffset[new_row],
9019 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 (size_t)len * sizeof(u8char_T));
9021 }
9022 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9023 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9024 ScreenLines2 + LineOffset[old_row],
9025 (size_t)len * sizeof(schar_T));
9026#endif
9027 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9028 ScreenAttrs + LineOffset[old_row],
9029 (size_t)len * sizeof(sattr_T));
9030 }
9031 }
9032 }
9033 /* Use the last line of the screen for the current line. */
9034 current_ScreenLine = new_ScreenLines + Rows * Columns;
9035 }
9036
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009037 free_screenlines();
9038
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 ScreenLines = new_ScreenLines;
9040#ifdef FEAT_MBYTE
9041 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009042 for (i = 0; i < p_mco; ++i)
9043 ScreenLinesC[i] = new_ScreenLinesC[i];
9044 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 ScreenLines2 = new_ScreenLines2;
9046#endif
9047 ScreenAttrs = new_ScreenAttrs;
9048 LineOffset = new_LineOffset;
9049 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009050 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051
9052 /* It's important that screen_Rows and screen_Columns reflect the actual
9053 * size of ScreenLines[]. Set them before calling anything. */
9054#ifdef FEAT_GUI
9055 old_Rows = screen_Rows;
9056#endif
9057 screen_Rows = Rows;
9058 screen_Columns = Columns;
9059
9060 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009061 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 screenclear2();
9063
9064#ifdef FEAT_GUI
9065 else if (gui.in_use
9066 && !gui.starting
9067 && ScreenLines != NULL
9068 && old_Rows != Rows)
9069 {
9070 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9071 /*
9072 * Adjust the position of the cursor, for when executing an external
9073 * command.
9074 */
9075 if (msg_row >= Rows) /* Rows got smaller */
9076 msg_row = Rows - 1; /* put cursor at last row */
9077 else if (Rows > old_Rows) /* Rows got bigger */
9078 msg_row += Rows - old_Rows; /* put cursor in same place */
9079 if (msg_col >= Columns) /* Columns got smaller */
9080 msg_col = Columns - 1; /* put cursor at last column */
9081 }
9082#endif
9083
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009085 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009086
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009087 /*
9088 * Do not apply autocommands more than 3 times to avoid an endless loop
9089 * in case applying autocommands always changes Rows or Columns.
9090 */
9091 if (starting == 0 && ++retry_count <= 3)
9092 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009093 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009094 /* In rare cases, autocommands may have altered Rows or Columns,
9095 * jump back to check if we need to allocate the screen again. */
9096 goto retry;
9097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098}
9099
9100 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009101free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009102{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009103#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009104 int i;
9105
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009106 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009107 for (i = 0; i < Screen_mco; ++i)
9108 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009109 vim_free(ScreenLines2);
9110#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009111 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009112 vim_free(ScreenAttrs);
9113 vim_free(LineOffset);
9114 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009115 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009116}
9117
9118 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009119screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
9121 check_for_delay(FALSE);
9122 screenalloc(FALSE); /* allocate screen buffers if size changed */
9123 screenclear2(); /* clear the screen */
9124}
9125
9126 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009127screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128{
9129 int i;
9130
9131 if (starting == NO_SCREEN || ScreenLines == NULL
9132#ifdef FEAT_GUI
9133 || (gui.in_use && gui.starting)
9134#endif
9135 )
9136 return;
9137
9138#ifdef FEAT_GUI
9139 if (!gui.in_use)
9140#endif
9141 screen_attr = -1; /* force setting the Normal colors */
9142 screen_stop_highlight(); /* don't want highlighting here */
9143
9144#ifdef FEAT_CLIPBOARD
9145 /* disable selection without redrawing it */
9146 clip_scroll_selection(9999);
9147#endif
9148
9149 /* blank out ScreenLines */
9150 for (i = 0; i < Rows; ++i)
9151 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009152 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 LineWraps[i] = FALSE;
9154 }
9155
9156 if (can_clear(T_CL))
9157 {
9158 out_str(T_CL); /* clear the display */
9159 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009160 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 }
9162 else
9163 {
9164 /* can't clear the screen, mark all chars with invalid attributes */
9165 for (i = 0; i < Rows; ++i)
9166 lineinvalid(LineOffset[i], (int)Columns);
9167 clear_cmdline = TRUE;
9168 }
9169
9170 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9171
9172 win_rest_invalid(firstwin);
9173 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009174 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 if (must_redraw == CLEAR) /* no need to clear again */
9176 must_redraw = NOT_VALID;
9177 compute_cmdrow();
9178 msg_row = cmdline_row; /* put cursor on last line for messages */
9179 msg_col = 0;
9180 screen_start(); /* don't know where cursor is now */
9181 msg_scrolled = 0; /* can't scroll back */
9182 msg_didany = FALSE;
9183 msg_didout = FALSE;
9184}
9185
9186/*
9187 * Clear one line in ScreenLines.
9188 */
9189 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009190lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191{
9192 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9193#ifdef FEAT_MBYTE
9194 if (enc_utf8)
9195 (void)vim_memset(ScreenLinesUC + off, 0,
9196 (size_t)width * sizeof(u8char_T));
9197#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009198 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199}
9200
9201/*
9202 * Mark one line in ScreenLines invalid by setting the attributes to an
9203 * invalid value.
9204 */
9205 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009206lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207{
9208 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9209}
9210
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211/*
9212 * Copy part of a Screenline for vertically split window "wp".
9213 */
9214 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009215linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216{
9217 unsigned off_to = LineOffset[to] + wp->w_wincol;
9218 unsigned off_from = LineOffset[from] + wp->w_wincol;
9219
9220 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9221 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009222#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 if (enc_utf8)
9224 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009225 int i;
9226
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9228 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009229 for (i = 0; i < p_mco; ++i)
9230 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9231 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 }
9233 if (enc_dbcs == DBCS_JPNU)
9234 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9235 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9238 wp->w_width * sizeof(sattr_T));
9239}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240
9241/*
9242 * Return TRUE if clearing with term string "p" would work.
9243 * It can't work when the string is empty or it won't set the right background.
9244 */
9245 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009246can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247{
9248 return (*p != NUL && (t_colors <= 1
9249#ifdef FEAT_GUI
9250 || gui.in_use
9251#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009252#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009253 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009254 || (!p_tgc && cterm_normal_bg_color == 0)
9255#else
9256 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009257#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009258 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259}
9260
9261/*
9262 * Reset cursor position. Use whenever cursor was moved because of outputting
9263 * something directly to the screen (shell commands) or a terminal control
9264 * code.
9265 */
9266 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009267screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268{
9269 screen_cur_row = screen_cur_col = 9999;
9270}
9271
9272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 * Move the cursor to position "row","col" in the screen.
9274 * This tries to find the most efficient way to move, minimizing the number of
9275 * characters sent to the terminal.
9276 */
9277 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009278windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009280 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 int i;
9282 int plan;
9283 int cost;
9284 int wouldbe_col;
9285 int noinvcurs;
9286 char_u *bs;
9287 int goto_cost;
9288 int attr;
9289
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009290#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9292
9293#define PLAN_LE 1
9294#define PLAN_CR 2
9295#define PLAN_NL 3
9296#define PLAN_WRITE 4
9297 /* Can't use ScreenLines unless initialized */
9298 if (ScreenLines == NULL)
9299 return;
9300
9301 if (col != screen_cur_col || row != screen_cur_row)
9302 {
9303 /* Check for valid position. */
9304 if (row < 0) /* window without text lines? */
9305 row = 0;
9306 if (row >= screen_Rows)
9307 row = screen_Rows - 1;
9308 if (col >= screen_Columns)
9309 col = screen_Columns - 1;
9310
9311 /* check if no cursor movement is allowed in highlight mode */
9312 if (screen_attr && *T_MS == NUL)
9313 noinvcurs = HIGHL_COST;
9314 else
9315 noinvcurs = 0;
9316 goto_cost = GOTO_COST + noinvcurs;
9317
9318 /*
9319 * Plan how to do the positioning:
9320 * 1. Use CR to move it to column 0, same row.
9321 * 2. Use T_LE to move it a few columns to the left.
9322 * 3. Use NL to move a few lines down, column 0.
9323 * 4. Move a few columns to the right with T_ND or by writing chars.
9324 *
9325 * Don't do this if the cursor went beyond the last column, the cursor
9326 * position is unknown then (some terminals wrap, some don't )
9327 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009328 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 * characters to move the cursor to the right.
9330 */
9331 if (row >= screen_cur_row && screen_cur_col < Columns)
9332 {
9333 /*
9334 * If the cursor is in the same row, bigger col, we can use CR
9335 * or T_LE.
9336 */
9337 bs = NULL; /* init for GCC */
9338 attr = screen_attr;
9339 if (row == screen_cur_row && col < screen_cur_col)
9340 {
9341 /* "le" is preferred over "bc", because "bc" is obsolete */
9342 if (*T_LE)
9343 bs = T_LE; /* "cursor left" */
9344 else
9345 bs = T_BC; /* "backspace character (old) */
9346 if (*bs)
9347 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9348 else
9349 cost = 999;
9350 if (col + 1 < cost) /* using CR is less characters */
9351 {
9352 plan = PLAN_CR;
9353 wouldbe_col = 0;
9354 cost = 1; /* CR is just one character */
9355 }
9356 else
9357 {
9358 plan = PLAN_LE;
9359 wouldbe_col = col;
9360 }
9361 if (noinvcurs) /* will stop highlighting */
9362 {
9363 cost += noinvcurs;
9364 attr = 0;
9365 }
9366 }
9367
9368 /*
9369 * If the cursor is above where we want to be, we can use CR LF.
9370 */
9371 else if (row > screen_cur_row)
9372 {
9373 plan = PLAN_NL;
9374 wouldbe_col = 0;
9375 cost = (row - screen_cur_row) * 2; /* CR LF */
9376 if (noinvcurs) /* will stop highlighting */
9377 {
9378 cost += noinvcurs;
9379 attr = 0;
9380 }
9381 }
9382
9383 /*
9384 * If the cursor is in the same row, smaller col, just use write.
9385 */
9386 else
9387 {
9388 plan = PLAN_WRITE;
9389 wouldbe_col = screen_cur_col;
9390 cost = 0;
9391 }
9392
9393 /*
9394 * Check if any characters that need to be written have the
9395 * correct attributes. Also avoid UTF-8 characters.
9396 */
9397 i = col - wouldbe_col;
9398 if (i > 0)
9399 cost += i;
9400 if (cost < goto_cost && i > 0)
9401 {
9402 /*
9403 * Check if the attributes are correct without additionally
9404 * stopping highlighting.
9405 */
9406 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9407 while (i && *p++ == attr)
9408 --i;
9409 if (i != 0)
9410 {
9411 /*
9412 * Try if it works when highlighting is stopped here.
9413 */
9414 if (*--p == 0)
9415 {
9416 cost += noinvcurs;
9417 while (i && *p++ == 0)
9418 --i;
9419 }
9420 if (i != 0)
9421 cost = 999; /* different attributes, don't do it */
9422 }
9423#ifdef FEAT_MBYTE
9424 if (enc_utf8)
9425 {
9426 /* Don't use an UTF-8 char for positioning, it's slow. */
9427 for (i = wouldbe_col; i < col; ++i)
9428 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9429 {
9430 cost = 999;
9431 break;
9432 }
9433 }
9434#endif
9435 }
9436
9437 /*
9438 * We can do it without term_windgoto()!
9439 */
9440 if (cost < goto_cost)
9441 {
9442 if (plan == PLAN_LE)
9443 {
9444 if (noinvcurs)
9445 screen_stop_highlight();
9446 while (screen_cur_col > col)
9447 {
9448 out_str(bs);
9449 --screen_cur_col;
9450 }
9451 }
9452 else if (plan == PLAN_CR)
9453 {
9454 if (noinvcurs)
9455 screen_stop_highlight();
9456 out_char('\r');
9457 screen_cur_col = 0;
9458 }
9459 else if (plan == PLAN_NL)
9460 {
9461 if (noinvcurs)
9462 screen_stop_highlight();
9463 while (screen_cur_row < row)
9464 {
9465 out_char('\n');
9466 ++screen_cur_row;
9467 }
9468 screen_cur_col = 0;
9469 }
9470
9471 i = col - screen_cur_col;
9472 if (i > 0)
9473 {
9474 /*
9475 * Use cursor-right if it's one character only. Avoids
9476 * removing a line of pixels from the last bold char, when
9477 * using the bold trick in the GUI.
9478 */
9479 if (T_ND[0] != NUL && T_ND[1] == NUL)
9480 {
9481 while (i-- > 0)
9482 out_char(*T_ND);
9483 }
9484 else
9485 {
9486 int off;
9487
9488 off = LineOffset[row] + screen_cur_col;
9489 while (i-- > 0)
9490 {
9491 if (ScreenAttrs[off] != screen_attr)
9492 screen_stop_highlight();
9493#ifdef FEAT_MBYTE
9494 out_flush_check();
9495#endif
9496 out_char(ScreenLines[off]);
9497#ifdef FEAT_MBYTE
9498 if (enc_dbcs == DBCS_JPNU
9499 && ScreenLines[off] == 0x8e)
9500 out_char(ScreenLines2[off]);
9501#endif
9502 ++off;
9503 }
9504 }
9505 }
9506 }
9507 }
9508 else
9509 cost = 999;
9510
9511 if (cost >= goto_cost)
9512 {
9513 if (noinvcurs)
9514 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009515 if (row == screen_cur_row && (col > screen_cur_col)
9516 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 term_cursor_right(col - screen_cur_col);
9518 else
9519 term_windgoto(row, col);
9520 }
9521 screen_cur_row = row;
9522 screen_cur_col = col;
9523 }
9524}
9525
9526/*
9527 * Set cursor to its position in the current window.
9528 */
9529 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009530setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009532 setcursor_mayforce(FALSE);
9533}
9534
9535/*
9536 * Set cursor to its position in the current window.
9537 * When "force" is TRUE also when not redrawing.
9538 */
9539 void
9540setcursor_mayforce(int force)
9541{
9542 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 {
9544 validate_cursor();
9545 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009546 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009548 /* With 'rightleft' set and the cursor on a double-wide
9549 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009550 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009552 (has_mbyte
9553 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9554 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555# endif
9556 1)) :
9557#endif
9558 curwin->w_wcol));
9559 }
9560}
9561
9562
9563/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009564 * Insert 'line_count' lines at 'row' in window 'wp'.
9565 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9566 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 * scrolling.
9568 * Returns FAIL if the lines are not inserted, OK for success.
9569 */
9570 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009571win_ins_lines(
9572 win_T *wp,
9573 int row,
9574 int line_count,
9575 int invalid,
9576 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577{
9578 int did_delete;
9579 int nextrow;
9580 int lastrow;
9581 int retval;
9582
9583 if (invalid)
9584 wp->w_lines_valid = 0;
9585
9586 if (wp->w_height < 5)
9587 return FAIL;
9588
9589 if (line_count > wp->w_height - row)
9590 line_count = wp->w_height - row;
9591
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009592 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 if (retval != MAYBE)
9594 return retval;
9595
9596 /*
9597 * If there is a next window or a status line, we first try to delete the
9598 * lines at the bottom to avoid messing what is after the window.
9599 * If this fails and there are following windows, don't do anything to avoid
9600 * messing up those windows, better just redraw.
9601 */
9602 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 if (wp->w_next != NULL || wp->w_status_height)
9604 {
9605 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009606 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 did_delete = TRUE;
9608 else if (wp->w_next)
9609 return FAIL;
9610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 /*
9612 * if no lines deleted, blank the lines that will end up below the window
9613 */
9614 if (!did_delete)
9615 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009618 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 lastrow = nextrow + line_count;
9620 if (lastrow > Rows)
9621 lastrow = Rows;
9622 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009623 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 ' ', ' ', 0);
9625 }
9626
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009627 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 == FAIL)
9629 {
9630 /* deletion will have messed up other windows */
9631 if (did_delete)
9632 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 win_rest_invalid(W_NEXT(wp));
9635 }
9636 return FAIL;
9637 }
9638
9639 return OK;
9640}
9641
9642/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009643 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9645 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9646 * scrolling
9647 * Return OK for success, FAIL if the lines are not deleted.
9648 */
9649 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009650win_del_lines(
9651 win_T *wp,
9652 int row,
9653 int line_count,
9654 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009655 int mayclear,
9656 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657{
9658 int retval;
9659
9660 if (invalid)
9661 wp->w_lines_valid = 0;
9662
9663 if (line_count > wp->w_height - row)
9664 line_count = wp->w_height - row;
9665
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009666 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 if (retval != MAYBE)
9668 return retval;
9669
9670 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009671 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 return FAIL;
9673
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 /*
9675 * If there are windows or status lines below, try to put them at the
9676 * correct place. If we can't do that, they have to be redrawn.
9677 */
9678 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9679 {
9680 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009681 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 {
9683 wp->w_redr_status = TRUE;
9684 win_rest_invalid(wp->w_next);
9685 }
9686 }
9687 /*
9688 * If this is the last window and there is no status line, redraw the
9689 * command line later.
9690 */
9691 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 redraw_cmdline = TRUE;
9693 return OK;
9694}
9695
9696/*
9697 * Common code for win_ins_lines() and win_del_lines().
9698 * Returns OK or FAIL when the work has been done.
9699 * Returns MAYBE when not finished yet.
9700 */
9701 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009702win_do_lines(
9703 win_T *wp,
9704 int row,
9705 int line_count,
9706 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009707 int del,
9708 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709{
9710 int retval;
9711
9712 if (!redrawing() || line_count <= 0)
9713 return FAIL;
9714
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009715 /* When inserting lines would result in loss of command output, just redraw
9716 * the lines. */
9717 if (no_win_do_lines_ins && !del)
9718 return FAIL;
9719
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009721 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009723 if (!no_win_do_lines_ins)
9724 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 return FAIL;
9726 }
9727
9728 /*
9729 * Delete all remaining lines
9730 */
9731 if (row + line_count >= wp->w_height)
9732 {
9733 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009734 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 ' ', ' ', 0);
9736 return OK;
9737 }
9738
9739 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009740 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009742 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009744 if (!no_win_do_lines_ins)
9745 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746
9747 /*
9748 * If the terminal can set a scroll region, use that.
9749 * Always do this in a vertically split window. This will redraw from
9750 * ScreenLines[] when t_CV isn't defined. That's faster than using
9751 * win_line().
9752 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009753 * a character in the lower right corner of the scroll region may cause a
9754 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009756 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 scroll_region_set(wp, row);
9760 if (del)
9761 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009762 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 else
9764 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009765 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 scroll_region_reset();
9768 return retval;
9769 }
9770
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9772 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773
9774 return MAYBE;
9775}
9776
9777/*
9778 * window 'wp' and everything after it is messed up, mark it for redraw
9779 */
9780 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009781win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 {
9785 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 wp->w_redr_status = TRUE;
9787 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 }
9789 redraw_cmdline = TRUE;
9790}
9791
9792/*
9793 * The rest of the routines in this file perform screen manipulations. The
9794 * given operation is performed physically on the screen. The corresponding
9795 * change is also made to the internal screen image. In this way, the editor
9796 * anticipates the effect of editing changes on the appearance of the screen.
9797 * That way, when we call screenupdate a complete redraw isn't usually
9798 * necessary. Another advantage is that we can keep adding code to anticipate
9799 * screen changes, and in the meantime, everything still works.
9800 */
9801
9802/*
9803 * types for inserting or deleting lines
9804 */
9805#define USE_T_CAL 1
9806#define USE_T_CDL 2
9807#define USE_T_AL 3
9808#define USE_T_CE 4
9809#define USE_T_DL 5
9810#define USE_T_SR 6
9811#define USE_NL 7
9812#define USE_T_CD 8
9813#define USE_REDRAW 9
9814
9815/*
9816 * insert lines on the screen and update ScreenLines[]
9817 * 'end' is the line after the scrolled part. Normally it is Rows.
9818 * When scrolling region used 'off' is the offset from the top for the region.
9819 * 'row' and 'end' are relative to the start of the region.
9820 *
9821 * return FAIL for failure, OK for success.
9822 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009823 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009824screen_ins_lines(
9825 int off,
9826 int row,
9827 int line_count,
9828 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009829 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009830 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831{
9832 int i;
9833 int j;
9834 unsigned temp;
9835 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009836 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 int type;
9838 int result_empty;
9839 int can_ce = can_clear(T_CE);
9840
9841 /*
9842 * FAIL if
9843 * - there is no valid screen
9844 * - the screen has to be redrawn completely
9845 * - the line count is less than one
9846 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009847 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009849 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9850#ifdef FEAT_CLIPBOARD
9851 || (clip_star.state != SELECT_CLEARED
9852 && redrawing_for_callback > 0)
9853#endif
9854 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 return FAIL;
9856
9857 /*
9858 * There are seven ways to insert lines:
9859 * 0. When in a vertically split window and t_CV isn't set, redraw the
9860 * characters from ScreenLines[].
9861 * 1. Use T_CD (clear to end of display) if it exists and the result of
9862 * the insert is just empty lines
9863 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9864 * present or line_count > 1. It looks better if we do all the inserts
9865 * at once.
9866 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9867 * insert is just empty lines and T_CE is not present or line_count >
9868 * 1.
9869 * 4. Use T_AL (insert line) if it exists.
9870 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9871 * just empty lines.
9872 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9873 * just empty lines.
9874 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9875 * the 'da' flag is not set or we have clear line capability.
9876 * 8. redraw the characters from ScreenLines[].
9877 *
9878 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9879 * the scrollbar for the window. It does have insert line, use that if it
9880 * exists.
9881 */
9882 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9884 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009885 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 type = USE_T_CD;
9887 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9888 type = USE_T_CAL;
9889 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9890 type = USE_T_CDL;
9891 else if (*T_AL != NUL)
9892 type = USE_T_AL;
9893 else if (can_ce && result_empty)
9894 type = USE_T_CE;
9895 else if (*T_DL != NUL && result_empty)
9896 type = USE_T_DL;
9897 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9898 type = USE_T_SR;
9899 else
9900 return FAIL;
9901
9902 /*
9903 * For clearing the lines screen_del_lines() is used. This will also take
9904 * care of t_db if necessary.
9905 */
9906 if (type == USE_T_CD || type == USE_T_CDL ||
9907 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009908 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909
9910 /*
9911 * If text is retained below the screen, first clear or delete as many
9912 * lines at the bottom of the window as are about to be inserted so that
9913 * the deleted lines won't later surface during a screen_del_lines.
9914 */
9915 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009916 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917
9918#ifdef FEAT_CLIPBOARD
9919 /* Remove a modeless selection when inserting lines halfway the screen
9920 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009921 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009922 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 else
9924 clip_scroll_selection(-line_count);
9925#endif
9926
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927#ifdef FEAT_GUI
9928 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9929 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009930 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931#endif
9932
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009933 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9934 cursor_col = wp->w_wincol;
9935
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 if (*T_CCS != NUL) /* cursor relative to region */
9937 cursor_row = row;
9938 else
9939 cursor_row = row + off;
9940
9941 /*
9942 * Shift LineOffset[] line_count down to reflect the inserted lines.
9943 * Clear the inserted lines in ScreenLines[].
9944 */
9945 row += off;
9946 end += off;
9947 for (i = 0; i < line_count; ++i)
9948 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 if (wp != NULL && wp->w_width != Columns)
9950 {
9951 /* need to copy part of a line */
9952 j = end - 1 - i;
9953 while ((j -= line_count) >= row)
9954 linecopy(j + line_count, j, wp);
9955 j += line_count;
9956 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009957 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9958 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 else
9960 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9961 LineWraps[j] = FALSE;
9962 }
9963 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 {
9965 j = end - 1 - i;
9966 temp = LineOffset[j];
9967 while ((j -= line_count) >= row)
9968 {
9969 LineOffset[j + line_count] = LineOffset[j];
9970 LineWraps[j + line_count] = LineWraps[j];
9971 }
9972 LineOffset[j + line_count] = temp;
9973 LineWraps[j + line_count] = FALSE;
9974 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009975 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 else
9977 lineinvalid(temp, (int)Columns);
9978 }
9979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980
9981 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009982 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009983 if (clear_attr != 0)
9984 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 /* redraw the characters */
9987 if (type == USE_REDRAW)
9988 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009989 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 {
9991 term_append_lines(line_count);
9992 screen_start(); /* don't know where cursor is now */
9993 }
9994 else
9995 {
9996 for (i = 0; i < line_count; i++)
9997 {
9998 if (type == USE_T_AL)
9999 {
10000 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010001 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 out_str(T_AL);
10003 }
10004 else /* type == USE_T_SR */
10005 out_str(T_SR);
10006 screen_start(); /* don't know where cursor is now */
10007 }
10008 }
10009
10010 /*
10011 * With scroll-reverse and 'da' flag set we need to clear the lines that
10012 * have been scrolled down into the region.
10013 */
10014 if (type == USE_T_SR && *T_DA)
10015 {
10016 for (i = 0; i < line_count; ++i)
10017 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010018 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 out_str(T_CE);
10020 screen_start(); /* don't know where cursor is now */
10021 }
10022 }
10023
10024#ifdef FEAT_GUI
10025 gui_can_update_cursor();
10026 if (gui.in_use)
10027 out_flush(); /* always flush after a scroll */
10028#endif
10029 return OK;
10030}
10031
10032/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010033 * Delete lines on the screen and update ScreenLines[].
10034 * "end" is the line after the scrolled part. Normally it is Rows.
10035 * When scrolling region used "off" is the offset from the top for the region.
10036 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 *
10038 * Return OK for success, FAIL if the lines are not deleted.
10039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010041screen_del_lines(
10042 int off,
10043 int row,
10044 int line_count,
10045 int end,
10046 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010047 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010048 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049{
10050 int j;
10051 int i;
10052 unsigned temp;
10053 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010054 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 int cursor_end;
10056 int result_empty; /* result is empty until end of region */
10057 int can_delete; /* deleting line codes can be used */
10058 int type;
10059
10060 /*
10061 * FAIL if
10062 * - there is no valid screen
10063 * - the screen has to be redrawn completely
10064 * - the line count is less than one
10065 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010066 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010068 if (!screen_valid(TRUE) || line_count <= 0
10069 || (!force && line_count > p_ttyscroll)
10070#ifdef FEAT_CLIPBOARD
10071 || (clip_star.state != SELECT_CLEARED
10072 && redrawing_for_callback > 0)
10073#endif
10074 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 return FAIL;
10076
10077 /*
10078 * Check if the rest of the current region will become empty.
10079 */
10080 result_empty = row + line_count >= end;
10081
10082 /*
10083 * We can delete lines only when 'db' flag not set or when 'ce' option
10084 * available.
10085 */
10086 can_delete = (*T_DB == NUL || can_clear(T_CE));
10087
10088 /*
10089 * There are six ways to delete lines:
10090 * 0. When in a vertically split window and t_CV isn't set, redraw the
10091 * characters from ScreenLines[].
10092 * 1. Use T_CD if it exists and the result is empty.
10093 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10094 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10095 * none of the other ways work.
10096 * 4. Use T_CE (erase line) if the result is empty.
10097 * 5. Use T_DL (delete line) if it exists.
10098 * 6. redraw the characters from ScreenLines[].
10099 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10101 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010102 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 type = USE_T_CD;
10104#if defined(__BEOS__) && defined(BEOS_DR8)
10105 /*
10106 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10107 * its internal termcap... this works okay for tests which test *T_DB !=
10108 * NUL. It has the disadvantage that the user cannot use any :set t_*
10109 * command to get T_DB (back) to empty_option, only :set term=... will do
10110 * the trick...
10111 * Anyway, this hack will hopefully go away with the next OS release.
10112 * (Olaf Seibert)
10113 */
10114 else if (row == 0 && T_DB == empty_option
10115 && (line_count == 1 || *T_CDL == NUL))
10116#else
10117 else if (row == 0 && (
10118#ifndef AMIGA
10119 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10120 * up, so use delete-line command */
10121 line_count == 1 ||
10122#endif
10123 *T_CDL == NUL))
10124#endif
10125 type = USE_NL;
10126 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10127 type = USE_T_CDL;
10128 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010129 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 type = USE_T_CE;
10131 else if (*T_DL != NUL && can_delete)
10132 type = USE_T_DL;
10133 else if (*T_CDL != NUL && can_delete)
10134 type = USE_T_CDL;
10135 else
10136 return FAIL;
10137
10138#ifdef FEAT_CLIPBOARD
10139 /* Remove a modeless selection when deleting lines halfway the screen or
10140 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010141 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010142 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 else
10144 clip_scroll_selection(line_count);
10145#endif
10146
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147#ifdef FEAT_GUI
10148 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10149 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010150 gui_dont_update_cursor(gui.cursor_row >= row + off
10151 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152#endif
10153
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010154 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10155 cursor_col = wp->w_wincol;
10156
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 if (*T_CCS != NUL) /* cursor relative to region */
10158 {
10159 cursor_row = row;
10160 cursor_end = end;
10161 }
10162 else
10163 {
10164 cursor_row = row + off;
10165 cursor_end = end + off;
10166 }
10167
10168 /*
10169 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10170 * Clear the inserted lines in ScreenLines[].
10171 */
10172 row += off;
10173 end += off;
10174 for (i = 0; i < line_count; ++i)
10175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 if (wp != NULL && wp->w_width != Columns)
10177 {
10178 /* need to copy part of a line */
10179 j = row + i;
10180 while ((j += line_count) <= end - 1)
10181 linecopy(j - line_count, j, wp);
10182 j -= line_count;
10183 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010184 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10185 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 else
10187 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10188 LineWraps[j] = FALSE;
10189 }
10190 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 {
10192 /* whole width, moving the line pointers is faster */
10193 j = row + i;
10194 temp = LineOffset[j];
10195 while ((j += line_count) <= end - 1)
10196 {
10197 LineOffset[j - line_count] = LineOffset[j];
10198 LineWraps[j - line_count] = LineWraps[j];
10199 }
10200 LineOffset[j - line_count] = temp;
10201 LineWraps[j - line_count] = FALSE;
10202 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010203 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 else
10205 lineinvalid(temp, (int)Columns);
10206 }
10207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010209 if (screen_attr != clear_attr)
10210 screen_stop_highlight();
10211 if (clear_attr != 0)
10212 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 /* redraw the characters */
10215 if (type == USE_REDRAW)
10216 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010217 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010219 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 out_str(T_CD);
10221 screen_start(); /* don't know where cursor is now */
10222 }
10223 else if (type == USE_T_CDL)
10224 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010225 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 term_delete_lines(line_count);
10227 screen_start(); /* don't know where cursor is now */
10228 }
10229 /*
10230 * Deleting lines at top of the screen or scroll region: Just scroll
10231 * the whole screen (scroll region) up by outputting newlines on the
10232 * last line.
10233 */
10234 else if (type == USE_NL)
10235 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010236 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 for (i = line_count; --i >= 0; )
10238 out_char('\n'); /* cursor will remain on same line */
10239 }
10240 else
10241 {
10242 for (i = line_count; --i >= 0; )
10243 {
10244 if (type == USE_T_DL)
10245 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010246 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 out_str(T_DL); /* delete a line */
10248 }
10249 else /* type == USE_T_CE */
10250 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010251 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 out_str(T_CE); /* erase a line */
10253 }
10254 screen_start(); /* don't know where cursor is now */
10255 }
10256 }
10257
10258 /*
10259 * If the 'db' flag is set, we need to clear the lines that have been
10260 * scrolled up at the bottom of the region.
10261 */
10262 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10263 {
10264 for (i = line_count; i > 0; --i)
10265 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010266 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 out_str(T_CE); /* erase a line */
10268 screen_start(); /* don't know where cursor is now */
10269 }
10270 }
10271
10272#ifdef FEAT_GUI
10273 gui_can_update_cursor();
10274 if (gui.in_use)
10275 out_flush(); /* always flush after a scroll */
10276#endif
10277
10278 return OK;
10279}
10280
10281/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010282 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 *
10284 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10285 * If clear_cmdline is FALSE there may be a message there that needs to be
10286 * cleared only if a mode is shown.
10287 * Return the length of the message (0 if no message).
10288 */
10289 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010290showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291{
10292 int need_clear;
10293 int length = 0;
10294 int do_mode;
10295 int attr;
10296 int nwr_save;
10297#ifdef FEAT_INS_EXPAND
10298 int sub_attr;
10299#endif
10300
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010301 do_mode = ((p_smd && msg_silent == 0)
10302 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010303 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010304 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010305 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 {
10307 /*
10308 * Don't show mode right now, when not redrawing or inside a mapping.
10309 * Call char_avail() only when we are going to show something, because
10310 * it takes a bit of time.
10311 */
10312 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10313 {
10314 redraw_cmdline = TRUE; /* show mode later */
10315 return 0;
10316 }
10317
10318 nwr_save = need_wait_return;
10319
10320 /* wait a bit before overwriting an important message */
10321 check_for_delay(FALSE);
10322
10323 /* if the cmdline is more than one line high, erase top lines */
10324 need_clear = clear_cmdline;
10325 if (clear_cmdline && cmdline_row < Rows - 1)
10326 msg_clr_cmdline(); /* will reset clear_cmdline */
10327
10328 /* Position on the last line in the window, column 0 */
10329 msg_pos_mode();
10330 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010331 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 if (do_mode)
10333 {
10334 MSG_PUTS_ATTR("--", attr);
10335#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010336 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010337# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010338 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010339# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010340 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010341# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010342 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010343# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 MSG_PUTS_ATTR(" IM", attr);
10345# else
10346 MSG_PUTS_ATTR(" XIM", attr);
10347# endif
10348#endif
10349#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10350 if (gui.in_use)
10351 {
10352 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010353 {
10354 /* HANGUL */
10355 if (enc_utf8)
10356 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10357 else
10358 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 }
10361#endif
10362#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010363 /* CTRL-X in Insert mode */
10364 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 {
10366 /* These messages can get long, avoid a wrap in a narrow
10367 * window. Prefer showing edit_submode_extra. */
10368 length = (Rows - msg_row) * Columns - 3;
10369 if (edit_submode_extra != NULL)
10370 length -= vim_strsize(edit_submode_extra);
10371 if (length > 0)
10372 {
10373 if (edit_submode_pre != NULL)
10374 length -= vim_strsize(edit_submode_pre);
10375 if (length - vim_strsize(edit_submode) > 0)
10376 {
10377 if (edit_submode_pre != NULL)
10378 msg_puts_attr(edit_submode_pre, attr);
10379 msg_puts_attr(edit_submode, attr);
10380 }
10381 if (edit_submode_extra != NULL)
10382 {
10383 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10384 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010385 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 else
10387 sub_attr = attr;
10388 msg_puts_attr(edit_submode_extra, sub_attr);
10389 }
10390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 }
10392 else
10393#endif
10394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 if (State & VREPLACE_FLAG)
10396 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010397 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10399 else if (State & INSERT)
10400 {
10401#ifdef FEAT_RIGHTLEFT
10402 if (p_ri)
10403 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10404#endif
10405 MSG_PUTS_ATTR(_(" INSERT"), attr);
10406 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010407 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 MSG_PUTS_ATTR(_(" (insert)"), attr);
10409 else if (restart_edit == 'R')
10410 MSG_PUTS_ATTR(_(" (replace)"), attr);
10411 else if (restart_edit == 'V')
10412 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10413#ifdef FEAT_RIGHTLEFT
10414 if (p_hkmap)
10415 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10416# ifdef FEAT_FKMAP
10417 if (p_fkmap)
10418 MSG_PUTS_ATTR(farsi_text_5, attr);
10419# endif
10420#endif
10421#ifdef FEAT_KEYMAP
10422 if (State & LANGMAP)
10423 {
10424# ifdef FEAT_ARABIC
10425 if (curwin->w_p_arab)
10426 MSG_PUTS_ATTR(_(" Arabic"), attr);
10427 else
10428# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010429 if (get_keymap_str(curwin, (char_u *)" (%s)",
10430 NameBuff, MAXPATHL))
10431 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 }
10433#endif
10434 if ((State & INSERT) && p_paste)
10435 MSG_PUTS_ATTR(_(" (paste)"), attr);
10436
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 if (VIsual_active)
10438 {
10439 char *p;
10440
10441 /* Don't concatenate separate words to avoid translation
10442 * problems. */
10443 switch ((VIsual_select ? 4 : 0)
10444 + (VIsual_mode == Ctrl_V) * 2
10445 + (VIsual_mode == 'V'))
10446 {
10447 case 0: p = N_(" VISUAL"); break;
10448 case 1: p = N_(" VISUAL LINE"); break;
10449 case 2: p = N_(" VISUAL BLOCK"); break;
10450 case 4: p = N_(" SELECT"); break;
10451 case 5: p = N_(" SELECT LINE"); break;
10452 default: p = N_(" SELECT BLOCK"); break;
10453 }
10454 MSG_PUTS_ATTR(_(p), attr);
10455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 MSG_PUTS_ATTR(" --", attr);
10457 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010458
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 need_clear = TRUE;
10460 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010461 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462#ifdef FEAT_INS_EXPAND
10463 && edit_submode == NULL /* otherwise it gets too long */
10464#endif
10465 )
10466 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010467 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 need_clear = TRUE;
10469 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010470
10471 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 if (need_clear || clear_cmdline)
10473 msg_clr_eos();
10474 msg_didout = FALSE; /* overwrite this message */
10475 length = msg_col;
10476 msg_col = 0;
10477 need_wait_return = nwr_save; /* never ask for hit-return for this */
10478 }
10479 else if (clear_cmdline && msg_silent == 0)
10480 /* Clear the whole command line. Will reset "clear_cmdline". */
10481 msg_clr_cmdline();
10482
10483#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 /* In Visual mode the size of the selected area must be redrawn. */
10485 if (VIsual_active)
10486 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487
10488 /* If the last window has no status line, the ruler is after the mode
10489 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010490 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010491 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492#endif
10493 redraw_cmdline = FALSE;
10494 clear_cmdline = FALSE;
10495
10496 return length;
10497}
10498
10499/*
10500 * Position for a mode message.
10501 */
10502 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010503msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504{
10505 msg_col = 0;
10506 msg_row = Rows - 1;
10507}
10508
10509/*
10510 * Delete mode message. Used when ESC is typed which is expected to end
10511 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010512 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 */
10514 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010515unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516{
10517 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010518 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 */
10520 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10521 redraw_cmdline = TRUE; /* delete mode later */
10522 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010523 clearmode();
10524}
10525
10526/*
10527 * Clear the mode message.
10528 */
10529 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010530clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010531{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010532 int save_msg_row = msg_row;
10533 int save_msg_col = msg_col;
10534
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010535 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010536 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010537 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010538 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010539
10540 msg_col = save_msg_col;
10541 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542}
10543
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010544 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010545recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010546{
10547 MSG_PUTS_ATTR(_("recording"), attr);
10548 if (!shortmess(SHM_RECORDING))
10549 {
10550 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010551 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010552 MSG_PUTS_ATTR(s, attr);
10553 }
10554}
10555
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010556/*
10557 * Draw the tab pages line at the top of the Vim window.
10558 */
10559 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010560draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010561{
10562 int tabcount = 0;
10563 tabpage_T *tp;
10564 int tabwidth;
10565 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010566 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010567 int attr;
10568 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010569 win_T *cwp;
10570 int wincount;
10571 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010572 int c;
10573 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010574 int attr_sel = HL_ATTR(HLF_TPS);
10575 int attr_nosel = HL_ATTR(HLF_TP);
10576 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010577 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010578 int room;
10579 int use_sep_chars = (t_colors < 8
10580#ifdef FEAT_GUI
10581 && !gui.in_use
10582#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010583#ifdef FEAT_TERMGUICOLORS
10584 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010585#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010586 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010587
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010588 if (ScreenLines == NULL)
10589 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010590 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010591
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010592#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010593 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010594 if (gui_use_tabline())
10595 {
10596 gui_update_tabline();
10597 return;
10598 }
10599#endif
10600
10601 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010602 return;
10603
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010604#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010605
10606 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10607 for (scol = 0; scol < Columns; ++scol)
10608 TabPageIdxs[scol] = 0;
10609
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010610 /* Use the 'tabline' option if it's set. */
10611 if (*p_tal != NUL)
10612 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010613 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010614
10615 /* Check for an error. If there is one we would loop in redrawing the
10616 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010617 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010618 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010619 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010620 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010621 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010622 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010623 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010624 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010625#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010626 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010627 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010628 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010629
Bram Moolenaar238a5642006-02-21 22:12:05 +000010630 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10631 if (tabwidth < 6)
10632 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010633
Bram Moolenaar238a5642006-02-21 22:12:05 +000010634 attr = attr_nosel;
10635 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010636 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010637 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10638 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010639 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010640 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010641
Bram Moolenaar238a5642006-02-21 22:12:05 +000010642 if (tp->tp_topframe == topframe)
10643 attr = attr_sel;
10644 if (use_sep_chars && col > 0)
10645 screen_putchar('|', 0, col++, attr);
10646
10647 if (tp->tp_topframe != topframe)
10648 attr = attr_nosel;
10649
10650 screen_putchar(' ', 0, col++, attr);
10651
10652 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010653 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010654 cwp = curwin;
10655 wp = firstwin;
10656 }
10657 else
10658 {
10659 cwp = tp->tp_curwin;
10660 wp = tp->tp_firstwin;
10661 }
10662
10663 modified = FALSE;
10664 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10665 if (bufIsChanged(wp->w_buffer))
10666 modified = TRUE;
10667 if (modified || wincount > 1)
10668 {
10669 if (wincount > 1)
10670 {
10671 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010672 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010673 if (col + len >= Columns - 3)
10674 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010675 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010676#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010677 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010678#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010679 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010680#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010681 );
10682 col += len;
10683 }
10684 if (modified)
10685 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10686 screen_putchar(' ', 0, col++, attr);
10687 }
10688
10689 room = scol - col + tabwidth - 1;
10690 if (room > 0)
10691 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010692 /* Get buffer name in NameBuff[] */
10693 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010694 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010695 len = vim_strsize(NameBuff);
10696 p = NameBuff;
10697#ifdef FEAT_MBYTE
10698 if (has_mbyte)
10699 while (len > room)
10700 {
10701 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010702 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010703 }
10704 else
10705#endif
10706 if (len > room)
10707 {
10708 p += len - room;
10709 len = room;
10710 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010711 if (len > Columns - col - 1)
10712 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010713
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010714 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010715 col += len;
10716 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010717 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010718
10719 /* Store the tab page number in TabPageIdxs[], so that
10720 * jump_to_mouse() knows where each one is. */
10721 ++tabcount;
10722 while (scol < col)
10723 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010724 }
10725
Bram Moolenaar238a5642006-02-21 22:12:05 +000010726 if (use_sep_chars)
10727 c = '_';
10728 else
10729 c = ' ';
10730 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010731
10732 /* Put an "X" for closing the current tab if there are several. */
10733 if (first_tabpage->tp_next != NULL)
10734 {
10735 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10736 TabPageIdxs[Columns - 1] = -999;
10737 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010738 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010739
10740 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10741 * set. */
10742 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010743}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010744
10745/*
10746 * Get buffer name for "buf" into NameBuff[].
10747 * Takes care of special buffer names and translates special characters.
10748 */
10749 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010750get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010751{
10752 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010753 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010754 else
10755 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10756 trans_characters(NameBuff, MAXPATHL);
10757}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010758
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759/*
10760 * Get the character to use in a status line. Get its attributes in "*attr".
10761 */
10762 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010763fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764{
10765 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010766
10767#ifdef FEAT_TERMINAL
10768 if (bt_terminal(wp->w_buffer))
10769 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010770 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010771 {
10772 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010773 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010774 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010775 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010776 {
10777 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010778 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010779 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010780 }
10781 else
10782#endif
10783 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010785 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 fill = fill_stl;
10787 }
10788 else
10789 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010790 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 fill = fill_stlnc;
10792 }
10793 /* Use fill when there is highlighting, and highlighting of current
10794 * window differs, or the fillchars differ, or this is not the
10795 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010796 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010797 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 || (fill_stl != fill_stlnc)))
10799 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010800 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 return '^';
10802 return '=';
10803}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805/*
10806 * Get the character to use in a separator between vertically split windows.
10807 * Get its attributes in "*attr".
10808 */
10809 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010810fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010812 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813 if (*attr == 0 && fill_vert == ' ')
10814 return '|';
10815 else
10816 return fill_vert;
10817}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818
10819/*
10820 * Return TRUE if redrawing should currently be done.
10821 */
10822 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010823redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010825#ifdef FEAT_EVAL
10826 if (disable_redraw_for_testing)
10827 return 0;
10828 else
10829#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010830 return ((!RedrawingDisabled
10831#ifdef FEAT_EVAL
10832 || ignore_redraw_flag_for_testing
10833#endif
10834 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835}
10836
10837/*
10838 * Return TRUE if printing messages should currently be done.
10839 */
10840 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010841messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842{
10843 return (!(p_lz && char_avail() && !KeyTyped));
10844}
10845
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010846#ifdef FEAT_MENU
10847/*
10848 * Draw the window toolbar.
10849 */
10850 static void
10851redraw_win_toolbar(win_T *wp)
10852{
10853 vimmenu_T *menu;
10854 int item_idx = 0;
10855 int item_count = 0;
10856 int col = 0;
10857 int next_col;
10858 int off = (int)(current_ScreenLine - ScreenLines);
10859 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10860 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10861
10862 vim_free(wp->w_winbar_items);
10863 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10864 ++item_count;
10865 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10866 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10867
10868 /* TODO: use fewer spaces if there is not enough room */
10869 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010870 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010871 {
10872 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010873 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010874 break;
10875 if (col > 1)
10876 {
10877 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010878 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010879 break;
10880 }
10881
10882 wp->w_winbar_items[item_idx].wb_startcol = col;
10883 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010884 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010885 break;
10886
10887 next_col = text_to_screenline(wp, menu->name, col);
10888 while (col < next_col)
10889 {
10890 ScreenAttrs[off + col] = button_attr;
10891 ++col;
10892 }
10893 wp->w_winbar_items[item_idx].wb_endcol = col;
10894 wp->w_winbar_items[item_idx].wb_menu = menu;
10895 ++item_idx;
10896
Bram Moolenaar02631462017-09-22 15:20:32 +020010897 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010898 break;
10899 space_to_screenline(off + col, button_attr);
10900 ++col;
10901 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010902 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010903 {
10904 space_to_screenline(off + col, fill_attr);
10905 ++col;
10906 }
10907 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10908
Bram Moolenaar02631462017-09-22 15:20:32 +020010909 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10910 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010911}
10912#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010913
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914/*
10915 * Show current status info in ruler and various other places
10916 * If always is FALSE, only show ruler if position has changed.
10917 */
10918 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010919showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920{
10921 if (!always && !redrawing())
10922 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010923#ifdef FEAT_INS_EXPAND
10924 if (pum_visible())
10925 {
10926 /* Don't redraw right now, do it later. */
10927 curwin->w_redr_status = TRUE;
10928 return;
10929 }
10930#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010931#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010932 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010933 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010934 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 else
10937#endif
10938#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010939 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940#endif
10941
10942#ifdef FEAT_TITLE
10943 if (need_maketitle
10944# ifdef FEAT_STL_OPT
10945 || (p_icon && (stl_syntax & STL_IN_ICON))
10946 || (p_title && (stl_syntax & STL_IN_TITLE))
10947# endif
10948 )
10949 maketitle();
10950#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010951 /* Redraw the tab pages line if needed. */
10952 if (redraw_tabline)
10953 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954}
10955
10956#ifdef FEAT_CMDL_INFO
10957 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010958win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010960#define RULER_BUF_LEN 70
10961 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 int row;
10963 int fillchar;
10964 int attr;
10965 int empty_line = FALSE;
10966 colnr_T virtcol;
10967 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010968 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 int this_ru_col;
10971 int off = 0;
10972 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973
10974 /* If 'ruler' off or redrawing disabled, don't do anything */
10975 if (!p_ru)
10976 return;
10977
10978 /*
10979 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10980 * after deleting lines, before cursor.lnum is corrected.
10981 */
10982 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10983 return;
10984
10985#ifdef FEAT_INS_EXPAND
10986 /* Don't draw the ruler while doing insert-completion, it might overwrite
10987 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 if (edit_submode != NULL)
10990 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010991 // Don't draw the ruler when the popup menu is visible, it may overlap.
10992 // Except when the popup menu will be redrawn anyway.
10993 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010994 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995#endif
10996
10997#ifdef FEAT_STL_OPT
10998 if (*p_ruf)
10999 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011000 int save_called_emsg = called_emsg;
11001
11002 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011004 if (called_emsg)
11005 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011006 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011007 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 return;
11009 }
11010#endif
11011
11012 /*
11013 * Check if not in Insert mode and the line is empty (will show "0-1").
11014 */
11015 if (!(State & INSERT)
11016 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11017 empty_line = TRUE;
11018
11019 /*
11020 * Only draw the ruler when something changed.
11021 */
11022 validate_virtcol_win(wp);
11023 if ( redraw_cmdline
11024 || always
11025 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11026 || wp->w_cursor.col != wp->w_ru_cursor.col
11027 || wp->w_virtcol != wp->w_ru_virtcol
11028#ifdef FEAT_VIRTUALEDIT
11029 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11030#endif
11031 || wp->w_topline != wp->w_ru_topline
11032 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11033#ifdef FEAT_DIFF
11034 || wp->w_topfill != wp->w_ru_topfill
11035#endif
11036 || empty_line != wp->w_ru_empty)
11037 {
11038 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 if (wp->w_status_height)
11040 {
11041 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011042 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011043 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011044 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 }
11046 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 {
11048 row = Rows - 1;
11049 fillchar = ' ';
11050 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 width = Columns;
11052 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 }
11054
11055 /* In list mode virtcol needs to be recomputed */
11056 virtcol = wp->w_virtcol;
11057 if (wp->w_p_list && lcs_tab1 == NUL)
11058 {
11059 wp->w_p_list = FALSE;
11060 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11061 wp->w_p_list = TRUE;
11062 }
11063
11064 /*
11065 * Some sprintfs return the length, some return a pointer.
11066 * To avoid portability problems we use strlen() here.
11067 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011068 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11070 ? 0L
11071 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011072 len = STRLEN(buffer);
11073 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11075 (int)virtcol + 1);
11076
11077 /*
11078 * Add a "50%" if there is room for it.
11079 * On the last line, don't print in the last column (scrolls the
11080 * screen up on some terminals).
11081 */
11082 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011083 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087 this_ru_col = ru_col - (Columns - width);
11088 if (this_ru_col < 0)
11089 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 /* Never use more than half the window/screen width, leave the other
11091 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011092 if (this_ru_col < (width + 1) / 2)
11093 this_ru_col = (width + 1) / 2;
11094 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011096 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011097 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 {
11099#ifdef FEAT_MBYTE
11100 if (has_mbyte)
11101 i += (*mb_char2bytes)(fillchar, buffer + i);
11102 else
11103#endif
11104 buffer[i++] = fillchar;
11105 ++o;
11106 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011107 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 }
11109 /* Truncate at window boundary. */
11110#ifdef FEAT_MBYTE
11111 if (has_mbyte)
11112 {
11113 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011114 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 {
11116 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011117 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 {
11119 buffer[i] = NUL;
11120 break;
11121 }
11122 }
11123 }
11124 else
11125#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011126 if (this_ru_col + (int)STRLEN(buffer) > width)
11127 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128
Bram Moolenaar4033c552017-09-16 20:54:51 +020011129 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 i = redraw_cmdline;
11131 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011132 this_ru_col + off + (int)STRLEN(buffer),
11133 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134 fillchar, fillchar, attr);
11135 /* don't redraw the cmdline because of showing the ruler */
11136 redraw_cmdline = i;
11137 wp->w_ru_cursor = wp->w_cursor;
11138 wp->w_ru_virtcol = wp->w_virtcol;
11139 wp->w_ru_empty = empty_line;
11140 wp->w_ru_topline = wp->w_topline;
11141 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11142#ifdef FEAT_DIFF
11143 wp->w_ru_topfill = wp->w_topfill;
11144#endif
11145 }
11146}
11147#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011148
11149#if defined(FEAT_LINEBREAK) || defined(PROTO)
11150/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011151 * Return the width of the 'number' and 'relativenumber' column.
11152 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011153 * Otherwise it depends on 'numberwidth' and the line count.
11154 */
11155 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011156number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011157{
11158 int n;
11159 linenr_T lnum;
11160
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011161 if (wp->w_p_rnu && !wp->w_p_nu)
11162 /* cursor line shows "0" */
11163 lnum = wp->w_height;
11164 else
11165 /* cursor line shows absolute line number */
11166 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011167
Bram Moolenaar6b314672015-03-20 15:42:10 +010011168 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011169 return wp->w_nrwidth_width;
11170 wp->w_nrwidth_line_count = lnum;
11171
11172 n = 0;
11173 do
11174 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011175 lnum /= 10;
11176 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011177 } while (lnum > 0);
11178
11179 /* 'numberwidth' gives the minimal width plus one */
11180 if (n < wp->w_p_nuw - 1)
11181 n = wp->w_p_nuw - 1;
11182
11183 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011184 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011185 return n;
11186}
11187#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011188
11189/*
11190 * Return the current cursor column. This is the actual position on the
11191 * screen. First column is 0.
11192 */
11193 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011194screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011195{
11196 return screen_cur_col;
11197}
11198
11199/*
11200 * Return the current cursor row. This is the actual position on the screen.
11201 * First row is 0.
11202 */
11203 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011204screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011205{
11206 return screen_cur_row;
11207}