blob: 880d78407bd412a63ba6f65f646d2e134299073d [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 Moolenaar3633cf52017-07-31 22:29:35 +0200157static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100158static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200159#ifdef FEAT_MENU
160static void redraw_win_toolbar(win_T *wp);
161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#endif
165#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200166static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#endif
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169/* Ugly global: overrule attribute used by screen_char() */
170static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200172#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
173/* Can limit syntax highlight time to 'redrawtime'. */
174# define SYN_TIME_LIMIT 1
175#endif
176
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200177#ifdef FEAT_RIGHTLEFT
178# define HAS_RIGHTLEFT(x) x
179#else
180# define HAS_RIGHTLEFT(x) FALSE
181#endif
182
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183/*
184 * Redraw the current window later, with update_screen(type).
185 * Set must_redraw only if not already set to a higher value.
186 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
187 */
188 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100189redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190{
191 redraw_win_later(curwin, type);
192}
193
194 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100195redraw_win_later(
196 win_T *wp,
197 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200199 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 {
201 wp->w_redr_type = type;
202 if (type >= NOT_VALID)
203 wp->w_lines_valid = 0;
204 if (must_redraw < type) /* must_redraw is the maximum of all windows */
205 must_redraw = type;
206 }
207}
208
209/*
210 * Force a complete redraw later. Also resets the highlighting. To be used
211 * after executing a shell command that messes up the screen.
212 */
213 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100214redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215{
216 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000217#ifdef FEAT_GUI
218 if (gui.in_use)
219 /* Use a code that will reset gui.highlight_mask in
220 * gui_stop_highlight(). */
221 screen_attr = HL_ALL + 1;
222 else
223#endif
224 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200225 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226}
227
228/*
229 * Mark all windows to be redrawn later.
230 */
231 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100232redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 win_T *wp;
235
236 FOR_ALL_WINDOWS(wp)
237 {
238 redraw_win_later(wp, type);
239 }
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100240 // This may be needed when switching tabs.
241 if (must_redraw < type)
242 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243}
244
245/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000246 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 */
248 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100249redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250{
251 redraw_buf_later(curbuf, type);
252}
253
254 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100255redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256{
257 win_T *wp;
258
259 FOR_ALL_WINDOWS(wp)
260 {
261 if (wp->w_buffer == buf)
262 redraw_win_later(wp, type);
263 }
264}
265
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200266 void
267redraw_buf_and_status_later(buf_T *buf, int type)
268{
269 win_T *wp;
270
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200271#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200272 if (wild_menu_showing != 0)
273 /* Don't redraw while the command line completion is displayed, it
274 * would disappear. */
275 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200276#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200277 FOR_ALL_WINDOWS(wp)
278 {
279 if (wp->w_buffer == buf)
280 {
281 redraw_win_later(wp, type);
282 wp->w_redr_status = TRUE;
283 }
284 }
285}
286
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200288 * Redraw as soon as possible. When the command line is not scrolled redraw
289 * right away and restore what was on the command line.
290 * Return a code indicating what happened.
291 */
292 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100293redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200294{
295 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200296 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200297 int r;
298 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200299 schar_T *screenline; /* copy from ScreenLines[] */
300 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200301#ifdef FEAT_MBYTE
302 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200303 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200304 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200305 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306#endif
307
308 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200309 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200310 return ret;
311
312 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200315 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200317 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200318 if (screenline == NULL || screenattr == NULL)
319 ret = 2;
320#ifdef FEAT_MBYTE
321 if (enc_utf8)
322 {
323 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200324 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200325 if (screenlineUC == NULL)
326 ret = 2;
327 for (i = 0; i < p_mco; ++i)
328 {
329 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200330 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200331 if (screenlineC[i] == NULL)
332 ret = 2;
333 }
334 }
335 if (enc_dbcs == DBCS_JPNU)
336 {
337 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200338 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200339 if (screenline2 == NULL)
340 ret = 2;
341 }
342#endif
343
344 if (ret != 2)
345 {
346 /* Save the text displayed in the command line area. */
347 for (r = 0; r < rows; ++r)
348 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200349 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200350 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200351 (size_t)cols * sizeof(schar_T));
352 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200353 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200354 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200355#ifdef FEAT_MBYTE
356 if (enc_utf8)
357 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200359 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200360 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200361 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200362 mch_memmove(screenlineC[i] + r * cols,
363 ScreenLinesC[i] + LineOffset[cmdline_row + r],
364 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 }
366 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370#endif
371 }
372
373 update_screen(0);
374 ret = 3;
375
376 if (must_redraw == 0)
377 {
378 int off = (int)(current_ScreenLine - ScreenLines);
379
380 /* Restore the text displayed in the command line area. */
381 for (r = 0; r < rows; ++r)
382 {
383 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200384 screenline + r * cols,
385 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200386 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200387 screenattr + r * cols,
388 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200389#ifdef FEAT_MBYTE
390 if (enc_utf8)
391 {
392 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200393 screenlineUC + r * cols,
394 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200395 for (i = 0; i < p_mco; ++i)
396 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200397 screenlineC[i] + r * cols,
398 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200399 }
400 if (enc_dbcs == DBCS_JPNU)
401 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200402 screenline2 + r * cols,
403 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200404#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200405 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200406 }
407 ret = 4;
408 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200409 }
410
411 vim_free(screenline);
412 vim_free(screenattr);
413#ifdef FEAT_MBYTE
414 if (enc_utf8)
415 {
416 vim_free(screenlineUC);
417 for (i = 0; i < p_mco; ++i)
418 vim_free(screenlineC[i]);
419 }
420 if (enc_dbcs == DBCS_JPNU)
421 vim_free(screenline2);
422#endif
423
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200424 /* Show the intro message when appropriate. */
425 maybe_intro_message();
426
427 setcursor();
428
Bram Moolenaar2951b772013-07-03 12:45:31 +0200429 return ret;
430}
431
432/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100433 * Invoked after an asynchronous callback is called.
434 * If an echo command was used the cursor needs to be put back where
435 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200436 * If "call_update_screen" is FALSE don't call update_screen() when at the
437 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100438 */
439 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200440redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100441{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200442 ++redrawing_for_callback;
443
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100444 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200445 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100446 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200447 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200448 // Don't redraw when in prompt_for_number().
449 if (cmdline_row > 0)
450 {
451 // Redrawing only works when the screen didn't scroll. Don't clear
452 // wildmenu entries.
453 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200454#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200455 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200456#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200457 && call_update_screen)
458 update_screen(0);
459
460 // Redraw in the same position, so that the user can continue
461 // editing the command.
462 redrawcmdline_ex(FALSE);
463 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200464 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200465 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100466 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200467 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200468 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100469 setcursor();
470 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100471 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100472#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100473 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200474 // Don't update the cursor when it is blinking and off to avoid
475 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100476 out_flush_cursor(FALSE, FALSE);
477 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100478#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100479 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200480
481 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100482}
483
484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 * Changed something in the current window, at buffer line "lnum", that
486 * requires that line and possibly other lines to be redrawn.
487 * Used when entering/leaving Insert mode with the cursor on a folded line.
488 * Used to remove the "$" from a change command.
489 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
490 * may become invalid and the whole window will have to be redrawn.
491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100493redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200494 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100495 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200497 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
498 wp->w_redraw_top = lnum;
499 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
500 wp->w_redraw_bot = lnum;
501 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502}
503
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200504 void
505reset_updating_screen(int may_resize_shell UNUSED)
506{
507 updating_screen = FALSE;
508#ifdef FEAT_GUI
509 if (may_resize_shell)
510 gui_may_resize_shell();
511#endif
512#ifdef FEAT_TERMINAL
513 term_check_channel_closed_recently();
514#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200515
516#ifdef HAVE_DROP_FILE
517 // If handle_drop() was called while updating_screen was TRUE need to
518 // handle the drop now.
519 handle_any_postponed_drop();
520#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200521}
522
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200524 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 */
526 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100527update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528{
529 redraw_curbuf_later(type);
530 update_screen(type);
531}
532
533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 * Based on the current value of curwin->w_topline, transfer a screenfull
535 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200536 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200538 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200539update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200541 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 win_T *wp;
543 static int did_intro = FALSE;
544#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
545 int did_one;
546#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200547#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200548 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200549 int gui_cursor_col;
550 int gui_cursor_row;
551#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200552 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000554 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200556 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200558 if (type == VALID_NO_UPDATE)
559 {
560 no_update = TRUE;
561 type = 0;
562 }
563
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 if (must_redraw)
565 {
566 if (type < must_redraw) /* use maximal type */
567 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000568
569 /* must_redraw is reset here, so that when we run into some weird
570 * reason to redraw while busy redrawing (e.g., asynchronous
571 * scrolling), or update_topline() in win_update() will cause a
572 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 must_redraw = 0;
574 }
575
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200576 /* May need to update w_lines[]. */
577 if (curwin->w_lines_valid == 0 && type < NOT_VALID
578#ifdef FEAT_TERMINAL
579 && !term_do_update_window(curwin)
580#endif
581 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 type = NOT_VALID;
583
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000584 /* Postpone the redrawing when it's not needed and when being called
585 * recursively. */
586 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 {
588 redraw_later(type); /* remember type for next time */
589 must_redraw = type;
590 if (type > INVERTED_ALL)
591 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200592 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 }
594
595 updating_screen = TRUE;
596#ifdef FEAT_SYN_HL
597 ++display_tick; /* let syntax code know we're in a next round of
598 * display updating */
599#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200600 if (no_update)
601 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602
603 /*
604 * if the screen was scrolled up when displaying a message, scroll it down
605 */
606 if (msg_scrolled)
607 {
608 clear_cmdline = TRUE;
609 if (msg_scrolled > Rows - 5) /* clearing is faster */
610 type = CLEAR;
611 else if (type != CLEAR)
612 {
613 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200614 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
615 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 type = CLEAR;
617 FOR_ALL_WINDOWS(wp)
618 {
619 if (W_WINROW(wp) < msg_scrolled)
620 {
621 if (W_WINROW(wp) + wp->w_height > msg_scrolled
622 && wp->w_redr_type < REDRAW_TOP
623 && wp->w_lines_valid > 0
624 && wp->w_topline == wp->w_lines[0].wl_lnum)
625 {
626 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
627 wp->w_redr_type = REDRAW_TOP;
628 }
629 else
630 {
631 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200632 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
633 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 }
636 }
637 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200638 if (!no_update)
639 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000640 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 }
642 msg_scrolled = 0;
643 need_wait_return = FALSE;
644 }
645
646 /* reset cmdline_row now (may have been changed temporarily) */
647 compute_cmdrow();
648
649 /* Check for changed highlighting */
650 if (need_highlight_changed)
651 highlight_changed();
652
653 if (type == CLEAR) /* first clear screen */
654 {
655 screenclear(); /* will reset clear_cmdline */
656 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200657 /* must_redraw may be set indirectly, avoid another redraw later */
658 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
660
661 if (clear_cmdline) /* going to clear cmdline (done below) */
662 check_for_delay(FALSE);
663
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000664#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200665 /* Force redraw when width of 'number' or 'relativenumber' column
666 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000667 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200668 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
669 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000670 curwin->w_redr_type = NOT_VALID;
671#endif
672
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 /*
674 * Only start redrawing if there is really something to do.
675 */
676 if (type == INVERTED)
677 update_curswant();
678 if (curwin->w_redr_type < type
679 && !((type == VALID
680 && curwin->w_lines[0].wl_valid
681#ifdef FEAT_DIFF
682 && curwin->w_topfill == curwin->w_old_topfill
683 && curwin->w_botfill == curwin->w_old_botfill
684#endif
685 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000687 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
689 && curwin->w_old_visual_mode == VIsual_mode
690 && (curwin->w_valid & VALID_VIRTCOL)
691 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 ))
693 curwin->w_redr_type = type;
694
Bram Moolenaar5a305422006-04-28 22:38:25 +0000695 /* Redraw the tab pages line if needed. */
696 if (redraw_tabline || type >= NOT_VALID)
697 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000698
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699#ifdef FEAT_SYN_HL
700 /*
701 * Correct stored syntax highlighting info for changes in each displayed
702 * buffer. Each buffer must only be done once.
703 */
704 FOR_ALL_WINDOWS(wp)
705 {
706 if (wp->w_buffer->b_mod_set)
707 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 win_T *wwp;
709
710 /* Check if we already did this buffer. */
711 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
712 if (wwp->w_buffer == wp->w_buffer)
713 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200714 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 syn_stack_apply_changes(wp->w_buffer);
716 }
717 }
718#endif
719
720 /*
721 * Go from top to bottom through the windows, redrawing the ones that need
722 * it.
723 */
724#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
725 did_one = FALSE;
726#endif
727#ifdef FEAT_SEARCH_EXTRA
728 search_hl.rm.regprog = NULL;
729#endif
730 FOR_ALL_WINDOWS(wp)
731 {
732 if (wp->w_redr_type != 0)
733 {
734 cursor_off();
735#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
736 if (!did_one)
737 {
738 did_one = TRUE;
739# ifdef FEAT_SEARCH_EXTRA
740 start_search_hl();
741# endif
742# ifdef FEAT_CLIPBOARD
743 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200744 if (clip_star.available && clip_isautosel_star())
745 clip_update_selection(&clip_star);
746 if (clip_plus.available && clip_isautosel_plus())
747 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748# endif
749#ifdef FEAT_GUI
750 /* Remove the cursor before starting to do anything, because
751 * scrolling may make it difficult to redraw the text under
752 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200753 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200754 {
755 gui_cursor_col = gui.cursor_col;
756 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200758 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760#endif
761 }
762#endif
763 win_update(wp);
764 }
765
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 /* redraw status line after the window to minimize cursor movement */
767 if (wp->w_redr_status)
768 {
769 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200770 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 }
773#if defined(FEAT_SEARCH_EXTRA)
774 end_search_hl();
775#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100776#ifdef FEAT_INS_EXPAND
777 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200778 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 /* Reset b_mod_set flags. Going through all windows is probably faster
782 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200783 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200786 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
788 /* Clear or redraw the command line. Done last, because scrolling may
789 * mess up the command line. */
790 if (clear_cmdline || redraw_cmdline)
791 showmode();
792
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200793 if (no_update)
794 --no_win_do_lines_ins;
795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200797 if (!did_intro)
798 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 did_intro = TRUE;
800
801#ifdef FEAT_GUI
802 /* Redraw the cursor and update the scrollbars when all screen updating is
803 * done. */
804 if (gui.in_use)
805 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200806 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200807 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100808 mch_disable_flush();
809 out_flush(); /* required before updating the cursor */
810 mch_enable_flush();
811
Bram Moolenaar144445d2016-07-08 21:41:54 +0200812 /* Put the GUI position where the cursor was, gui_update_cursor()
813 * uses that. */
814 gui.col = gui_cursor_col;
815 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200816# ifdef FEAT_MBYTE
817 gui.col = mb_fix_col(gui.col, gui.row);
818# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100820 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200821 screen_cur_col = gui.col;
822 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200823 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100824 else
825 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 gui_update_scrollbars(FALSE);
827 }
828#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200829 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830}
831
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100832#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
833/*
834 * Prepare for updating one or more windows.
835 * Caller must check for "updating_screen" already set to avoid recursiveness.
836 */
837 static void
838update_prepare(void)
839{
840 cursor_off();
841 updating_screen = TRUE;
842#ifdef FEAT_GUI
843 /* Remove the cursor before starting to do anything, because scrolling may
844 * make it difficult to redraw the text under it. */
845 if (gui.in_use)
846 gui_undraw_cursor();
847#endif
848#ifdef FEAT_SEARCH_EXTRA
849 start_search_hl();
850#endif
851}
852
853/*
854 * Finish updating one or more windows.
855 */
856 static void
857update_finish(void)
858{
859 if (redraw_cmdline)
860 showmode();
861
862# ifdef FEAT_SEARCH_EXTRA
863 end_search_hl();
864# endif
865
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200866 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100867
868# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100869 /* Redraw the cursor and update the scrollbars when all screen updating is
870 * done. */
871 if (gui.in_use)
872 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100873 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100874 gui_update_scrollbars(FALSE);
875 }
876# endif
877}
878#endif
879
Bram Moolenaar860cae12010-06-05 23:22:07 +0200880#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200881/*
882 * Return TRUE if the cursor line in window "wp" may be concealed, according
883 * to the 'concealcursor' option.
884 */
885 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100886conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200887{
888 int c;
889
890 if (*wp->w_p_cocu == NUL)
891 return FALSE;
892 if (get_real_state() & VISUAL)
893 c = 'v';
894 else if (State & INSERT)
895 c = 'i';
896 else if (State & NORMAL)
897 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200898 else if (State & CMDLINE)
899 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200900 else
901 return FALSE;
902 return vim_strchr(wp->w_p_cocu, c) != NULL;
903}
904
905/*
906 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
907 */
908 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200909conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200910{
911 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
912 {
913 need_cursor_line_redraw = TRUE;
914 /* Need to recompute cursor column, e.g., when starting Visual mode
915 * without concealing. */
916 curs_columns(TRUE);
917 }
918}
919
Bram Moolenaar860cae12010-06-05 23:22:07 +0200920 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100921update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200922{
923 int row;
924 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200925#ifdef SYN_TIME_LIMIT
926 proftime_T syntax_tm;
927#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200928
Bram Moolenaar908be432016-05-24 10:51:30 +0200929 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100930 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200931 return;
932
Bram Moolenaar860cae12010-06-05 23:22:07 +0200933 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200934 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200935 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200936#ifdef SYN_TIME_LIMIT
937 /* Set the time limit to 'redrawtime'. */
938 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200939 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200940#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100941 update_prepare();
942
Bram Moolenaar860cae12010-06-05 23:22:07 +0200943 row = 0;
944 for (j = 0; j < wp->w_lines_valid; ++j)
945 {
946 if (lnum == wp->w_lines[j].wl_lnum)
947 {
948 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200949# ifdef FEAT_SEARCH_EXTRA
950 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200951 prepare_search_hl(wp, lnum);
952# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200953 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size,
954 FALSE, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200955 break;
956 }
957 row += wp->w_lines[j].wl_size;
958 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100959
960 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200961
962#ifdef SYN_TIME_LIMIT
963 syn_set_timeout(NULL);
964#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200965 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200966 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967}
968#endif
969
970#if defined(FEAT_SIGNS) || defined(PROTO)
971 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100972update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973{
974 win_T *wp;
975 int doit = FALSE;
976
977# ifdef FEAT_FOLDING
978 win_foldinfo.fi_level = 0;
979# endif
980
981 /* update/delete a specific mark */
982 FOR_ALL_WINDOWS(wp)
983 {
984 if (buf != NULL && lnum > 0)
985 {
986 if (wp->w_buffer == buf && lnum >= wp->w_topline
987 && lnum < wp->w_botline)
988 {
989 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
990 wp->w_redraw_top = lnum;
991 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
992 wp->w_redraw_bot = lnum;
993 redraw_win_later(wp, VALID);
994 }
995 }
996 else
997 redraw_win_later(wp, VALID);
998 if (wp->w_redr_type != 0)
999 doit = TRUE;
1000 }
1001
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001002 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001003 * happening (recursive call), messages on the screen or still starting up.
1004 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001005 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001006 || State == ASKMORE || State == HITRETURN
1007 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001008#ifdef FEAT_GUI
1009 || gui.starting
1010#endif
1011 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 return;
1013
1014 /* update all windows that need updating */
1015 update_prepare();
1016
Bram Moolenaar29323592016-07-24 22:04:11 +02001017 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 {
1019 if (wp->w_redr_type != 0)
1020 win_update(wp);
1021 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001022 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024
1025 update_finish();
1026}
1027#endif
1028
1029
1030#if defined(FEAT_GUI) || defined(PROTO)
1031/*
1032 * Update a single window, its status line and maybe the command line msg.
1033 * Used for the GUI scrollbar.
1034 */
1035 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001036updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001038 /* return if already busy updating */
1039 if (updating_screen)
1040 return;
1041
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 update_prepare();
1043
1044#ifdef FEAT_CLIPBOARD
1045 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001046 if (clip_star.available && clip_isautosel_star())
1047 clip_update_selection(&clip_star);
1048 if (clip_plus.available && clip_isautosel_plus())
1049 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001053
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001054 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001055 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001056 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001057
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 if (wp->w_redr_status
1059# ifdef FEAT_CMDL_INFO
1060 || p_ru
1061# endif
1062# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001063 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064# endif
1065 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001066 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067
1068 update_finish();
1069}
1070#endif
1071
1072/*
1073 * Update a single window.
1074 *
1075 * This may cause the windows below it also to be redrawn (when clearing the
1076 * screen or scrolling lines).
1077 *
1078 * How the window is redrawn depends on wp->w_redr_type. Each type also
1079 * implies the one below it.
1080 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001081 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1083 * INVERTED redraw the changed part of the Visual area
1084 * INVERTED_ALL redraw the whole Visual area
1085 * VALID 1. scroll up/down to adjust for a changed w_topline
1086 * 2. update lines at the top when scrolled down
1087 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001088 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 * b_mod_top and b_mod_bot.
1090 * - if wp->w_redraw_top non-zero, redraw lines between
1091 * wp->w_redraw_top and wp->w_redr_bot.
1092 * - continue redrawing when syntax status is invalid.
1093 * 4. if scrolled up, update lines at the bottom.
1094 * This results in three areas that may need updating:
1095 * top: from first row to top_end (when scrolled down)
1096 * mid: from mid_start to mid_end (update inversion or changed text)
1097 * bot: from bot_start to last row (when scrolled up)
1098 */
1099 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001100win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101{
1102 buf_T *buf = wp->w_buffer;
1103 int type;
1104 int top_end = 0; /* Below last row of the top area that needs
1105 updating. 0 when no top area updating. */
1106 int mid_start = 999;/* first row of the mid area that needs
1107 updating. 999 when no mid area updating. */
1108 int mid_end = 0; /* Below last row of the mid area that needs
1109 updating. 0 when no mid area updating. */
1110 int bot_start = 999;/* first row of the bot area that needs
1111 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 int scrolled_down = FALSE; /* TRUE when scrolled down when
1113 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001115 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 int top_to_mod = FALSE; /* redraw above mod_top */
1117#endif
1118
1119 int row; /* current window row to display */
1120 linenr_T lnum; /* current buffer lnum to display */
1121 int idx; /* current index in w_lines[] */
1122 int srow; /* starting row of the current line */
1123
1124 int eof = FALSE; /* if TRUE, we hit the end of the file */
1125 int didline = FALSE; /* if TRUE, we finished the last line */
1126 int i;
1127 long j;
1128 static int recursive = FALSE; /* being called recursively */
1129 int old_botline = wp->w_botline;
1130#ifdef FEAT_FOLDING
1131 long fold_count;
1132#endif
1133#ifdef FEAT_SYN_HL
1134 /* remember what happened to the previous line, to know if
1135 * check_visual_highlight() can be used */
1136#define DID_NONE 1 /* didn't update a line */
1137#define DID_LINE 2 /* updated a normal line */
1138#define DID_FOLD 3 /* updated a folded line */
1139 int did_update = DID_NONE;
1140 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1141#endif
1142 linenr_T mod_top = 0;
1143 linenr_T mod_bot = 0;
1144#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1145 int save_got_int;
1146#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001147#ifdef SYN_TIME_LIMIT
1148 proftime_T syntax_tm;
1149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
1151 type = wp->w_redr_type;
1152
1153 if (type == NOT_VALID)
1154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 wp->w_lines_valid = 0;
1157 }
1158
1159 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001160 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 {
1162 wp->w_redr_type = 0;
1163 return;
1164 }
1165
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 /* Window is zero-width: Only need to draw the separator. */
1167 if (wp->w_width == 0)
1168 {
1169 /* draw the vertical separator right of this window */
1170 draw_vsep_win(wp, 0);
1171 wp->w_redr_type = 0;
1172 return;
1173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001175#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001176 // If this window contains a terminal, redraw works completely differently.
1177 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001178 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001179 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001180# ifdef FEAT_MENU
1181 /* Draw the window toolbar, if there is one. */
1182 if (winbar_height(wp) > 0)
1183 redraw_win_toolbar(wp);
1184# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001185 wp->w_redr_type = 0;
1186 return;
1187 }
1188#endif
1189
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001191 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192#endif
1193
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001194#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001195 /* Force redraw when width of 'number' or 'relativenumber' column
1196 * changes. */
1197 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001198 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001199 {
1200 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001201 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001202 }
1203 else
1204#endif
1205
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1207 {
1208 /*
1209 * When there are both inserted/deleted lines and specific lines to be
1210 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1211 * everything (only happens when redrawing is off for while).
1212 */
1213 type = NOT_VALID;
1214 }
1215 else
1216 {
1217 /*
1218 * Set mod_top to the first line that needs displaying because of
1219 * changes. Set mod_bot to the first line after the changes.
1220 */
1221 mod_top = wp->w_redraw_top;
1222 if (wp->w_redraw_bot != 0)
1223 mod_bot = wp->w_redraw_bot + 1;
1224 else
1225 mod_bot = 0;
1226 wp->w_redraw_top = 0; /* reset for next time */
1227 wp->w_redraw_bot = 0;
1228 if (buf->b_mod_set)
1229 {
1230 if (mod_top == 0 || mod_top > buf->b_mod_top)
1231 {
1232 mod_top = buf->b_mod_top;
1233#ifdef FEAT_SYN_HL
1234 /* Need to redraw lines above the change that may be included
1235 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001236 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001238 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 if (mod_top < 1)
1240 mod_top = 1;
1241 }
1242#endif
1243 }
1244 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1245 mod_bot = buf->b_mod_bot;
1246
1247#ifdef FEAT_SEARCH_EXTRA
1248 /* When 'hlsearch' is on and using a multi-line search pattern, a
1249 * change in one line may make the Search highlighting in a
1250 * previous line invalid. Simple solution: redraw all visible
1251 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001252 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001254 if (search_hl.rm.regprog != NULL
1255 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001257 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001258 {
1259 cur = wp->w_match_head;
1260 while (cur != NULL)
1261 {
1262 if (cur->match.regprog != NULL
1263 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001264 {
1265 top_to_mod = TRUE;
1266 break;
1267 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001268 cur = cur->next;
1269 }
1270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271#endif
1272 }
1273#ifdef FEAT_FOLDING
1274 if (mod_top != 0 && hasAnyFolding(wp))
1275 {
1276 linenr_T lnumt, lnumb;
1277
1278 /*
1279 * A change in a line can cause lines above it to become folded or
1280 * unfolded. Find the top most buffer line that may be affected.
1281 * If the line was previously folded and displayed, get the first
1282 * line of that fold. If the line is folded now, get the first
1283 * folded line. Use the minimum of these two.
1284 */
1285
1286 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1287 * the line below it. If there is no valid entry, use w_topline.
1288 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1289 * to this line. If there is no valid entry, use MAXLNUM. */
1290 lnumt = wp->w_topline;
1291 lnumb = MAXLNUM;
1292 for (i = 0; i < wp->w_lines_valid; ++i)
1293 if (wp->w_lines[i].wl_valid)
1294 {
1295 if (wp->w_lines[i].wl_lastlnum < mod_top)
1296 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1297 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1298 {
1299 lnumb = wp->w_lines[i].wl_lnum;
1300 /* When there is a fold column it might need updating
1301 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001302 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 ++lnumb;
1304 }
1305 }
1306
1307 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1308 if (mod_top > lnumt)
1309 mod_top = lnumt;
1310
1311 /* Now do the same for the bottom line (one above mod_bot). */
1312 --mod_bot;
1313 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1314 ++mod_bot;
1315 if (mod_bot < lnumb)
1316 mod_bot = lnumb;
1317 }
1318#endif
1319
1320 /* When a change starts above w_topline and the end is below
1321 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001322 * If the end of the change is above w_topline: do like no change was
1323 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 if (mod_top != 0 && mod_top < wp->w_topline)
1325 {
1326 if (mod_bot > wp->w_topline)
1327 mod_top = wp->w_topline;
1328#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001329 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 top_end = 1;
1331#endif
1332 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001333
1334 /* When line numbers are displayed need to redraw all lines below
1335 * inserted/deleted lines. */
1336 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1337 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 }
1339
1340 /*
1341 * When only displaying the lines at the top, set top_end. Used when
1342 * window has scrolled down for msg_scrolled.
1343 */
1344 if (type == REDRAW_TOP)
1345 {
1346 j = 0;
1347 for (i = 0; i < wp->w_lines_valid; ++i)
1348 {
1349 j += wp->w_lines[i].wl_size;
1350 if (j >= wp->w_upd_rows)
1351 {
1352 top_end = j;
1353 break;
1354 }
1355 }
1356 if (top_end == 0)
1357 /* not found (cannot happen?): redraw everything */
1358 type = NOT_VALID;
1359 else
1360 /* top area defined, the rest is VALID */
1361 type = VALID;
1362 }
1363
Bram Moolenaar367329b2007-08-30 11:53:22 +00001364 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001365 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1366 * non-zero and thus not FALSE) will indicate that screenclear() was not
1367 * called. */
1368 if (screen_cleared)
1369 screen_cleared = MAYBE;
1370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 /*
1372 * If there are no changes on the screen that require a complete redraw,
1373 * handle three cases:
1374 * 1: we are off the top of the screen by a few lines: scroll down
1375 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1376 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1377 * w_lines[] that needs updating.
1378 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001379 if ((type == VALID || type == SOME_VALID
1380 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381#ifdef FEAT_DIFF
1382 && !wp->w_botfill && !wp->w_old_botfill
1383#endif
1384 )
1385 {
1386 if (mod_top != 0 && wp->w_topline == mod_top)
1387 {
1388 /*
1389 * w_topline is the first changed line, the scrolling will be done
1390 * further down.
1391 */
1392 }
1393 else if (wp->w_lines[0].wl_valid
1394 && (wp->w_topline < wp->w_lines[0].wl_lnum
1395#ifdef FEAT_DIFF
1396 || (wp->w_topline == wp->w_lines[0].wl_lnum
1397 && wp->w_topfill > wp->w_old_topfill)
1398#endif
1399 ))
1400 {
1401 /*
1402 * New topline is above old topline: May scroll down.
1403 */
1404#ifdef FEAT_FOLDING
1405 if (hasAnyFolding(wp))
1406 {
1407 linenr_T ln;
1408
1409 /* count the number of lines we are off, counting a sequence
1410 * of folded lines as one */
1411 j = 0;
1412 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1413 {
1414 ++j;
1415 if (j >= wp->w_height - 2)
1416 break;
1417 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1418 }
1419 }
1420 else
1421#endif
1422 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1423 if (j < wp->w_height - 2) /* not too far off */
1424 {
1425 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1426#ifdef FEAT_DIFF
1427 /* insert extra lines for previously invisible filler lines */
1428 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1429 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1430 - wp->w_old_topfill;
1431#endif
1432 if (i < wp->w_height - 2) /* less than a screen off */
1433 {
1434 /*
1435 * Try to insert the correct number of lines.
1436 * If not the last window, delete the lines at the bottom.
1437 * win_ins_lines may fail when the terminal can't do it.
1438 */
1439 if (i > 0)
1440 check_for_delay(FALSE);
1441 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1442 {
1443 if (wp->w_lines_valid != 0)
1444 {
1445 /* Need to update rows that are new, stop at the
1446 * first one that scrolled down. */
1447 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449
1450 /* Move the entries that were scrolled, disable
1451 * the entries for the lines to be redrawn. */
1452 if ((wp->w_lines_valid += j) > wp->w_height)
1453 wp->w_lines_valid = wp->w_height;
1454 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1455 wp->w_lines[idx] = wp->w_lines[idx - j];
1456 while (idx >= 0)
1457 wp->w_lines[idx--].wl_valid = FALSE;
1458 }
1459 }
1460 else
1461 mid_start = 0; /* redraw all lines */
1462 }
1463 else
1464 mid_start = 0; /* redraw all lines */
1465 }
1466 else
1467 mid_start = 0; /* redraw all lines */
1468 }
1469 else
1470 {
1471 /*
1472 * New topline is at or below old topline: May scroll up.
1473 * When topline didn't change, find first entry in w_lines[] that
1474 * needs updating.
1475 */
1476
1477 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1478 j = -1;
1479 row = 0;
1480 for (i = 0; i < wp->w_lines_valid; i++)
1481 {
1482 if (wp->w_lines[i].wl_valid
1483 && wp->w_lines[i].wl_lnum == wp->w_topline)
1484 {
1485 j = i;
1486 break;
1487 }
1488 row += wp->w_lines[i].wl_size;
1489 }
1490 if (j == -1)
1491 {
1492 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1493 * lines */
1494 mid_start = 0;
1495 }
1496 else
1497 {
1498 /*
1499 * Try to delete the correct number of lines.
1500 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1501 */
1502#ifdef FEAT_DIFF
1503 /* If the topline didn't change, delete old filler lines,
1504 * otherwise delete filler lines of the new topline... */
1505 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1506 row += wp->w_old_topfill;
1507 else
1508 row += diff_check_fill(wp, wp->w_topline);
1509 /* ... but don't delete new filler lines. */
1510 row -= wp->w_topfill;
1511#endif
1512 if (row > 0)
1513 {
1514 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001515 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1516 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 bot_start = wp->w_height - row;
1518 else
1519 mid_start = 0; /* redraw all lines */
1520 }
1521 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1522 {
1523 /*
1524 * Skip the lines (below the deleted lines) that are still
1525 * valid and don't need redrawing. Copy their info
1526 * upwards, to compensate for the deleted lines. Set
1527 * bot_start to the first row that needs redrawing.
1528 */
1529 bot_start = 0;
1530 idx = 0;
1531 for (;;)
1532 {
1533 wp->w_lines[idx] = wp->w_lines[j];
1534 /* stop at line that didn't fit, unless it is still
1535 * valid (no lines deleted) */
1536 if (row > 0 && bot_start + row
1537 + (int)wp->w_lines[j].wl_size > wp->w_height)
1538 {
1539 wp->w_lines_valid = idx + 1;
1540 break;
1541 }
1542 bot_start += wp->w_lines[idx++].wl_size;
1543
1544 /* stop at the last valid entry in w_lines[].wl_size */
1545 if (++j >= wp->w_lines_valid)
1546 {
1547 wp->w_lines_valid = idx;
1548 break;
1549 }
1550 }
1551#ifdef FEAT_DIFF
1552 /* Correct the first entry for filler lines at the top
1553 * when it won't get updated below. */
1554 if (wp->w_p_diff && bot_start > 0)
1555 wp->w_lines[0].wl_size =
1556 plines_win_nofill(wp, wp->w_topline, TRUE)
1557 + wp->w_topfill;
1558#endif
1559 }
1560 }
1561 }
1562
1563 /* When starting redraw in the first line, redraw all lines. When
1564 * there is only one window it's probably faster to clear the screen
1565 * first. */
1566 if (mid_start == 0)
1567 {
1568 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001569 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001570 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001571 /* Clear the screen when it was not done by win_del_lines() or
1572 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1573 * then. */
1574 if (screen_cleared != TRUE)
1575 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001576 /* The screen was cleared, redraw the tab pages line. */
1577 if (redraw_tabline)
1578 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001581
1582 /* When win_del_lines() or win_ins_lines() caused the screen to be
1583 * cleared (only happens for the first window) or when screenclear()
1584 * was called directly above, "must_redraw" will have been set to
1585 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1586 if (screen_cleared == TRUE)
1587 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 }
1589 else
1590 {
1591 /* Not VALID or INVERTED: redraw all lines. */
1592 mid_start = 0;
1593 mid_end = wp->w_height;
1594 }
1595
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001596 if (type == SOME_VALID)
1597 {
1598 /* SOME_VALID: redraw all lines. */
1599 mid_start = 0;
1600 mid_end = wp->w_height;
1601 type = NOT_VALID;
1602 }
1603
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 /* check if we are updating or removing the inverted part */
1605 if ((VIsual_active && buf == curwin->w_buffer)
1606 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1607 {
1608 linenr_T from, to;
1609
1610 if (VIsual_active)
1611 {
1612 if (VIsual_active
1613 && (VIsual_mode != wp->w_old_visual_mode
1614 || type == INVERTED_ALL))
1615 {
1616 /*
1617 * If the type of Visual selection changed, redraw the whole
1618 * selection. Also when the ownership of the X selection is
1619 * gained or lost.
1620 */
1621 if (curwin->w_cursor.lnum < VIsual.lnum)
1622 {
1623 from = curwin->w_cursor.lnum;
1624 to = VIsual.lnum;
1625 }
1626 else
1627 {
1628 from = VIsual.lnum;
1629 to = curwin->w_cursor.lnum;
1630 }
1631 /* redraw more when the cursor moved as well */
1632 if (wp->w_old_cursor_lnum < from)
1633 from = wp->w_old_cursor_lnum;
1634 if (wp->w_old_cursor_lnum > to)
1635 to = wp->w_old_cursor_lnum;
1636 if (wp->w_old_visual_lnum < from)
1637 from = wp->w_old_visual_lnum;
1638 if (wp->w_old_visual_lnum > to)
1639 to = wp->w_old_visual_lnum;
1640 }
1641 else
1642 {
1643 /*
1644 * Find the line numbers that need to be updated: The lines
1645 * between the old cursor position and the current cursor
1646 * position. Also check if the Visual position changed.
1647 */
1648 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1649 {
1650 from = curwin->w_cursor.lnum;
1651 to = wp->w_old_cursor_lnum;
1652 }
1653 else
1654 {
1655 from = wp->w_old_cursor_lnum;
1656 to = curwin->w_cursor.lnum;
1657 if (from == 0) /* Visual mode just started */
1658 from = to;
1659 }
1660
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001661 if (VIsual.lnum != wp->w_old_visual_lnum
1662 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 if (wp->w_old_visual_lnum < from
1665 && wp->w_old_visual_lnum != 0)
1666 from = wp->w_old_visual_lnum;
1667 if (wp->w_old_visual_lnum > to)
1668 to = wp->w_old_visual_lnum;
1669 if (VIsual.lnum < from)
1670 from = VIsual.lnum;
1671 if (VIsual.lnum > to)
1672 to = VIsual.lnum;
1673 }
1674 }
1675
1676 /*
1677 * If in block mode and changed column or curwin->w_curswant:
1678 * update all lines.
1679 * First compute the actual start and end column.
1680 */
1681 if (VIsual_mode == Ctrl_V)
1682 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001683 colnr_T fromc, toc;
1684#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1685 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
Bram Moolenaar404406a2014-10-09 13:24:43 +02001687 if (curwin->w_p_lbr)
1688 ve_flags = VE_ALL;
1689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001691#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1692 ve_flags = save_ve_flags;
1693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 ++toc;
1695 if (curwin->w_curswant == MAXCOL)
1696 toc = MAXCOL;
1697
1698 if (fromc != wp->w_old_cursor_fcol
1699 || toc != wp->w_old_cursor_lcol)
1700 {
1701 if (from > VIsual.lnum)
1702 from = VIsual.lnum;
1703 if (to < VIsual.lnum)
1704 to = VIsual.lnum;
1705 }
1706 wp->w_old_cursor_fcol = fromc;
1707 wp->w_old_cursor_lcol = toc;
1708 }
1709 }
1710 else
1711 {
1712 /* Use the line numbers of the old Visual area. */
1713 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1714 {
1715 from = wp->w_old_cursor_lnum;
1716 to = wp->w_old_visual_lnum;
1717 }
1718 else
1719 {
1720 from = wp->w_old_visual_lnum;
1721 to = wp->w_old_cursor_lnum;
1722 }
1723 }
1724
1725 /*
1726 * There is no need to update lines above the top of the window.
1727 */
1728 if (from < wp->w_topline)
1729 from = wp->w_topline;
1730
1731 /*
1732 * If we know the value of w_botline, use it to restrict the update to
1733 * the lines that are visible in the window.
1734 */
1735 if (wp->w_valid & VALID_BOTLINE)
1736 {
1737 if (from >= wp->w_botline)
1738 from = wp->w_botline - 1;
1739 if (to >= wp->w_botline)
1740 to = wp->w_botline - 1;
1741 }
1742
1743 /*
1744 * Find the minimal part to be updated.
1745 * Watch out for scrolling that made entries in w_lines[] invalid.
1746 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1747 * top_end; need to redraw from top_end to the "to" line.
1748 * A middle mouse click with a Visual selection may change the text
1749 * above the Visual area and reset wl_valid, do count these for
1750 * mid_end (in srow).
1751 */
1752 if (mid_start > 0)
1753 {
1754 lnum = wp->w_topline;
1755 idx = 0;
1756 srow = 0;
1757 if (scrolled_down)
1758 mid_start = top_end;
1759 else
1760 mid_start = 0;
1761 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1762 {
1763 if (wp->w_lines[idx].wl_valid)
1764 mid_start += wp->w_lines[idx].wl_size;
1765 else if (!scrolled_down)
1766 srow += wp->w_lines[idx].wl_size;
1767 ++idx;
1768# ifdef FEAT_FOLDING
1769 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1770 lnum = wp->w_lines[idx].wl_lnum;
1771 else
1772# endif
1773 ++lnum;
1774 }
1775 srow += mid_start;
1776 mid_end = wp->w_height;
1777 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1778 {
1779 if (wp->w_lines[idx].wl_valid
1780 && wp->w_lines[idx].wl_lnum >= to + 1)
1781 {
1782 /* Only update until first row of this line */
1783 mid_end = srow;
1784 break;
1785 }
1786 srow += wp->w_lines[idx].wl_size;
1787 }
1788 }
1789 }
1790
1791 if (VIsual_active && buf == curwin->w_buffer)
1792 {
1793 wp->w_old_visual_mode = VIsual_mode;
1794 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1795 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001796 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 wp->w_old_curswant = curwin->w_curswant;
1798 }
1799 else
1800 {
1801 wp->w_old_visual_mode = 0;
1802 wp->w_old_cursor_lnum = 0;
1803 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001804 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
1807#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1808 /* reset got_int, otherwise regexp won't work */
1809 save_got_int = got_int;
1810 got_int = 0;
1811#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001812#ifdef SYN_TIME_LIMIT
1813 /* Set the time limit to 'redrawtime'. */
1814 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001815 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817#ifdef FEAT_FOLDING
1818 win_foldinfo.fi_level = 0;
1819#endif
1820
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001821#ifdef FEAT_MENU
1822 /*
1823 * Draw the window toolbar, if there is one.
1824 * TODO: only when needed.
1825 */
1826 if (winbar_height(wp) > 0)
1827 redraw_win_toolbar(wp);
1828#endif
1829
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 /*
1831 * Update all the window rows.
1832 */
1833 idx = 0; /* first entry in w_lines[].wl_size */
1834 row = 0;
1835 srow = 0;
1836 lnum = wp->w_topline; /* first line shown in window */
1837 for (;;)
1838 {
1839 /* stop updating when reached the end of the window (check for _past_
1840 * the end of the window is at the end of the loop) */
1841 if (row == wp->w_height)
1842 {
1843 didline = TRUE;
1844 break;
1845 }
1846
1847 /* stop updating when hit the end of the file */
1848 if (lnum > buf->b_ml.ml_line_count)
1849 {
1850 eof = TRUE;
1851 break;
1852 }
1853
1854 /* Remember the starting row of the line that is going to be dealt
1855 * with. It is used further down when the line doesn't fit. */
1856 srow = row;
1857
1858 /*
1859 * Update a line when it is in an area that needs updating, when it
1860 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001861 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 * win_del_lines(), check if the current line includes it.
1863 * When syntax folding is being used, the saved syntax states will
1864 * already have been updated, we can't see where the syntax state is
1865 * the same again, just update until the end of the window.
1866 */
1867 if (row < top_end
1868 || (row >= mid_start && row < mid_end)
1869#ifdef FEAT_SEARCH_EXTRA
1870 || top_to_mod
1871#endif
1872 || idx >= wp->w_lines_valid
1873 || (row + wp->w_lines[idx].wl_size > bot_start)
1874 || (mod_top != 0
1875 && (lnum == mod_top
1876 || (lnum >= mod_top
1877 && (lnum < mod_bot
1878#ifdef FEAT_SYN_HL
1879 || did_update == DID_FOLD
1880 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001881 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 && (
1883# ifdef FEAT_FOLDING
1884 (foldmethodIsSyntax(wp)
1885 && hasAnyFolding(wp)) ||
1886# endif
1887 syntax_check_changed(lnum)))
1888#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001889#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001890 /* match in fixed position might need redraw
1891 * if lines were inserted or deleted */
1892 || (wp->w_match_head != NULL
1893 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 )))))
1896 {
1897#ifdef FEAT_SEARCH_EXTRA
1898 if (lnum == mod_top)
1899 top_to_mod = FALSE;
1900#endif
1901
1902 /*
1903 * When at start of changed lines: May scroll following lines
1904 * up or down to minimize redrawing.
1905 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001906 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 */
1908 if (lnum == mod_top
1909 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001910 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 {
1912 int old_rows = 0;
1913 int new_rows = 0;
1914 int xtra_rows;
1915 linenr_T l;
1916
1917 /* Count the old number of window rows, using w_lines[], which
1918 * should still contain the sizes for the lines as they are
1919 * currently displayed. */
1920 for (i = idx; i < wp->w_lines_valid; ++i)
1921 {
1922 /* Only valid lines have a meaningful wl_lnum. Invalid
1923 * lines are part of the changed area. */
1924 if (wp->w_lines[i].wl_valid
1925 && wp->w_lines[i].wl_lnum == mod_bot)
1926 break;
1927 old_rows += wp->w_lines[i].wl_size;
1928#ifdef FEAT_FOLDING
1929 if (wp->w_lines[i].wl_valid
1930 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1931 {
1932 /* Must have found the last valid entry above mod_bot.
1933 * Add following invalid entries. */
1934 ++i;
1935 while (i < wp->w_lines_valid
1936 && !wp->w_lines[i].wl_valid)
1937 old_rows += wp->w_lines[i++].wl_size;
1938 break;
1939 }
1940#endif
1941 }
1942
1943 if (i >= wp->w_lines_valid)
1944 {
1945 /* We can't find a valid line below the changed lines,
1946 * need to redraw until the end of the window.
1947 * Inserting/deleting lines has no use. */
1948 bot_start = 0;
1949 }
1950 else
1951 {
1952 /* Able to count old number of rows: Count new window
1953 * rows, and may insert/delete lines */
1954 j = idx;
1955 for (l = lnum; l < mod_bot; ++l)
1956 {
1957#ifdef FEAT_FOLDING
1958 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1959 ++new_rows;
1960 else
1961#endif
1962#ifdef FEAT_DIFF
1963 if (l == wp->w_topline)
1964 new_rows += plines_win_nofill(wp, l, TRUE)
1965 + wp->w_topfill;
1966 else
1967#endif
1968 new_rows += plines_win(wp, l, TRUE);
1969 ++j;
1970 if (new_rows > wp->w_height - row - 2)
1971 {
1972 /* it's getting too much, must redraw the rest */
1973 new_rows = 9999;
1974 break;
1975 }
1976 }
1977 xtra_rows = new_rows - old_rows;
1978 if (xtra_rows < 0)
1979 {
1980 /* May scroll text up. If there is not enough
1981 * remaining text or scrolling fails, must redraw the
1982 * rest. If scrolling works, must redraw the text
1983 * below the scrolled text. */
1984 if (row - xtra_rows >= wp->w_height - 2)
1985 mod_bot = MAXLNUM;
1986 else
1987 {
1988 check_for_delay(FALSE);
1989 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001990 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 mod_bot = MAXLNUM;
1992 else
1993 bot_start = wp->w_height + xtra_rows;
1994 }
1995 }
1996 else if (xtra_rows > 0)
1997 {
1998 /* May scroll text down. If there is not enough
1999 * remaining text of scrolling fails, must redraw the
2000 * rest. */
2001 if (row + xtra_rows >= wp->w_height - 2)
2002 mod_bot = MAXLNUM;
2003 else
2004 {
2005 check_for_delay(FALSE);
2006 if (win_ins_lines(wp, row + old_rows,
2007 xtra_rows, FALSE, FALSE) == FAIL)
2008 mod_bot = MAXLNUM;
2009 else if (top_end > row + old_rows)
2010 /* Scrolled the part at the top that requires
2011 * updating down. */
2012 top_end += xtra_rows;
2013 }
2014 }
2015
2016 /* When not updating the rest, may need to move w_lines[]
2017 * entries. */
2018 if (mod_bot != MAXLNUM && i != j)
2019 {
2020 if (j < i)
2021 {
2022 int x = row + new_rows;
2023
2024 /* move entries in w_lines[] upwards */
2025 for (;;)
2026 {
2027 /* stop at last valid entry in w_lines[] */
2028 if (i >= wp->w_lines_valid)
2029 {
2030 wp->w_lines_valid = j;
2031 break;
2032 }
2033 wp->w_lines[j] = wp->w_lines[i];
2034 /* stop at a line that won't fit */
2035 if (x + (int)wp->w_lines[j].wl_size
2036 > wp->w_height)
2037 {
2038 wp->w_lines_valid = j + 1;
2039 break;
2040 }
2041 x += wp->w_lines[j++].wl_size;
2042 ++i;
2043 }
2044 if (bot_start > x)
2045 bot_start = x;
2046 }
2047 else /* j > i */
2048 {
2049 /* move entries in w_lines[] downwards */
2050 j -= i;
2051 wp->w_lines_valid += j;
2052 if (wp->w_lines_valid > wp->w_height)
2053 wp->w_lines_valid = wp->w_height;
2054 for (i = wp->w_lines_valid; i - j >= idx; --i)
2055 wp->w_lines[i] = wp->w_lines[i - j];
2056
2057 /* The w_lines[] entries for inserted lines are
2058 * now invalid, but wl_size may be used above.
2059 * Reset to zero. */
2060 while (i >= idx)
2061 {
2062 wp->w_lines[i].wl_size = 0;
2063 wp->w_lines[i--].wl_valid = FALSE;
2064 }
2065 }
2066 }
2067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 }
2069
2070#ifdef FEAT_FOLDING
2071 /*
2072 * When lines are folded, display one line for all of them.
2073 * Otherwise, display normally (can be several display lines when
2074 * 'wrap' is on).
2075 */
2076 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2077 if (fold_count != 0)
2078 {
2079 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2080 ++row;
2081 --fold_count;
2082 wp->w_lines[idx].wl_folded = TRUE;
2083 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2084# ifdef FEAT_SYN_HL
2085 did_update = DID_FOLD;
2086# endif
2087 }
2088 else
2089#endif
2090 if (idx < wp->w_lines_valid
2091 && wp->w_lines[idx].wl_valid
2092 && wp->w_lines[idx].wl_lnum == lnum
2093 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002094 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 && srow + wp->w_lines[idx].wl_size > wp->w_height
2096#ifdef FEAT_DIFF
2097 && diff_check_fill(wp, lnum) == 0
2098#endif
2099 )
2100 {
2101 /* This line is not going to fit. Don't draw anything here,
2102 * will draw "@ " lines below. */
2103 row = wp->w_height + 1;
2104 }
2105 else
2106 {
2107#ifdef FEAT_SEARCH_EXTRA
2108 prepare_search_hl(wp, lnum);
2109#endif
2110#ifdef FEAT_SYN_HL
2111 /* Let the syntax stuff know we skipped a few lines. */
2112 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002113 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 syntax_end_parsing(syntax_last_parsed + 1);
2115#endif
2116
2117 /*
2118 * Display one line.
2119 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002120 row = win_line(wp, lnum, srow, wp->w_height,
2121 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
2123#ifdef FEAT_FOLDING
2124 wp->w_lines[idx].wl_folded = FALSE;
2125 wp->w_lines[idx].wl_lastlnum = lnum;
2126#endif
2127#ifdef FEAT_SYN_HL
2128 did_update = DID_LINE;
2129 syntax_last_parsed = lnum;
2130#endif
2131 }
2132
2133 wp->w_lines[idx].wl_lnum = lnum;
2134 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002135
2136 /* Past end of the window or end of the screen. Note that after
2137 * resizing wp->w_height may be end up too big. That's a problem
2138 * elsewhere, but prevent a crash here. */
2139 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 {
2141 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002142 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2144 ++idx;
2145 break;
2146 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002147 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 wp->w_lines[idx].wl_size = row - srow;
2149 ++idx;
2150#ifdef FEAT_FOLDING
2151 lnum += fold_count + 1;
2152#else
2153 ++lnum;
2154#endif
2155 }
2156 else
2157 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002158 if (wp->w_p_rnu)
2159 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002160#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002161 // 'relativenumber' set: The text doesn't need to be drawn, but
2162 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002163 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2164 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002165 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002166 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002167#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002168 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002169 }
2170
2171 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 row += wp->w_lines[idx++].wl_size;
2173 if (row > wp->w_height) /* past end of screen */
2174 break;
2175#ifdef FEAT_FOLDING
2176 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2177#else
2178 ++lnum;
2179#endif
2180#ifdef FEAT_SYN_HL
2181 did_update = DID_NONE;
2182#endif
2183 }
2184
2185 if (lnum > buf->b_ml.ml_line_count)
2186 {
2187 eof = TRUE;
2188 break;
2189 }
2190 }
2191 /*
2192 * End of loop over all window lines.
2193 */
2194
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002195#ifdef FEAT_VTP
2196 /* Rewrite the character at the end of the screen line. */
2197 if (use_vtp())
2198 {
2199 int i;
2200
2201 for (i = 0; i < Rows; ++i)
2202# ifdef FEAT_MBYTE
2203 if (enc_utf8)
2204 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2205 LineOffset[i] + screen_Columns) > 1)
2206 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2207 else
2208 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2209 else
2210# endif
2211 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2212 }
2213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214
2215 if (idx > wp->w_lines_valid)
2216 wp->w_lines_valid = idx;
2217
2218#ifdef FEAT_SYN_HL
2219 /*
2220 * Let the syntax stuff know we stop parsing here.
2221 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002222 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 syntax_end_parsing(syntax_last_parsed + 1);
2224#endif
2225
2226 /*
2227 * If we didn't hit the end of the file, and we didn't finish the last
2228 * line we were working on, then the line didn't fit.
2229 */
2230 wp->w_empty_rows = 0;
2231#ifdef FEAT_DIFF
2232 wp->w_filler_rows = 0;
2233#endif
2234 if (!eof && !didline)
2235 {
2236 if (lnum == wp->w_topline)
2237 {
2238 /*
2239 * Single line that does not fit!
2240 * Don't overwrite it, it can be edited.
2241 */
2242 wp->w_botline = lnum + 1;
2243 }
2244#ifdef FEAT_DIFF
2245 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2246 {
2247 /* Window ends in filler lines. */
2248 wp->w_botline = lnum;
2249 wp->w_filler_rows = wp->w_height - srow;
2250 }
2251#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002252 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2253 {
2254 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2255
2256 /*
2257 * Last line isn't finished: Display "@@@" in the last screen line.
2258 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002259 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002260 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002261 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002262 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002263 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002264 set_empty_rows(wp, srow);
2265 wp->w_botline = lnum;
2266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2268 {
2269 /*
2270 * Last line isn't finished: Display "@@@" at the end.
2271 */
2272 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2273 W_WINROW(wp) + wp->w_height,
2274 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002275 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 set_empty_rows(wp, srow);
2277 wp->w_botline = lnum;
2278 }
2279 else
2280 {
2281 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2282 wp->w_botline = lnum;
2283 }
2284 }
2285 else
2286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 if (eof) /* we hit the end of the file */
2289 {
2290 wp->w_botline = buf->b_ml.ml_line_count + 1;
2291#ifdef FEAT_DIFF
2292 j = diff_check_fill(wp, wp->w_botline);
2293 if (j > 0 && !wp->w_botfill)
2294 {
2295 /*
2296 * Display filler lines at the end of the file
2297 */
2298 if (char2cells(fill_diff) > 1)
2299 i = '-';
2300 else
2301 i = fill_diff;
2302 if (row + j > wp->w_height)
2303 j = wp->w_height - row;
2304 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2305 row += j;
2306 }
2307#endif
2308 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002309 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 wp->w_botline = lnum;
2311
2312 /* make sure the rest of the screen is blank */
2313 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002314 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 }
2316
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002317#ifdef SYN_TIME_LIMIT
2318 syn_set_timeout(NULL);
2319#endif
2320
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 /* Reset the type of redrawing required, the window has been updated. */
2322 wp->w_redr_type = 0;
2323#ifdef FEAT_DIFF
2324 wp->w_old_topfill = wp->w_topfill;
2325 wp->w_old_botfill = wp->w_botfill;
2326#endif
2327
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002328 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
2330 /*
2331 * There is a trick with w_botline. If we invalidate it on each
2332 * change that might modify it, this will cause a lot of expensive
2333 * calls to plines() in update_topline() each time. Therefore the
2334 * value of w_botline is often approximated, and this value is used to
2335 * compute the value of w_topline. If the value of w_botline was
2336 * wrong, check that the value of w_topline is correct (cursor is on
2337 * the visible part of the text). If it's not, we need to redraw
2338 * again. Mostly this just means scrolling up a few lines, so it
2339 * doesn't look too bad. Only do this for the current window (where
2340 * changes are relevant).
2341 */
2342 wp->w_valid |= VALID_BOTLINE;
2343 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2344 {
2345 recursive = TRUE;
2346 curwin->w_valid &= ~VALID_TOPLINE;
2347 update_topline(); /* may invalidate w_botline again */
2348 if (must_redraw != 0)
2349 {
2350 /* Don't update for changes in buffer again. */
2351 i = curbuf->b_mod_set;
2352 curbuf->b_mod_set = FALSE;
2353 win_update(curwin);
2354 must_redraw = 0;
2355 curbuf->b_mod_set = i;
2356 }
2357 recursive = FALSE;
2358 }
2359 }
2360
2361#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2362 /* restore got_int, unless CTRL-C was hit while redrawing */
2363 if (!got_int)
2364 got_int = save_got_int;
2365#endif
2366}
2367
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368/*
2369 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2370 * as the filler character.
2371 */
2372 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002373win_draw_end(
2374 win_T *wp,
2375 int c1,
2376 int c2,
2377 int row,
2378 int endrow,
2379 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380{
2381#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2382 int n = 0;
2383# define FDC_OFF n
2384#else
2385# define FDC_OFF 0
2386#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002387#ifdef FEAT_FOLDING
2388 int fdc = compute_foldcolumn(wp, 0);
2389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
2391#ifdef FEAT_RIGHTLEFT
2392 if (wp->w_p_rl)
2393 {
2394 /* No check for cmdline window: should never be right-left. */
2395# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002396 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398 if (n > 0)
2399 {
2400 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002401 if (n > wp->w_width)
2402 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2404 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002405 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
2407# endif
2408# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002409 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
2411 int nn = n + 2;
2412
2413 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002414 if (nn > wp->w_width)
2415 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2417 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002418 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 n = nn;
2420 }
2421# endif
2422 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002423 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002424 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2426 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002427 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
2429 else
2430#endif
2431 {
2432#ifdef FEAT_CMDWIN
2433 if (cmdwin_type != 0 && wp == curwin)
2434 {
2435 /* draw the cmdline character in the leftmost column */
2436 n = 1;
2437 if (n > wp->w_width)
2438 n = wp->w_width;
2439 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002440 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002441 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 }
2443#endif
2444#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002445 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002447 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002450 if (nn > wp->w_width)
2451 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002453 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002454 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 n = nn;
2456 }
2457#endif
2458#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002459 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 {
2461 int nn = n + 2;
2462
2463 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002464 if (nn > wp->w_width)
2465 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002467 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002468 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 n = nn;
2470 }
2471#endif
2472 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002473 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002474 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 }
2476 set_empty_rows(wp, row);
2477}
2478
Bram Moolenaar1a384422010-07-14 19:53:30 +02002479#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002480/*
2481 * Advance **color_cols and return TRUE when there are columns to draw.
2482 */
2483 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002484advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002485{
2486 while (**color_cols >= 0 && vcol > **color_cols)
2487 ++*color_cols;
2488 return (**color_cols >= 0);
2489}
2490#endif
2491
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002492#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2493/*
2494 * Copy "text" to ScreenLines using "attr".
2495 * Returns the next screen column.
2496 */
2497 static int
2498text_to_screenline(win_T *wp, char_u *text, int col)
2499{
2500 int off = (int)(current_ScreenLine - ScreenLines);
2501
2502#ifdef FEAT_MBYTE
2503 if (has_mbyte)
2504 {
2505 int cells;
2506 int u8c, u8cc[MAX_MCO];
2507 int i;
2508 int idx;
2509 int c_len;
2510 char_u *p;
2511# ifdef FEAT_ARABIC
2512 int prev_c = 0; /* previous Arabic character */
2513 int prev_c1 = 0; /* first composing char for prev_c */
2514# endif
2515
2516# ifdef FEAT_RIGHTLEFT
2517 if (wp->w_p_rl)
2518 idx = off;
2519 else
2520# endif
2521 idx = off + col;
2522
2523 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2524 for (p = text; *p != NUL; )
2525 {
2526 cells = (*mb_ptr2cells)(p);
2527 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002528 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002529# ifdef FEAT_RIGHTLEFT
2530 - (wp->w_p_rl ? col : 0)
2531# endif
2532 )
2533 break;
2534 ScreenLines[idx] = *p;
2535 if (enc_utf8)
2536 {
2537 u8c = utfc_ptr2char(p, u8cc);
2538 if (*p < 0x80 && u8cc[0] == 0)
2539 {
2540 ScreenLinesUC[idx] = 0;
2541#ifdef FEAT_ARABIC
2542 prev_c = u8c;
2543#endif
2544 }
2545 else
2546 {
2547#ifdef FEAT_ARABIC
2548 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2549 {
2550 /* Do Arabic shaping. */
2551 int pc, pc1, nc;
2552 int pcc[MAX_MCO];
2553 int firstbyte = *p;
2554
2555 /* The idea of what is the previous and next
2556 * character depends on 'rightleft'. */
2557 if (wp->w_p_rl)
2558 {
2559 pc = prev_c;
2560 pc1 = prev_c1;
2561 nc = utf_ptr2char(p + c_len);
2562 prev_c1 = u8cc[0];
2563 }
2564 else
2565 {
2566 pc = utfc_ptr2char(p + c_len, pcc);
2567 nc = prev_c;
2568 pc1 = pcc[0];
2569 }
2570 prev_c = u8c;
2571
2572 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2573 pc, pc1, nc);
2574 ScreenLines[idx] = firstbyte;
2575 }
2576 else
2577 prev_c = u8c;
2578#endif
2579 /* Non-BMP character: display as ? or fullwidth ?. */
2580#ifdef UNICODE16
2581 if (u8c >= 0x10000)
2582 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2583 else
2584#endif
2585 ScreenLinesUC[idx] = u8c;
2586 for (i = 0; i < Screen_mco; ++i)
2587 {
2588 ScreenLinesC[i][idx] = u8cc[i];
2589 if (u8cc[i] == 0)
2590 break;
2591 }
2592 }
2593 if (cells > 1)
2594 ScreenLines[idx + 1] = 0;
2595 }
2596 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2597 /* double-byte single width character */
2598 ScreenLines2[idx] = p[1];
2599 else if (cells > 1)
2600 /* double-width character */
2601 ScreenLines[idx + 1] = p[1];
2602 col += cells;
2603 idx += cells;
2604 p += c_len;
2605 }
2606 }
2607 else
2608#endif
2609 {
2610 int len = (int)STRLEN(text);
2611
Bram Moolenaar02631462017-09-22 15:20:32 +02002612 if (len > wp->w_width - col)
2613 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002614 if (len > 0)
2615 {
2616#ifdef FEAT_RIGHTLEFT
2617 if (wp->w_p_rl)
2618 STRNCPY(current_ScreenLine, text, len);
2619 else
2620#endif
2621 STRNCPY(current_ScreenLine + col, text, len);
2622 col += len;
2623 }
2624 }
2625 return col;
2626}
2627#endif
2628
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629#ifdef FEAT_FOLDING
2630/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002631 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2632 * space is available for window "wp", minus "col".
2633 */
2634 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002635compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002636{
2637 int fdc = wp->w_p_fdc;
2638 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002639 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002640
2641 if (fdc > wwidth - (col + wmw))
2642 fdc = wwidth - (col + wmw);
2643 return fdc;
2644}
2645
2646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 * Display one folded line.
2648 */
2649 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002650fold_line(
2651 win_T *wp,
2652 long fold_count,
2653 foldinfo_T *foldinfo,
2654 linenr_T lnum,
2655 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002657 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 pos_T *top, *bot;
2659 linenr_T lnume = lnum + fold_count - 1;
2660 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002661 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 int col;
2664 int txtcol;
2665 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002666 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
2668 /* Build the fold line:
2669 * 1. Add the cmdwin_type for the command-line window
2670 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002671 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 * 4. Compose the text
2673 * 5. Add the text
2674 * 6. set highlighting for the Visual area an other text
2675 */
2676 col = 0;
2677
2678 /*
2679 * 1. Add the cmdwin_type for the command-line window
2680 * Ignores 'rightleft', this window is never right-left.
2681 */
2682#ifdef FEAT_CMDWIN
2683 if (cmdwin_type != 0 && wp == curwin)
2684 {
2685 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002686 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687#ifdef FEAT_MBYTE
2688 if (enc_utf8)
2689 ScreenLinesUC[off] = 0;
2690#endif
2691 ++col;
2692 }
2693#endif
2694
2695 /*
2696 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002697 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002699 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 if (fdc > 0)
2701 {
2702 fill_foldcolumn(buf, wp, TRUE, lnum);
2703#ifdef FEAT_RIGHTLEFT
2704 if (wp->w_p_rl)
2705 {
2706 int i;
2707
Bram Moolenaar02631462017-09-22 15:20:32 +02002708 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002709 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 /* reverse the fold column */
2711 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002712 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714 else
2715#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002716 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 col += fdc;
2718 }
2719
2720#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002721# define RL_MEMSET(p, v, l) \
2722 do { \
2723 if (wp->w_p_rl) \
2724 for (ri = 0; ri < l; ++ri) \
2725 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2726 else \
2727 for (ri = 0; ri < l; ++ri) \
2728 ScreenAttrs[off + (p) + ri] = v; \
2729 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002731# define RL_MEMSET(p, v, l) \
2732 do { \
2733 for (ri = 0; ri < l; ++ri) \
2734 ScreenAttrs[off + (p) + ri] = v; \
2735 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736#endif
2737
Bram Moolenaar64486672010-05-16 15:46:46 +02002738 /* Set all attributes of the 'number' or 'relativenumber' column and the
2739 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002740 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
2742#ifdef FEAT_SIGNS
2743 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002744 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002746 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 if (len > 0)
2748 {
2749 if (len > 2)
2750 len = 2;
2751# ifdef FEAT_RIGHTLEFT
2752 if (wp->w_p_rl)
2753 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002754 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002755 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 else
2757# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002758 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 col += len;
2760 }
2761 }
2762#endif
2763
2764 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002765 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002767 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002769 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 if (len > 0)
2771 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002772 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002773 long num;
2774 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002775
2776 if (len > w + 1)
2777 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002778
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002779 if (wp->w_p_nu && !wp->w_p_rnu)
2780 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002781 num = (long)lnum;
2782 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002783 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002784 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002785 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002786 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002787 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002788 /* 'number' + 'relativenumber': cursor line shows absolute
2789 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002790 num = lnum;
2791 fmt = "%-*ld ";
2792 }
2793 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002794
Bram Moolenaar700e7342013-01-30 12:31:36 +01002795 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796#ifdef FEAT_RIGHTLEFT
2797 if (wp->w_p_rl)
2798 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002799 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002800 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 else
2802#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002803 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 col += len;
2805 }
2806 }
2807
2808 /*
2809 * 4. Compose the folded-line string with 'foldtext', if set.
2810 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002811 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812
2813 txtcol = col; /* remember where text starts */
2814
2815 /*
2816 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2817 * Right-left text is put in columns 0 - number-col, normal text is put
2818 * in columns number-col - window-width.
2819 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002820 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821
2822 /* Fill the rest of the line with the fold filler */
2823#ifdef FEAT_RIGHTLEFT
2824 if (wp->w_p_rl)
2825 col -= txtcol;
2826#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002827 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828#ifdef FEAT_RIGHTLEFT
2829 - (wp->w_p_rl ? txtcol : 0)
2830#endif
2831 )
2832 {
2833#ifdef FEAT_MBYTE
2834 if (enc_utf8)
2835 {
2836 if (fill_fold >= 0x80)
2837 {
2838 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002839 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002840 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 }
2842 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002843 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002845 ScreenLines[off + col] = fill_fold;
2846 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002847 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002849 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002851 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 }
2853
2854 if (text != buf)
2855 vim_free(text);
2856
2857 /*
2858 * 6. set highlighting for the Visual area an other text.
2859 * If all folded lines are in the Visual area, highlight the line.
2860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2862 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002863 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 {
2865 /* Visual is after curwin->w_cursor */
2866 top = &curwin->w_cursor;
2867 bot = &VIsual;
2868 }
2869 else
2870 {
2871 /* Visual is before curwin->w_cursor */
2872 top = &VIsual;
2873 bot = &curwin->w_cursor;
2874 }
2875 if (lnum >= top->lnum
2876 && lnume <= bot->lnum
2877 && (VIsual_mode != 'v'
2878 || ((lnum > top->lnum
2879 || (lnum == top->lnum
2880 && top->col == 0))
2881 && (lnume < bot->lnum
2882 || (lnume == bot->lnum
2883 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002884 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 {
2886 if (VIsual_mode == Ctrl_V)
2887 {
2888 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002889 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002891 if (wp->w_old_cursor_lcol != MAXCOL
2892 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002893 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 len = wp->w_old_cursor_lcol;
2895 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002896 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002897 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002898 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
2900 }
2901 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002902 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002904 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 }
2907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002909#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002910 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2911 if (wp->w_p_cc_cols)
2912 {
2913 int i = 0;
2914 int j = wp->w_p_cc_cols[i];
2915 int old_txtcol = txtcol;
2916
2917 while (j > -1)
2918 {
2919 txtcol += j;
2920 if (wp->w_p_wrap)
2921 txtcol -= wp->w_skipcol;
2922 else
2923 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002924 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002925 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002926 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002927 txtcol = old_txtcol;
2928 j = wp->w_p_cc_cols[++i];
2929 }
2930 }
2931
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002932 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002933 if (wp->w_p_cuc)
2934 {
2935 txtcol += wp->w_virtcol;
2936 if (wp->w_p_wrap)
2937 txtcol -= wp->w_skipcol;
2938 else
2939 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002940 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002941 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002942 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002943 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
Bram Moolenaar02631462017-09-22 15:20:32 +02002946 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2947 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
2949 /*
2950 * Update w_cline_height and w_cline_folded if the cursor line was
2951 * updated (saves a call to plines() later).
2952 */
2953 if (wp == curwin
2954 && lnum <= curwin->w_cursor.lnum
2955 && lnume >= curwin->w_cursor.lnum)
2956 {
2957 curwin->w_cline_row = row;
2958 curwin->w_cline_height = 1;
2959 curwin->w_cline_folded = TRUE;
2960 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2961 }
2962}
2963
2964/*
2965 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2966 */
2967 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002968copy_text_attr(
2969 int off,
2970 char_u *buf,
2971 int len,
2972 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002974 int i;
2975
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 mch_memmove(ScreenLines + off, buf, (size_t)len);
2977# ifdef FEAT_MBYTE
2978 if (enc_utf8)
2979 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2980# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002981 for (i = 0; i < len; ++i)
2982 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983}
2984
2985/*
2986 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002987 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 */
2989 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002990fill_foldcolumn(
2991 char_u *p,
2992 win_T *wp,
2993 int closed, /* TRUE of FALSE */
2994 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995{
2996 int i = 0;
2997 int level;
2998 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002999 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003000 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
3002 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003003 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004
3005 level = win_foldinfo.fi_level;
3006 if (level > 0)
3007 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003008 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003009 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003010
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 /* If the column is too narrow, we start at the lowest level that
3012 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003013 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 if (first_level < 1)
3015 first_level = 1;
3016
Bram Moolenaar1c934292015-01-27 16:39:29 +01003017 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 {
3019 if (win_foldinfo.fi_lnum == lnum
3020 && first_level + i >= win_foldinfo.fi_low_level)
3021 p[i] = '-';
3022 else if (first_level == 1)
3023 p[i] = '|';
3024 else if (first_level + i <= 9)
3025 p[i] = '0' + first_level + i;
3026 else
3027 p[i] = '>';
3028 if (first_level + i == level)
3029 break;
3030 }
3031 }
3032 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003033 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034}
3035#endif /* FEAT_FOLDING */
3036
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003037#ifdef FEAT_TEXT_PROP
3038static textprop_T *current_text_props = NULL;
3039static buf_T *current_buf = NULL;
3040
3041 static int
3042#ifdef __BORLANDC__
3043_RTLENTRYF
3044#endif
3045text_prop_compare(const void *s1, const void *s2)
3046{
3047 int idx1, idx2;
3048 proptype_T *pt1, *pt2;
3049 colnr_T col1, col2;
3050
3051 idx1 = *(int *)s1;
3052 idx2 = *(int *)s2;
3053 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3054 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3055 if (pt1 == pt2)
3056 return 0;
3057 if (pt1 == NULL)
3058 return -1;
3059 if (pt2 == NULL)
3060 return 1;
3061 if (pt1->pt_priority != pt2->pt_priority)
3062 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3063 col1 = current_text_props[idx1].tp_col;
3064 col2 = current_text_props[idx2].tp_col;
3065 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3066}
3067#endif
3068
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069/*
3070 * Display line "lnum" of window 'wp' on the screen.
3071 * Start at row "startrow", stop when "endrow" is reached.
3072 * wp->w_virtcol needs to be valid.
3073 *
3074 * Return the number of last row the line occupies.
3075 */
3076 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003077win_line(
3078 win_T *wp,
3079 linenr_T lnum,
3080 int startrow,
3081 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003082 int nochange UNUSED, // not updating for changed text
3083 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003085 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3087 int c = 0; /* init for GCC */
3088 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003089#ifdef FEAT_LINEBREAK
3090 long vcol_sbr = -1; /* virtual column after showbreak */
3091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 long vcol_prev = -1; /* "vcol" of previous character */
3093 char_u *line; /* current line */
3094 char_u *ptr; /* current position in "line" */
3095 int row; /* row in the window, excl w_winrow */
3096 int screen_row; /* row on the screen, incl w_winrow */
3097
3098 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3099 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003100 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003101 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 int c_extra = NUL; /* extra chars, all the same */
3103 int extra_attr = 0; /* attributes when n_extra != 0 */
3104 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3105 displaying lcs_eol at end-of-line */
3106 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3107 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3108
3109 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3110 int saved_n_extra = 0;
3111 char_u *saved_p_extra = NULL;
3112 int saved_c_extra = 0;
3113 int saved_char_attr = 0;
3114
3115 int n_attr = 0; /* chars with special attr */
3116 int saved_attr2 = 0; /* char_attr saved for n_attr */
3117 int n_attr3 = 0; /* chars with overruling special attr */
3118 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3119
3120 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3121
3122 int fromcol, tocol; /* start/end of inverting */
3123 int fromcol_prev = -2; /* start of inverting after cursor */
3124 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003126 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 pos_T pos;
3128 long v;
3129
3130 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003131 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3133 in this line */
3134 int attr = 0; /* attributes for area highlighting */
3135 int area_attr = 0; /* attributes desired by highlighting */
3136 int search_attr = 0; /* attributes desired by 'hlsearch' */
3137#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003138 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 int syntax_attr = 0; /* attributes desired by syntax */
3140 int has_syntax = FALSE; /* this buffer has syntax highl. */
3141 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003142 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003143 int draw_color_col = FALSE; /* highlight colorcolumn */
3144 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003145#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003146#ifdef FEAT_TEXT_PROP
3147 int text_prop_count;
3148 int text_prop_next = 0; // next text property to use
3149 textprop_T *text_props = NULL;
3150 int *text_prop_idxs = NULL;
3151 int text_props_active = 0;
3152 proptype_T *text_prop_type = NULL;
3153 int text_prop_attr = 0;
3154#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003155#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003156 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003157# define SPWORDLEN 150
3158 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003159 int nextlinecol = 0; /* column where nextline[] starts */
3160 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003161 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003162 int spell_attr = 0; /* attributes desired by spelling */
3163 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003164 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3165 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003166 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003167 static int cap_col = -1; /* column to check for Cap word */
3168 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003169 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003171 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172#ifdef FEAT_MBYTE
3173 int multi_attr = 0; /* attributes desired by multibyte */
3174 int mb_l = 1; /* multi-byte byte length */
3175 int mb_c = 0; /* decoded multi-byte character */
3176 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003177 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178#endif
3179#ifdef FEAT_DIFF
3180 int filler_lines; /* nr of filler lines to be drawn */
3181 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003182 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 int change_start = MAXCOL; /* first col of changed area */
3184 int change_end = -1; /* last col of changed area */
3185#endif
3186 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3187#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003188 int need_showbreak = FALSE; /* overlong line, skipping first x
3189 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003191#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003192 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003194 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195#endif
3196#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003197 matchitem_T *cur; /* points to the match list */
3198 match_T *shl; /* points to search_hl or a match */
3199 int shl_flag; /* flag to indicate whether search_hl
3200 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003201 int pos_inprogress; /* marks that position match search is
3202 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003203 int prevcol_hl_flag; /* flag to indicate whether prevcol
3204 equals startcol of search_hl or one
3205 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206#endif
3207#ifdef FEAT_ARABIC
3208 int prev_c = 0; /* previous Arabic character */
3209 int prev_c1 = 0; /* first composing char for prev_c */
3210#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003211#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003212 int did_line_attr = 0;
3213#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003214#ifdef FEAT_TERMINAL
3215 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003216 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
3219 /* draw_state: items that are drawn in sequence: */
3220#define WL_START 0 /* nothing done yet */
3221#ifdef FEAT_CMDWIN
3222# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3223#else
3224# define WL_CMDLINE WL_START
3225#endif
3226#ifdef FEAT_FOLDING
3227# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3228#else
3229# define WL_FOLD WL_CMDLINE
3230#endif
3231#ifdef FEAT_SIGNS
3232# define WL_SIGN WL_FOLD + 1 /* column for signs */
3233#else
3234# define WL_SIGN WL_FOLD /* column for signs */
3235#endif
3236#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003237#ifdef FEAT_LINEBREAK
3238# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003240# define WL_BRI WL_NR
3241#endif
3242#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3243# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3244#else
3245# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246#endif
3247#define WL_LINE WL_SBR + 1 /* text in the line */
3248 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003249#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 int feedback_col = 0;
3251 int feedback_old_attr = -1;
3252#endif
3253
Bram Moolenaar860cae12010-06-05 23:22:07 +02003254#ifdef FEAT_CONCEAL
3255 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003256 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003257 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003258 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003259 int is_concealing = FALSE;
3260 int boguscols = 0; /* nonexistent columns added to force
3261 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003262 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003263 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003264 int match_conc = 0; /* cchar for match functions */
3265 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003266 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003267# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003268# define FIX_FOR_BOGUSCOLS \
3269 { \
3270 n_extra += vcol_off; \
3271 vcol -= vcol_off; \
3272 vcol_off = 0; \
3273 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003274 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003275 boguscols = 0; \
3276 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003277#else
3278# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
3281 if (startrow > endrow) /* past the end already! */
3282 return startrow;
3283
3284 row = startrow;
3285 screen_row = row + W_WINROW(wp);
3286
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003287 if (!number_only)
3288 {
3289 /*
3290 * To speed up the loop below, set extra_check when there is linebreak,
3291 * trailing white space and/or syntax processing to be done.
3292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003294 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295#endif
3296#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003297 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003298# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003299 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003300# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003301 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003303 /* Prepare for syntax highlighting in this line. When there is an
3304 * error, stop syntax highlighting. */
3305 save_did_emsg = did_emsg;
3306 did_emsg = FALSE;
3307 syntax_start(wp, lnum);
3308 if (did_emsg)
3309 wp->w_s->b_syn_error = TRUE;
3310 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003311 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003312 did_emsg = save_did_emsg;
3313#ifdef SYN_TIME_LIMIT
3314 if (!wp->w_s->b_syn_slow)
3315#endif
3316 {
3317 has_syntax = TRUE;
3318 extra_check = TRUE;
3319 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003322
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003323 /* Check for columns to display for 'colorcolumn'. */
3324 color_cols = wp->w_p_cc_cols;
3325 if (color_cols != NULL)
3326 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003327#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003328
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003329#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003330 if (term_show_buffer(wp->w_buffer))
3331 {
3332 extra_check = TRUE;
3333 get_term_attr = TRUE;
3334 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3335 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003336#endif
3337
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003338#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003339 if (wp->w_p_spell
3340 && *wp->w_s->b_p_spl != NUL
3341 && wp->w_s->b_langp.ga_len > 0
3342 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003343 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003344 /* Prepare for spell checking. */
3345 has_spell = TRUE;
3346 extra_check = TRUE;
3347
Bram Moolenaar7701f302018-10-02 21:20:32 +02003348 /* Get the start of the next line, so that words that wrap to the
3349 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003350 * Trick: skip a few chars for C/shell/Vim comments */
3351 nextline[SPWORDLEN] = NUL;
3352 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3353 {
3354 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3355 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3356 }
3357
Bram Moolenaar7701f302018-10-02 21:20:32 +02003358 /* When a word wrapped from the previous line the start of the
3359 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003360 if (lnum == checked_lnum)
3361 cur_checked_col = checked_col;
3362 checked_lnum = 0;
3363
3364 /* When there was a sentence end in the previous line may require a
3365 * word starting with capital in this line. In line 1 always check
3366 * the first word. */
3367 if (lnum != capcol_lnum)
3368 cap_col = -1;
3369 if (lnum == 1)
3370 cap_col = 0;
3371 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373#endif
3374
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003375 /*
3376 * handle visual active in this window
3377 */
3378 fromcol = -10;
3379 tocol = MAXCOL;
3380 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003382 /* Visual is after curwin->w_cursor */
3383 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003385 top = &curwin->w_cursor;
3386 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003388 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003390 top = &VIsual;
3391 bot = &curwin->w_cursor;
3392 }
3393 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3394 if (VIsual_mode == Ctrl_V) /* block mode */
3395 {
3396 if (lnum_in_visual_area)
3397 {
3398 fromcol = wp->w_old_cursor_fcol;
3399 tocol = wp->w_old_cursor_lcol;
3400 }
3401 }
3402 else /* non-block mode */
3403 {
3404 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003406 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003408 if (VIsual_mode == 'V') /* linewise */
3409 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 else
3411 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003412 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3413 if (gchar_pos(top) == NUL)
3414 tocol = fromcol + 1;
3415 }
3416 }
3417 if (VIsual_mode != 'V' && lnum == bot->lnum)
3418 {
3419 if (*p_sel == 'e' && bot->col == 0
3420#ifdef FEAT_VIRTUALEDIT
3421 && bot->coladd == 0
3422#endif
3423 )
3424 {
3425 fromcol = -10;
3426 tocol = MAXCOL;
3427 }
3428 else if (bot->col == MAXCOL)
3429 tocol = MAXCOL;
3430 else
3431 {
3432 pos = *bot;
3433 if (*p_sel == 'e')
3434 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3435 else
3436 {
3437 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3438 ++tocol;
3439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 }
3441 }
3442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003444 /* Check if the character under the cursor should not be inverted */
3445 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003446#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003447 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003448#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003449 )
3450 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003452 /* if inverting in this line set area_highlighting */
3453 if (fromcol >= 0)
3454 {
3455 area_highlighting = TRUE;
3456 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003458 if ((clip_star.available && !clip_star.owned
3459 && clip_isautosel_star())
3460 || (clip_plus.available && !clip_plus.owned
3461 && clip_isautosel_plus()))
3462 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003467 /*
3468 * handle 'incsearch' and ":s///c" highlighting
3469 */
3470 else if (highlight_match
3471 && wp == curwin
3472 && lnum >= curwin->w_cursor.lnum
3473 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003475 if (lnum == curwin->w_cursor.lnum)
3476 getvcol(curwin, &(curwin->w_cursor),
3477 (colnr_T *)&fromcol, NULL, NULL);
3478 else
3479 fromcol = 0;
3480 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3481 {
3482 pos.lnum = lnum;
3483 pos.col = search_match_endcol;
3484 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3485 }
3486 else
3487 tocol = MAXCOL;
3488 /* do at least one character; happens when past end of line */
3489 if (fromcol == tocol)
3490 tocol = fromcol + 1;
3491 area_highlighting = TRUE;
3492 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
3495
3496#ifdef FEAT_DIFF
3497 filler_lines = diff_check(wp, lnum);
3498 if (filler_lines < 0)
3499 {
3500 if (filler_lines == -1)
3501 {
3502 if (diff_find_change(wp, lnum, &change_start, &change_end))
3503 diff_hlf = HLF_ADD; /* added line */
3504 else if (change_start == 0)
3505 diff_hlf = HLF_TXD; /* changed text */
3506 else
3507 diff_hlf = HLF_CHD; /* changed line */
3508 }
3509 else
3510 diff_hlf = HLF_ADD; /* added line */
3511 filler_lines = 0;
3512 area_highlighting = TRUE;
3513 }
3514 if (lnum == wp->w_topline)
3515 filler_lines = wp->w_topfill;
3516 filler_todo = filler_lines;
3517#endif
3518
3519#ifdef LINE_ATTR
3520# ifdef FEAT_SIGNS
3521 /* If this line has a sign with line highlighting set line_attr. */
3522 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3523 if (v != 0)
3524 line_attr = sign_get_attr((int)v, TRUE);
3525# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003526# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003528 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003529 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530# endif
3531 if (line_attr != 0)
3532 area_highlighting = TRUE;
3533#endif
3534
3535 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3536 ptr = line;
3537
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003538#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003539 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003540 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003541 /* For checking first word with a capital skip white space. */
3542 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003543 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003544
Bram Moolenaar30abd282005-06-22 22:35:10 +00003545 /* To be able to spell-check over line boundaries copy the end of the
3546 * current line into nextline[]. Above the start of the next line was
3547 * copied to nextline[SPWORDLEN]. */
3548 if (nextline[SPWORDLEN] == NUL)
3549 {
3550 /* No next line or it is empty. */
3551 nextlinecol = MAXCOL;
3552 nextline_idx = 0;
3553 }
3554 else
3555 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003556 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003557 if (v < SPWORDLEN)
3558 {
3559 /* Short line, use it completely and append the start of the
3560 * next line. */
3561 nextlinecol = 0;
3562 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003563 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003564 nextline_idx = v + 1;
3565 }
3566 else
3567 {
3568 /* Long line, use only the last SPWORDLEN bytes. */
3569 nextlinecol = v - SPWORDLEN;
3570 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3571 nextline_idx = SPWORDLEN + 1;
3572 }
3573 }
3574 }
3575#endif
3576
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003577 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003579 if (lcs_space || lcs_trail)
3580 extra_check = TRUE;
3581 /* find start of trailing whitespace */
3582 if (lcs_trail)
3583 {
3584 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003585 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003586 --trailcol;
3587 trailcol += (colnr_T) (ptr - line);
3588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
3590
3591 /*
3592 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3593 * first character to be displayed.
3594 */
3595 if (wp->w_p_wrap)
3596 v = wp->w_skipcol;
3597 else
3598 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003599 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 {
3601#ifdef FEAT_MBYTE
3602 char_u *prev_ptr = ptr;
3603#endif
3604 while (vcol < v && *ptr != NUL)
3605 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003606 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 vcol += c;
3608#ifdef FEAT_MBYTE
3609 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003611 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
3613
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003614 /* When:
3615 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003616 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003617 * - 'virtualedit' is set, or
3618 * - the visual mode is active,
3619 * the end of the line may be before the start of the displayed part.
3620 */
3621 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003622#ifdef FEAT_SYN_HL
3623 wp->w_p_cuc || draw_color_col ||
3624#endif
3625#ifdef FEAT_VIRTUALEDIT
3626 virtual_active() ||
3627#endif
3628 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003629 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
3633 /* Handle a character that's not completely on the screen: Put ptr at
3634 * that character but skip the first few screen characters. */
3635 if (vcol > v)
3636 {
3637 vcol -= c;
3638#ifdef FEAT_MBYTE
3639 ptr = prev_ptr;
3640#else
3641 --ptr;
3642#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003643 /* If the character fits on the screen, don't need to skip it.
3644 * Except for a TAB. */
3645 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003646#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003647 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003648#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003649 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003650 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 }
3652
3653 /*
3654 * Adjust for when the inverted text is before the screen,
3655 * and when the start of the inverted text is before the screen.
3656 */
3657 if (tocol <= vcol)
3658 fromcol = 0;
3659 else if (fromcol >= 0 && fromcol < vcol)
3660 fromcol = vcol;
3661
3662#ifdef FEAT_LINEBREAK
3663 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3664 if (wp->w_p_wrap)
3665 need_showbreak = TRUE;
3666#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003667#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003668 /* When spell checking a word we need to figure out the start of the
3669 * word and if it's badly spelled or not. */
3670 if (has_spell)
3671 {
3672 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003673 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003674 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003675
3676 pos = wp->w_cursor;
3677 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003678 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003679 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003680
3681 /* spell_move_to() may call ml_get() and make "line" invalid */
3682 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3683 ptr = line + linecol;
3684
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003685 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003686 {
3687 /* no bad word found at line start, don't check until end of a
3688 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003689 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003690 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003691 }
3692 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003693 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003694 /* bad word found, use attributes until end of word */
3695 word_end = wp->w_cursor.col + len + 1;
3696
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003697 /* Turn index into actual attributes. */
3698 if (spell_hlf != HLF_COUNT)
3699 spell_attr = highlight_attr[spell_hlf];
3700 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003701 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003702
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003703# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003704 /* Need to restart syntax highlighting for this line. */
3705 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003706 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003707# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003708 }
3709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711
3712 /*
3713 * Correct highlighting for cursor that can't be disabled.
3714 * Avoids having to check this for each character.
3715 */
3716 if (fromcol >= 0)
3717 {
3718 if (noinvcur)
3719 {
3720 if ((colnr_T)fromcol == wp->w_virtcol)
3721 {
3722 /* highlighting starts at cursor, let it start just after the
3723 * cursor */
3724 fromcol_prev = fromcol;
3725 fromcol = -1;
3726 }
3727 else if ((colnr_T)fromcol < wp->w_virtcol)
3728 /* restart highlighting after the cursor */
3729 fromcol_prev = wp->w_virtcol;
3730 }
3731 if (fromcol >= tocol)
3732 fromcol = -1;
3733 }
3734
3735#ifdef FEAT_SEARCH_EXTRA
3736 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003737 * Handle highlighting the last used search pattern and matches.
3738 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003740 cur = wp->w_match_head;
3741 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003742 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003744 if (shl_flag == FALSE)
3745 {
3746 shl = &search_hl;
3747 shl_flag = TRUE;
3748 }
3749 else
3750 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003751 shl->startcol = MAXCOL;
3752 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003754 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003755 v = (long)(ptr - line);
3756 if (cur != NULL)
3757 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003758 next_search_hl(wp, shl, lnum, (colnr_T)v,
3759 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003760
3761 /* Need to get the line again, a multi-line regexp may have made it
3762 * invalid. */
3763 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3764 ptr = line + v;
3765
3766 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003768 if (shl->lnum == lnum)
3769 shl->startcol = shl->rm.startpos[0].col;
3770 else
3771 shl->startcol = 0;
3772 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3773 - shl->rm.startpos[0].lnum)
3774 shl->endcol = shl->rm.endpos[0].col;
3775 else
3776 shl->endcol = MAXCOL;
3777 /* Highlight one character for an empty match. */
3778 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003781 if (has_mbyte && line[shl->endcol] != NUL)
3782 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3783 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003785 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003787 if ((long)shl->startcol < v) /* match at leftcol */
3788 {
3789 shl->attr_cur = shl->attr;
3790 search_attr = shl->attr;
3791 }
3792 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003794 if (shl != &search_hl && cur != NULL)
3795 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 }
3797#endif
3798
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003799#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003800 /* Cursor line highlighting for 'cursorline' in the current window. Not
3801 * when Visual mode is active, because it's not clear what is selected
3802 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003803 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003804 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003805 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003806 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003807 area_highlighting = TRUE;
3808 }
3809#endif
3810
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003811#ifdef FEAT_TEXT_PROP
3812 {
3813 char_u *prop_start;
3814
3815 text_prop_count = get_text_props(wp->w_buffer, lnum,
3816 &prop_start, FALSE);
3817 if (text_prop_count > 0)
3818 {
3819 // Make a copy of the properties, so that they are properly
3820 // aligned.
3821 text_props = (textprop_T *)alloc(
3822 text_prop_count * sizeof(textprop_T));
3823 if (text_props != NULL)
3824 mch_memmove(text_props, prop_start,
3825 text_prop_count * sizeof(textprop_T));
3826
3827 // Allocate an array for the indexes.
3828 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3829 area_highlighting = TRUE;
3830 extra_check = TRUE;
3831 }
3832 }
3833#endif
3834
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003835 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 col = 0;
3837#ifdef FEAT_RIGHTLEFT
3838 if (wp->w_p_rl)
3839 {
3840 /* Rightleft window: process the text in the normal direction, but put
3841 * it in current_ScreenLine[] from right to left. Start at the
3842 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003843 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 off += col;
3845 }
3846#endif
3847
3848 /*
3849 * Repeat for the whole displayed line.
3850 */
3851 for (;;)
3852 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003853#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003854 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 /* Skip this quickly when working on the text. */
3857 if (draw_state != WL_LINE)
3858 {
3859#ifdef FEAT_CMDWIN
3860 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3861 {
3862 draw_state = WL_CMDLINE;
3863 if (cmdwin_type != 0 && wp == curwin)
3864 {
3865 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003867 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003868 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
3870 }
3871#endif
3872
3873#ifdef FEAT_FOLDING
3874 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3875 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003876 int fdc = compute_foldcolumn(wp, 0);
3877
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003879 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003881 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003882 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003883 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003884 p_extra_free = alloc(12 + 1);
3885
3886 if (p_extra_free != NULL)
3887 {
3888 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3889 n_extra = fdc;
3890 p_extra_free[n_extra] = NUL;
3891 p_extra = p_extra_free;
3892 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003893 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 }
3896 }
3897#endif
3898
3899#ifdef FEAT_SIGNS
3900 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3901 {
3902 draw_state = WL_SIGN;
3903 /* Show the sign column when there are any signs in this
3904 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003905 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003907 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003909 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910# endif
3911
3912 /* Draw two cells with the sign value or blank. */
3913 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003914 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 n_extra = 2;
3916
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003917 if (row == startrow
3918#ifdef FEAT_DIFF
3919 + filler_lines && filler_todo <= 0
3920#endif
3921 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 {
3923 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3924 SIGN_TEXT);
3925# ifdef FEAT_SIGN_ICONS
3926 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3927 SIGN_ICON);
3928 if (gui.in_use && icon_sign != 0)
3929 {
3930 /* Use the image in this position. */
3931 c_extra = SIGN_BYTE;
3932# ifdef FEAT_NETBEANS_INTG
3933 if (buf_signcount(wp->w_buffer, lnum) > 1)
3934 c_extra = MULTISIGN_BYTE;
3935# endif
3936 char_attr = icon_sign;
3937 }
3938 else
3939# endif
3940 if (text_sign != 0)
3941 {
3942 p_extra = sign_get_text(text_sign);
3943 if (p_extra != NULL)
3944 {
3945 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003946 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
3948 char_attr = sign_get_attr(text_sign, FALSE);
3949 }
3950 }
3951 }
3952 }
3953#endif
3954
3955 if (draw_state == WL_NR - 1 && n_extra == 0)
3956 {
3957 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003958 /* Display the absolute or relative line number. After the
3959 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3960 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 && (row == startrow
3962#ifdef FEAT_DIFF
3963 + filler_lines
3964#endif
3965 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3966 {
3967 /* Draw the line number (empty space after wrapping). */
3968 if (row == startrow
3969#ifdef FEAT_DIFF
3970 + filler_lines
3971#endif
3972 )
3973 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003974 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003975 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003976
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003977 if (wp->w_p_nu && !wp->w_p_rnu)
3978 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003979 num = (long)lnum;
3980 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003981 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003982 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003983 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003984 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003985 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003986 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003987 num = lnum;
3988 fmt = "%-*ld ";
3989 }
3990 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003991
Bram Moolenaar700e7342013-01-30 12:31:36 +01003992 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003993 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 if (wp->w_skipcol > 0)
3995 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3996 *p_extra = '-';
3997#ifdef FEAT_RIGHTLEFT
3998 if (wp->w_p_rl) /* reverse line numbers */
3999 rl_mirror(extra);
4000#endif
4001 p_extra = extra;
4002 c_extra = NUL;
4003 }
4004 else
4005 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004006 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004007 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004008#ifdef FEAT_SYN_HL
4009 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004010 * the current line differently.
4011 * TODO: Can we use CursorLine instead of CursorLineNr
4012 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004013 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004014 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004015 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 }
4018 }
4019
Bram Moolenaar597a4222014-06-25 14:39:50 +02004020#ifdef FEAT_LINEBREAK
4021 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4022 && n_extra == 0 && *p_sbr != NUL)
4023 /* draw indent after showbreak value */
4024 draw_state = WL_BRI;
4025 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4026 /* After the showbreak, draw the breakindent */
4027 draw_state = WL_BRI - 1;
4028
4029 /* draw 'breakindent': indent wrapped text accordingly */
4030 if (draw_state == WL_BRI - 1 && n_extra == 0)
4031 {
4032 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004033 /* if need_showbreak is set, breakindent also applies */
4034 if (wp->w_p_bri && n_extra == 0
4035 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004036# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004037 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004038# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004039 )
4040 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004041 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004042# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004043 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004044 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004045 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004046# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004047 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4048 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004049 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004050# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004051 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004052# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004053 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004054 c_extra = ' ';
4055 n_extra = get_breakindent_win(wp,
4056 ml_get_buf(wp->w_buffer, lnum, FALSE));
4057 /* Correct end of highlighted area for 'breakindent',
4058 * required when 'linebreak' is also set. */
4059 if (tocol == vcol)
4060 tocol += n_extra;
4061 }
4062 }
4063#endif
4064
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4066 if (draw_state == WL_SBR - 1 && n_extra == 0)
4067 {
4068 draw_state = WL_SBR;
4069# ifdef FEAT_DIFF
4070 if (filler_todo > 0)
4071 {
4072 /* Draw "deleted" diff line(s). */
4073 if (char2cells(fill_diff) > 1)
4074 c_extra = '-';
4075 else
4076 c_extra = fill_diff;
4077# ifdef FEAT_RIGHTLEFT
4078 if (wp->w_p_rl)
4079 n_extra = col + 1;
4080 else
4081# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004082 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004083 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 }
4085# endif
4086# ifdef FEAT_LINEBREAK
4087 if (*p_sbr != NUL && need_showbreak)
4088 {
4089 /* Draw 'showbreak' at the start of each broken line. */
4090 p_extra = p_sbr;
4091 c_extra = NUL;
4092 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004093 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004095 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 /* Correct end of highlighted area for 'showbreak',
4097 * required when 'linebreak' is also set. */
4098 if (tocol == vcol)
4099 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004100#ifdef FEAT_SYN_HL
4101 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004102 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004103 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004104 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
4107# endif
4108 }
4109#endif
4110
4111 if (draw_state == WL_LINE - 1 && n_extra == 0)
4112 {
4113 draw_state = WL_LINE;
4114 if (saved_n_extra)
4115 {
4116 /* Continue item from end of wrapped line. */
4117 n_extra = saved_n_extra;
4118 c_extra = saved_c_extra;
4119 p_extra = saved_p_extra;
4120 char_attr = saved_char_attr;
4121 }
4122 else
4123 char_attr = 0;
4124 }
4125 }
4126
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004127 // When still displaying '$' of change command, stop at cursor.
4128 // When only displaying the (relative) line number and that's done,
4129 // stop here.
4130 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004131 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132#ifdef FEAT_DIFF
4133 && filler_todo <= 0
4134#endif
4135 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004136 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004138 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004139 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004140 /* Pretend we have finished updating the window. Except when
4141 * 'cursorcolumn' is set. */
4142#ifdef FEAT_SYN_HL
4143 if (wp->w_p_cuc)
4144 row = wp->w_cline_row + wp->w_cline_height;
4145 else
4146#endif
4147 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 break;
4149 }
4150
Bram Moolenaar637532b2019-01-03 21:44:40 +01004151 if (draw_state == WL_LINE && (area_highlighting
4152#ifdef FEAT_SPELL
4153 || has_spell
4154#endif
4155 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
4157 /* handle Visual or match highlighting in this line */
4158 if (vcol == fromcol
4159#ifdef FEAT_MBYTE
4160 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4161 && (*mb_ptr2cells)(ptr) > 1)
4162#endif
4163 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004164 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 && vcol < tocol))
4166 area_attr = attr; /* start highlighting */
4167 else if (area_attr != 0
4168 && (vcol == tocol
4169 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171
4172#ifdef FEAT_SEARCH_EXTRA
4173 if (!n_extra)
4174 {
4175 /*
4176 * Check for start/end of search pattern match.
4177 * After end, check for start/end of next match.
4178 * When another match, have to check for start again.
4179 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004180 * Do this for 'search_hl' and the match list (ordered by
4181 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004183 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004184 cur = wp->w_match_head;
4185 shl_flag = FALSE;
4186 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004188 if (shl_flag == FALSE
4189 && ((cur != NULL
4190 && cur->priority > SEARCH_HL_PRIORITY)
4191 || cur == NULL))
4192 {
4193 shl = &search_hl;
4194 shl_flag = TRUE;
4195 }
4196 else
4197 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004198 if (cur != NULL)
4199 cur->pos.cur = 0;
4200 pos_inprogress = TRUE;
4201 while (shl->rm.regprog != NULL
4202 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004204 if (shl->startcol != MAXCOL
4205 && v >= (long)shl->startcol
4206 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004208#ifdef FEAT_MBYTE
4209 int tmp_col = v + MB_PTR2LEN(ptr);
4210
4211 if (shl->endcol < tmp_col)
4212 shl->endcol = tmp_col;
4213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004215#ifdef FEAT_CONCEAL
4216 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4217 == cur->hlg_id)
4218 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004219 has_match_conc =
4220 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004221 match_conc = cur->conceal_char;
4222 }
4223 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004224 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004227 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
4229 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004230 next_search_hl(wp, shl, lnum, (colnr_T)v,
4231 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004232 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004233 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
4235 /* Need to get the line again, a multi-line regexp
4236 * may have made it invalid. */
4237 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4238 ptr = line + v;
4239
4240 if (shl->lnum == lnum)
4241 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004242 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004244 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004246 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004248 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
4250 /* highlight empty match, try again after
4251 * it */
4252#ifdef FEAT_MBYTE
4253 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004254 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004255 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 else
4257#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004258 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 }
4260
4261 /* Loop to check if the match starts at the
4262 * current position */
4263 continue;
4264 }
4265 }
4266 break;
4267 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004268 if (shl != &search_hl && cur != NULL)
4269 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004271
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004272 /* Use attributes from match with highest priority among
4273 * 'search_hl' and the match list. */
4274 search_attr = search_hl.attr_cur;
4275 cur = wp->w_match_head;
4276 shl_flag = FALSE;
4277 while (cur != NULL || shl_flag == FALSE)
4278 {
4279 if (shl_flag == FALSE
4280 && ((cur != NULL
4281 && cur->priority > SEARCH_HL_PRIORITY)
4282 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004283 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004284 shl = &search_hl;
4285 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004286 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004287 else
4288 shl = &cur->hl;
4289 if (shl->attr_cur != 0)
4290 search_attr = shl->attr_cur;
4291 if (shl != &search_hl && cur != NULL)
4292 cur = cur->next;
4293 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004294 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004295 if (*ptr == NUL && (did_line_attr >= 1
4296 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004297 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299#endif
4300
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004302 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004304 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4305 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004307 if (diff_hlf == HLF_TXD && ptr - line > change_end
4308 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004310 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004311 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004312 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 }
4314#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004315
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004316#ifdef FEAT_TEXT_PROP
4317 if (text_props != NULL)
4318 {
4319 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004320 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004321
4322 // Check if any active property ends.
4323 for (pi = 0; pi < text_props_active; ++pi)
4324 {
4325 int tpi = text_prop_idxs[pi];
4326
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004327 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004328 + text_props[tpi].tp_len)
4329 {
4330 if (pi + 1 < text_props_active)
4331 mch_memmove(text_prop_idxs + pi,
4332 text_prop_idxs + pi + 1,
4333 sizeof(int)
4334 * (text_props_active - (pi + 1)));
4335 --text_props_active;
4336 --pi;
4337 }
4338 }
4339
4340 // Add any text property that starts in this column.
4341 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004342 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004343 text_prop_idxs[text_props_active++] = text_prop_next++;
4344
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004345 text_prop_attr = 0;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004346 if (text_props_active > 0)
4347 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004348 // Sort the properties on priority and/or starting last.
4349 // Then combine the attributes, highest priority last.
4350 current_text_props = text_props;
4351 current_buf = wp->w_buffer;
4352 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4353 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004354
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004355 for (pi = 0; pi < text_props_active; ++pi)
4356 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004357 int tpi = text_prop_idxs[pi];
4358 proptype_T *pt = text_prop_type_by_id(wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004359
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004360 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004361 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004362 int pt_attr = syn_id2attr(pt->pt_hl_id);
4363
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004364 text_prop_type = pt;
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004365 if (text_prop_attr == 0)
4366 text_prop_attr = pt_attr;
4367 else
4368 text_prop_attr = hl_combine_attr(text_prop_attr, pt_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004369 }
4370 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004371 }
4372 }
4373#endif
4374
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004375 /* Decide which of the highlight attributes to use. */
4376 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004377#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004378 if (area_attr != 0)
4379 char_attr = hl_combine_attr(line_attr, area_attr);
4380 else if (search_attr != 0)
4381 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004382 /* Use line_attr when not in the Visual or 'incsearch' area
4383 * (area_attr may be 0 when "noinvcur" is set). */
4384 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004385 || vcol < fromcol || vcol_prev < fromcol_prev
4386 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004387 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004388#else
4389 if (area_attr != 0)
4390 char_attr = area_attr;
4391 else if (search_attr != 0)
4392 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004393#endif
4394 else
4395 {
4396 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004397#ifdef FEAT_TEXT_PROP
4398 if (text_prop_type != NULL)
4399 char_attr = text_prop_attr;
4400 else
4401#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004402#ifdef FEAT_SYN_HL
4403 if (has_syntax)
4404 char_attr = syntax_attr;
4405 else
4406#endif
4407 char_attr = 0;
4408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 }
4410
4411 /*
4412 * Get the next character to put on the screen.
4413 */
4414 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004415 * The "p_extra" points to the extra stuff that is inserted to
4416 * represent special characters (non-printable stuff) and other
4417 * things. When all characters are the same, c_extra is used.
4418 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4419 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4421 */
4422 if (n_extra > 0)
4423 {
4424 if (c_extra != NUL)
4425 {
4426 c = c_extra;
4427#ifdef FEAT_MBYTE
4428 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004429 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 {
4431 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004432 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004433 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
4435 else
4436 mb_utf8 = FALSE;
4437#endif
4438 }
4439 else
4440 {
4441 c = *p_extra;
4442#ifdef FEAT_MBYTE
4443 if (has_mbyte)
4444 {
4445 mb_c = c;
4446 if (enc_utf8)
4447 {
4448 /* If the UTF-8 character is more than one byte:
4449 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004450 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 mb_utf8 = FALSE;
4452 if (mb_l > n_extra)
4453 mb_l = 1;
4454 else if (mb_l > 1)
4455 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004456 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004458 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
4460 }
4461 else
4462 {
4463 /* if this is a DBCS character, put it in "mb_c" */
4464 mb_l = MB_BYTE2LEN(c);
4465 if (mb_l >= n_extra)
4466 mb_l = 1;
4467 else if (mb_l > 1)
4468 mb_c = (c << 8) + p_extra[1];
4469 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004470 if (mb_l == 0) /* at the NUL at end-of-line */
4471 mb_l = 1;
4472
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 /* If a double-width char doesn't fit display a '>' in the
4474 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004475 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476# ifdef FEAT_RIGHTLEFT
4477 wp->w_p_rl ? (col <= 0) :
4478# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004479 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 && (*mb_char2cells)(mb_c) == 2)
4481 {
4482 c = '>';
4483 mb_c = c;
4484 mb_l = 1;
4485 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004486 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 /* put the pointer back to output the double-width
4488 * character at the start of the next line. */
4489 ++n_extra;
4490 --p_extra;
4491 }
4492 else
4493 {
4494 n_extra -= mb_l - 1;
4495 p_extra += mb_l - 1;
4496 }
4497 }
4498#endif
4499 ++p_extra;
4500 }
4501 --n_extra;
4502 }
4503 else
4504 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004505#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004506 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004507#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004508
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004509 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004510 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 /*
4512 * Get a character from the line itself.
4513 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004514 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004515#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004516 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518#ifdef FEAT_MBYTE
4519 if (has_mbyte)
4520 {
4521 mb_c = c;
4522 if (enc_utf8)
4523 {
4524 /* If the UTF-8 character is more than one byte: Decode it
4525 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004526 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 mb_utf8 = FALSE;
4528 if (mb_l > 1)
4529 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004530 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 /* Overlong encoded ASCII or ASCII with composing char
4532 * is displayed normally, except a NUL. */
4533 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004534 {
4535 c = mb_c;
4536# ifdef FEAT_LINEBREAK
4537 c0 = mb_c;
4538# endif
4539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004541
4542 /* At start of the line we can have a composing char.
4543 * Draw it as a space with a composing char. */
4544 if (utf_iscomposing(mb_c))
4545 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004546 int i;
4547
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004548 for (i = Screen_mco - 1; i > 0; --i)
4549 u8cc[i] = u8cc[i - 1];
4550 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004551 mb_c = ' ';
4552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 }
4554
4555 if ((mb_l == 1 && c >= 0x80)
4556 || (mb_l >= 1 && mb_c == 0)
4557 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004558# ifdef UNICODE16
4559 || mb_c >= 0x10000
4560# endif
4561 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
4563 /*
4564 * Illegal UTF-8 byte: display as <xx>.
4565 * Non-BMP character : display as ? or fullwidth ?.
4566 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004567# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004569# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 {
4571 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004572# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 if (wp->w_p_rl) /* reverse */
4574 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004575# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004577# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 else if (utf_char2cells(mb_c) != 2)
4579 STRCPY(extra, "?");
4580 else
4581 /* 0xff1f in UTF-8: full-width '?' */
4582 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004583# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584
4585 p_extra = extra;
4586 c = *p_extra;
4587 mb_c = mb_ptr2char_adv(&p_extra);
4588 mb_utf8 = (c >= 0x80);
4589 n_extra = (int)STRLEN(p_extra);
4590 c_extra = NUL;
4591 if (area_attr == 0 && search_attr == 0)
4592 {
4593 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004594 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 saved_attr2 = char_attr; /* save current attr */
4596 }
4597 }
4598 else if (mb_l == 0) /* at the NUL at end-of-line */
4599 mb_l = 1;
4600#ifdef FEAT_ARABIC
4601 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4602 {
4603 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004604 int pc, pc1, nc;
4605 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606
4607 /* The idea of what is the previous and next
4608 * character depends on 'rightleft'. */
4609 if (wp->w_p_rl)
4610 {
4611 pc = prev_c;
4612 pc1 = prev_c1;
4613 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004614 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 }
4616 else
4617 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004618 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004620 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 }
4622 prev_c = mb_c;
4623
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004624 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
4626 else
4627 prev_c = mb_c;
4628#endif
4629 }
4630 else /* enc_dbcs */
4631 {
4632 mb_l = MB_BYTE2LEN(c);
4633 if (mb_l == 0) /* at the NUL at end-of-line */
4634 mb_l = 1;
4635 else if (mb_l > 1)
4636 {
4637 /* We assume a second byte below 32 is illegal.
4638 * Hopefully this is OK for all double-byte encodings!
4639 */
4640 if (ptr[1] >= 32)
4641 mb_c = (c << 8) + ptr[1];
4642 else
4643 {
4644 if (ptr[1] == NUL)
4645 {
4646 /* head byte at end of line */
4647 mb_l = 1;
4648 transchar_nonprint(extra, c);
4649 }
4650 else
4651 {
4652 /* illegal tail byte */
4653 mb_l = 2;
4654 STRCPY(extra, "XX");
4655 }
4656 p_extra = extra;
4657 n_extra = (int)STRLEN(extra) - 1;
4658 c_extra = NUL;
4659 c = *p_extra++;
4660 if (area_attr == 0 && search_attr == 0)
4661 {
4662 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004663 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 saved_attr2 = char_attr; /* save current attr */
4665 }
4666 mb_c = c;
4667 }
4668 }
4669 }
4670 /* If a double-width char doesn't fit display a '>' in the
4671 * last column; the character is displayed at the start of the
4672 * next line. */
4673 if ((
4674# ifdef FEAT_RIGHTLEFT
4675 wp->w_p_rl ? (col <= 0) :
4676# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004677 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 && (*mb_char2cells)(mb_c) == 2)
4679 {
4680 c = '>';
4681 mb_c = c;
4682 mb_utf8 = FALSE;
4683 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004684 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 /* Put pointer back so that the character will be
4686 * displayed at the start of the next line. */
4687 --ptr;
4688 }
4689 else if (*ptr != NUL)
4690 ptr += mb_l - 1;
4691
4692 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004693 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004694 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004695 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004698 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 c = ' ';
4700 if (area_attr == 0 && search_attr == 0)
4701 {
4702 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004703 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 saved_attr2 = char_attr; /* save current attr */
4705 }
4706 mb_c = c;
4707 mb_utf8 = FALSE;
4708 mb_l = 1;
4709 }
4710
4711 }
4712#endif
4713 ++ptr;
4714
4715 if (extra_check)
4716 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004717#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004718 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004719#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004720
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004721#ifdef FEAT_TERMINAL
4722 if (get_term_attr)
4723 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004724 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004725
4726 if (!attr_pri)
4727 char_attr = syntax_attr;
4728 else
4729 char_attr = hl_combine_attr(syntax_attr, char_attr);
4730 }
4731#endif
4732
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004733#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004734 // Get syntax attribute, unless still at the start of the line
4735 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004736 v = (long)(ptr - line);
4737 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 {
4739 /* Get the syntax attribute for the character. If there
4740 * is an error, disable syntax highlighting. */
4741 save_did_emsg = did_emsg;
4742 did_emsg = FALSE;
4743
Bram Moolenaar217ad922005-03-20 22:37:15 +00004744 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004745# ifdef FEAT_SPELL
4746 has_spell ? &can_spell :
4747# endif
4748 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749
4750 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004751 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004752 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004753 has_syntax = FALSE;
4754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 else
4756 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004757#ifdef SYN_TIME_LIMIT
4758 if (wp->w_s->b_syn_slow)
4759 has_syntax = FALSE;
4760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761
4762 /* Need to get the line again, a multi-line regexp may
4763 * have made it invalid. */
4764 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4765 ptr = line + v;
4766
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004767# ifdef FEAT_TEXT_PROP
4768 // Text properties overrule syntax highlighting.
4769 if (text_prop_attr == 0)
4770#endif
4771 {
4772 if (!attr_pri)
4773 char_attr = syntax_attr;
4774 else
4775 char_attr = hl_combine_attr(syntax_attr, char_attr);
4776 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004777# ifdef FEAT_CONCEAL
4778 /* no concealing past the end of the line, it interferes
4779 * with line highlighting */
4780 if (c == NUL)
4781 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004782 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004783 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004786#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004787
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004788#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004789 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004790 * Only do this when there is no syntax highlighting, the
4791 * @Spell cluster is not used or the current syntax item
4792 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004793 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004794 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004795 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004796 if (c != 0 && (
4797# ifdef FEAT_SYN_HL
4798 !has_syntax ||
4799# endif
4800 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004801 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004802 char_u *prev_ptr, *p;
4803 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004804 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004805# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004806 if (has_mbyte)
4807 {
4808 prev_ptr = ptr - mb_l;
4809 v -= mb_l - 1;
4810 }
4811 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004812# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004813 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004814
4815 /* Use nextline[] if possible, it has the start of the
4816 * next line concatenated. */
4817 if ((prev_ptr - line) - nextlinecol >= 0)
4818 p = nextline + (prev_ptr - line) - nextlinecol;
4819 else
4820 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004821 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004822 len = spell_check(wp, p, &spell_hlf, &cap_col,
4823 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004824 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004825
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004826 /* In Insert mode only highlight a word that
4827 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004828 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004829 && (State & INSERT) != 0
4830 && wp->w_cursor.lnum == lnum
4831 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004832 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004833 && wp->w_cursor.col < (colnr_T)word_end)
4834 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004835 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004836 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004837 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004838
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004839 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004840 && (p - nextline) + len > nextline_idx)
4841 {
4842 /* Remember that the good word continues at the
4843 * start of the next line. */
4844 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004845 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004846 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004847
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004848 /* Turn index into actual attributes. */
4849 if (spell_hlf != HLF_COUNT)
4850 spell_attr = highlight_attr[spell_hlf];
4851
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004852 if (cap_col > 0)
4853 {
4854 if (p != prev_ptr
4855 && (p - nextline) + cap_col >= nextline_idx)
4856 {
4857 /* Remember that the word in the next line
4858 * must start with a capital. */
4859 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004860 cap_col = (int)((p - nextline) + cap_col
4861 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004862 }
4863 else
4864 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004865 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004866 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004867 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004868 }
4869 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004870 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004871 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004872 char_attr = hl_combine_attr(char_attr, spell_attr);
4873 else
4874 char_attr = hl_combine_attr(spell_attr, char_attr);
4875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876#endif
4877#ifdef FEAT_LINEBREAK
4878 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004879 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004881 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004882 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004884# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004885 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004886# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004887 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004889 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004891 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004892
Bram Moolenaar597a4222014-06-25 14:39:50 +02004893 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004894 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004895 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004896 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004897# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004898 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004899 wp->w_buffer->b_p_vts_array) - 1;
4900# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004901 n_extra = (int)wp->w_buffer->b_p_ts
4902 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004903# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004904
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004905# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004906 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004907# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004909# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004910 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004911 {
4912#ifdef FEAT_CONCEAL
4913 if (c == TAB)
4914 /* See "Tab alignment" below. */
4915 FIX_FOR_BOGUSCOLS;
4916#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004917 if (!wp->w_p_list)
4918 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 }
4921#endif
4922
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004923 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4924 */
4925 if (wp->w_p_list
4926 && (((c == 160
4927#ifdef FEAT_MBYTE
4928 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4929#endif
4930 ) && lcs_nbsp)
4931 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4932 {
4933 c = (c == ' ') ? lcs_space : lcs_nbsp;
4934 if (area_attr == 0 && search_attr == 0)
4935 {
4936 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004937 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004938 saved_attr2 = char_attr; /* save current attr */
4939 }
4940#ifdef FEAT_MBYTE
4941 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004942 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004943 {
4944 mb_utf8 = TRUE;
4945 u8cc[0] = 0;
4946 c = 0xc0;
4947 }
4948 else
4949 mb_utf8 = FALSE;
4950#endif
4951 }
4952
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4954 {
4955 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004956 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
4958 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004959 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 saved_attr2 = char_attr; /* save current attr */
4961 }
4962#ifdef FEAT_MBYTE
4963 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004964 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
4966 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004967 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004968 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 }
4970 else
4971 mb_utf8 = FALSE;
4972#endif
4973 }
4974 }
4975
4976 /*
4977 * Handling of non-printable characters.
4978 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004979 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
4981 /*
4982 * when getting a character from the file, we may have to
4983 * turn it into something else on the way to putting it
4984 * into "ScreenLines".
4985 */
4986 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4987 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004988 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004989 long vcol_adjusted = vcol; /* removed showbreak length */
4990#ifdef FEAT_LINEBREAK
4991 /* only adjust the tab_len, when at the first column
4992 * after the showbreak value was drawn */
4993 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4994 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004997#ifdef FEAT_VARTABS
4998 tab_len = tabstop_padding(vcol_adjusted,
4999 wp->w_buffer->b_p_ts,
5000 wp->w_buffer->b_p_vts_array) - 1;
5001#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005002 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005003 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5004#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005005
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005006#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005007 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005008#endif
5009 /* tab amount depends on current column */
5010 n_extra = tab_len;
5011#ifdef FEAT_LINEBREAK
5012 else
5013 {
5014 char_u *p;
5015 int len = n_extra;
5016 int i;
5017 int saved_nextra = n_extra;
5018
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005019#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005020 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005021 /* there are characters to conceal */
5022 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005023 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5024 */
5025 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5026 && n_extra > tab_len)
5027 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005028#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005029
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005030 /* if n_extra > 0, it gives the number of chars, to
5031 * use for a tab, else we need to calculate the width
5032 * for a tab */
5033#ifdef FEAT_MBYTE
5034 len = (tab_len * mb_char2len(lcs_tab2));
5035 if (n_extra > 0)
5036 len += n_extra - tab_len;
5037#endif
5038 c = lcs_tab1;
5039 p = alloc((unsigned)(len + 1));
5040 vim_memset(p, ' ', len);
5041 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005042 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005043 p_extra_free = p;
5044 for (i = 0; i < tab_len; i++)
5045 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005046 if (*p == NUL)
5047 {
5048 tab_len = i;
5049 break;
5050 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005051#ifdef FEAT_MBYTE
5052 mb_char2bytes(lcs_tab2, p);
5053 p += mb_char2len(lcs_tab2);
5054 n_extra += mb_char2len(lcs_tab2)
5055 - (saved_nextra > 0 ? 1 : 0);
5056#else
5057 p[i] = lcs_tab2;
5058#endif
5059 }
5060 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005061#ifdef FEAT_CONCEAL
5062 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5063 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005064 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005065 n_extra -= vcol_off;
5066#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005067 }
5068#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005069#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005070 {
5071 int vc_saved = vcol_off;
5072
5073 /* Tab alignment should be identical regardless of
5074 * 'conceallevel' value. So tab compensates of all
5075 * previous concealed characters, and thus resets
5076 * vcol_off and boguscols accumulated so far in the
5077 * line. Note that the tab can be longer than
5078 * 'tabstop' when there are concealed characters. */
5079 FIX_FOR_BOGUSCOLS;
5080
5081 /* Make sure, the highlighting for the tab char will be
5082 * correctly set further below (effectively reverts the
5083 * FIX_FOR_BOGSUCOLS macro */
5084 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005085 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005086 tab_len += vc_saved;
5087 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089#ifdef FEAT_MBYTE
5090 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5091#endif
5092 if (wp->w_p_list)
5093 {
5094 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005095#ifdef FEAT_LINEBREAK
5096 if (wp->w_p_lbr)
5097 c_extra = NUL; /* using p_extra from above */
5098 else
5099#endif
5100 c_extra = lcs_tab2;
5101 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005102 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 saved_attr2 = char_attr; /* save current attr */
5104#ifdef FEAT_MBYTE
5105 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005106 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
5108 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005109 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005110 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
5112#endif
5113 }
5114 else
5115 {
5116 c_extra = ' ';
5117 c = ' ';
5118 }
5119 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005120 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005121 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005122 || ((fromcol >= 0 || fromcol_prev >= 0)
5123 && tocol > vcol
5124 && VIsual_mode != Ctrl_V
5125 && (
5126# ifdef FEAT_RIGHTLEFT
5127 wp->w_p_rl ? (col >= 0) :
5128# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005129 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005130 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005131 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005132 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005133 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005135 /* Display a '$' after the line or highlight an extra
5136 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5138 /* For a diff line the highlighting continues after the
5139 * "$". */
5140 if (
5141# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005142 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143# ifdef LINE_ATTR
5144 &&
5145# endif
5146# endif
5147# ifdef LINE_ATTR
5148 line_attr == 0
5149# endif
5150 )
5151#endif
5152 {
5153#ifdef FEAT_VIRTUALEDIT
5154 /* In virtualedit, visual selections may extend
5155 * beyond end of line. */
5156 if (area_highlighting && virtual_active()
5157 && tocol != MAXCOL && vcol < tocol)
5158 n_extra = 0;
5159 else
5160#endif
5161 {
5162 p_extra = at_end_str;
5163 n_extra = 1;
5164 c_extra = NUL;
5165 }
5166 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005167 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005168 c = lcs_eol;
5169 else
5170 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 lcs_eol_one = -1;
5172 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005173 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005175 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 n_attr = 1;
5177 }
5178#ifdef FEAT_MBYTE
5179 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005180 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 {
5182 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005183 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005184 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186 else
5187 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5188#endif
5189 }
5190 else if (c != NUL)
5191 {
5192 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005193 if (n_extra == 0)
5194 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195#ifdef FEAT_RIGHTLEFT
5196 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5197 rl_mirror(p_extra); /* reverse "<12>" */
5198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005200#ifdef FEAT_LINEBREAK
5201 if (wp->w_p_lbr)
5202 {
5203 char_u *p;
5204
5205 c = *p_extra;
5206 p = alloc((unsigned)n_extra + 1);
5207 vim_memset(p, ' ', n_extra);
5208 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5209 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005210 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005211 p_extra_free = p_extra = p;
5212 }
5213 else
5214#endif
5215 {
5216 n_extra = byte2cells(c) - 1;
5217 c = *p_extra++;
5218 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005219 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {
5221 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005222 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 saved_attr2 = char_attr; /* save current attr */
5224 }
5225#ifdef FEAT_MBYTE
5226 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5227#endif
5228 }
5229#ifdef FEAT_VIRTUALEDIT
5230 else if (VIsual_active
5231 && (VIsual_mode == Ctrl_V
5232 || VIsual_mode == 'v')
5233 && virtual_active()
5234 && tocol != MAXCOL
5235 && vcol < tocol
5236 && (
5237# ifdef FEAT_RIGHTLEFT
5238 wp->w_p_rl ? (col >= 0) :
5239# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005240 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 {
5242 c = ' ';
5243 --ptr; /* put it back at the NUL */
5244 }
5245#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005246#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 else if ((
5248# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005249 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005251# ifdef FEAT_TERMINAL
5252 term_attr != 0 ||
5253# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 ) && (
5256# ifdef FEAT_RIGHTLEFT
5257 wp->w_p_rl ? (col >= 0) :
5258# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005259 (col
5260# ifdef FEAT_CONCEAL
5261 - boguscols
5262# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005263 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 {
5265 /* Highlight until the right side of the window */
5266 c = ' ';
5267 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005268
5269 /* Remember we do the char for line highlighting. */
5270 ++did_line_attr;
5271
5272 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005273 if (line_attr != 0 && char_attr == search_attr
5274 && (did_line_attr > 1
5275 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005276 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277# ifdef FEAT_DIFF
5278 if (diff_hlf == HLF_TXD)
5279 {
5280 diff_hlf = HLF_CHD;
5281 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005282 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005283 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005284 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5285 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005286 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 }
5289# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005290# ifdef FEAT_TERMINAL
5291 if (term_attr != 0)
5292 {
5293 char_attr = term_attr;
5294 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5295 char_attr = hl_combine_attr(char_attr,
5296 HL_ATTR(HLF_CUL));
5297 }
5298# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 }
5300#endif
5301 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005302
5303#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005304 if ( wp->w_p_cole > 0
5305 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005306 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005307 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005308 && !(lnum_in_visual_area
5309 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005310 {
5311 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005312 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005313 && (syn_get_sub_char() != NUL || match_conc
5314 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005315 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005316 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005317 /* First time at this concealed item: display one
5318 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005319 if (match_conc)
5320 c = match_conc;
5321 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005322 c = syn_get_sub_char();
5323 else if (lcs_conceal != NUL)
5324 c = lcs_conceal;
5325 else
5326 c = ' ';
5327
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005328 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005329
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005330 if (n_extra > 0)
5331 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005332 vcol += n_extra;
5333 if (wp->w_p_wrap && n_extra > 0)
5334 {
5335# ifdef FEAT_RIGHTLEFT
5336 if (wp->w_p_rl)
5337 {
5338 col -= n_extra;
5339 boguscols -= n_extra;
5340 }
5341 else
5342# endif
5343 {
5344 boguscols += n_extra;
5345 col += n_extra;
5346 }
5347 }
5348 n_extra = 0;
5349 n_attr = 0;
5350 }
5351 else if (n_skip == 0)
5352 {
5353 is_concealing = TRUE;
5354 n_skip = 1;
5355 }
5356# ifdef FEAT_MBYTE
5357 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005358 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005359 {
5360 mb_utf8 = TRUE;
5361 u8cc[0] = 0;
5362 c = 0xc0;
5363 }
5364 else
5365 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5366# endif
5367 }
5368 else
5369 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005370 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005371 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005372 }
5373#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
5375
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005376#ifdef FEAT_CONCEAL
5377 /* In the cursor line and we may be concealing characters: correct
5378 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005379 if (!did_wcol && draw_state == WL_LINE
5380 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005381 && conceal_cursor_line(wp)
5382 && (int)wp->w_virtcol <= vcol + n_skip)
5383 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005384# ifdef FEAT_RIGHTLEFT
5385 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005386 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005387 else
5388# endif
5389 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005390 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005391 did_wcol = TRUE;
5392 }
5393#endif
5394
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 /* Don't override visual selection highlighting. */
5396 if (n_attr > 0
5397 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005398 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 char_attr = extra_attr;
5400
Bram Moolenaar81695252004-12-29 20:58:21 +00005401#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 /* XIM don't send preedit_start and preedit_end, but they send
5403 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5404 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005405 if (p_imst == IM_ON_THE_SPOT
5406 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005407 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 && (State & INSERT)
5409 && !p_imdisable
5410 && im_is_preediting()
5411 && draw_state == WL_LINE)
5412 {
5413 colnr_T tcol;
5414
5415 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005416 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 else
5418 tcol = preedit_end_col;
5419 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5420 {
5421 if (feedback_old_attr < 0)
5422 {
5423 feedback_col = 0;
5424 feedback_old_attr = char_attr;
5425 }
5426 char_attr = im_get_feedback_attr(feedback_col);
5427 if (char_attr < 0)
5428 char_attr = feedback_old_attr;
5429 feedback_col++;
5430 }
5431 else if (feedback_old_attr >= 0)
5432 {
5433 char_attr = feedback_old_attr;
5434 feedback_old_attr = -1;
5435 feedback_col = 0;
5436 }
5437 }
5438#endif
5439 /*
5440 * Handle the case where we are in column 0 but not on the first
5441 * character of the line and the user wants us to show us a
5442 * special character (via 'listchars' option "precedes:<char>".
5443 */
5444 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005445 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5447#ifdef FEAT_DIFF
5448 && filler_todo <= 0
5449#endif
5450 && draw_state > WL_NR
5451 && c != NUL)
5452 {
5453 c = lcs_prec;
5454 lcs_prec_todo = NUL;
5455#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005456 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5457 {
5458 /* Double-width character being overwritten by the "precedes"
5459 * character, need to fill up half the character. */
5460 c_extra = MB_FILLER_CHAR;
5461 n_extra = 1;
5462 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005463 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005466 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 {
5468 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005469 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005470 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
5472 else
5473 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5474#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005475 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 {
5477 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005478 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 n_attr3 = 1;
5480 }
5481 }
5482
5483 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005484 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005486 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005487#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005488 || did_line_attr == 1
5489#endif
5490 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005492#ifdef FEAT_SEARCH_EXTRA
5493 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005494
5495 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005496 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005497 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005498#endif
5499
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005500 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 * highlight match at end of line. If it's beyond the last
5502 * char on the screen, just overwrite that one (tricky!) Not
5503 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005504#ifdef FEAT_SEARCH_EXTRA
5505 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005506 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005507 prevcol_hl_flag = TRUE;
5508 else
5509 {
5510 cur = wp->w_match_head;
5511 while (cur != NULL)
5512 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005513 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005514 {
5515 prevcol_hl_flag = TRUE;
5516 break;
5517 }
5518 cur = cur->next;
5519 }
5520 }
5521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005523 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005524 && (VIsual_mode != Ctrl_V
5525 || lnum == VIsual.lnum
5526 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005527 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528#ifdef FEAT_SEARCH_EXTRA
5529 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005530 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005531# ifdef FEAT_SYN_HL
5532 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5533 && !(wp == curwin && VIsual_active))
5534# endif
5535# ifdef FEAT_DIFF
5536 && diff_hlf == (hlf_T)0
5537# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005538# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005539 && did_line_attr <= 1
5540# endif
5541 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542#endif
5543 ))
5544 {
5545 int n = 0;
5546
5547#ifdef FEAT_RIGHTLEFT
5548 if (wp->w_p_rl)
5549 {
5550 if (col < 0)
5551 n = 1;
5552 }
5553 else
5554#endif
5555 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005556 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 n = -1;
5558 }
5559 if (n != 0)
5560 {
5561 /* At the window boundary, highlight the last character
5562 * instead (better than nothing). */
5563 off += n;
5564 col += n;
5565 }
5566 else
5567 {
5568 /* Add a blank character to highlight. */
5569 ScreenLines[off] = ' ';
5570#ifdef FEAT_MBYTE
5571 if (enc_utf8)
5572 ScreenLinesUC[off] = 0;
5573#endif
5574 }
5575#ifdef FEAT_SEARCH_EXTRA
5576 if (area_attr == 0)
5577 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005578 /* Use attributes from match with highest priority among
5579 * 'search_hl' and the match list. */
5580 char_attr = search_hl.attr;
5581 cur = wp->w_match_head;
5582 shl_flag = FALSE;
5583 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005584 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005585 if (shl_flag == FALSE
5586 && ((cur != NULL
5587 && cur->priority > SEARCH_HL_PRIORITY)
5588 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005589 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005590 shl = &search_hl;
5591 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005592 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005593 else
5594 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005595 if ((ptr - line) - 1 == (long)shl->startcol
5596 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005597 char_attr = shl->attr;
5598 if (shl != &search_hl && cur != NULL)
5599 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
5602#endif
5603 ScreenAttrs[off] = char_attr;
5604#ifdef FEAT_RIGHTLEFT
5605 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005606 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005608 --off;
5609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 else
5611#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005612 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005614 ++off;
5615 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005616 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005617#ifdef FEAT_SYN_HL
5618 eol_hl_off = 1;
5619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622
Bram Moolenaar91170f82006-05-05 21:15:17 +00005623 /*
5624 * At end of the text line.
5625 */
5626 if (c == NUL)
5627 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005628#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005629 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005630 if (wp->w_p_wrap)
5631 v = wp->w_skipcol;
5632 else
5633 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005634
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005635 /* check if line ends before left margin */
5636 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005637 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005638#ifdef FEAT_CONCEAL
5639 /* Get rid of the boguscols now, we want to draw until the right
5640 * edge for 'cursorcolumn'. */
5641 col -= boguscols;
5642 boguscols = 0;
5643#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005644
5645 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005646 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005647
5648 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005649 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5650 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005651 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005652 && lnum != wp->w_cursor.lnum)
5653 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005654# ifdef FEAT_RIGHTLEFT
5655 && !wp->w_p_rl
5656# endif
5657 )
5658 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005659 int rightmost_vcol = 0;
5660 int i;
5661
5662 if (wp->w_p_cuc)
5663 rightmost_vcol = wp->w_virtcol;
5664 if (draw_color_col)
5665 /* determine rightmost colorcolumn to possibly draw */
5666 for (i = 0; color_cols[i] >= 0; ++i)
5667 if (rightmost_vcol < color_cols[i])
5668 rightmost_vcol = color_cols[i];
5669
Bram Moolenaar02631462017-09-22 15:20:32 +02005670 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005671 {
5672 ScreenLines[off] = ' ';
5673#ifdef FEAT_MBYTE
5674 if (enc_utf8)
5675 ScreenLinesUC[off] = 0;
5676#endif
5677 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005678 if (draw_color_col)
5679 draw_color_col = advance_color_col(VCOL_HLC,
5680 &color_cols);
5681
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005682 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005683 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005684 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005685 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005686 else
5687 ScreenAttrs[off++] = 0;
5688
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005689 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005690 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005691
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005692 ++vcol;
5693 }
5694 }
5695#endif
5696
Bram Moolenaar53f81742017-09-22 14:35:51 +02005697 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005698 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 row++;
5700
5701 /*
5702 * Update w_cline_height and w_cline_folded if the cursor line was
5703 * updated (saves a call to plines() later).
5704 */
5705 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5706 {
5707 curwin->w_cline_row = startrow;
5708 curwin->w_cline_height = row - startrow;
5709#ifdef FEAT_FOLDING
5710 curwin->w_cline_folded = FALSE;
5711#endif
5712 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5713 }
5714
5715 break;
5716 }
5717
5718 /* line continues beyond line end */
5719 if (lcs_ext
5720 && !wp->w_p_wrap
5721#ifdef FEAT_DIFF
5722 && filler_todo <= 0
5723#endif
5724 && (
5725#ifdef FEAT_RIGHTLEFT
5726 wp->w_p_rl ? col == 0 :
5727#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005728 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005730 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5732 {
5733 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005734 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735#ifdef FEAT_MBYTE
5736 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005737 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 {
5739 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005740 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005741 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 }
5743 else
5744 mb_utf8 = FALSE;
5745#endif
5746 }
5747
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005748#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005749 /* advance to the next 'colorcolumn' */
5750 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005751 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005752
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005753 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005754 * highlight the cursor position itself.
5755 * Also highlight the 'colorcolumn' if it is different than
5756 * 'cursorcolumn' */
5757 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005758 if (draw_state == WL_LINE && !lnum_in_visual_area
5759 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005760 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005761 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005762 && lnum != wp->w_cursor.lnum)
5763 {
5764 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005765 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005766 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005767 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005768 {
5769 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005770 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005771 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005772 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005773#endif
5774
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 /*
5776 * Store character to be displayed.
5777 * Skip characters that are left of the screen for 'nowrap'.
5778 */
5779 vcol_prev = vcol;
5780 if (draw_state < WL_LINE || n_skip <= 0)
5781 {
5782 /*
5783 * Store the character.
5784 */
5785#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5786 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5787 {
5788 /* A double-wide character is: put first halve in left cell. */
5789 --off;
5790 --col;
5791 }
5792#endif
5793 ScreenLines[off] = c;
5794#ifdef FEAT_MBYTE
5795 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005796 {
5797 if ((mb_c & 0xff00) == 0x8e00)
5798 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 else if (enc_utf8)
5802 {
5803 if (mb_utf8)
5804 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005805 int i;
5806
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005808 if ((c & 0xff) == 0)
5809 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005810 for (i = 0; i < Screen_mco; ++i)
5811 {
5812 ScreenLinesC[i][off] = u8cc[i];
5813 if (u8cc[i] == 0)
5814 break;
5815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 }
5817 else
5818 ScreenLinesUC[off] = 0;
5819 }
5820 if (multi_attr)
5821 {
5822 ScreenAttrs[off] = multi_attr;
5823 multi_attr = 0;
5824 }
5825 else
5826#endif
5827 ScreenAttrs[off] = char_attr;
5828
5829#ifdef FEAT_MBYTE
5830 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5831 {
5832 /* Need to fill two screen columns. */
5833 ++off;
5834 ++col;
5835 if (enc_utf8)
5836 /* UTF-8: Put a 0 in the second screen char. */
5837 ScreenLines[off] = 0;
5838 else
5839 /* DBCS: Put second byte in the second screen char. */
5840 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005841 if (draw_state > WL_NR
5842#ifdef FEAT_DIFF
5843 && filler_todo <= 0
5844#endif
5845 )
5846 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 /* When "tocol" is halfway a character, set it to the end of
5848 * the character, otherwise highlighting won't stop. */
5849 if (tocol == vcol)
5850 ++tocol;
5851#ifdef FEAT_RIGHTLEFT
5852 if (wp->w_p_rl)
5853 {
5854 /* now it's time to backup one cell */
5855 --off;
5856 --col;
5857 }
5858#endif
5859 }
5860#endif
5861#ifdef FEAT_RIGHTLEFT
5862 if (wp->w_p_rl)
5863 {
5864 --off;
5865 --col;
5866 }
5867 else
5868#endif
5869 {
5870 ++off;
5871 ++col;
5872 }
5873 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005874#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005875 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005876 {
5877 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005878 ++vcol_off;
5879 if (n_extra > 0)
5880 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005881 if (wp->w_p_wrap)
5882 {
5883 /*
5884 * Special voodoo required if 'wrap' is on.
5885 *
5886 * Advance the column indicator to force the line
5887 * drawing to wrap early. This will make the line
5888 * take up the same screen space when parts are concealed,
5889 * so that cursor line computations aren't messed up.
5890 *
5891 * To avoid the fictitious advance of 'col' causing
5892 * trailing junk to be written out of the screen line
5893 * we are building, 'boguscols' keeps track of the number
5894 * of bad columns we have advanced.
5895 */
5896 if (n_extra > 0)
5897 {
5898 vcol += n_extra;
5899# ifdef FEAT_RIGHTLEFT
5900 if (wp->w_p_rl)
5901 {
5902 col -= n_extra;
5903 boguscols -= n_extra;
5904 }
5905 else
5906# endif
5907 {
5908 col += n_extra;
5909 boguscols += n_extra;
5910 }
5911 n_extra = 0;
5912 n_attr = 0;
5913 }
5914
5915
5916# ifdef FEAT_MBYTE
5917 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5918 {
5919 /* Need to fill two screen columns. */
5920# ifdef FEAT_RIGHTLEFT
5921 if (wp->w_p_rl)
5922 {
5923 --boguscols;
5924 --col;
5925 }
5926 else
5927# endif
5928 {
5929 ++boguscols;
5930 ++col;
5931 }
5932 }
5933# endif
5934
5935# ifdef FEAT_RIGHTLEFT
5936 if (wp->w_p_rl)
5937 {
5938 --boguscols;
5939 --col;
5940 }
5941 else
5942# endif
5943 {
5944 ++boguscols;
5945 ++col;
5946 }
5947 }
5948 else
5949 {
5950 if (n_extra > 0)
5951 {
5952 vcol += n_extra;
5953 n_extra = 0;
5954 n_attr = 0;
5955 }
5956 }
5957
5958 }
5959#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 else
5961 --n_skip;
5962
Bram Moolenaar64486672010-05-16 15:46:46 +02005963 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5964 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005965 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966#ifdef FEAT_DIFF
5967 && filler_todo <= 0
5968#endif
5969 )
5970 ++vcol;
5971
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005972#ifdef FEAT_SYN_HL
5973 if (vcol_save_attr >= 0)
5974 char_attr = vcol_save_attr;
5975#endif
5976
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 /* restore attributes after "predeces" in 'listchars' */
5978 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5979 char_attr = saved_attr3;
5980
5981 /* restore attributes after last 'listchars' or 'number' char */
5982 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5983 char_attr = saved_attr2;
5984
5985 /*
5986 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005987 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 */
5989 if ((
5990#ifdef FEAT_RIGHTLEFT
5991 wp->w_p_rl ? (col < 0) :
5992#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005993 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 && (*ptr != NUL
5995#ifdef FEAT_DIFF
5996 || filler_todo > 0
5997#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005998 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6000 )
6001 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006002#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006003 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02006004 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02006005 boguscols = 0;
6006#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006007 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02006008 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02006009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 ++row;
6011 ++screen_row;
6012
6013 /* When not wrapping and finished diff lines, or when displayed
6014 * '$' and highlighting until last column, break here. */
6015 if ((!wp->w_p_wrap
6016#ifdef FEAT_DIFF
6017 && filler_todo <= 0
6018#endif
6019 ) || lcs_eol_one == -1)
6020 break;
6021
6022 /* When the window is too narrow draw all "@" lines. */
6023 if (draw_state != WL_LINE
6024#ifdef FEAT_DIFF
6025 && filler_todo <= 0
6026#endif
6027 )
6028 {
6029 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 row = endrow;
6032 }
6033
6034 /* When line got too long for screen break here. */
6035 if (row == endrow)
6036 {
6037 ++row;
6038 break;
6039 }
6040
6041 if (screen_cur_row == screen_row - 1
6042#ifdef FEAT_DIFF
6043 && filler_todo <= 0
6044#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006045 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 {
6047 /* Remember that the line wraps, used for modeless copy. */
6048 LineWraps[screen_row - 1] = TRUE;
6049
6050 /*
6051 * Special trick to make copy/paste of wrapped lines work with
6052 * xterm/screen: write an extra character beyond the end of
6053 * the line. This will work with all terminal types
6054 * (regardless of the xn,am settings).
6055 * Only do this on a fast tty.
6056 * Only do this if the cursor is on the current line
6057 * (something has been written in it).
6058 * Don't do this for the GUI.
6059 * Don't do this for double-width characters.
6060 * Don't do this for a window not at the right screen border.
6061 */
6062 if (p_tf
6063#ifdef FEAT_GUI
6064 && !gui.in_use
6065#endif
6066#ifdef FEAT_MBYTE
6067 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006068 && ((*mb_off2cells)(LineOffset[screen_row],
6069 LineOffset[screen_row] + screen_Columns)
6070 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006072 + (int)Columns - 2,
6073 LineOffset[screen_row] + screen_Columns)
6074 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075#endif
6076 )
6077 {
6078 /* First make sure we are at the end of the screen line,
6079 * then output the same character again to let the
6080 * terminal know about the wrap. If the terminal doesn't
6081 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006082 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 screen_char(LineOffset[screen_row - 1]
6084 + (unsigned)Columns - 1,
6085 screen_row - 1, (int)(Columns - 1));
6086
6087#ifdef FEAT_MBYTE
6088 /* When there is a multi-byte character, just output a
6089 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006090 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6091 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 out_char(' ');
6093 else
6094#endif
6095 out_char(ScreenLines[LineOffset[screen_row - 1]
6096 + (Columns - 1)]);
6097 /* force a redraw of the first char on the next line */
6098 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6099 screen_start(); /* don't know where cursor is now */
6100 }
6101 }
6102
6103 col = 0;
6104 off = (unsigned)(current_ScreenLine - ScreenLines);
6105#ifdef FEAT_RIGHTLEFT
6106 if (wp->w_p_rl)
6107 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006108 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 off += col;
6110 }
6111#endif
6112
6113 /* reset the drawing state for the start of a wrapped line */
6114 draw_state = WL_START;
6115 saved_n_extra = n_extra;
6116 saved_p_extra = p_extra;
6117 saved_c_extra = c_extra;
6118 saved_char_attr = char_attr;
6119 n_extra = 0;
6120 lcs_prec_todo = lcs_prec;
6121#ifdef FEAT_LINEBREAK
6122# ifdef FEAT_DIFF
6123 if (filler_todo <= 0)
6124# endif
6125 need_showbreak = TRUE;
6126#endif
6127#ifdef FEAT_DIFF
6128 --filler_todo;
6129 /* When the filler lines are actually below the last line of the
6130 * file, don't draw the line itself, break here. */
6131 if (filler_todo == 0 && wp->w_botfill)
6132 break;
6133#endif
6134 }
6135
6136 } /* for every character in the line */
6137
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006138#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006139 /* After an empty line check first word for capital. */
6140 if (*skipwhite(line) == NUL)
6141 {
6142 capcol_lnum = lnum + 1;
6143 cap_col = 0;
6144 }
6145#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006146#ifdef FEAT_TEXT_PROP
6147 vim_free(text_props);
6148 vim_free(text_prop_idxs);
6149#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006150
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006151 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 return row;
6153}
6154
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006155#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006156/*
6157 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006158 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006159 */
6160 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006161comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006162{
6163 int i;
6164
6165 for (i = 0; i < Screen_mco; ++i)
6166 {
6167 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6168 return TRUE;
6169 if (ScreenLinesC[i][off_from] == 0)
6170 break;
6171 }
6172 return FALSE;
6173}
6174#endif
6175
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176/*
6177 * Check whether the given character needs redrawing:
6178 * - the (first byte of the) character is different
6179 * - the attributes are different
6180 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006181 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 */
6183 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006184char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185{
6186 if (cols > 0
6187 && ((ScreenLines[off_from] != ScreenLines[off_to]
6188 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6189
6190#ifdef FEAT_MBYTE
6191 || (enc_dbcs != 0
6192 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6193 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6194 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6195 : (cols > 1 && ScreenLines[off_from + 1]
6196 != ScreenLines[off_to + 1])))
6197 || (enc_utf8
6198 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6199 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006200 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006201 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6202 && ScreenLines[off_from + 1]
6203 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204#endif
6205 ))
6206 return TRUE;
6207 return FALSE;
6208}
6209
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006210#if defined(FEAT_TERMINAL) || defined(PROTO)
6211/*
6212 * Return the index in ScreenLines[] for the current screen line.
6213 */
6214 int
6215screen_get_current_line_off()
6216{
6217 return (int)(current_ScreenLine - ScreenLines);
6218}
6219#endif
6220
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221/*
6222 * Move one "cooked" screen line to the screen, but only the characters that
6223 * have actually changed. Handle insert/delete character.
6224 * "coloff" gives the first column on the screen for this line.
6225 * "endcol" gives the columns where valid characters are.
6226 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6227 * needs to be cleared, negative otherwise.
6228 * "rlflag" is TRUE in a rightleft window:
6229 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6230 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6231 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006232 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006233screen_line(
6234 int row,
6235 int coloff,
6236 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006237 int clear_width,
6238 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239{
6240 unsigned off_from;
6241 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006242#ifdef FEAT_MBYTE
6243 unsigned max_off_from;
6244 unsigned max_off_to;
6245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 int force = FALSE; /* force update rest of the line */
6249 int redraw_this /* bool: does character need redraw? */
6250#ifdef FEAT_GUI
6251 = TRUE /* For GUI when while-loop empty */
6252#endif
6253 ;
6254 int redraw_next; /* redraw_this for next character */
6255#ifdef FEAT_MBYTE
6256 int clear_next = FALSE;
6257 int char_cells; /* 1: normal char */
6258 /* 2: occupies two display cells */
6259# define CHAR_CELLS char_cells
6260#else
6261# define CHAR_CELLS 1
6262#endif
6263
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006264 /* Check for illegal row and col, just in case. */
6265 if (row >= Rows)
6266 row = Rows - 1;
6267 if (endcol > Columns)
6268 endcol = Columns;
6269
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270# ifdef FEAT_CLIPBOARD
6271 clip_may_clear_selection(row, row);
6272# endif
6273
6274 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6275 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006276#ifdef FEAT_MBYTE
6277 max_off_from = off_from + screen_Columns;
6278 max_off_to = LineOffset[row] + screen_Columns;
6279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280
6281#ifdef FEAT_RIGHTLEFT
6282 if (rlflag)
6283 {
6284 /* Clear rest first, because it's left of the text. */
6285 if (clear_width > 0)
6286 {
6287 while (col <= endcol && ScreenLines[off_to] == ' '
6288 && ScreenAttrs[off_to] == 0
6289# ifdef FEAT_MBYTE
6290 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6291# endif
6292 )
6293 {
6294 ++off_to;
6295 ++col;
6296 }
6297 if (col <= endcol)
6298 screen_fill(row, row + 1, col + coloff,
6299 endcol + coloff + 1, ' ', ' ', 0);
6300 }
6301 col = endcol + 1;
6302 off_to = LineOffset[row] + col + coloff;
6303 off_from += col;
6304 endcol = (clear_width > 0 ? clear_width : -clear_width);
6305 }
6306#endif /* FEAT_RIGHTLEFT */
6307
6308 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6309
6310 while (col < endcol)
6311 {
6312#ifdef FEAT_MBYTE
6313 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006314 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 else
6316 char_cells = 1;
6317#endif
6318
6319 redraw_this = redraw_next;
6320 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6321 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6322
6323#ifdef FEAT_GUI
6324 /* If the next character was bold, then redraw the current character to
6325 * remove any pixels that might have spilt over into us. This only
6326 * happens in the GUI.
6327 */
6328 if (redraw_next && gui.in_use)
6329 {
6330 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006331 if (hl > HL_ALL)
6332 hl = syn_attr2attr(hl);
6333 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 redraw_this = TRUE;
6335 }
6336#endif
6337
6338 if (redraw_this)
6339 {
6340 /*
6341 * Special handling when 'xs' termcap flag set (hpterm):
6342 * Attributes for characters are stored at the position where the
6343 * cursor is when writing the highlighting code. The
6344 * start-highlighting code must be written with the cursor on the
6345 * first highlighted character. The stop-highlighting code must
6346 * be written with the cursor just after the last highlighted
6347 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006348 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 * to clear the rest of the line, and force redrawing it
6350 * completely.
6351 */
6352 if ( p_wiv
6353 && !force
6354#ifdef FEAT_GUI
6355 && !gui.in_use
6356#endif
6357 && ScreenAttrs[off_to] != 0
6358 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6359 {
6360 /*
6361 * Need to remove highlighting attributes here.
6362 */
6363 windgoto(row, col + coloff);
6364 out_str(T_CE); /* clear rest of this screen line */
6365 screen_start(); /* don't know where cursor is now */
6366 force = TRUE; /* force redraw of rest of the line */
6367 redraw_next = TRUE; /* or else next char would miss out */
6368
6369 /*
6370 * If the previous character was highlighted, need to stop
6371 * highlighting at this character.
6372 */
6373 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6374 {
6375 screen_attr = ScreenAttrs[off_to - 1];
6376 term_windgoto(row, col + coloff);
6377 screen_stop_highlight();
6378 }
6379 else
6380 screen_attr = 0; /* highlighting has stopped */
6381 }
6382#ifdef FEAT_MBYTE
6383 if (enc_dbcs != 0)
6384 {
6385 /* Check if overwriting a double-byte with a single-byte or
6386 * the other way around requires another character to be
6387 * redrawn. For UTF-8 this isn't needed, because comparing
6388 * ScreenLinesUC[] is sufficient. */
6389 if (char_cells == 1
6390 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006391 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 {
6393 /* Writing a single-cell character over a double-cell
6394 * character: need to redraw the next cell. */
6395 ScreenLines[off_to + 1] = 0;
6396 redraw_next = TRUE;
6397 }
6398 else if (char_cells == 2
6399 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006400 && (*mb_off2cells)(off_to, max_off_to) == 1
6401 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 {
6403 /* Writing the second half of a double-cell character over
6404 * a double-cell character: need to redraw the second
6405 * cell. */
6406 ScreenLines[off_to + 2] = 0;
6407 redraw_next = TRUE;
6408 }
6409
6410 if (enc_dbcs == DBCS_JPNU)
6411 ScreenLines2[off_to] = ScreenLines2[off_from];
6412 }
6413 /* When writing a single-width character over a double-width
6414 * character and at the end of the redrawn text, need to clear out
6415 * the right halve of the old character.
6416 * Also required when writing the right halve of a double-width
6417 * char over the left halve of an existing one. */
6418 if (has_mbyte && col + char_cells == endcol
6419 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006420 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006422 && (*mb_off2cells)(off_to, max_off_to) == 1
6423 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 clear_next = TRUE;
6425#endif
6426
6427 ScreenLines[off_to] = ScreenLines[off_from];
6428#ifdef FEAT_MBYTE
6429 if (enc_utf8)
6430 {
6431 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6432 if (ScreenLinesUC[off_from] != 0)
6433 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006434 int i;
6435
6436 for (i = 0; i < Screen_mco; ++i)
6437 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 }
6439 }
6440 if (char_cells == 2)
6441 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6442#endif
6443
6444#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006445 /* The bold trick makes a single column of pixels appear in the
6446 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 * character should be redrawn too. This happens for our own GUI
6448 * and for some xterms. */
6449 if (
6450# ifdef FEAT_GUI
6451 gui.in_use
6452# endif
6453# if defined(FEAT_GUI) && defined(UNIX)
6454 ||
6455# endif
6456# ifdef UNIX
6457 term_is_xterm
6458# endif
6459 )
6460 {
6461 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006462 if (hl > HL_ALL)
6463 hl = syn_attr2attr(hl);
6464 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 redraw_next = TRUE;
6466 }
6467#endif
6468 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6469#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006470 /* For simplicity set the attributes of second half of a
6471 * double-wide character equal to the first half. */
6472 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006474
6475 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 else
6478#endif
6479 screen_char(off_to, row, col + coloff);
6480 }
6481 else if ( p_wiv
6482#ifdef FEAT_GUI
6483 && !gui.in_use
6484#endif
6485 && col + coloff > 0)
6486 {
6487 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6488 {
6489 /*
6490 * Don't output stop-highlight when moving the cursor, it will
6491 * stop the highlighting when it should continue.
6492 */
6493 screen_attr = 0;
6494 }
6495 else if (screen_attr != 0)
6496 screen_stop_highlight();
6497 }
6498
6499 off_to += CHAR_CELLS;
6500 off_from += CHAR_CELLS;
6501 col += CHAR_CELLS;
6502 }
6503
6504#ifdef FEAT_MBYTE
6505 if (clear_next)
6506 {
6507 /* Clear the second half of a double-wide character of which the left
6508 * half was overwritten with a single-wide character. */
6509 ScreenLines[off_to] = ' ';
6510 if (enc_utf8)
6511 ScreenLinesUC[off_to] = 0;
6512 screen_char(off_to, row, col + coloff);
6513 }
6514#endif
6515
6516 if (clear_width > 0
6517#ifdef FEAT_RIGHTLEFT
6518 && !rlflag
6519#endif
6520 )
6521 {
6522#ifdef FEAT_GUI
6523 int startCol = col;
6524#endif
6525
6526 /* blank out the rest of the line */
6527 while (col < clear_width && ScreenLines[off_to] == ' '
6528 && ScreenAttrs[off_to] == 0
6529#ifdef FEAT_MBYTE
6530 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6531#endif
6532 )
6533 {
6534 ++off_to;
6535 ++col;
6536 }
6537 if (col < clear_width)
6538 {
6539#ifdef FEAT_GUI
6540 /*
6541 * In the GUI, clearing the rest of the line may leave pixels
6542 * behind if the first character cleared was bold. Some bold
6543 * fonts spill over the left. In this case we redraw the previous
6544 * character too. If we didn't skip any blanks above, then we
6545 * only redraw if the character wasn't already redrawn anyway.
6546 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006547 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 {
6549 hl = ScreenAttrs[off_to];
6550 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006551 {
6552 int prev_cells = 1;
6553# ifdef FEAT_MBYTE
6554 if (enc_utf8)
6555 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6556 * that its width is 2. */
6557 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6558 else if (enc_dbcs != 0)
6559 {
6560 /* find previous character by counting from first
6561 * column and get its width. */
6562 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006563 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006564
6565 while (off < off_to)
6566 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006567 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006568 off += prev_cells;
6569 }
6570 }
6571
6572 if (enc_dbcs != 0 && prev_cells > 1)
6573 screen_char_2(off_to - prev_cells, row,
6574 col + coloff - prev_cells);
6575 else
6576# endif
6577 screen_char(off_to - prev_cells, row,
6578 col + coloff - prev_cells);
6579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 }
6581#endif
6582 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6583 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 off_to += clear_width - col;
6585 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 }
6587 }
6588
6589 if (clear_width > 0)
6590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 /* For a window that's left of another, draw the separator char. */
6592 if (col + coloff < Columns)
6593 {
6594 int c;
6595
6596 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006597 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006598#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006599 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6600 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 || ScreenAttrs[off_to] != hl)
6603 {
6604 ScreenLines[off_to] = c;
6605 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006606#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 if (enc_utf8)
6608 {
6609 if (c >= 0x80)
6610 {
6611 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006612 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 }
6614 else
6615 ScreenLinesUC[off_to] = 0;
6616 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 screen_char(off_to, row, col + coloff);
6619 }
6620 }
6621 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 LineWraps[row] = FALSE;
6623 }
6624}
6625
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006626#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006628 * Mirror text "str" for right-left displaying.
6629 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006631 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006632rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633{
6634 char_u *p1, *p2;
6635 int t;
6636
6637 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6638 {
6639 t = *p1;
6640 *p1 = *p2;
6641 *p2 = t;
6642 }
6643}
6644#endif
6645
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646/*
6647 * mark all status lines for redraw; used after first :cd
6648 */
6649 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006650status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651{
6652 win_T *wp;
6653
Bram Moolenaar29323592016-07-24 22:04:11 +02006654 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 if (wp->w_status_height)
6656 {
6657 wp->w_redr_status = TRUE;
6658 redraw_later(VALID);
6659 }
6660}
6661
6662/*
6663 * mark all status lines of the current buffer for redraw
6664 */
6665 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006666status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667{
6668 win_T *wp;
6669
Bram Moolenaar29323592016-07-24 22:04:11 +02006670 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6672 {
6673 wp->w_redr_status = TRUE;
6674 redraw_later(VALID);
6675 }
6676}
6677
6678/*
6679 * Redraw all status lines that need to be redrawn.
6680 */
6681 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006682redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683{
6684 win_T *wp;
6685
Bram Moolenaar29323592016-07-24 22:04:11 +02006686 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006688 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006689 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006690 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692
Bram Moolenaar4033c552017-09-16 20:54:51 +02006693#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694/*
6695 * Redraw all status lines at the bottom of frame "frp".
6696 */
6697 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006698win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699{
6700 if (frp->fr_layout == FR_LEAF)
6701 frp->fr_win->w_redr_status = TRUE;
6702 else if (frp->fr_layout == FR_ROW)
6703 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006704 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 win_redraw_last_status(frp);
6706 }
6707 else /* frp->fr_layout == FR_COL */
6708 {
6709 frp = frp->fr_child;
6710 while (frp->fr_next != NULL)
6711 frp = frp->fr_next;
6712 win_redraw_last_status(frp);
6713 }
6714}
6715#endif
6716
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717/*
6718 * Draw the verticap separator right of window "wp" starting with line "row".
6719 */
6720 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006721draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
6723 int hl;
6724 int c;
6725
6726 if (wp->w_vsep_width)
6727 {
6728 /* draw the vertical separator right of this window */
6729 c = fillchar_vsep(&hl);
6730 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6731 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6732 c, ' ', hl);
6733 }
6734}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735
6736#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006737static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738
6739/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006740 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 */
6742 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006743status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
6745 int len = 0;
6746
6747#ifdef FEAT_MENU
6748 int emenu = (xp->xp_context == EXPAND_MENUS
6749 || xp->xp_context == EXPAND_MENUNAMES);
6750
6751 /* Check for menu separators - replace with '|'. */
6752 if (emenu && menu_is_separator(s))
6753 return 1;
6754#endif
6755
6756 while (*s != NUL)
6757 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006758 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006759 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006760 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 }
6762
6763 return len;
6764}
6765
6766/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006767 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006768 * These are backslashes used for escaping. Do show backslashes in help tags.
6769 */
6770 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006771skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006772{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006773 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006774#ifdef FEAT_MENU
6775 || ((xp->xp_context == EXPAND_MENUS
6776 || xp->xp_context == EXPAND_MENUNAMES)
6777 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6778#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006779 )
6780 {
6781#ifndef BACKSLASH_IN_FILENAME
6782 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6783 return 2;
6784#endif
6785 return 1;
6786 }
6787 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006788}
6789
6790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 * Show wildchar matches in the status line.
6792 * Show at least the "match" item.
6793 * We start at item 'first_match' in the list and show all matches that fit.
6794 *
6795 * If inversion is possible we use it. Else '=' characters are used.
6796 */
6797 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006798win_redr_status_matches(
6799 expand_T *xp,
6800 int num_matches,
6801 char_u **matches, /* list of matches */
6802 int match,
6803 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804{
6805#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6806 int row;
6807 char_u *buf;
6808 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006809 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 int fillchar;
6811 int attr;
6812 int i;
6813 int highlight = TRUE;
6814 char_u *selstart = NULL;
6815 int selstart_col = 0;
6816 char_u *selend = NULL;
6817 static int first_match = 0;
6818 int add_left = FALSE;
6819 char_u *s;
6820#ifdef FEAT_MENU
6821 int emenu;
6822#endif
6823#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6824 int l;
6825#endif
6826
6827 if (matches == NULL) /* interrupted completion? */
6828 return;
6829
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006830#ifdef FEAT_MBYTE
6831 if (has_mbyte)
6832 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6833 else
6834#endif
6835 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 if (buf == NULL)
6837 return;
6838
6839 if (match == -1) /* don't show match but original text */
6840 {
6841 match = 0;
6842 highlight = FALSE;
6843 }
6844 /* count 1 for the ending ">" */
6845 clen = status_match_len(xp, L_MATCH(match)) + 3;
6846 if (match == 0)
6847 first_match = 0;
6848 else if (match < first_match)
6849 {
6850 /* jumping left, as far as we can go */
6851 first_match = match;
6852 add_left = TRUE;
6853 }
6854 else
6855 {
6856 /* check if match fits on the screen */
6857 for (i = first_match; i < match; ++i)
6858 clen += status_match_len(xp, L_MATCH(i)) + 2;
6859 if (first_match > 0)
6860 clen += 2;
6861 /* jumping right, put match at the left */
6862 if ((long)clen > Columns)
6863 {
6864 first_match = match;
6865 /* if showing the last match, we can add some on the left */
6866 clen = 2;
6867 for (i = match; i < num_matches; ++i)
6868 {
6869 clen += status_match_len(xp, L_MATCH(i)) + 2;
6870 if ((long)clen >= Columns)
6871 break;
6872 }
6873 if (i == num_matches)
6874 add_left = TRUE;
6875 }
6876 }
6877 if (add_left)
6878 while (first_match > 0)
6879 {
6880 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6881 if ((long)clen >= Columns)
6882 break;
6883 --first_match;
6884 }
6885
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006886 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887
6888 if (first_match == 0)
6889 {
6890 *buf = NUL;
6891 len = 0;
6892 }
6893 else
6894 {
6895 STRCPY(buf, "< ");
6896 len = 2;
6897 }
6898 clen = len;
6899
6900 i = first_match;
6901 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6902 {
6903 if (i == match)
6904 {
6905 selstart = buf + len;
6906 selstart_col = clen;
6907 }
6908
6909 s = L_MATCH(i);
6910 /* Check for menu separators - replace with '|' */
6911#ifdef FEAT_MENU
6912 emenu = (xp->xp_context == EXPAND_MENUS
6913 || xp->xp_context == EXPAND_MENUNAMES);
6914 if (emenu && menu_is_separator(s))
6915 {
6916 STRCPY(buf + len, transchar('|'));
6917 l = (int)STRLEN(buf + len);
6918 len += l;
6919 clen += l;
6920 }
6921 else
6922#endif
6923 for ( ; *s != NUL; ++s)
6924 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006925 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 clen += ptr2cells(s);
6927#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006928 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 {
6930 STRNCPY(buf + len, s, l);
6931 s += l - 1;
6932 len += l;
6933 }
6934 else
6935#endif
6936 {
6937 STRCPY(buf + len, transchar_byte(*s));
6938 len += (int)STRLEN(buf + len);
6939 }
6940 }
6941 if (i == match)
6942 selend = buf + len;
6943
6944 *(buf + len++) = ' ';
6945 *(buf + len++) = ' ';
6946 clen += 2;
6947 if (++i == num_matches)
6948 break;
6949 }
6950
6951 if (i != num_matches)
6952 {
6953 *(buf + len++) = '>';
6954 ++clen;
6955 }
6956
6957 buf[len] = NUL;
6958
6959 row = cmdline_row - 1;
6960 if (row >= 0)
6961 {
6962 if (wild_menu_showing == 0)
6963 {
6964 if (msg_scrolled > 0)
6965 {
6966 /* Put the wildmenu just above the command line. If there is
6967 * no room, scroll the screen one line up. */
6968 if (cmdline_row == Rows - 1)
6969 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006970 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 ++msg_scrolled;
6972 }
6973 else
6974 {
6975 ++cmdline_row;
6976 ++row;
6977 }
6978 wild_menu_showing = WM_SCROLLED;
6979 }
6980 else
6981 {
6982 /* Create status line if needed by setting 'laststatus' to 2.
6983 * Set 'winminheight' to zero to avoid that the window is
6984 * resized. */
6985 if (lastwin->w_status_height == 0)
6986 {
6987 save_p_ls = p_ls;
6988 save_p_wmh = p_wmh;
6989 p_ls = 2;
6990 p_wmh = 0;
6991 last_status(FALSE);
6992 }
6993 wild_menu_showing = WM_SHOWN;
6994 }
6995 }
6996
6997 screen_puts(buf, row, 0, attr);
6998 if (selstart != NULL && highlight)
6999 {
7000 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007001 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 }
7003
7004 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7005 }
7006
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 vim_free(buf);
7009}
7010#endif
7011
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012/*
7013 * Redraw the status line of window wp.
7014 *
7015 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007016 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7017 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007019 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007020win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021{
7022 int row;
7023 char_u *p;
7024 int len;
7025 int fillchar;
7026 int attr;
7027 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007028 static int busy = FALSE;
7029
7030 /* It's possible to get here recursively when 'statusline' (indirectly)
7031 * invokes ":redrawstatus". Simply ignore the call then. */
7032 if (busy)
7033 return;
7034 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035
7036 wp->w_redr_status = FALSE;
7037 if (wp->w_status_height == 0)
7038 {
7039 /* no status line, can only be last window */
7040 redraw_cmdline = TRUE;
7041 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007042 else if (!redrawing()
7043#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007044 // don't update status line when popup menu is visible and may be
7045 // drawn over it, unless it will be redrawn later
7046 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007047#endif
7048 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 {
7050 /* Don't redraw right now, do it later. */
7051 wp->w_redr_status = TRUE;
7052 }
7053#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007054 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {
7056 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007057 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 }
7059#endif
7060 else
7061 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007062 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007064 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 p = NameBuff;
7066 len = (int)STRLEN(p);
7067
Bram Moolenaard85f2712017-07-28 21:51:57 +02007068 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069#ifdef FEAT_QUICKFIX
7070 || wp->w_p_pvw
7071#endif
7072 || bufIsChanged(wp->w_buffer)
7073 || wp->w_buffer->b_p_ro)
7074 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007075 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007077 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 len += (int)STRLEN(p + len);
7079 }
7080#ifdef FEAT_QUICKFIX
7081 if (wp->w_p_pvw)
7082 {
7083 STRCPY(p + len, _("[Preview]"));
7084 len += (int)STRLEN(p + len);
7085 }
7086#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007087 if (bufIsChanged(wp->w_buffer)
7088#ifdef FEAT_TERMINAL
7089 && !bt_terminal(wp->w_buffer)
7090#endif
7091 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 {
7093 STRCPY(p + len, "[+]");
7094 len += 3;
7095 }
7096 if (wp->w_buffer->b_p_ro)
7097 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007098 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007099 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 }
7101
Bram Moolenaar02631462017-09-22 15:20:32 +02007102 this_ru_col = ru_col - (Columns - wp->w_width);
7103 if (this_ru_col < (wp->w_width + 1) / 2)
7104 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 if (this_ru_col <= 1)
7106 {
7107 p = (char_u *)"<"; /* No room for file name! */
7108 len = 1;
7109 }
7110 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111#ifdef FEAT_MBYTE
7112 if (has_mbyte)
7113 {
7114 int clen = 0, i;
7115
7116 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02007117 clen = mb_string2cells(p, -1);
7118
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 /* Find first character that will fit.
7120 * Going from start to end is much faster for DBCS. */
7121 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007122 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 clen -= (*mb_ptr2cells)(p + i);
7124 len = clen;
7125 if (i > 0)
7126 {
7127 p = p + i - 1;
7128 *p = '<';
7129 ++len;
7130 }
7131
7132 }
7133 else
7134#endif
7135 if (len > this_ru_col - 1)
7136 {
7137 p += len - (this_ru_col - 1);
7138 *p = '<';
7139 len = this_ru_col - 1;
7140 }
7141
7142 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007143 screen_puts(p, row, wp->w_wincol, attr);
7144 screen_fill(row, row + 1, len + wp->w_wincol,
7145 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007147 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7149 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007150 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151
7152#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007153 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154#endif
7155 }
7156
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 /*
7158 * May need to draw the character below the vertical separator.
7159 */
7160 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7161 {
7162 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007163 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 else
7165 fillchar = fillchar_vsep(&attr);
7166 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7167 attr);
7168 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007169 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170}
7171
Bram Moolenaar238a5642006-02-21 22:12:05 +00007172#ifdef FEAT_STL_OPT
7173/*
7174 * Redraw the status line according to 'statusline' and take care of any
7175 * errors encountered.
7176 */
7177 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007178redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007179{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007180 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007181 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007182
7183 /* When called recursively return. This can happen when the statusline
7184 * contains an expression that triggers a redraw. */
7185 if (entered)
7186 return;
7187 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007188
Bram Moolenaara742e082016-04-05 21:10:38 +02007189 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007190 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007191 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007192 {
7193 /* When there is an error disable the statusline, otherwise the
7194 * display is messed up with errors and a redraw triggers the problem
7195 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007196 set_string_option_direct((char_u *)"statusline", -1,
7197 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007198 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007199 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007200 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007201 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007202}
7203#endif
7204
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205/*
7206 * Return TRUE if the status line of window "wp" is connected to the status
7207 * line of the window right of it. If not, then it's a vertical separator.
7208 * Only call if (wp->w_vsep_width != 0).
7209 */
7210 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007211stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212{
7213 frame_T *fr;
7214
7215 fr = wp->w_frame;
7216 while (fr->fr_parent != NULL)
7217 {
7218 if (fr->fr_parent->fr_layout == FR_COL)
7219 {
7220 if (fr->fr_next != NULL)
7221 break;
7222 }
7223 else
7224 {
7225 if (fr->fr_next != NULL)
7226 return TRUE;
7227 }
7228 fr = fr->fr_parent;
7229 }
7230 return FALSE;
7231}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234/*
7235 * Get the value to show for the language mappings, active 'keymap'.
7236 */
7237 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007238get_keymap_str(
7239 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007240 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007241 char_u *buf, /* buffer for the result */
7242 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243{
7244 char_u *p;
7245
7246 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7247 return FALSE;
7248
7249 {
7250#ifdef FEAT_EVAL
7251 buf_T *old_curbuf = curbuf;
7252 win_T *old_curwin = curwin;
7253 char_u *s;
7254
7255 curbuf = wp->w_buffer;
7256 curwin = wp;
7257 STRCPY(buf, "b:keymap_name"); /* must be writable */
7258 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007259 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 --emsg_skip;
7261 curbuf = old_curbuf;
7262 curwin = old_curwin;
7263 if (p == NULL || *p == NUL)
7264#endif
7265 {
7266#ifdef FEAT_KEYMAP
7267 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7268 p = wp->w_buffer->b_p_keymap;
7269 else
7270#endif
7271 p = (char_u *)"lang";
7272 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007273 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 buf[0] = NUL;
7275#ifdef FEAT_EVAL
7276 vim_free(s);
7277#endif
7278 }
7279 return buf[0] != NUL;
7280}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281
7282#if defined(FEAT_STL_OPT) || defined(PROTO)
7283/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007284 * Redraw the status line or ruler of window "wp".
7285 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 */
7287 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007288win_redr_custom(
7289 win_T *wp,
7290 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007292 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 int attr;
7294 int curattr;
7295 int row;
7296 int col = 0;
7297 int maxwidth;
7298 int width;
7299 int n;
7300 int len;
7301 int fillchar;
7302 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007303 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007305 struct stl_hlrec hltab[STL_MAX_ITEM];
7306 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007307 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007308 win_T *ewp;
7309 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310
Bram Moolenaar1d633412013-12-11 15:52:01 +01007311 /* There is a tiny chance that this gets called recursively: When
7312 * redrawing a status line triggers redrawing the ruler or tabline.
7313 * Avoid trouble by not allowing recursion. */
7314 if (entered)
7315 return;
7316 entered = TRUE;
7317
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007319 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007321 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007322 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007323 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007324 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007325 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007326 maxwidth = Columns;
7327# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007328 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007331 else
7332 {
7333 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007334 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007335 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007336
7337 if (draw_ruler)
7338 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007339 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007340 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007341 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007342 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007343 if (*++stl == '-')
7344 stl++;
7345 if (atoi((char *)stl))
7346 while (VIM_ISDIGIT(*stl))
7347 stl++;
7348 if (*stl++ != '(')
7349 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007350 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007351 col = ru_col - (Columns - wp->w_width);
7352 if (col < (wp->w_width + 1) / 2)
7353 col = (wp->w_width + 1) / 2;
7354 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007355 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007356 {
7357 row = Rows - 1;
7358 --maxwidth; /* writing in last column may cause scrolling */
7359 fillchar = ' ';
7360 attr = 0;
7361 }
7362
7363# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007364 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007365# endif
7366 }
7367 else
7368 {
7369 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007370 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007371 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007372 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007373# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007374 use_sandbox = was_set_insecurely((char_u *)"statusline",
7375 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007376# endif
7377 }
7378
Bram Moolenaar53f81742017-09-22 14:35:51 +02007379 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007380 }
7381
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007383 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384
Bram Moolenaar61452852011-02-01 18:01:11 +01007385 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7386 * the cursor away and back. */
7387 ewp = wp == NULL ? curwin : wp;
7388 p_crb_save = ewp->w_p_crb;
7389 ewp->w_p_crb = FALSE;
7390
Bram Moolenaar362f3562009-11-03 16:20:34 +00007391 /* Make a copy, because the statusline may include a function call that
7392 * might change the option value and free the memory. */
7393 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007394 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007395 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007396 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007397 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007398 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007400 /* Make all characters printable. */
7401 p = transstr(buf);
7402 if (p != NULL)
7403 {
7404 vim_strncpy(buf, p, sizeof(buf) - 1);
7405 vim_free(p);
7406 }
7407
7408 /* fill up with "fillchar" */
7409 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007410 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 {
7412#ifdef FEAT_MBYTE
7413 len += (*mb_char2bytes)(fillchar, buf + len);
7414#else
7415 buf[len++] = fillchar;
7416#endif
7417 ++width;
7418 }
7419 buf[len] = NUL;
7420
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007421 /*
7422 * Draw each snippet with the specified highlighting.
7423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 curattr = attr;
7425 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007426 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007428 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 screen_puts_len(p, len, row, col, curattr);
7430 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007431 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007433 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007435 else if (hltab[n].userhl < 0)
7436 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007437#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007438 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7439 && wp->w_status_height != 0)
7440 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007441 else if (wp != NULL && bt_terminal(wp->w_buffer)
7442 && wp->w_status_height != 0)
7443 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007444#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007445 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007446 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007448 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 }
7450 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007451
7452 if (wp == NULL)
7453 {
7454 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7455 col = 0;
7456 len = 0;
7457 p = buf;
7458 fillchar = 0;
7459 for (n = 0; tabtab[n].start != NULL; n++)
7460 {
7461 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7462 while (col < len)
7463 TabPageIdxs[col++] = fillchar;
7464 p = tabtab[n].start;
7465 fillchar = tabtab[n].userhl;
7466 }
7467 while (col < Columns)
7468 TabPageIdxs[col++] = fillchar;
7469 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007470
7471theend:
7472 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473}
7474
7475#endif /* FEAT_STL_OPT */
7476
7477/*
7478 * Output a single character directly to the screen and update ScreenLines.
7479 */
7480 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007481screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 char_u buf[MB_MAXBYTES + 1];
7484
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007485#ifdef FEAT_MBYTE
7486 if (has_mbyte)
7487 buf[(*mb_char2bytes)(c, buf)] = NUL;
7488 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007490 {
7491 buf[0] = c;
7492 buf[1] = NUL;
7493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 screen_puts(buf, row, col, attr);
7495}
7496
7497/*
7498 * Get a single character directly from ScreenLines into "bytes[]".
7499 * Also return its attribute in *attrp;
7500 */
7501 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007502screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503{
7504 unsigned off;
7505
7506 /* safety check */
7507 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7508 {
7509 off = LineOffset[row] + col;
7510 *attrp = ScreenAttrs[off];
7511 bytes[0] = ScreenLines[off];
7512 bytes[1] = NUL;
7513
7514#ifdef FEAT_MBYTE
7515 if (enc_utf8 && ScreenLinesUC[off] != 0)
7516 bytes[utfc_char2bytes(off, bytes)] = NUL;
7517 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7518 {
7519 bytes[0] = ScreenLines[off];
7520 bytes[1] = ScreenLines2[off];
7521 bytes[2] = NUL;
7522 }
7523 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7524 {
7525 bytes[1] = ScreenLines[off + 1];
7526 bytes[2] = NUL;
7527 }
7528#endif
7529 }
7530}
7531
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007532#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007533/*
7534 * Return TRUE if composing characters for screen posn "off" differs from
7535 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007536 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007537 */
7538 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007539screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007540{
7541 int i;
7542
7543 for (i = 0; i < Screen_mco; ++i)
7544 {
7545 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7546 return TRUE;
7547 if (u8cc[i] == 0)
7548 break;
7549 }
7550 return FALSE;
7551}
7552#endif
7553
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554/*
7555 * Put string '*text' on the screen at position 'row' and 'col', with
7556 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7557 * Note: only outputs within one row, message is truncated at screen boundary!
7558 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7559 */
7560 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007561screen_puts(
7562 char_u *text,
7563 int row,
7564 int col,
7565 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566{
7567 screen_puts_len(text, -1, row, col, attr);
7568}
7569
7570/*
7571 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7572 * a NUL.
7573 */
7574 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007575screen_puts_len(
7576 char_u *text,
7577 int textlen,
7578 int row,
7579 int col,
7580 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581{
7582 unsigned off;
7583 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007584 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 int c;
7586#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007587 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 int mbyte_blen = 1;
7589 int mbyte_cells = 1;
7590 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007591 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 int clear_next_cell = FALSE;
7593# ifdef FEAT_ARABIC
7594 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007595 int pc, nc, nc1;
7596 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597# endif
7598#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007599#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7600 int force_redraw_this;
7601 int force_redraw_next = FALSE;
7602#endif
7603 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604
7605 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7606 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007607 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608
Bram Moolenaarc236c162008-07-13 17:41:49 +00007609#ifdef FEAT_MBYTE
7610 /* When drawing over the right halve of a double-wide char clear out the
7611 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007612 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007613# ifdef FEAT_GUI
7614 && !gui.in_use
7615# endif
7616 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007617 {
7618 ScreenLines[off - 1] = ' ';
7619 ScreenAttrs[off - 1] = 0;
7620 if (enc_utf8)
7621 {
7622 ScreenLinesUC[off - 1] = 0;
7623 ScreenLinesC[0][off - 1] = 0;
7624 }
7625 /* redraw the previous cell, make it empty */
7626 screen_char(off - 1, row, col - 1);
7627 /* force the cell at "col" to be redrawn */
7628 force_redraw_next = TRUE;
7629 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007630#endif
7631
Bram Moolenaar367329b2007-08-30 11:53:22 +00007632#ifdef FEAT_MBYTE
7633 max_off = LineOffset[row] + screen_Columns;
7634#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007635 while (col < screen_Columns
7636 && (len < 0 || (int)(ptr - text) < len)
7637 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 {
7639 c = *ptr;
7640#ifdef FEAT_MBYTE
7641 /* check if this is the first byte of a multibyte */
7642 if (has_mbyte)
7643 {
7644 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007645 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007647 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7649 mbyte_cells = 1;
7650 else if (enc_dbcs != 0)
7651 mbyte_cells = mbyte_blen;
7652 else /* enc_utf8 */
7653 {
7654 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007655 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 (int)((text + len) - ptr));
7657 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007658 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007660# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 /* Non-BMP character: display as ? or fullwidth ?. */
7662 if (u8c >= 0x10000)
7663 {
7664 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7665 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007666 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669# ifdef FEAT_ARABIC
7670 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7671 {
7672 /* Do Arabic shaping. */
7673 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7674 {
7675 /* Past end of string to be displayed. */
7676 nc = NUL;
7677 nc1 = NUL;
7678 }
7679 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007680 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007681 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7682 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007683 nc1 = pcc[0];
7684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 pc = prev_c;
7686 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007687 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 }
7689 else
7690 prev_c = u8c;
7691# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007692 if (col + mbyte_cells > screen_Columns)
7693 {
7694 /* Only 1 cell left, but character requires 2 cells:
7695 * display a '>' in the last column to avoid wrapping. */
7696 c = '>';
7697 mbyte_cells = 1;
7698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 }
7700 }
7701#endif
7702
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007703#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7704 force_redraw_this = force_redraw_next;
7705 force_redraw_next = FALSE;
7706#endif
7707
7708 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709#ifdef FEAT_MBYTE
7710 || (mbyte_cells == 2
7711 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7712 || (enc_dbcs == DBCS_JPNU
7713 && c == 0x8e
7714 && ScreenLines2[off] != ptr[1])
7715 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007716 && (ScreenLinesUC[off] !=
7717 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7718 || (ScreenLinesUC[off] != 0
7719 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720#endif
7721 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007722 || exmode_active;
7723
7724 if (need_redraw
7725#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7726 || force_redraw_this
7727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 )
7729 {
7730#if defined(FEAT_GUI) || defined(UNIX)
7731 /* The bold trick makes a single row of pixels appear in the next
7732 * character. When a bold character is removed, the next
7733 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007734 * and for some xterms. */
7735 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736# ifdef FEAT_GUI
7737 gui.in_use
7738# endif
7739# if defined(FEAT_GUI) && defined(UNIX)
7740 ||
7741# endif
7742# ifdef UNIX
7743 term_is_xterm
7744# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007745 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007747 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007749 if (n > HL_ALL)
7750 n = syn_attr2attr(n);
7751 if (n & HL_BOLD)
7752 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 }
7754#endif
7755#ifdef FEAT_MBYTE
7756 /* When at the end of the text and overwriting a two-cell
7757 * character with a one-cell character, need to clear the next
7758 * cell. Also when overwriting the left halve of a two-cell char
7759 * with the right halve of a two-cell char. Do this only once
7760 * (mb_off2cells() may return 2 on the right halve). */
7761 if (clear_next_cell)
7762 clear_next_cell = FALSE;
7763 else if (has_mbyte
7764 && (len < 0 ? ptr[mbyte_blen] == NUL
7765 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007766 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007768 && (*mb_off2cells)(off, max_off) == 1
7769 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 clear_next_cell = TRUE;
7771
7772 /* Make sure we never leave a second byte of a double-byte behind,
7773 * it confuses mb_off2cells(). */
7774 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007775 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007777 && (*mb_off2cells)(off, max_off) == 1
7778 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 ScreenLines[off + mbyte_blen] = 0;
7780#endif
7781 ScreenLines[off] = c;
7782 ScreenAttrs[off] = attr;
7783#ifdef FEAT_MBYTE
7784 if (enc_utf8)
7785 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007786 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 ScreenLinesUC[off] = 0;
7788 else
7789 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007790 int i;
7791
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007793 for (i = 0; i < Screen_mco; ++i)
7794 {
7795 ScreenLinesC[i][off] = u8cc[i];
7796 if (u8cc[i] == 0)
7797 break;
7798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 }
7800 if (mbyte_cells == 2)
7801 {
7802 ScreenLines[off + 1] = 0;
7803 ScreenAttrs[off + 1] = attr;
7804 }
7805 screen_char(off, row, col);
7806 }
7807 else if (mbyte_cells == 2)
7808 {
7809 ScreenLines[off + 1] = ptr[1];
7810 ScreenAttrs[off + 1] = attr;
7811 screen_char_2(off, row, col);
7812 }
7813 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7814 {
7815 ScreenLines2[off] = ptr[1];
7816 screen_char(off, row, col);
7817 }
7818 else
7819#endif
7820 screen_char(off, row, col);
7821 }
7822#ifdef FEAT_MBYTE
7823 if (has_mbyte)
7824 {
7825 off += mbyte_cells;
7826 col += mbyte_cells;
7827 ptr += mbyte_blen;
7828 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007829 {
7830 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007832 len = -1;
7833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 }
7835 else
7836#endif
7837 {
7838 ++off;
7839 ++col;
7840 ++ptr;
7841 }
7842 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007843
7844#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7845 /* If we detected the next character needs to be redrawn, but the text
7846 * doesn't extend up to there, update the character here. */
7847 if (force_redraw_next && col < screen_Columns)
7848 {
7849# ifdef FEAT_MBYTE
7850 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7851 screen_char_2(off, row, col);
7852 else
7853# endif
7854 screen_char(off, row, col);
7855 }
7856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857}
7858
7859#ifdef FEAT_SEARCH_EXTRA
7860/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007861 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 */
7863 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007864start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865{
7866 if (p_hls && !no_hlsearch)
7867 {
7868 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007869 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007870# ifdef FEAT_RELTIME
7871 /* Set the time limit to 'redrawtime'. */
7872 profile_setlimit(p_rdt, &search_hl.tm);
7873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 }
7875}
7876
7877/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007878 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 */
7880 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007881end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882{
7883 if (search_hl.rm.regprog != NULL)
7884 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007885 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 search_hl.rm.regprog = NULL;
7887 }
7888}
7889
7890/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007891 * Init for calling prepare_search_hl().
7892 */
7893 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007894init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007895{
7896 matchitem_T *cur;
7897
7898 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7899 * match */
7900 cur = wp->w_match_head;
7901 while (cur != NULL)
7902 {
7903 cur->hl.rm = cur->match;
7904 if (cur->hlg_id == 0)
7905 cur->hl.attr = 0;
7906 else
7907 cur->hl.attr = syn_id2attr(cur->hlg_id);
7908 cur->hl.buf = wp->w_buffer;
7909 cur->hl.lnum = 0;
7910 cur->hl.first_lnum = 0;
7911# ifdef FEAT_RELTIME
7912 /* Set the time limit to 'redrawtime'. */
7913 profile_setlimit(p_rdt, &(cur->hl.tm));
7914# endif
7915 cur = cur->next;
7916 }
7917 search_hl.buf = wp->w_buffer;
7918 search_hl.lnum = 0;
7919 search_hl.first_lnum = 0;
7920 /* time limit is set at the toplevel, for all windows */
7921}
7922
7923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 * Advance to the match in window "wp" line "lnum" or past it.
7925 */
7926 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007927prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007929 matchitem_T *cur; /* points to the match list */
7930 match_T *shl; /* points to search_hl or a match */
7931 int shl_flag; /* flag to indicate whether search_hl
7932 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007933 int pos_inprogress; /* marks that position match search is
7934 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 int n;
7936
7937 /*
7938 * When using a multi-line pattern, start searching at the top
7939 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007940 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007942 cur = wp->w_match_head;
7943 shl_flag = FALSE;
7944 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007946 if (shl_flag == FALSE)
7947 {
7948 shl = &search_hl;
7949 shl_flag = TRUE;
7950 }
7951 else
7952 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 if (shl->rm.regprog != NULL
7954 && shl->lnum == 0
7955 && re_multiline(shl->rm.regprog))
7956 {
7957 if (shl->first_lnum == 0)
7958 {
7959# ifdef FEAT_FOLDING
7960 for (shl->first_lnum = lnum;
7961 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7962 if (hasFoldingWin(wp, shl->first_lnum - 1,
7963 NULL, NULL, TRUE, NULL))
7964 break;
7965# else
7966 shl->first_lnum = wp->w_topline;
7967# endif
7968 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007969 if (cur != NULL)
7970 cur->pos.cur = 0;
7971 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007973 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7974 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007976 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7977 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007978 pos_inprogress = cur == NULL || cur->pos.cur == 0
7979 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 if (shl->lnum != 0)
7981 {
7982 shl->first_lnum = shl->lnum
7983 + shl->rm.endpos[0].lnum
7984 - shl->rm.startpos[0].lnum;
7985 n = shl->rm.endpos[0].col;
7986 }
7987 else
7988 {
7989 ++shl->first_lnum;
7990 n = 0;
7991 }
7992 }
7993 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007994 if (shl != &search_hl && cur != NULL)
7995 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 }
7997}
7998
7999/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008000 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 * Uses shl->buf.
8002 * Sets shl->lnum and shl->rm contents.
8003 * Note: Assumes a previous match is always before "lnum", unless
8004 * shl->lnum is zero.
8005 * Careful: Any pointers for buffer lines will become invalid.
8006 */
8007 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008008next_search_hl(
8009 win_T *win,
8010 match_T *shl, /* points to search_hl or a match */
8011 linenr_T lnum,
8012 colnr_T mincol, /* minimal column for a match */
8013 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014{
8015 linenr_T l;
8016 colnr_T matchcol;
8017 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008018 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008020 // for :{range}s/pat only highlight inside the range
8021 if (lnum < search_first_line || lnum > search_last_line)
8022 {
8023 shl->lnum = 0;
8024 return;
8025 }
8026
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 if (shl->lnum != 0)
8028 {
8029 /* Check for three situations:
8030 * 1. If the "lnum" is below a previous match, start a new search.
8031 * 2. If the previous match includes "mincol", use it.
8032 * 3. Continue after the previous match.
8033 */
8034 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8035 if (lnum > l)
8036 shl->lnum = 0;
8037 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8038 return;
8039 }
8040
8041 /*
8042 * Repeat searching for a match until one is found that includes "mincol"
8043 * or none is found in this line.
8044 */
8045 called_emsg = FALSE;
8046 for (;;)
8047 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008048#ifdef FEAT_RELTIME
8049 /* Stop searching after passing the time limit. */
8050 if (profile_passed_limit(&(shl->tm)))
8051 {
8052 shl->lnum = 0; /* no match found in time */
8053 break;
8054 }
8055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 /* Three situations:
8057 * 1. No useful previous match: search from start of line.
8058 * 2. Not Vi compatible or empty match: continue at next character.
8059 * Break the loop if this is beyond the end of the line.
8060 * 3. Vi compatible searching: continue at end of previous match.
8061 */
8062 if (shl->lnum == 0)
8063 matchcol = 0;
8064 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8065 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008066 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008068 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008069
8070 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008071 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008072 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008074 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 shl->lnum = 0;
8076 break;
8077 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008078#ifdef FEAT_MBYTE
8079 if (has_mbyte)
8080 matchcol += mb_ptr2len(ml);
8081 else
8082#endif
8083 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 }
8085 else
8086 matchcol = shl->rm.endpos[0].col;
8087
8088 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008089 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008091 /* Remember whether shl->rm is using a copy of the regprog in
8092 * cur->match. */
8093 int regprog_is_copy = (shl != &search_hl && cur != NULL
8094 && shl == &cur->hl
8095 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008096 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008097
Bram Moolenaarb3414592014-06-17 17:48:32 +02008098 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8099 matchcol,
8100#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008101 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008102#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008103 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008104#endif
8105 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008106 /* Copy the regprog, in case it got freed and recompiled. */
8107 if (regprog_is_copy)
8108 cur->match.regprog = cur->hl.rm.regprog;
8109
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008110 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008111 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008112 /* Error while handling regexp: stop using this regexp. */
8113 if (shl == &search_hl)
8114 {
8115 /* don't free regprog in the match list, it's a copy */
8116 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008117 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008118 }
8119 shl->rm.regprog = NULL;
8120 shl->lnum = 0;
8121 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8122 message */
8123 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008124 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008125 }
8126 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008127 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008128 else
8129 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 if (nmatched == 0)
8131 {
8132 shl->lnum = 0; /* no match found */
8133 break;
8134 }
8135 if (shl->rm.startpos[0].lnum > 0
8136 || shl->rm.startpos[0].col >= mincol
8137 || nmatched > 1
8138 || shl->rm.endpos[0].col > mincol)
8139 {
8140 shl->lnum += shl->rm.startpos[0].lnum;
8141 break; /* useful match found */
8142 }
8143 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008144
8145 // Restore called_emsg for assert_fails().
8146 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148
Bram Moolenaar85077472016-10-16 14:35:48 +02008149/*
8150 * If there is a match fill "shl" and return one.
8151 * Return zero otherwise.
8152 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008153 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008154next_search_hl_pos(
8155 match_T *shl, /* points to a match */
8156 linenr_T lnum,
8157 posmatch_T *posmatch, /* match positions */
8158 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008159{
8160 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008161 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008162
Bram Moolenaarb3414592014-06-17 17:48:32 +02008163 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8164 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008165 llpos_T *pos = &posmatch->pos[i];
8166
8167 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008168 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008169 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008170 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008171 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008172 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008173 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008174 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008175 /* if this match comes before the one at "found" then swap
8176 * them */
8177 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008178 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008179 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008180
Bram Moolenaar85077472016-10-16 14:35:48 +02008181 *pos = posmatch->pos[found];
8182 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008183 }
8184 }
8185 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008186 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008187 }
8188 }
8189 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008190 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008191 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008192 colnr_T start = posmatch->pos[found].col == 0
8193 ? 0 : posmatch->pos[found].col - 1;
8194 colnr_T end = posmatch->pos[found].col == 0
8195 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008196
Bram Moolenaar85077472016-10-16 14:35:48 +02008197 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008198 shl->rm.startpos[0].lnum = 0;
8199 shl->rm.startpos[0].col = start;
8200 shl->rm.endpos[0].lnum = 0;
8201 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008202 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008203 posmatch->cur = found + 1;
8204 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008205 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008206 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008207}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008208#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008209
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008211screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212{
8213 attrentry_T *aep = NULL;
8214
8215 screen_attr = attr;
8216 if (full_screen
8217#ifdef WIN3264
8218 && termcap_active
8219#endif
8220 )
8221 {
8222#ifdef FEAT_GUI
8223 if (gui.in_use)
8224 {
8225 char buf[20];
8226
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008227 /* The GUI handles this internally. */
8228 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 OUT_STR(buf);
8230 }
8231 else
8232#endif
8233 {
8234 if (attr > HL_ALL) /* special HL attr. */
8235 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008236 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 aep = syn_cterm_attr2entry(attr);
8238 else
8239 aep = syn_term_attr2entry(attr);
8240 if (aep == NULL) /* did ":syntax clear" */
8241 attr = 0;
8242 else
8243 attr = aep->ae_attr;
8244 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008245 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008247 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008248#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008249 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8250 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8251 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008252#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008253 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008254 /* If the Normal FG color has BOLD attribute and the new HL
8255 * has a FG color defined, clear BOLD. */
8256 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008257 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008259 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008260 out_str(T_UCS);
8261 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008262 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8263 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008265 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008267 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008269 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008270 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271
8272 /*
8273 * Output the color or start string after bold etc., in case the
8274 * bold etc. override the color setting.
8275 */
8276 if (aep != NULL)
8277 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008278#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008279 /* When 'termguicolors' is set but fg or bg is unset,
8280 * fall back to the cterm colors. This helps for SpellBad,
8281 * where the GUI uses a red undercurl. */
8282 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008284 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008285 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008286 }
8287 else
8288#endif
8289 if (t_colors > 1)
8290 {
8291 if (aep->ae_u.cterm.fg_color)
8292 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8293 }
8294#ifdef FEAT_TERMGUICOLORS
8295 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8296 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008297 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008298 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 }
8300 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008301#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008302 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008304 if (aep->ae_u.cterm.bg_color)
8305 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8306 }
8307
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008308 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008309 {
8310 if (aep->ae_u.term.start != NULL)
8311 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 }
8313 }
8314 }
8315 }
8316}
8317
8318 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008319screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320{
8321 int do_ME = FALSE; /* output T_ME code */
8322
8323 if (screen_attr != 0
8324#ifdef WIN3264
8325 && termcap_active
8326#endif
8327 )
8328 {
8329#ifdef FEAT_GUI
8330 if (gui.in_use)
8331 {
8332 char buf[20];
8333
8334 /* use internal GUI code */
8335 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8336 OUT_STR(buf);
8337 }
8338 else
8339#endif
8340 {
8341 if (screen_attr > HL_ALL) /* special HL attr. */
8342 {
8343 attrentry_T *aep;
8344
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008345 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 {
8347 /*
8348 * Assume that t_me restores the original colors!
8349 */
8350 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008351 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008352#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008353 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8354 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8355 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008356#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008357 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008358#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008359 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8360 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8361 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008362#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008363 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 do_ME = TRUE;
8365 }
8366 else
8367 {
8368 aep = syn_term_attr2entry(screen_attr);
8369 if (aep != NULL && aep->ae_u.term.stop != NULL)
8370 {
8371 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8372 do_ME = TRUE;
8373 else
8374 out_str(aep->ae_u.term.stop);
8375 }
8376 }
8377 if (aep == NULL) /* did ":syntax clear" */
8378 screen_attr = 0;
8379 else
8380 screen_attr = aep->ae_attr;
8381 }
8382
8383 /*
8384 * Often all ending-codes are equal to T_ME. Avoid outputting the
8385 * same sequence several times.
8386 */
8387 if (screen_attr & HL_STANDOUT)
8388 {
8389 if (STRCMP(T_SE, T_ME) == 0)
8390 do_ME = TRUE;
8391 else
8392 out_str(T_SE);
8393 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008394 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008395 {
8396 if (STRCMP(T_UCE, T_ME) == 0)
8397 do_ME = TRUE;
8398 else
8399 out_str(T_UCE);
8400 }
8401 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008402 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 {
8404 if (STRCMP(T_UE, T_ME) == 0)
8405 do_ME = TRUE;
8406 else
8407 out_str(T_UE);
8408 }
8409 if (screen_attr & HL_ITALIC)
8410 {
8411 if (STRCMP(T_CZR, T_ME) == 0)
8412 do_ME = TRUE;
8413 else
8414 out_str(T_CZR);
8415 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008416 if (screen_attr & HL_STRIKETHROUGH)
8417 {
8418 if (STRCMP(T_STE, T_ME) == 0)
8419 do_ME = TRUE;
8420 else
8421 out_str(T_STE);
8422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8424 out_str(T_ME);
8425
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008426#ifdef FEAT_TERMGUICOLORS
8427 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008429 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008430 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008431 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008432 term_bg_rgb_color(cterm_normal_bg_gui_color);
8433 }
8434 else
8435#endif
8436 {
8437 if (t_colors > 1)
8438 {
8439 /* set Normal cterm colors */
8440 if (cterm_normal_fg_color != 0)
8441 term_fg_color(cterm_normal_fg_color - 1);
8442 if (cterm_normal_bg_color != 0)
8443 term_bg_color(cterm_normal_bg_color - 1);
8444 if (cterm_normal_fg_bold)
8445 out_str(T_MD);
8446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 }
8448 }
8449 }
8450 screen_attr = 0;
8451}
8452
8453/*
8454 * Reset the colors for a cterm. Used when leaving Vim.
8455 * The machine specific code may override this again.
8456 */
8457 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008458reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008460 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 {
8462 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008463#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008464 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8465 || cterm_normal_bg_gui_color != INVALCOLOR)
8466 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008467#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {
8471 out_str(T_OP);
8472 screen_attr = -1;
8473 }
8474 if (cterm_normal_fg_bold)
8475 {
8476 out_str(T_ME);
8477 screen_attr = -1;
8478 }
8479 }
8480}
8481
8482/*
8483 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8484 * using the attributes from ScreenAttrs["off"].
8485 */
8486 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008487screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488{
8489 int attr;
8490
8491 /* Check for illegal values, just in case (could happen just after
8492 * resizing). */
8493 if (row >= screen_Rows || col >= screen_Columns)
8494 return;
8495
Bram Moolenaar494838a2015-02-10 19:20:37 +01008496 /* Outputting a character in the last cell on the screen may scroll the
8497 * screen up. Only do it when the "xn" termcap property is set, otherwise
8498 * mark the character invalid (update it when scrolled up). */
8499 if (*T_XN == NUL
8500 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501#ifdef FEAT_RIGHTLEFT
8502 /* account for first command-line character in rightleft mode */
8503 && !cmdmsg_rl
8504#endif
8505 )
8506 {
8507 ScreenAttrs[off] = (sattr_T)-1;
8508 return;
8509 }
8510
8511 /*
8512 * Stop highlighting first, so it's easier to move the cursor.
8513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 if (screen_char_attr != 0)
8515 attr = screen_char_attr;
8516 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 attr = ScreenAttrs[off];
8518 if (screen_attr != attr)
8519 screen_stop_highlight();
8520
8521 windgoto(row, col);
8522
8523 if (screen_attr != attr)
8524 screen_start_highlight(attr);
8525
8526#ifdef FEAT_MBYTE
8527 if (enc_utf8 && ScreenLinesUC[off] != 0)
8528 {
8529 char_u buf[MB_MAXBYTES + 1];
8530
Bram Moolenaarcb070082016-04-02 22:14:51 +02008531 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008532 {
8533 if (*p_ambw == 'd'
8534# ifdef FEAT_GUI
8535 && !gui.in_use
8536# endif
8537 )
8538 {
8539 /* Clear the two screen cells. If the character is actually
8540 * single width it won't change the second cell. */
8541 out_str((char_u *)" ");
8542 term_windgoto(row, col);
8543 }
8544 /* not sure where the cursor is after drawing the ambiguous width
8545 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008546 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008547 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008548 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008550
8551 /* Convert the UTF-8 character to bytes and write it. */
8552 buf[utfc_char2bytes(off, buf)] = NUL;
8553 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 }
8555 else
8556#endif
8557 {
8558#ifdef FEAT_MBYTE
8559 out_flush_check();
8560#endif
8561 out_char(ScreenLines[off]);
8562#ifdef FEAT_MBYTE
8563 /* double-byte character in single-width cell */
8564 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8565 out_char(ScreenLines2[off]);
8566#endif
8567 }
8568
8569 screen_cur_col++;
8570}
8571
8572#ifdef FEAT_MBYTE
8573
8574/*
8575 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8576 * on the screen at position 'row' and 'col'.
8577 * The attributes of the first byte is used for all. This is required to
8578 * output the two bytes of a double-byte character with nothing in between.
8579 */
8580 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008581screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582{
8583 /* Check for illegal values (could be wrong when screen was resized). */
8584 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8585 return;
8586
8587 /* Outputting the last character on the screen may scrollup the screen.
8588 * Don't to it! Mark the character invalid (update it when scrolled up) */
8589 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8590 {
8591 ScreenAttrs[off] = (sattr_T)-1;
8592 return;
8593 }
8594
8595 /* Output the first byte normally (positions the cursor), then write the
8596 * second byte directly. */
8597 screen_char(off, row, col);
8598 out_char(ScreenLines[off + 1]);
8599 ++screen_cur_col;
8600}
8601#endif
8602
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603/*
8604 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8605 * This uses the contents of ScreenLines[] and doesn't change it.
8606 */
8607 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008608screen_draw_rectangle(
8609 int row,
8610 int col,
8611 int height,
8612 int width,
8613 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614{
8615 int r, c;
8616 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008617#ifdef FEAT_MBYTE
8618 int max_off;
8619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008621 /* Can't use ScreenLines unless initialized */
8622 if (ScreenLines == NULL)
8623 return;
8624
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 if (invert)
8626 screen_char_attr = HL_INVERSE;
8627 for (r = row; r < row + height; ++r)
8628 {
8629 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008630#ifdef FEAT_MBYTE
8631 max_off = off + screen_Columns;
8632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 for (c = col; c < col + width; ++c)
8634 {
8635#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008636 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 {
8638 screen_char_2(off + c, r, c);
8639 ++c;
8640 }
8641 else
8642#endif
8643 {
8644 screen_char(off + c, r, c);
8645#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008646 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 ++c;
8648#endif
8649 }
8650 }
8651 }
8652 screen_char_attr = 0;
8653}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655/*
8656 * Redraw the characters for a vertically split window.
8657 */
8658 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008659redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660{
8661 int col;
8662 int width;
8663
8664# ifdef FEAT_CLIPBOARD
8665 clip_may_clear_selection(row, end - 1);
8666# endif
8667
8668 if (wp == NULL)
8669 {
8670 col = 0;
8671 width = Columns;
8672 }
8673 else
8674 {
8675 col = wp->w_wincol;
8676 width = wp->w_width;
8677 }
8678 screen_draw_rectangle(row, col, end - row, width, FALSE);
8679}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008681 static void
8682space_to_screenline(int off, int attr)
8683{
8684 ScreenLines[off] = ' ';
8685 ScreenAttrs[off] = attr;
8686# ifdef FEAT_MBYTE
8687 if (enc_utf8)
8688 ScreenLinesUC[off] = 0;
8689# endif
8690}
8691
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692/*
8693 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8694 * with character 'c1' in first column followed by 'c2' in the other columns.
8695 * Use attributes 'attr'.
8696 */
8697 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008698screen_fill(
8699 int start_row,
8700 int end_row,
8701 int start_col,
8702 int end_col,
8703 int c1,
8704 int c2,
8705 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706{
8707 int row;
8708 int col;
8709 int off;
8710 int end_off;
8711 int did_delete;
8712 int c;
8713 int norm_term;
8714#if defined(FEAT_GUI) || defined(UNIX)
8715 int force_next = FALSE;
8716#endif
8717
8718 if (end_row > screen_Rows) /* safety check */
8719 end_row = screen_Rows;
8720 if (end_col > screen_Columns) /* safety check */
8721 end_col = screen_Columns;
8722 if (ScreenLines == NULL
8723 || start_row >= end_row
8724 || start_col >= end_col) /* nothing to do */
8725 return;
8726
8727 /* it's a "normal" terminal when not in a GUI or cterm */
8728 norm_term = (
8729#ifdef FEAT_GUI
8730 !gui.in_use &&
8731#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008732 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 for (row = start_row; row < end_row; ++row)
8734 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008735#ifdef FEAT_MBYTE
8736 if (has_mbyte
8737# ifdef FEAT_GUI
8738 && !gui.in_use
8739# endif
8740 )
8741 {
8742 /* When drawing over the right halve of a double-wide char clear
8743 * out the left halve. When drawing over the left halve of a
8744 * double wide-char clear out the right halve. Only needed in a
8745 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008746 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008747 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008748 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008749 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008750 }
8751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 /*
8753 * Try to use delete-line termcap code, when no attributes or in a
8754 * "normal" terminal, where a bold/italic space is just a
8755 * space.
8756 */
8757 did_delete = FALSE;
8758 if (c2 == ' '
8759 && end_col == Columns
8760 && can_clear(T_CE)
8761 && (attr == 0
8762 || (norm_term
8763 && attr <= HL_ALL
8764 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8765 {
8766 /*
8767 * check if we really need to clear something
8768 */
8769 col = start_col;
8770 if (c1 != ' ') /* don't clear first char */
8771 ++col;
8772
8773 off = LineOffset[row] + col;
8774 end_off = LineOffset[row] + end_col;
8775
8776 /* skip blanks (used often, keep it fast!) */
8777#ifdef FEAT_MBYTE
8778 if (enc_utf8)
8779 while (off < end_off && ScreenLines[off] == ' '
8780 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8781 ++off;
8782 else
8783#endif
8784 while (off < end_off && ScreenLines[off] == ' '
8785 && ScreenAttrs[off] == 0)
8786 ++off;
8787 if (off < end_off) /* something to be cleared */
8788 {
8789 col = off - LineOffset[row];
8790 screen_stop_highlight();
8791 term_windgoto(row, col);/* clear rest of this screen line */
8792 out_str(T_CE);
8793 screen_start(); /* don't know where cursor is now */
8794 col = end_col - col;
8795 while (col--) /* clear chars in ScreenLines */
8796 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008797 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 ++off;
8799 }
8800 }
8801 did_delete = TRUE; /* the chars are cleared now */
8802 }
8803
8804 off = LineOffset[row] + start_col;
8805 c = c1;
8806 for (col = start_col; col < end_col; ++col)
8807 {
8808 if (ScreenLines[off] != c
8809#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008810 || (enc_utf8 && (int)ScreenLinesUC[off]
8811 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812#endif
8813 || ScreenAttrs[off] != attr
8814#if defined(FEAT_GUI) || defined(UNIX)
8815 || force_next
8816#endif
8817 )
8818 {
8819#if defined(FEAT_GUI) || defined(UNIX)
8820 /* The bold trick may make a single row of pixels appear in
8821 * the next character. When a bold character is removed, the
8822 * next character should be redrawn too. This happens for our
8823 * own GUI and for some xterms. */
8824 if (
8825# ifdef FEAT_GUI
8826 gui.in_use
8827# endif
8828# if defined(FEAT_GUI) && defined(UNIX)
8829 ||
8830# endif
8831# ifdef UNIX
8832 term_is_xterm
8833# endif
8834 )
8835 {
8836 if (ScreenLines[off] != ' '
8837 && (ScreenAttrs[off] > HL_ALL
8838 || ScreenAttrs[off] & HL_BOLD))
8839 force_next = TRUE;
8840 else
8841 force_next = FALSE;
8842 }
8843#endif
8844 ScreenLines[off] = c;
8845#ifdef FEAT_MBYTE
8846 if (enc_utf8)
8847 {
8848 if (c >= 0x80)
8849 {
8850 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008851 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 }
8853 else
8854 ScreenLinesUC[off] = 0;
8855 }
8856#endif
8857 ScreenAttrs[off] = attr;
8858 if (!did_delete || c != ' ')
8859 screen_char(off, row, col);
8860 }
8861 ++off;
8862 if (col == start_col)
8863 {
8864 if (did_delete)
8865 break;
8866 c = c2;
8867 }
8868 }
8869 if (end_col == Columns)
8870 LineWraps[row] = FALSE;
8871 if (row == Rows - 1) /* overwritten the command line */
8872 {
8873 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008874 if (start_col == 0 && end_col == Columns
8875 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008877 if (start_col == 0)
8878 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 }
8880 }
8881}
8882
8883/*
8884 * Check if there should be a delay. Used before clearing or redrawing the
8885 * screen or the command line.
8886 */
8887 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008888check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889{
8890 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8891 && !did_wait_return
8892 && emsg_silent == 0)
8893 {
8894 out_flush();
8895 ui_delay(1000L, TRUE);
8896 emsg_on_display = FALSE;
8897 if (check_msg_scroll)
8898 msg_scroll = FALSE;
8899 }
8900}
8901
8902/*
8903 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008904 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 * Returns TRUE if there is a valid screen to write to.
8906 * Returns FALSE when starting up and screen not initialized yet.
8907 */
8908 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008909screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008911 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 return (ScreenLines != NULL);
8913}
8914
8915/*
8916 * Resize the shell to Rows and Columns.
8917 * Allocate ScreenLines[] and associated items.
8918 *
8919 * There may be some time between setting Rows and Columns and (re)allocating
8920 * ScreenLines[]. This happens when starting up and when (manually) changing
8921 * the shell size. Always use screen_Rows and screen_Columns to access items
8922 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8923 * final size of the shell is needed.
8924 */
8925 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008926screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927{
8928 int new_row, old_row;
8929#ifdef FEAT_GUI
8930 int old_Rows;
8931#endif
8932 win_T *wp;
8933 int outofmem = FALSE;
8934 int len;
8935 schar_T *new_ScreenLines;
8936#ifdef FEAT_MBYTE
8937 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008938 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008940 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941#endif
8942 sattr_T *new_ScreenAttrs;
8943 unsigned *new_LineOffset;
8944 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008945 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008946 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008948 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008949 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008951retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 /*
8953 * Allocation of the screen buffers is done only when the size changes and
8954 * when Rows and Columns have been set and we have started doing full
8955 * screen stuff.
8956 */
8957 if ((ScreenLines != NULL
8958 && Rows == screen_Rows
8959 && Columns == screen_Columns
8960#ifdef FEAT_MBYTE
8961 && enc_utf8 == (ScreenLinesUC != NULL)
8962 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008963 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964#endif
8965 )
8966 || Rows == 0
8967 || Columns == 0
8968 || (!full_screen && ScreenLines == NULL))
8969 return;
8970
8971 /*
8972 * It's possible that we produce an out-of-memory message below, which
8973 * will cause this function to be called again. To break the loop, just
8974 * return here.
8975 */
8976 if (entered)
8977 return;
8978 entered = TRUE;
8979
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008980 /*
8981 * Note that the window sizes are updated before reallocating the arrays,
8982 * thus we must not redraw here!
8983 */
8984 ++RedrawingDisabled;
8985
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 win_new_shellsize(); /* fit the windows in the new sized shell */
8987
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 comp_col(); /* recompute columns for shown command and ruler */
8989
8990 /*
8991 * We're changing the size of the screen.
8992 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8993 * - Move lines from the old arrays into the new arrays, clear extra
8994 * lines (unless the screen is going to be cleared).
8995 * - Free the old arrays.
8996 *
8997 * If anything fails, make ScreenLines NULL, so we don't do anything!
8998 * Continuing with the old ScreenLines may result in a crash, because the
8999 * size is wrong.
9000 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009001 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009003 if (aucmd_win != NULL)
9004 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005
9006 new_ScreenLines = (schar_T *)lalloc((long_u)(
9007 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9008#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01009009 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 if (enc_utf8)
9011 {
9012 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
9013 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009014 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01009015 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
9017 }
9018 if (enc_dbcs == DBCS_JPNU)
9019 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
9020 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9021#endif
9022 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
9023 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
9024 new_LineOffset = (unsigned *)lalloc((long_u)(
9025 Rows * sizeof(unsigned)), FALSE);
9026 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009027 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009029 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 {
9031 if (win_alloc_lines(wp) == FAIL)
9032 {
9033 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009034 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 }
9036 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009037 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9038 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009039 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009040give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009042#ifdef FEAT_MBYTE
9043 for (i = 0; i < p_mco; ++i)
9044 if (new_ScreenLinesC[i] == NULL)
9045 break;
9046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 if (new_ScreenLines == NULL
9048#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009049 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
9051#endif
9052 || new_ScreenAttrs == NULL
9053 || new_LineOffset == NULL
9054 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009055 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 || outofmem)
9057 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009058 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009059 {
9060 /* guess the size */
9061 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9062
9063 /* Remember we did this to avoid getting outofmem messages over
9064 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009065 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009066 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009067 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01009069 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009070 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009071 VIM_CLEAR(new_ScreenLinesC[i]);
9072 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01009074 VIM_CLEAR(new_ScreenAttrs);
9075 VIM_CLEAR(new_LineOffset);
9076 VIM_CLEAR(new_LineWraps);
9077 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 }
9079 else
9080 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009081 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009082
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 for (new_row = 0; new_row < Rows; ++new_row)
9084 {
9085 new_LineOffset[new_row] = new_row * Columns;
9086 new_LineWraps[new_row] = FALSE;
9087
9088 /*
9089 * If the screen is not going to be cleared, copy as much as
9090 * possible from the old screen to the new one and clear the rest
9091 * (used when resizing the window at the "--more--" prompt or when
9092 * executing an external command, for the GUI).
9093 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009094 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 {
9096 (void)vim_memset(new_ScreenLines + new_row * Columns,
9097 ' ', (size_t)Columns * sizeof(schar_T));
9098#ifdef FEAT_MBYTE
9099 if (enc_utf8)
9100 {
9101 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9102 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009103 for (i = 0; i < p_mco; ++i)
9104 (void)vim_memset(new_ScreenLinesC[i]
9105 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 0, (size_t)Columns * sizeof(u8char_T));
9107 }
9108 if (enc_dbcs == DBCS_JPNU)
9109 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9110 0, (size_t)Columns * sizeof(schar_T));
9111#endif
9112 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9113 0, (size_t)Columns * sizeof(sattr_T));
9114 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009115 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 {
9117 if (screen_Columns < Columns)
9118 len = screen_Columns;
9119 else
9120 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009121#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009122 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009123 * may be invalid now. Also when p_mco changes. */
9124 if (!(enc_utf8 && ScreenLinesUC == NULL)
9125 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009126#endif
9127 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9128 ScreenLines + LineOffset[old_row],
9129 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009131 if (enc_utf8 && ScreenLinesUC != NULL
9132 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 {
9134 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9135 ScreenLinesUC + LineOffset[old_row],
9136 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009137 for (i = 0; i < p_mco; ++i)
9138 mch_memmove(new_ScreenLinesC[i]
9139 + new_LineOffset[new_row],
9140 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 (size_t)len * sizeof(u8char_T));
9142 }
9143 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9144 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9145 ScreenLines2 + LineOffset[old_row],
9146 (size_t)len * sizeof(schar_T));
9147#endif
9148 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9149 ScreenAttrs + LineOffset[old_row],
9150 (size_t)len * sizeof(sattr_T));
9151 }
9152 }
9153 }
9154 /* Use the last line of the screen for the current line. */
9155 current_ScreenLine = new_ScreenLines + Rows * Columns;
9156 }
9157
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009158 free_screenlines();
9159
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 ScreenLines = new_ScreenLines;
9161#ifdef FEAT_MBYTE
9162 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009163 for (i = 0; i < p_mco; ++i)
9164 ScreenLinesC[i] = new_ScreenLinesC[i];
9165 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 ScreenLines2 = new_ScreenLines2;
9167#endif
9168 ScreenAttrs = new_ScreenAttrs;
9169 LineOffset = new_LineOffset;
9170 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009171 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172
9173 /* It's important that screen_Rows and screen_Columns reflect the actual
9174 * size of ScreenLines[]. Set them before calling anything. */
9175#ifdef FEAT_GUI
9176 old_Rows = screen_Rows;
9177#endif
9178 screen_Rows = Rows;
9179 screen_Columns = Columns;
9180
9181 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009182 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 screenclear2();
9184
9185#ifdef FEAT_GUI
9186 else if (gui.in_use
9187 && !gui.starting
9188 && ScreenLines != NULL
9189 && old_Rows != Rows)
9190 {
9191 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9192 /*
9193 * Adjust the position of the cursor, for when executing an external
9194 * command.
9195 */
9196 if (msg_row >= Rows) /* Rows got smaller */
9197 msg_row = Rows - 1; /* put cursor at last row */
9198 else if (Rows > old_Rows) /* Rows got bigger */
9199 msg_row += Rows - old_Rows; /* put cursor in same place */
9200 if (msg_col >= Columns) /* Columns got smaller */
9201 msg_col = Columns - 1; /* put cursor at last column */
9202 }
9203#endif
9204
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009206 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009207
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009208 /*
9209 * Do not apply autocommands more than 3 times to avoid an endless loop
9210 * in case applying autocommands always changes Rows or Columns.
9211 */
9212 if (starting == 0 && ++retry_count <= 3)
9213 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009214 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009215 /* In rare cases, autocommands may have altered Rows or Columns,
9216 * jump back to check if we need to allocate the screen again. */
9217 goto retry;
9218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219}
9220
9221 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009222free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009223{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009224#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009225 int i;
9226
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009227 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009228 for (i = 0; i < Screen_mco; ++i)
9229 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009230 vim_free(ScreenLines2);
9231#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009232 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009233 vim_free(ScreenAttrs);
9234 vim_free(LineOffset);
9235 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009236 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009237}
9238
9239 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009240screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241{
9242 check_for_delay(FALSE);
9243 screenalloc(FALSE); /* allocate screen buffers if size changed */
9244 screenclear2(); /* clear the screen */
9245}
9246
9247 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009248screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249{
9250 int i;
9251
9252 if (starting == NO_SCREEN || ScreenLines == NULL
9253#ifdef FEAT_GUI
9254 || (gui.in_use && gui.starting)
9255#endif
9256 )
9257 return;
9258
9259#ifdef FEAT_GUI
9260 if (!gui.in_use)
9261#endif
9262 screen_attr = -1; /* force setting the Normal colors */
9263 screen_stop_highlight(); /* don't want highlighting here */
9264
9265#ifdef FEAT_CLIPBOARD
9266 /* disable selection without redrawing it */
9267 clip_scroll_selection(9999);
9268#endif
9269
9270 /* blank out ScreenLines */
9271 for (i = 0; i < Rows; ++i)
9272 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009273 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 LineWraps[i] = FALSE;
9275 }
9276
9277 if (can_clear(T_CL))
9278 {
9279 out_str(T_CL); /* clear the display */
9280 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009281 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 }
9283 else
9284 {
9285 /* can't clear the screen, mark all chars with invalid attributes */
9286 for (i = 0; i < Rows; ++i)
9287 lineinvalid(LineOffset[i], (int)Columns);
9288 clear_cmdline = TRUE;
9289 }
9290
9291 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9292
9293 win_rest_invalid(firstwin);
9294 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009295 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 if (must_redraw == CLEAR) /* no need to clear again */
9297 must_redraw = NOT_VALID;
9298 compute_cmdrow();
9299 msg_row = cmdline_row; /* put cursor on last line for messages */
9300 msg_col = 0;
9301 screen_start(); /* don't know where cursor is now */
9302 msg_scrolled = 0; /* can't scroll back */
9303 msg_didany = FALSE;
9304 msg_didout = FALSE;
9305}
9306
9307/*
9308 * Clear one line in ScreenLines.
9309 */
9310 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009311lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312{
9313 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9314#ifdef FEAT_MBYTE
9315 if (enc_utf8)
9316 (void)vim_memset(ScreenLinesUC + off, 0,
9317 (size_t)width * sizeof(u8char_T));
9318#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009319 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320}
9321
9322/*
9323 * Mark one line in ScreenLines invalid by setting the attributes to an
9324 * invalid value.
9325 */
9326 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009327lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328{
9329 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9330}
9331
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332/*
9333 * Copy part of a Screenline for vertically split window "wp".
9334 */
9335 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009336linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337{
9338 unsigned off_to = LineOffset[to] + wp->w_wincol;
9339 unsigned off_from = LineOffset[from] + wp->w_wincol;
9340
9341 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9342 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009343#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 if (enc_utf8)
9345 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009346 int i;
9347
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9349 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009350 for (i = 0; i < p_mco; ++i)
9351 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9352 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 }
9354 if (enc_dbcs == DBCS_JPNU)
9355 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9356 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9359 wp->w_width * sizeof(sattr_T));
9360}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361
9362/*
9363 * Return TRUE if clearing with term string "p" would work.
9364 * It can't work when the string is empty or it won't set the right background.
9365 */
9366 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009367can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368{
9369 return (*p != NUL && (t_colors <= 1
9370#ifdef FEAT_GUI
9371 || gui.in_use
9372#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009373#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009374 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009375 || (!p_tgc && cterm_normal_bg_color == 0)
9376#else
9377 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009378#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009379 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380}
9381
9382/*
9383 * Reset cursor position. Use whenever cursor was moved because of outputting
9384 * something directly to the screen (shell commands) or a terminal control
9385 * code.
9386 */
9387 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009388screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389{
9390 screen_cur_row = screen_cur_col = 9999;
9391}
9392
9393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 * Move the cursor to position "row","col" in the screen.
9395 * This tries to find the most efficient way to move, minimizing the number of
9396 * characters sent to the terminal.
9397 */
9398 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009399windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009401 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 int i;
9403 int plan;
9404 int cost;
9405 int wouldbe_col;
9406 int noinvcurs;
9407 char_u *bs;
9408 int goto_cost;
9409 int attr;
9410
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009411#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9413
9414#define PLAN_LE 1
9415#define PLAN_CR 2
9416#define PLAN_NL 3
9417#define PLAN_WRITE 4
9418 /* Can't use ScreenLines unless initialized */
9419 if (ScreenLines == NULL)
9420 return;
9421
9422 if (col != screen_cur_col || row != screen_cur_row)
9423 {
9424 /* Check for valid position. */
9425 if (row < 0) /* window without text lines? */
9426 row = 0;
9427 if (row >= screen_Rows)
9428 row = screen_Rows - 1;
9429 if (col >= screen_Columns)
9430 col = screen_Columns - 1;
9431
9432 /* check if no cursor movement is allowed in highlight mode */
9433 if (screen_attr && *T_MS == NUL)
9434 noinvcurs = HIGHL_COST;
9435 else
9436 noinvcurs = 0;
9437 goto_cost = GOTO_COST + noinvcurs;
9438
9439 /*
9440 * Plan how to do the positioning:
9441 * 1. Use CR to move it to column 0, same row.
9442 * 2. Use T_LE to move it a few columns to the left.
9443 * 3. Use NL to move a few lines down, column 0.
9444 * 4. Move a few columns to the right with T_ND or by writing chars.
9445 *
9446 * Don't do this if the cursor went beyond the last column, the cursor
9447 * position is unknown then (some terminals wrap, some don't )
9448 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009449 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 * characters to move the cursor to the right.
9451 */
9452 if (row >= screen_cur_row && screen_cur_col < Columns)
9453 {
9454 /*
9455 * If the cursor is in the same row, bigger col, we can use CR
9456 * or T_LE.
9457 */
9458 bs = NULL; /* init for GCC */
9459 attr = screen_attr;
9460 if (row == screen_cur_row && col < screen_cur_col)
9461 {
9462 /* "le" is preferred over "bc", because "bc" is obsolete */
9463 if (*T_LE)
9464 bs = T_LE; /* "cursor left" */
9465 else
9466 bs = T_BC; /* "backspace character (old) */
9467 if (*bs)
9468 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9469 else
9470 cost = 999;
9471 if (col + 1 < cost) /* using CR is less characters */
9472 {
9473 plan = PLAN_CR;
9474 wouldbe_col = 0;
9475 cost = 1; /* CR is just one character */
9476 }
9477 else
9478 {
9479 plan = PLAN_LE;
9480 wouldbe_col = col;
9481 }
9482 if (noinvcurs) /* will stop highlighting */
9483 {
9484 cost += noinvcurs;
9485 attr = 0;
9486 }
9487 }
9488
9489 /*
9490 * If the cursor is above where we want to be, we can use CR LF.
9491 */
9492 else if (row > screen_cur_row)
9493 {
9494 plan = PLAN_NL;
9495 wouldbe_col = 0;
9496 cost = (row - screen_cur_row) * 2; /* CR LF */
9497 if (noinvcurs) /* will stop highlighting */
9498 {
9499 cost += noinvcurs;
9500 attr = 0;
9501 }
9502 }
9503
9504 /*
9505 * If the cursor is in the same row, smaller col, just use write.
9506 */
9507 else
9508 {
9509 plan = PLAN_WRITE;
9510 wouldbe_col = screen_cur_col;
9511 cost = 0;
9512 }
9513
9514 /*
9515 * Check if any characters that need to be written have the
9516 * correct attributes. Also avoid UTF-8 characters.
9517 */
9518 i = col - wouldbe_col;
9519 if (i > 0)
9520 cost += i;
9521 if (cost < goto_cost && i > 0)
9522 {
9523 /*
9524 * Check if the attributes are correct without additionally
9525 * stopping highlighting.
9526 */
9527 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9528 while (i && *p++ == attr)
9529 --i;
9530 if (i != 0)
9531 {
9532 /*
9533 * Try if it works when highlighting is stopped here.
9534 */
9535 if (*--p == 0)
9536 {
9537 cost += noinvcurs;
9538 while (i && *p++ == 0)
9539 --i;
9540 }
9541 if (i != 0)
9542 cost = 999; /* different attributes, don't do it */
9543 }
9544#ifdef FEAT_MBYTE
9545 if (enc_utf8)
9546 {
9547 /* Don't use an UTF-8 char for positioning, it's slow. */
9548 for (i = wouldbe_col; i < col; ++i)
9549 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9550 {
9551 cost = 999;
9552 break;
9553 }
9554 }
9555#endif
9556 }
9557
9558 /*
9559 * We can do it without term_windgoto()!
9560 */
9561 if (cost < goto_cost)
9562 {
9563 if (plan == PLAN_LE)
9564 {
9565 if (noinvcurs)
9566 screen_stop_highlight();
9567 while (screen_cur_col > col)
9568 {
9569 out_str(bs);
9570 --screen_cur_col;
9571 }
9572 }
9573 else if (plan == PLAN_CR)
9574 {
9575 if (noinvcurs)
9576 screen_stop_highlight();
9577 out_char('\r');
9578 screen_cur_col = 0;
9579 }
9580 else if (plan == PLAN_NL)
9581 {
9582 if (noinvcurs)
9583 screen_stop_highlight();
9584 while (screen_cur_row < row)
9585 {
9586 out_char('\n');
9587 ++screen_cur_row;
9588 }
9589 screen_cur_col = 0;
9590 }
9591
9592 i = col - screen_cur_col;
9593 if (i > 0)
9594 {
9595 /*
9596 * Use cursor-right if it's one character only. Avoids
9597 * removing a line of pixels from the last bold char, when
9598 * using the bold trick in the GUI.
9599 */
9600 if (T_ND[0] != NUL && T_ND[1] == NUL)
9601 {
9602 while (i-- > 0)
9603 out_char(*T_ND);
9604 }
9605 else
9606 {
9607 int off;
9608
9609 off = LineOffset[row] + screen_cur_col;
9610 while (i-- > 0)
9611 {
9612 if (ScreenAttrs[off] != screen_attr)
9613 screen_stop_highlight();
9614#ifdef FEAT_MBYTE
9615 out_flush_check();
9616#endif
9617 out_char(ScreenLines[off]);
9618#ifdef FEAT_MBYTE
9619 if (enc_dbcs == DBCS_JPNU
9620 && ScreenLines[off] == 0x8e)
9621 out_char(ScreenLines2[off]);
9622#endif
9623 ++off;
9624 }
9625 }
9626 }
9627 }
9628 }
9629 else
9630 cost = 999;
9631
9632 if (cost >= goto_cost)
9633 {
9634 if (noinvcurs)
9635 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009636 if (row == screen_cur_row && (col > screen_cur_col)
9637 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 term_cursor_right(col - screen_cur_col);
9639 else
9640 term_windgoto(row, col);
9641 }
9642 screen_cur_row = row;
9643 screen_cur_col = col;
9644 }
9645}
9646
9647/*
9648 * Set cursor to its position in the current window.
9649 */
9650 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009651setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009653 setcursor_mayforce(FALSE);
9654}
9655
9656/*
9657 * Set cursor to its position in the current window.
9658 * When "force" is TRUE also when not redrawing.
9659 */
9660 void
9661setcursor_mayforce(int force)
9662{
9663 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 {
9665 validate_cursor();
9666 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009667 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009669 /* With 'rightleft' set and the cursor on a double-wide
9670 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009671 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009673 (has_mbyte
9674 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9675 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676# endif
9677 1)) :
9678#endif
9679 curwin->w_wcol));
9680 }
9681}
9682
9683
9684/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009685 * Insert 'line_count' lines at 'row' in window 'wp'.
9686 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9687 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 * scrolling.
9689 * Returns FAIL if the lines are not inserted, OK for success.
9690 */
9691 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009692win_ins_lines(
9693 win_T *wp,
9694 int row,
9695 int line_count,
9696 int invalid,
9697 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698{
9699 int did_delete;
9700 int nextrow;
9701 int lastrow;
9702 int retval;
9703
9704 if (invalid)
9705 wp->w_lines_valid = 0;
9706
9707 if (wp->w_height < 5)
9708 return FAIL;
9709
9710 if (line_count > wp->w_height - row)
9711 line_count = wp->w_height - row;
9712
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009713 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 if (retval != MAYBE)
9715 return retval;
9716
9717 /*
9718 * If there is a next window or a status line, we first try to delete the
9719 * lines at the bottom to avoid messing what is after the window.
9720 * If this fails and there are following windows, don't do anything to avoid
9721 * messing up those windows, better just redraw.
9722 */
9723 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 if (wp->w_next != NULL || wp->w_status_height)
9725 {
9726 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009727 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 did_delete = TRUE;
9729 else if (wp->w_next)
9730 return FAIL;
9731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 /*
9733 * if no lines deleted, blank the lines that will end up below the window
9734 */
9735 if (!did_delete)
9736 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009739 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 lastrow = nextrow + line_count;
9741 if (lastrow > Rows)
9742 lastrow = Rows;
9743 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009744 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 ' ', ' ', 0);
9746 }
9747
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009748 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 == FAIL)
9750 {
9751 /* deletion will have messed up other windows */
9752 if (did_delete)
9753 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 win_rest_invalid(W_NEXT(wp));
9756 }
9757 return FAIL;
9758 }
9759
9760 return OK;
9761}
9762
9763/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009764 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9766 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9767 * scrolling
9768 * Return OK for success, FAIL if the lines are not deleted.
9769 */
9770 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009771win_del_lines(
9772 win_T *wp,
9773 int row,
9774 int line_count,
9775 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009776 int mayclear,
9777 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778{
9779 int retval;
9780
9781 if (invalid)
9782 wp->w_lines_valid = 0;
9783
9784 if (line_count > wp->w_height - row)
9785 line_count = wp->w_height - row;
9786
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009787 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 if (retval != MAYBE)
9789 return retval;
9790
9791 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009792 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 return FAIL;
9794
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 /*
9796 * If there are windows or status lines below, try to put them at the
9797 * correct place. If we can't do that, they have to be redrawn.
9798 */
9799 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9800 {
9801 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009802 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 {
9804 wp->w_redr_status = TRUE;
9805 win_rest_invalid(wp->w_next);
9806 }
9807 }
9808 /*
9809 * If this is the last window and there is no status line, redraw the
9810 * command line later.
9811 */
9812 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 redraw_cmdline = TRUE;
9814 return OK;
9815}
9816
9817/*
9818 * Common code for win_ins_lines() and win_del_lines().
9819 * Returns OK or FAIL when the work has been done.
9820 * Returns MAYBE when not finished yet.
9821 */
9822 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009823win_do_lines(
9824 win_T *wp,
9825 int row,
9826 int line_count,
9827 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009828 int del,
9829 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830{
9831 int retval;
9832
9833 if (!redrawing() || line_count <= 0)
9834 return FAIL;
9835
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009836 /* When inserting lines would result in loss of command output, just redraw
9837 * the lines. */
9838 if (no_win_do_lines_ins && !del)
9839 return FAIL;
9840
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009842 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009844 if (!no_win_do_lines_ins)
9845 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 return FAIL;
9847 }
9848
9849 /*
9850 * Delete all remaining lines
9851 */
9852 if (row + line_count >= wp->w_height)
9853 {
9854 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009855 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 ' ', ' ', 0);
9857 return OK;
9858 }
9859
9860 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009861 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009863 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009865 if (!no_win_do_lines_ins)
9866 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867
9868 /*
9869 * If the terminal can set a scroll region, use that.
9870 * Always do this in a vertically split window. This will redraw from
9871 * ScreenLines[] when t_CV isn't defined. That's faster than using
9872 * win_line().
9873 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009874 * a character in the lower right corner of the scroll region may cause a
9875 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009877 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 scroll_region_set(wp, row);
9881 if (del)
9882 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009883 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 else
9885 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009886 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 scroll_region_reset();
9889 return retval;
9890 }
9891
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9893 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894
9895 return MAYBE;
9896}
9897
9898/*
9899 * window 'wp' and everything after it is messed up, mark it for redraw
9900 */
9901 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009902win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 {
9906 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 wp->w_redr_status = TRUE;
9908 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 }
9910 redraw_cmdline = TRUE;
9911}
9912
9913/*
9914 * The rest of the routines in this file perform screen manipulations. The
9915 * given operation is performed physically on the screen. The corresponding
9916 * change is also made to the internal screen image. In this way, the editor
9917 * anticipates the effect of editing changes on the appearance of the screen.
9918 * That way, when we call screenupdate a complete redraw isn't usually
9919 * necessary. Another advantage is that we can keep adding code to anticipate
9920 * screen changes, and in the meantime, everything still works.
9921 */
9922
9923/*
9924 * types for inserting or deleting lines
9925 */
9926#define USE_T_CAL 1
9927#define USE_T_CDL 2
9928#define USE_T_AL 3
9929#define USE_T_CE 4
9930#define USE_T_DL 5
9931#define USE_T_SR 6
9932#define USE_NL 7
9933#define USE_T_CD 8
9934#define USE_REDRAW 9
9935
9936/*
9937 * insert lines on the screen and update ScreenLines[]
9938 * 'end' is the line after the scrolled part. Normally it is Rows.
9939 * When scrolling region used 'off' is the offset from the top for the region.
9940 * 'row' and 'end' are relative to the start of the region.
9941 *
9942 * return FAIL for failure, OK for success.
9943 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009944 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009945screen_ins_lines(
9946 int off,
9947 int row,
9948 int line_count,
9949 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009950 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009951 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952{
9953 int i;
9954 int j;
9955 unsigned temp;
9956 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009957 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 int type;
9959 int result_empty;
9960 int can_ce = can_clear(T_CE);
9961
9962 /*
9963 * FAIL if
9964 * - there is no valid screen
9965 * - the screen has to be redrawn completely
9966 * - the line count is less than one
9967 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009968 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009970 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9971#ifdef FEAT_CLIPBOARD
9972 || (clip_star.state != SELECT_CLEARED
9973 && redrawing_for_callback > 0)
9974#endif
9975 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 return FAIL;
9977
9978 /*
9979 * There are seven ways to insert lines:
9980 * 0. When in a vertically split window and t_CV isn't set, redraw the
9981 * characters from ScreenLines[].
9982 * 1. Use T_CD (clear to end of display) if it exists and the result of
9983 * the insert is just empty lines
9984 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9985 * present or line_count > 1. It looks better if we do all the inserts
9986 * at once.
9987 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9988 * insert is just empty lines and T_CE is not present or line_count >
9989 * 1.
9990 * 4. Use T_AL (insert line) if it exists.
9991 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9992 * just empty lines.
9993 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9994 * just empty lines.
9995 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9996 * the 'da' flag is not set or we have clear line capability.
9997 * 8. redraw the characters from ScreenLines[].
9998 *
9999 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10000 * the scrollbar for the window. It does have insert line, use that if it
10001 * exists.
10002 */
10003 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10005 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010006 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 type = USE_T_CD;
10008 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10009 type = USE_T_CAL;
10010 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10011 type = USE_T_CDL;
10012 else if (*T_AL != NUL)
10013 type = USE_T_AL;
10014 else if (can_ce && result_empty)
10015 type = USE_T_CE;
10016 else if (*T_DL != NUL && result_empty)
10017 type = USE_T_DL;
10018 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10019 type = USE_T_SR;
10020 else
10021 return FAIL;
10022
10023 /*
10024 * For clearing the lines screen_del_lines() is used. This will also take
10025 * care of t_db if necessary.
10026 */
10027 if (type == USE_T_CD || type == USE_T_CDL ||
10028 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010029 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030
10031 /*
10032 * If text is retained below the screen, first clear or delete as many
10033 * lines at the bottom of the window as are about to be inserted so that
10034 * the deleted lines won't later surface during a screen_del_lines.
10035 */
10036 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010037 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038
10039#ifdef FEAT_CLIPBOARD
10040 /* Remove a modeless selection when inserting lines halfway the screen
10041 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010042 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010043 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 else
10045 clip_scroll_selection(-line_count);
10046#endif
10047
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048#ifdef FEAT_GUI
10049 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10050 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010051 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052#endif
10053
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010054 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10055 cursor_col = wp->w_wincol;
10056
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 if (*T_CCS != NUL) /* cursor relative to region */
10058 cursor_row = row;
10059 else
10060 cursor_row = row + off;
10061
10062 /*
10063 * Shift LineOffset[] line_count down to reflect the inserted lines.
10064 * Clear the inserted lines in ScreenLines[].
10065 */
10066 row += off;
10067 end += off;
10068 for (i = 0; i < line_count; ++i)
10069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 if (wp != NULL && wp->w_width != Columns)
10071 {
10072 /* need to copy part of a line */
10073 j = end - 1 - i;
10074 while ((j -= line_count) >= row)
10075 linecopy(j + line_count, j, wp);
10076 j += line_count;
10077 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010078 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10079 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 else
10081 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10082 LineWraps[j] = FALSE;
10083 }
10084 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 {
10086 j = end - 1 - i;
10087 temp = LineOffset[j];
10088 while ((j -= line_count) >= row)
10089 {
10090 LineOffset[j + line_count] = LineOffset[j];
10091 LineWraps[j + line_count] = LineWraps[j];
10092 }
10093 LineOffset[j + line_count] = temp;
10094 LineWraps[j + line_count] = FALSE;
10095 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010096 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 else
10098 lineinvalid(temp, (int)Columns);
10099 }
10100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101
10102 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010103 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010104 if (clear_attr != 0)
10105 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 /* redraw the characters */
10108 if (type == USE_REDRAW)
10109 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010110 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 {
10112 term_append_lines(line_count);
10113 screen_start(); /* don't know where cursor is now */
10114 }
10115 else
10116 {
10117 for (i = 0; i < line_count; i++)
10118 {
10119 if (type == USE_T_AL)
10120 {
10121 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010122 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 out_str(T_AL);
10124 }
10125 else /* type == USE_T_SR */
10126 out_str(T_SR);
10127 screen_start(); /* don't know where cursor is now */
10128 }
10129 }
10130
10131 /*
10132 * With scroll-reverse and 'da' flag set we need to clear the lines that
10133 * have been scrolled down into the region.
10134 */
10135 if (type == USE_T_SR && *T_DA)
10136 {
10137 for (i = 0; i < line_count; ++i)
10138 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010139 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 out_str(T_CE);
10141 screen_start(); /* don't know where cursor is now */
10142 }
10143 }
10144
10145#ifdef FEAT_GUI
10146 gui_can_update_cursor();
10147 if (gui.in_use)
10148 out_flush(); /* always flush after a scroll */
10149#endif
10150 return OK;
10151}
10152
10153/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010154 * Delete lines on the screen and update ScreenLines[].
10155 * "end" is the line after the scrolled part. Normally it is Rows.
10156 * When scrolling region used "off" is the offset from the top for the region.
10157 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 *
10159 * Return OK for success, FAIL if the lines are not deleted.
10160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010162screen_del_lines(
10163 int off,
10164 int row,
10165 int line_count,
10166 int end,
10167 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010168 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010169 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170{
10171 int j;
10172 int i;
10173 unsigned temp;
10174 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010175 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 int cursor_end;
10177 int result_empty; /* result is empty until end of region */
10178 int can_delete; /* deleting line codes can be used */
10179 int type;
10180
10181 /*
10182 * FAIL if
10183 * - there is no valid screen
10184 * - the screen has to be redrawn completely
10185 * - the line count is less than one
10186 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010187 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010189 if (!screen_valid(TRUE) || line_count <= 0
10190 || (!force && line_count > p_ttyscroll)
10191#ifdef FEAT_CLIPBOARD
10192 || (clip_star.state != SELECT_CLEARED
10193 && redrawing_for_callback > 0)
10194#endif
10195 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 return FAIL;
10197
10198 /*
10199 * Check if the rest of the current region will become empty.
10200 */
10201 result_empty = row + line_count >= end;
10202
10203 /*
10204 * We can delete lines only when 'db' flag not set or when 'ce' option
10205 * available.
10206 */
10207 can_delete = (*T_DB == NUL || can_clear(T_CE));
10208
10209 /*
10210 * There are six ways to delete lines:
10211 * 0. When in a vertically split window and t_CV isn't set, redraw the
10212 * characters from ScreenLines[].
10213 * 1. Use T_CD if it exists and the result is empty.
10214 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10215 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10216 * none of the other ways work.
10217 * 4. Use T_CE (erase line) if the result is empty.
10218 * 5. Use T_DL (delete line) if it exists.
10219 * 6. redraw the characters from ScreenLines[].
10220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10222 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010223 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 type = USE_T_CD;
10225#if defined(__BEOS__) && defined(BEOS_DR8)
10226 /*
10227 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10228 * its internal termcap... this works okay for tests which test *T_DB !=
10229 * NUL. It has the disadvantage that the user cannot use any :set t_*
10230 * command to get T_DB (back) to empty_option, only :set term=... will do
10231 * the trick...
10232 * Anyway, this hack will hopefully go away with the next OS release.
10233 * (Olaf Seibert)
10234 */
10235 else if (row == 0 && T_DB == empty_option
10236 && (line_count == 1 || *T_CDL == NUL))
10237#else
10238 else if (row == 0 && (
10239#ifndef AMIGA
10240 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10241 * up, so use delete-line command */
10242 line_count == 1 ||
10243#endif
10244 *T_CDL == NUL))
10245#endif
10246 type = USE_NL;
10247 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10248 type = USE_T_CDL;
10249 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010250 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 type = USE_T_CE;
10252 else if (*T_DL != NUL && can_delete)
10253 type = USE_T_DL;
10254 else if (*T_CDL != NUL && can_delete)
10255 type = USE_T_CDL;
10256 else
10257 return FAIL;
10258
10259#ifdef FEAT_CLIPBOARD
10260 /* Remove a modeless selection when deleting lines halfway the screen or
10261 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010262 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010263 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 else
10265 clip_scroll_selection(line_count);
10266#endif
10267
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268#ifdef FEAT_GUI
10269 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10270 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010271 gui_dont_update_cursor(gui.cursor_row >= row + off
10272 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273#endif
10274
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010275 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10276 cursor_col = wp->w_wincol;
10277
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 if (*T_CCS != NUL) /* cursor relative to region */
10279 {
10280 cursor_row = row;
10281 cursor_end = end;
10282 }
10283 else
10284 {
10285 cursor_row = row + off;
10286 cursor_end = end + off;
10287 }
10288
10289 /*
10290 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10291 * Clear the inserted lines in ScreenLines[].
10292 */
10293 row += off;
10294 end += off;
10295 for (i = 0; i < line_count; ++i)
10296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 if (wp != NULL && wp->w_width != Columns)
10298 {
10299 /* need to copy part of a line */
10300 j = row + i;
10301 while ((j += line_count) <= end - 1)
10302 linecopy(j - line_count, j, wp);
10303 j -= line_count;
10304 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010305 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10306 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 else
10308 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10309 LineWraps[j] = FALSE;
10310 }
10311 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 {
10313 /* whole width, moving the line pointers is faster */
10314 j = row + i;
10315 temp = LineOffset[j];
10316 while ((j += line_count) <= end - 1)
10317 {
10318 LineOffset[j - line_count] = LineOffset[j];
10319 LineWraps[j - line_count] = LineWraps[j];
10320 }
10321 LineOffset[j - line_count] = temp;
10322 LineWraps[j - line_count] = FALSE;
10323 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010324 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 else
10326 lineinvalid(temp, (int)Columns);
10327 }
10328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010330 if (screen_attr != clear_attr)
10331 screen_stop_highlight();
10332 if (clear_attr != 0)
10333 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 /* redraw the characters */
10336 if (type == USE_REDRAW)
10337 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010338 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010340 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 out_str(T_CD);
10342 screen_start(); /* don't know where cursor is now */
10343 }
10344 else if (type == USE_T_CDL)
10345 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010346 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 term_delete_lines(line_count);
10348 screen_start(); /* don't know where cursor is now */
10349 }
10350 /*
10351 * Deleting lines at top of the screen or scroll region: Just scroll
10352 * the whole screen (scroll region) up by outputting newlines on the
10353 * last line.
10354 */
10355 else if (type == USE_NL)
10356 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010357 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 for (i = line_count; --i >= 0; )
10359 out_char('\n'); /* cursor will remain on same line */
10360 }
10361 else
10362 {
10363 for (i = line_count; --i >= 0; )
10364 {
10365 if (type == USE_T_DL)
10366 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010367 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 out_str(T_DL); /* delete a line */
10369 }
10370 else /* type == USE_T_CE */
10371 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010372 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 out_str(T_CE); /* erase a line */
10374 }
10375 screen_start(); /* don't know where cursor is now */
10376 }
10377 }
10378
10379 /*
10380 * If the 'db' flag is set, we need to clear the lines that have been
10381 * scrolled up at the bottom of the region.
10382 */
10383 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10384 {
10385 for (i = line_count; i > 0; --i)
10386 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010387 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 out_str(T_CE); /* erase a line */
10389 screen_start(); /* don't know where cursor is now */
10390 }
10391 }
10392
10393#ifdef FEAT_GUI
10394 gui_can_update_cursor();
10395 if (gui.in_use)
10396 out_flush(); /* always flush after a scroll */
10397#endif
10398
10399 return OK;
10400}
10401
10402/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010403 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 *
10405 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10406 * If clear_cmdline is FALSE there may be a message there that needs to be
10407 * cleared only if a mode is shown.
10408 * Return the length of the message (0 if no message).
10409 */
10410 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010411showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412{
10413 int need_clear;
10414 int length = 0;
10415 int do_mode;
10416 int attr;
10417 int nwr_save;
10418#ifdef FEAT_INS_EXPAND
10419 int sub_attr;
10420#endif
10421
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010422 do_mode = ((p_smd && msg_silent == 0)
10423 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010424 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010425 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010426 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 {
10428 /*
10429 * Don't show mode right now, when not redrawing or inside a mapping.
10430 * Call char_avail() only when we are going to show something, because
10431 * it takes a bit of time.
10432 */
10433 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10434 {
10435 redraw_cmdline = TRUE; /* show mode later */
10436 return 0;
10437 }
10438
10439 nwr_save = need_wait_return;
10440
10441 /* wait a bit before overwriting an important message */
10442 check_for_delay(FALSE);
10443
10444 /* if the cmdline is more than one line high, erase top lines */
10445 need_clear = clear_cmdline;
10446 if (clear_cmdline && cmdline_row < Rows - 1)
10447 msg_clr_cmdline(); /* will reset clear_cmdline */
10448
10449 /* Position on the last line in the window, column 0 */
10450 msg_pos_mode();
10451 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010452 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 if (do_mode)
10454 {
10455 MSG_PUTS_ATTR("--", attr);
10456#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010457 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010458# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010459 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010460# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010461 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010462# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010463 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010464# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 MSG_PUTS_ATTR(" IM", attr);
10466# else
10467 MSG_PUTS_ATTR(" XIM", attr);
10468# endif
10469#endif
10470#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10471 if (gui.in_use)
10472 {
10473 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010474 {
10475 /* HANGUL */
10476 if (enc_utf8)
10477 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10478 else
10479 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 }
10482#endif
10483#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010484 /* CTRL-X in Insert mode */
10485 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 {
10487 /* These messages can get long, avoid a wrap in a narrow
10488 * window. Prefer showing edit_submode_extra. */
10489 length = (Rows - msg_row) * Columns - 3;
10490 if (edit_submode_extra != NULL)
10491 length -= vim_strsize(edit_submode_extra);
10492 if (length > 0)
10493 {
10494 if (edit_submode_pre != NULL)
10495 length -= vim_strsize(edit_submode_pre);
10496 if (length - vim_strsize(edit_submode) > 0)
10497 {
10498 if (edit_submode_pre != NULL)
10499 msg_puts_attr(edit_submode_pre, attr);
10500 msg_puts_attr(edit_submode, attr);
10501 }
10502 if (edit_submode_extra != NULL)
10503 {
10504 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10505 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010506 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 else
10508 sub_attr = attr;
10509 msg_puts_attr(edit_submode_extra, sub_attr);
10510 }
10511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 }
10513 else
10514#endif
10515 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 if (State & VREPLACE_FLAG)
10517 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010518 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10520 else if (State & INSERT)
10521 {
10522#ifdef FEAT_RIGHTLEFT
10523 if (p_ri)
10524 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10525#endif
10526 MSG_PUTS_ATTR(_(" INSERT"), attr);
10527 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010528 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 MSG_PUTS_ATTR(_(" (insert)"), attr);
10530 else if (restart_edit == 'R')
10531 MSG_PUTS_ATTR(_(" (replace)"), attr);
10532 else if (restart_edit == 'V')
10533 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10534#ifdef FEAT_RIGHTLEFT
10535 if (p_hkmap)
10536 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10537# ifdef FEAT_FKMAP
10538 if (p_fkmap)
10539 MSG_PUTS_ATTR(farsi_text_5, attr);
10540# endif
10541#endif
10542#ifdef FEAT_KEYMAP
10543 if (State & LANGMAP)
10544 {
10545# ifdef FEAT_ARABIC
10546 if (curwin->w_p_arab)
10547 MSG_PUTS_ATTR(_(" Arabic"), attr);
10548 else
10549# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010550 if (get_keymap_str(curwin, (char_u *)" (%s)",
10551 NameBuff, MAXPATHL))
10552 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 }
10554#endif
10555 if ((State & INSERT) && p_paste)
10556 MSG_PUTS_ATTR(_(" (paste)"), attr);
10557
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 if (VIsual_active)
10559 {
10560 char *p;
10561
10562 /* Don't concatenate separate words to avoid translation
10563 * problems. */
10564 switch ((VIsual_select ? 4 : 0)
10565 + (VIsual_mode == Ctrl_V) * 2
10566 + (VIsual_mode == 'V'))
10567 {
10568 case 0: p = N_(" VISUAL"); break;
10569 case 1: p = N_(" VISUAL LINE"); break;
10570 case 2: p = N_(" VISUAL BLOCK"); break;
10571 case 4: p = N_(" SELECT"); break;
10572 case 5: p = N_(" SELECT LINE"); break;
10573 default: p = N_(" SELECT BLOCK"); break;
10574 }
10575 MSG_PUTS_ATTR(_(p), attr);
10576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 MSG_PUTS_ATTR(" --", attr);
10578 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010579
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 need_clear = TRUE;
10581 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010582 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583#ifdef FEAT_INS_EXPAND
10584 && edit_submode == NULL /* otherwise it gets too long */
10585#endif
10586 )
10587 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010588 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 need_clear = TRUE;
10590 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010591
10592 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 if (need_clear || clear_cmdline)
10594 msg_clr_eos();
10595 msg_didout = FALSE; /* overwrite this message */
10596 length = msg_col;
10597 msg_col = 0;
10598 need_wait_return = nwr_save; /* never ask for hit-return for this */
10599 }
10600 else if (clear_cmdline && msg_silent == 0)
10601 /* Clear the whole command line. Will reset "clear_cmdline". */
10602 msg_clr_cmdline();
10603
10604#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 /* In Visual mode the size of the selected area must be redrawn. */
10606 if (VIsual_active)
10607 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608
10609 /* If the last window has no status line, the ruler is after the mode
10610 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010611 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010612 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613#endif
10614 redraw_cmdline = FALSE;
10615 clear_cmdline = FALSE;
10616
10617 return length;
10618}
10619
10620/*
10621 * Position for a mode message.
10622 */
10623 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010624msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625{
10626 msg_col = 0;
10627 msg_row = Rows - 1;
10628}
10629
10630/*
10631 * Delete mode message. Used when ESC is typed which is expected to end
10632 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010633 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 */
10635 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010636unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637{
10638 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010639 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 */
10641 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10642 redraw_cmdline = TRUE; /* delete mode later */
10643 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010644 clearmode();
10645}
10646
10647/*
10648 * Clear the mode message.
10649 */
10650 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010651clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010652{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010653 int save_msg_row = msg_row;
10654 int save_msg_col = msg_col;
10655
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010656 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010657 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010658 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010659 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010660
10661 msg_col = save_msg_col;
10662 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663}
10664
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010665 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010666recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010667{
10668 MSG_PUTS_ATTR(_("recording"), attr);
10669 if (!shortmess(SHM_RECORDING))
10670 {
10671 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010672 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010673 MSG_PUTS_ATTR(s, attr);
10674 }
10675}
10676
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010677/*
10678 * Draw the tab pages line at the top of the Vim window.
10679 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010680 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010681draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010682{
10683 int tabcount = 0;
10684 tabpage_T *tp;
10685 int tabwidth;
10686 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010687 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010688 int attr;
10689 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010690 win_T *cwp;
10691 int wincount;
10692 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010693 int c;
10694 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010695 int attr_sel = HL_ATTR(HLF_TPS);
10696 int attr_nosel = HL_ATTR(HLF_TP);
10697 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010698 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010699 int room;
10700 int use_sep_chars = (t_colors < 8
10701#ifdef FEAT_GUI
10702 && !gui.in_use
10703#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010704#ifdef FEAT_TERMGUICOLORS
10705 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010706#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010707 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010708
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010709 if (ScreenLines == NULL)
10710 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010711 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010712
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010713#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010714 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010715 if (gui_use_tabline())
10716 {
10717 gui_update_tabline();
10718 return;
10719 }
10720#endif
10721
10722 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010723 return;
10724
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010725#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010726
10727 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10728 for (scol = 0; scol < Columns; ++scol)
10729 TabPageIdxs[scol] = 0;
10730
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010731 /* Use the 'tabline' option if it's set. */
10732 if (*p_tal != NUL)
10733 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010734 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010735
10736 /* Check for an error. If there is one we would loop in redrawing the
10737 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010738 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010739 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010740 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010741 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010742 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010743 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010744 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010745 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010746#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010747 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010748 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010749 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010750
Bram Moolenaar238a5642006-02-21 22:12:05 +000010751 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10752 if (tabwidth < 6)
10753 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010754
Bram Moolenaar238a5642006-02-21 22:12:05 +000010755 attr = attr_nosel;
10756 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010757 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010758 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10759 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010760 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010761 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010762
Bram Moolenaar238a5642006-02-21 22:12:05 +000010763 if (tp->tp_topframe == topframe)
10764 attr = attr_sel;
10765 if (use_sep_chars && col > 0)
10766 screen_putchar('|', 0, col++, attr);
10767
10768 if (tp->tp_topframe != topframe)
10769 attr = attr_nosel;
10770
10771 screen_putchar(' ', 0, col++, attr);
10772
10773 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010774 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010775 cwp = curwin;
10776 wp = firstwin;
10777 }
10778 else
10779 {
10780 cwp = tp->tp_curwin;
10781 wp = tp->tp_firstwin;
10782 }
10783
10784 modified = FALSE;
10785 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10786 if (bufIsChanged(wp->w_buffer))
10787 modified = TRUE;
10788 if (modified || wincount > 1)
10789 {
10790 if (wincount > 1)
10791 {
10792 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010793 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010794 if (col + len >= Columns - 3)
10795 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010796 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010797#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010798 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010799#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010800 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010801#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010802 );
10803 col += len;
10804 }
10805 if (modified)
10806 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10807 screen_putchar(' ', 0, col++, attr);
10808 }
10809
10810 room = scol - col + tabwidth - 1;
10811 if (room > 0)
10812 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010813 /* Get buffer name in NameBuff[] */
10814 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010815 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010816 len = vim_strsize(NameBuff);
10817 p = NameBuff;
10818#ifdef FEAT_MBYTE
10819 if (has_mbyte)
10820 while (len > room)
10821 {
10822 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010823 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010824 }
10825 else
10826#endif
10827 if (len > room)
10828 {
10829 p += len - room;
10830 len = room;
10831 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010832 if (len > Columns - col - 1)
10833 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010834
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010835 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010836 col += len;
10837 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010838 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010839
10840 /* Store the tab page number in TabPageIdxs[], so that
10841 * jump_to_mouse() knows where each one is. */
10842 ++tabcount;
10843 while (scol < col)
10844 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010845 }
10846
Bram Moolenaar238a5642006-02-21 22:12:05 +000010847 if (use_sep_chars)
10848 c = '_';
10849 else
10850 c = ' ';
10851 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010852
10853 /* Put an "X" for closing the current tab if there are several. */
10854 if (first_tabpage->tp_next != NULL)
10855 {
10856 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10857 TabPageIdxs[Columns - 1] = -999;
10858 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010859 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010860
10861 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10862 * set. */
10863 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010864}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010865
10866/*
10867 * Get buffer name for "buf" into NameBuff[].
10868 * Takes care of special buffer names and translates special characters.
10869 */
10870 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010871get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010872{
10873 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010874 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010875 else
10876 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10877 trans_characters(NameBuff, MAXPATHL);
10878}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010879
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880/*
10881 * Get the character to use in a status line. Get its attributes in "*attr".
10882 */
10883 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010884fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885{
10886 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010887
10888#ifdef FEAT_TERMINAL
10889 if (bt_terminal(wp->w_buffer))
10890 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010891 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010892 {
10893 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010894 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010895 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010896 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010897 {
10898 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010899 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010900 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010901 }
10902 else
10903#endif
10904 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010906 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907 fill = fill_stl;
10908 }
10909 else
10910 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010911 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 fill = fill_stlnc;
10913 }
10914 /* Use fill when there is highlighting, and highlighting of current
10915 * window differs, or the fillchars differ, or this is not the
10916 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010917 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010918 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 || (fill_stl != fill_stlnc)))
10920 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010921 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 return '^';
10923 return '=';
10924}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926/*
10927 * Get the character to use in a separator between vertically split windows.
10928 * Get its attributes in "*attr".
10929 */
10930 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010931fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010933 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 if (*attr == 0 && fill_vert == ' ')
10935 return '|';
10936 else
10937 return fill_vert;
10938}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939
10940/*
10941 * Return TRUE if redrawing should currently be done.
10942 */
10943 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010944redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010946#ifdef FEAT_EVAL
10947 if (disable_redraw_for_testing)
10948 return 0;
10949 else
10950#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010951 return ((!RedrawingDisabled
10952#ifdef FEAT_EVAL
10953 || ignore_redraw_flag_for_testing
10954#endif
10955 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956}
10957
10958/*
10959 * Return TRUE if printing messages should currently be done.
10960 */
10961 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010962messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963{
10964 return (!(p_lz && char_avail() && !KeyTyped));
10965}
10966
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010967#ifdef FEAT_MENU
10968/*
10969 * Draw the window toolbar.
10970 */
10971 static void
10972redraw_win_toolbar(win_T *wp)
10973{
10974 vimmenu_T *menu;
10975 int item_idx = 0;
10976 int item_count = 0;
10977 int col = 0;
10978 int next_col;
10979 int off = (int)(current_ScreenLine - ScreenLines);
10980 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10981 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10982
10983 vim_free(wp->w_winbar_items);
10984 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10985 ++item_count;
10986 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10987 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10988
10989 /* TODO: use fewer spaces if there is not enough room */
10990 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010991 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010992 {
10993 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010994 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010995 break;
10996 if (col > 1)
10997 {
10998 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010999 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011000 break;
11001 }
11002
11003 wp->w_winbar_items[item_idx].wb_startcol = col;
11004 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011005 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011006 break;
11007
11008 next_col = text_to_screenline(wp, menu->name, col);
11009 while (col < next_col)
11010 {
11011 ScreenAttrs[off + col] = button_attr;
11012 ++col;
11013 }
11014 wp->w_winbar_items[item_idx].wb_endcol = col;
11015 wp->w_winbar_items[item_idx].wb_menu = menu;
11016 ++item_idx;
11017
Bram Moolenaar02631462017-09-22 15:20:32 +020011018 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011019 break;
11020 space_to_screenline(off + col, button_attr);
11021 ++col;
11022 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011023 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011024 {
11025 space_to_screenline(off + col, fill_attr);
11026 ++col;
11027 }
11028 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11029
Bram Moolenaar02631462017-09-22 15:20:32 +020011030 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
11031 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011032}
11033#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011034
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035/*
11036 * Show current status info in ruler and various other places
11037 * If always is FALSE, only show ruler if position has changed.
11038 */
11039 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011040showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041{
11042 if (!always && !redrawing())
11043 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011044#ifdef FEAT_INS_EXPAND
11045 if (pum_visible())
11046 {
11047 /* Don't redraw right now, do it later. */
11048 curwin->w_redr_status = TRUE;
11049 return;
11050 }
11051#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011052#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011053 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011054 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000011055 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 else
11058#endif
11059#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011060 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061#endif
11062
11063#ifdef FEAT_TITLE
11064 if (need_maketitle
11065# ifdef FEAT_STL_OPT
11066 || (p_icon && (stl_syntax & STL_IN_ICON))
11067 || (p_title && (stl_syntax & STL_IN_TITLE))
11068# endif
11069 )
11070 maketitle();
11071#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011072 /* Redraw the tab pages line if needed. */
11073 if (redraw_tabline)
11074 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075}
11076
11077#ifdef FEAT_CMDL_INFO
11078 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011079win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011081#define RULER_BUF_LEN 70
11082 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 int row;
11084 int fillchar;
11085 int attr;
11086 int empty_line = FALSE;
11087 colnr_T virtcol;
11088 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011089 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 int this_ru_col;
11092 int off = 0;
11093 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094
11095 /* If 'ruler' off or redrawing disabled, don't do anything */
11096 if (!p_ru)
11097 return;
11098
11099 /*
11100 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11101 * after deleting lines, before cursor.lnum is corrected.
11102 */
11103 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11104 return;
11105
11106#ifdef FEAT_INS_EXPAND
11107 /* Don't draw the ruler while doing insert-completion, it might overwrite
11108 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 if (edit_submode != NULL)
11111 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011112 // Don't draw the ruler when the popup menu is visible, it may overlap.
11113 // Except when the popup menu will be redrawn anyway.
11114 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011115 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116#endif
11117
11118#ifdef FEAT_STL_OPT
11119 if (*p_ruf)
11120 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011121 int save_called_emsg = called_emsg;
11122
11123 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011125 if (called_emsg)
11126 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011127 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011128 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 return;
11130 }
11131#endif
11132
11133 /*
11134 * Check if not in Insert mode and the line is empty (will show "0-1").
11135 */
11136 if (!(State & INSERT)
11137 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11138 empty_line = TRUE;
11139
11140 /*
11141 * Only draw the ruler when something changed.
11142 */
11143 validate_virtcol_win(wp);
11144 if ( redraw_cmdline
11145 || always
11146 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11147 || wp->w_cursor.col != wp->w_ru_cursor.col
11148 || wp->w_virtcol != wp->w_ru_virtcol
11149#ifdef FEAT_VIRTUALEDIT
11150 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11151#endif
11152 || wp->w_topline != wp->w_ru_topline
11153 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11154#ifdef FEAT_DIFF
11155 || wp->w_topfill != wp->w_ru_topfill
11156#endif
11157 || empty_line != wp->w_ru_empty)
11158 {
11159 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 if (wp->w_status_height)
11161 {
11162 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011163 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011164 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011165 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 }
11167 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 {
11169 row = Rows - 1;
11170 fillchar = ' ';
11171 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 width = Columns;
11173 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 }
11175
11176 /* In list mode virtcol needs to be recomputed */
11177 virtcol = wp->w_virtcol;
11178 if (wp->w_p_list && lcs_tab1 == NUL)
11179 {
11180 wp->w_p_list = FALSE;
11181 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11182 wp->w_p_list = TRUE;
11183 }
11184
11185 /*
11186 * Some sprintfs return the length, some return a pointer.
11187 * To avoid portability problems we use strlen() here.
11188 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011189 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11191 ? 0L
11192 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011193 len = STRLEN(buffer);
11194 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11196 (int)virtcol + 1);
11197
11198 /*
11199 * Add a "50%" if there is room for it.
11200 * On the last line, don't print in the last column (scrolls the
11201 * screen up on some terminals).
11202 */
11203 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011204 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 this_ru_col = ru_col - (Columns - width);
11209 if (this_ru_col < 0)
11210 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 /* Never use more than half the window/screen width, leave the other
11212 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011213 if (this_ru_col < (width + 1) / 2)
11214 this_ru_col = (width + 1) / 2;
11215 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011217 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011218 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 {
11220#ifdef FEAT_MBYTE
11221 if (has_mbyte)
11222 i += (*mb_char2bytes)(fillchar, buffer + i);
11223 else
11224#endif
11225 buffer[i++] = fillchar;
11226 ++o;
11227 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011228 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229 }
11230 /* Truncate at window boundary. */
11231#ifdef FEAT_MBYTE
11232 if (has_mbyte)
11233 {
11234 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011235 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 {
11237 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011238 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239 {
11240 buffer[i] = NUL;
11241 break;
11242 }
11243 }
11244 }
11245 else
11246#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011247 if (this_ru_col + (int)STRLEN(buffer) > width)
11248 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249
Bram Moolenaar4033c552017-09-16 20:54:51 +020011250 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 i = redraw_cmdline;
11252 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011253 this_ru_col + off + (int)STRLEN(buffer),
11254 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 fillchar, fillchar, attr);
11256 /* don't redraw the cmdline because of showing the ruler */
11257 redraw_cmdline = i;
11258 wp->w_ru_cursor = wp->w_cursor;
11259 wp->w_ru_virtcol = wp->w_virtcol;
11260 wp->w_ru_empty = empty_line;
11261 wp->w_ru_topline = wp->w_topline;
11262 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11263#ifdef FEAT_DIFF
11264 wp->w_ru_topfill = wp->w_topfill;
11265#endif
11266 }
11267}
11268#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011269
11270#if defined(FEAT_LINEBREAK) || defined(PROTO)
11271/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011272 * Return the width of the 'number' and 'relativenumber' column.
11273 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011274 * Otherwise it depends on 'numberwidth' and the line count.
11275 */
11276 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011277number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011278{
11279 int n;
11280 linenr_T lnum;
11281
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011282 if (wp->w_p_rnu && !wp->w_p_nu)
11283 /* cursor line shows "0" */
11284 lnum = wp->w_height;
11285 else
11286 /* cursor line shows absolute line number */
11287 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011288
Bram Moolenaar6b314672015-03-20 15:42:10 +010011289 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011290 return wp->w_nrwidth_width;
11291 wp->w_nrwidth_line_count = lnum;
11292
11293 n = 0;
11294 do
11295 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011296 lnum /= 10;
11297 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011298 } while (lnum > 0);
11299
11300 /* 'numberwidth' gives the minimal width plus one */
11301 if (n < wp->w_p_nuw - 1)
11302 n = wp->w_p_nuw - 1;
11303
11304 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011305 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011306 return n;
11307}
11308#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011309
11310/*
11311 * Return the current cursor column. This is the actual position on the
11312 * screen. First column is 0.
11313 */
11314 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011315screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011316{
11317 return screen_cur_col;
11318}
11319
11320/*
11321 * Return the current cursor row. This is the actual position on the screen.
11322 * First row is 0.
11323 */
11324 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011325screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011326{
11327 return screen_cur_row;
11328}