blob: 2586ad03c06ba689f4c39db96bd953a503ea3e4d [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
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200110#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
111static int text_to_screenline(win_T *wp, char_u *text, int col);
112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_FOLDING
114static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100115static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
117
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200118/* Flag that is set when drawing for a callback, not from the main command
119 * loop. */
120static int redrawing_for_callback = 0;
121
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122/*
123 * Buffer for one screen line (characters and attributes).
124 */
125static schar_T *current_ScreenLine;
126
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200128static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100129static 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 +0000130#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100131static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
132static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
133static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200135static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000138#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200142# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static void start_search_hl(void);
144static void end_search_hl(void);
145static void init_search_hl(win_T *wp);
146static void prepare_search_hl(win_T *wp, linenr_T lnum);
147static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
148static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void screen_start_highlight(int attr);
151static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200156static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static void lineinvalid(unsigned off, int width);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100158static void linecopy(int to, int from, win_T *wp);
159static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200160static 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 +0100161static void win_rest_invalid(win_T *wp);
162static void msg_pos_mode(void);
163static void recording_mode(int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static void draw_tabline(void);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200165static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100166static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200167#ifdef FEAT_MENU
168static void redraw_win_toolbar(win_T *wp);
169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200174static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177/* Ugly global: overrule attribute used by screen_char() */
178static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200180#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
181/* Can limit syntax highlight time to 'redrawtime'. */
182# define SYN_TIME_LIMIT 1
183#endif
184
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200185#ifdef FEAT_RIGHTLEFT
186# define HAS_RIGHTLEFT(x) x
187#else
188# define HAS_RIGHTLEFT(x) FALSE
189#endif
190
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191/*
192 * Redraw the current window later, with update_screen(type).
193 * Set must_redraw only if not already set to a higher value.
194 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
195 */
196 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100197redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
199 redraw_win_later(curwin, type);
200}
201
202 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100203redraw_win_later(
204 win_T *wp,
205 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200207 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 {
209 wp->w_redr_type = type;
210 if (type >= NOT_VALID)
211 wp->w_lines_valid = 0;
212 if (must_redraw < type) /* must_redraw is the maximum of all windows */
213 must_redraw = type;
214 }
215}
216
217/*
218 * Force a complete redraw later. Also resets the highlighting. To be used
219 * after executing a shell command that messes up the screen.
220 */
221 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100222redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223{
224 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000225#ifdef FEAT_GUI
226 if (gui.in_use)
227 /* Use a code that will reset gui.highlight_mask in
228 * gui_stop_highlight(). */
229 screen_attr = HL_ALL + 1;
230 else
231#endif
232 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200233 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234}
235
236/*
237 * Mark all windows to be redrawn later.
238 */
239 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100240redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241{
242 win_T *wp;
243
244 FOR_ALL_WINDOWS(wp)
245 {
246 redraw_win_later(wp, type);
247 }
248}
249
250/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000251 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 */
253 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100254redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255{
256 redraw_buf_later(curbuf, type);
257}
258
259 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100260redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261{
262 win_T *wp;
263
264 FOR_ALL_WINDOWS(wp)
265 {
266 if (wp->w_buffer == buf)
267 redraw_win_later(wp, type);
268 }
269}
270
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200271 void
272redraw_buf_and_status_later(buf_T *buf, int type)
273{
274 win_T *wp;
275
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200276#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200277 if (wild_menu_showing != 0)
278 /* Don't redraw while the command line completion is displayed, it
279 * would disappear. */
280 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200281#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200282 FOR_ALL_WINDOWS(wp)
283 {
284 if (wp->w_buffer == buf)
285 {
286 redraw_win_later(wp, type);
287 wp->w_redr_status = TRUE;
288 }
289 }
290}
291
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200293 * Redraw as soon as possible. When the command line is not scrolled redraw
294 * right away and restore what was on the command line.
295 * Return a code indicating what happened.
296 */
297 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100298redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299{
300 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200301 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200302 int r;
303 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200304 schar_T *screenline; /* copy from ScreenLines[] */
305 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306#ifdef FEAT_MBYTE
307 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200308 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200309 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200310 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200311#endif
312
313 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200314 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200315 return ret;
316
317 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200318 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200319 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200320 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200321 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200322 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200323 if (screenline == NULL || screenattr == NULL)
324 ret = 2;
325#ifdef FEAT_MBYTE
326 if (enc_utf8)
327 {
328 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200329 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200330 if (screenlineUC == NULL)
331 ret = 2;
332 for (i = 0; i < p_mco; ++i)
333 {
334 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200335 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200336 if (screenlineC[i] == NULL)
337 ret = 2;
338 }
339 }
340 if (enc_dbcs == DBCS_JPNU)
341 {
342 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200343 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200344 if (screenline2 == NULL)
345 ret = 2;
346 }
347#endif
348
349 if (ret != 2)
350 {
351 /* Save the text displayed in the command line area. */
352 for (r = 0; r < rows; ++r)
353 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200354 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200355 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 (size_t)cols * sizeof(schar_T));
357 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200358 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200359 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200360#ifdef FEAT_MBYTE
361 if (enc_utf8)
362 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200364 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 mch_memmove(screenlineC[i] + r * cols,
368 ScreenLinesC[i] + LineOffset[cmdline_row + r],
369 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370 }
371 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200372 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200373 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200375#endif
376 }
377
378 update_screen(0);
379 ret = 3;
380
381 if (must_redraw == 0)
382 {
383 int off = (int)(current_ScreenLine - ScreenLines);
384
385 /* Restore the text displayed in the command line area. */
386 for (r = 0; r < rows; ++r)
387 {
388 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200389 screenline + r * cols,
390 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200391 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200392 screenattr + r * cols,
393 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200394#ifdef FEAT_MBYTE
395 if (enc_utf8)
396 {
397 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200398 screenlineUC + r * cols,
399 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200400 for (i = 0; i < p_mco; ++i)
401 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200402 screenlineC[i] + r * cols,
403 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200404 }
405 if (enc_dbcs == DBCS_JPNU)
406 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200407 screenline2 + r * cols,
408 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200409#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200410 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200411 }
412 ret = 4;
413 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200414 }
415
416 vim_free(screenline);
417 vim_free(screenattr);
418#ifdef FEAT_MBYTE
419 if (enc_utf8)
420 {
421 vim_free(screenlineUC);
422 for (i = 0; i < p_mco; ++i)
423 vim_free(screenlineC[i]);
424 }
425 if (enc_dbcs == DBCS_JPNU)
426 vim_free(screenline2);
427#endif
428
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200429 /* Show the intro message when appropriate. */
430 maybe_intro_message();
431
432 setcursor();
433
Bram Moolenaar2951b772013-07-03 12:45:31 +0200434 return ret;
435}
436
437/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100438 * Invoked after an asynchronous callback is called.
439 * If an echo command was used the cursor needs to be put back where
440 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200441 * If "call_update_screen" is FALSE don't call update_screen() when at the
442 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100443 */
444 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200445redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100446{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200447 ++redrawing_for_callback;
448
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100449 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200450 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100451 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200452 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200453 // Don't redraw when in prompt_for_number().
454 if (cmdline_row > 0)
455 {
456 // Redrawing only works when the screen didn't scroll. Don't clear
457 // wildmenu entries.
458 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200459#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200460 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200461#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200462 && call_update_screen)
463 update_screen(0);
464
465 // Redraw in the same position, so that the user can continue
466 // editing the command.
467 redrawcmdline_ex(FALSE);
468 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200469 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200470 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100471 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200472 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200473 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100474 setcursor();
475 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100476 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100477#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100478 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200479 // Don't update the cursor when it is blinking and off to avoid
480 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100481 out_flush_cursor(FALSE, FALSE);
482 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100483#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100484 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200485
486 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100487}
488
489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 * Changed something in the current window, at buffer line "lnum", that
491 * requires that line and possibly other lines to be redrawn.
492 * Used when entering/leaving Insert mode with the cursor on a folded line.
493 * Used to remove the "$" from a change command.
494 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
495 * may become invalid and the whole window will have to be redrawn.
496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100498redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200499 win_T *wp,
Bram Moolenaar05540972016-01-30 20:31:25 +0100500 linenr_T lnum,
501 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502{
503#ifdef FEAT_FOLDING
504 int i;
505#endif
506
Bram Moolenaar90a99792018-09-12 21:52:18 +0200507 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
508 wp->w_redraw_top = lnum;
509 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
510 wp->w_redraw_bot = lnum;
511 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512
513#ifdef FEAT_FOLDING
514 if (invalid)
515 {
516 /* A w_lines[] entry for this lnum has become invalid. */
Bram Moolenaar90a99792018-09-12 21:52:18 +0200517 i = find_wl_entry(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 if (i >= 0)
Bram Moolenaar90a99792018-09-12 21:52:18 +0200519 wp->w_lines[i].wl_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 }
521#endif
522}
523
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200524 void
525reset_updating_screen(int may_resize_shell UNUSED)
526{
527 updating_screen = FALSE;
528#ifdef FEAT_GUI
529 if (may_resize_shell)
530 gui_may_resize_shell();
531#endif
532#ifdef FEAT_TERMINAL
533 term_check_channel_closed_recently();
534#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200535
536#ifdef HAVE_DROP_FILE
537 // If handle_drop() was called while updating_screen was TRUE need to
538 // handle the drop now.
539 handle_any_postponed_drop();
540#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200541}
542
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200544 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 */
546 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100547update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
549 redraw_curbuf_later(type);
550 update_screen(type);
551}
552
553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 * Based on the current value of curwin->w_topline, transfer a screenfull
555 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200556 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200558 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200559update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200561 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 win_T *wp;
563 static int did_intro = FALSE;
564#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
565 int did_one;
566#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200567#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200568 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200569 int gui_cursor_col;
570 int gui_cursor_row;
571#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200572 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000574 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200576 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200578 if (type == VALID_NO_UPDATE)
579 {
580 no_update = TRUE;
581 type = 0;
582 }
583
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 if (must_redraw)
585 {
586 if (type < must_redraw) /* use maximal type */
587 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000588
589 /* must_redraw is reset here, so that when we run into some weird
590 * reason to redraw while busy redrawing (e.g., asynchronous
591 * scrolling), or update_topline() in win_update() will cause a
592 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 must_redraw = 0;
594 }
595
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200596 /* May need to update w_lines[]. */
597 if (curwin->w_lines_valid == 0 && type < NOT_VALID
598#ifdef FEAT_TERMINAL
599 && !term_do_update_window(curwin)
600#endif
601 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 type = NOT_VALID;
603
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000604 /* Postpone the redrawing when it's not needed and when being called
605 * recursively. */
606 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 {
608 redraw_later(type); /* remember type for next time */
609 must_redraw = type;
610 if (type > INVERTED_ALL)
611 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200612 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 }
614
615 updating_screen = TRUE;
616#ifdef FEAT_SYN_HL
617 ++display_tick; /* let syntax code know we're in a next round of
618 * display updating */
619#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200620 if (no_update)
621 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
623 /*
624 * if the screen was scrolled up when displaying a message, scroll it down
625 */
626 if (msg_scrolled)
627 {
628 clear_cmdline = TRUE;
629 if (msg_scrolled > Rows - 5) /* clearing is faster */
630 type = CLEAR;
631 else if (type != CLEAR)
632 {
633 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200634 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
635 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 type = CLEAR;
637 FOR_ALL_WINDOWS(wp)
638 {
639 if (W_WINROW(wp) < msg_scrolled)
640 {
641 if (W_WINROW(wp) + wp->w_height > msg_scrolled
642 && wp->w_redr_type < REDRAW_TOP
643 && wp->w_lines_valid > 0
644 && wp->w_topline == wp->w_lines[0].wl_lnum)
645 {
646 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
647 wp->w_redr_type = REDRAW_TOP;
648 }
649 else
650 {
651 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200652 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
653 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 }
656 }
657 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200658 if (!no_update)
659 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000660 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 }
662 msg_scrolled = 0;
663 need_wait_return = FALSE;
664 }
665
666 /* reset cmdline_row now (may have been changed temporarily) */
667 compute_cmdrow();
668
669 /* Check for changed highlighting */
670 if (need_highlight_changed)
671 highlight_changed();
672
673 if (type == CLEAR) /* first clear screen */
674 {
675 screenclear(); /* will reset clear_cmdline */
676 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200677 /* must_redraw may be set indirectly, avoid another redraw later */
678 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
680
681 if (clear_cmdline) /* going to clear cmdline (done below) */
682 check_for_delay(FALSE);
683
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000684#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200685 /* Force redraw when width of 'number' or 'relativenumber' column
686 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000687 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200688 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
689 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000690 curwin->w_redr_type = NOT_VALID;
691#endif
692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 /*
694 * Only start redrawing if there is really something to do.
695 */
696 if (type == INVERTED)
697 update_curswant();
698 if (curwin->w_redr_type < type
699 && !((type == VALID
700 && curwin->w_lines[0].wl_valid
701#ifdef FEAT_DIFF
702 && curwin->w_topfill == curwin->w_old_topfill
703 && curwin->w_botfill == curwin->w_old_botfill
704#endif
705 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000707 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
709 && curwin->w_old_visual_mode == VIsual_mode
710 && (curwin->w_valid & VALID_VIRTCOL)
711 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 ))
713 curwin->w_redr_type = type;
714
Bram Moolenaar5a305422006-04-28 22:38:25 +0000715 /* Redraw the tab pages line if needed. */
716 if (redraw_tabline || type >= NOT_VALID)
717 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000718
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719#ifdef FEAT_SYN_HL
720 /*
721 * Correct stored syntax highlighting info for changes in each displayed
722 * buffer. Each buffer must only be done once.
723 */
724 FOR_ALL_WINDOWS(wp)
725 {
726 if (wp->w_buffer->b_mod_set)
727 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 win_T *wwp;
729
730 /* Check if we already did this buffer. */
731 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
732 if (wwp->w_buffer == wp->w_buffer)
733 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200734 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 syn_stack_apply_changes(wp->w_buffer);
736 }
737 }
738#endif
739
740 /*
741 * Go from top to bottom through the windows, redrawing the ones that need
742 * it.
743 */
744#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
745 did_one = FALSE;
746#endif
747#ifdef FEAT_SEARCH_EXTRA
748 search_hl.rm.regprog = NULL;
749#endif
750 FOR_ALL_WINDOWS(wp)
751 {
752 if (wp->w_redr_type != 0)
753 {
754 cursor_off();
755#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
756 if (!did_one)
757 {
758 did_one = TRUE;
759# ifdef FEAT_SEARCH_EXTRA
760 start_search_hl();
761# endif
762# ifdef FEAT_CLIPBOARD
763 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200764 if (clip_star.available && clip_isautosel_star())
765 clip_update_selection(&clip_star);
766 if (clip_plus.available && clip_isautosel_plus())
767 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768# endif
769#ifdef FEAT_GUI
770 /* Remove the cursor before starting to do anything, because
771 * scrolling may make it difficult to redraw the text under
772 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200773 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200774 {
775 gui_cursor_col = gui.cursor_col;
776 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200778 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#endif
781 }
782#endif
783 win_update(wp);
784 }
785
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 /* redraw status line after the window to minimize cursor movement */
787 if (wp->w_redr_status)
788 {
789 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200790 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793#if defined(FEAT_SEARCH_EXTRA)
794 end_search_hl();
795#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100796#ifdef FEAT_INS_EXPAND
797 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200798 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 /* Reset b_mod_set flags. Going through all windows is probably faster
802 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200803 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200806 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
808 /* Clear or redraw the command line. Done last, because scrolling may
809 * mess up the command line. */
810 if (clear_cmdline || redraw_cmdline)
811 showmode();
812
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200813 if (no_update)
814 --no_win_do_lines_ins;
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200817 if (!did_intro)
818 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 did_intro = TRUE;
820
821#ifdef FEAT_GUI
822 /* Redraw the cursor and update the scrollbars when all screen updating is
823 * done. */
824 if (gui.in_use)
825 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200826 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200827 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100828 mch_disable_flush();
829 out_flush(); /* required before updating the cursor */
830 mch_enable_flush();
831
Bram Moolenaar144445d2016-07-08 21:41:54 +0200832 /* Put the GUI position where the cursor was, gui_update_cursor()
833 * uses that. */
834 gui.col = gui_cursor_col;
835 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200836# ifdef FEAT_MBYTE
837 gui.col = mb_fix_col(gui.col, gui.row);
838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100840 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200841 screen_cur_col = gui.col;
842 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200843 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100844 else
845 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 gui_update_scrollbars(FALSE);
847 }
848#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200849 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850}
851
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100852#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
853/*
854 * Prepare for updating one or more windows.
855 * Caller must check for "updating_screen" already set to avoid recursiveness.
856 */
857 static void
858update_prepare(void)
859{
860 cursor_off();
861 updating_screen = TRUE;
862#ifdef FEAT_GUI
863 /* Remove the cursor before starting to do anything, because scrolling may
864 * make it difficult to redraw the text under it. */
865 if (gui.in_use)
866 gui_undraw_cursor();
867#endif
868#ifdef FEAT_SEARCH_EXTRA
869 start_search_hl();
870#endif
871}
872
873/*
874 * Finish updating one or more windows.
875 */
876 static void
877update_finish(void)
878{
879 if (redraw_cmdline)
880 showmode();
881
882# ifdef FEAT_SEARCH_EXTRA
883 end_search_hl();
884# endif
885
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200886 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100887
888# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100889 /* Redraw the cursor and update the scrollbars when all screen updating is
890 * done. */
891 if (gui.in_use)
892 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100893 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100894 gui_update_scrollbars(FALSE);
895 }
896# endif
897}
898#endif
899
Bram Moolenaar860cae12010-06-05 23:22:07 +0200900#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200901/*
902 * Return TRUE if the cursor line in window "wp" may be concealed, according
903 * to the 'concealcursor' option.
904 */
905 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100906conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200907{
908 int c;
909
910 if (*wp->w_p_cocu == NUL)
911 return FALSE;
912 if (get_real_state() & VISUAL)
913 c = 'v';
914 else if (State & INSERT)
915 c = 'i';
916 else if (State & NORMAL)
917 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200918 else if (State & CMDLINE)
919 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200920 else
921 return FALSE;
922 return vim_strchr(wp->w_p_cocu, c) != NULL;
923}
924
925/*
926 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
927 */
928 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200929conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200930{
931 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
932 {
933 need_cursor_line_redraw = TRUE;
934 /* Need to recompute cursor column, e.g., when starting Visual mode
935 * without concealing. */
936 curs_columns(TRUE);
937 }
938}
939
Bram Moolenaar860cae12010-06-05 23:22:07 +0200940 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100941update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200942{
943 int row;
944 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200945#ifdef SYN_TIME_LIMIT
946 proftime_T syntax_tm;
947#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200948
Bram Moolenaar908be432016-05-24 10:51:30 +0200949 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100950 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200951 return;
952
Bram Moolenaar860cae12010-06-05 23:22:07 +0200953 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200954 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200955 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200956#ifdef SYN_TIME_LIMIT
957 /* Set the time limit to 'redrawtime'. */
958 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200959 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200960#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100961 update_prepare();
962
Bram Moolenaar860cae12010-06-05 23:22:07 +0200963 row = 0;
964 for (j = 0; j < wp->w_lines_valid; ++j)
965 {
966 if (lnum == wp->w_lines[j].wl_lnum)
967 {
968 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200969# ifdef FEAT_SEARCH_EXTRA
970 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200971 start_search_hl();
972 prepare_search_hl(wp, lnum);
973# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200974 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size,
975 FALSE, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200976# if defined(FEAT_SEARCH_EXTRA)
977 end_search_hl();
978# endif
979 break;
980 }
981 row += wp->w_lines[j].wl_size;
982 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100983
984 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200985
986#ifdef SYN_TIME_LIMIT
987 syn_set_timeout(NULL);
988#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200989 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200990 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991}
992#endif
993
994#if defined(FEAT_SIGNS) || defined(PROTO)
995 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100996update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997{
998 win_T *wp;
999 int doit = FALSE;
1000
1001# ifdef FEAT_FOLDING
1002 win_foldinfo.fi_level = 0;
1003# endif
1004
1005 /* update/delete a specific mark */
1006 FOR_ALL_WINDOWS(wp)
1007 {
1008 if (buf != NULL && lnum > 0)
1009 {
1010 if (wp->w_buffer == buf && lnum >= wp->w_topline
1011 && lnum < wp->w_botline)
1012 {
1013 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1014 wp->w_redraw_top = lnum;
1015 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1016 wp->w_redraw_bot = lnum;
1017 redraw_win_later(wp, VALID);
1018 }
1019 }
1020 else
1021 redraw_win_later(wp, VALID);
1022 if (wp->w_redr_type != 0)
1023 doit = TRUE;
1024 }
1025
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001026 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001027 * happening (recursive call), messages on the screen or still starting up.
1028 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001029 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001030 || State == ASKMORE || State == HITRETURN
1031 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001032#ifdef FEAT_GUI
1033 || gui.starting
1034#endif
1035 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 return;
1037
1038 /* update all windows that need updating */
1039 update_prepare();
1040
Bram Moolenaar29323592016-07-24 22:04:11 +02001041 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 {
1043 if (wp->w_redr_type != 0)
1044 win_update(wp);
1045 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001046 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
1049 update_finish();
1050}
1051#endif
1052
1053
1054#if defined(FEAT_GUI) || defined(PROTO)
1055/*
1056 * Update a single window, its status line and maybe the command line msg.
1057 * Used for the GUI scrollbar.
1058 */
1059 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001060updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001062 /* return if already busy updating */
1063 if (updating_screen)
1064 return;
1065
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 update_prepare();
1067
1068#ifdef FEAT_CLIPBOARD
1069 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001070 if (clip_star.available && clip_isautosel_star())
1071 clip_update_selection(&clip_star);
1072 if (clip_plus.available && clip_isautosel_plus())
1073 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001075
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001077
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001078 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001079 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001080 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001081
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 if (wp->w_redr_status
1083# ifdef FEAT_CMDL_INFO
1084 || p_ru
1085# endif
1086# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001087 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088# endif
1089 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001090 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091
1092 update_finish();
1093}
1094#endif
1095
1096/*
1097 * Update a single window.
1098 *
1099 * This may cause the windows below it also to be redrawn (when clearing the
1100 * screen or scrolling lines).
1101 *
1102 * How the window is redrawn depends on wp->w_redr_type. Each type also
1103 * implies the one below it.
1104 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001105 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1107 * INVERTED redraw the changed part of the Visual area
1108 * INVERTED_ALL redraw the whole Visual area
1109 * VALID 1. scroll up/down to adjust for a changed w_topline
1110 * 2. update lines at the top when scrolled down
1111 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001112 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 * b_mod_top and b_mod_bot.
1114 * - if wp->w_redraw_top non-zero, redraw lines between
1115 * wp->w_redraw_top and wp->w_redr_bot.
1116 * - continue redrawing when syntax status is invalid.
1117 * 4. if scrolled up, update lines at the bottom.
1118 * This results in three areas that may need updating:
1119 * top: from first row to top_end (when scrolled down)
1120 * mid: from mid_start to mid_end (update inversion or changed text)
1121 * bot: from bot_start to last row (when scrolled up)
1122 */
1123 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001124win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125{
1126 buf_T *buf = wp->w_buffer;
1127 int type;
1128 int top_end = 0; /* Below last row of the top area that needs
1129 updating. 0 when no top area updating. */
1130 int mid_start = 999;/* first row of the mid area that needs
1131 updating. 999 when no mid area updating. */
1132 int mid_end = 0; /* Below last row of the mid area that needs
1133 updating. 0 when no mid area updating. */
1134 int bot_start = 999;/* first row of the bot area that needs
1135 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 int scrolled_down = FALSE; /* TRUE when scrolled down when
1137 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001139 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 int top_to_mod = FALSE; /* redraw above mod_top */
1141#endif
1142
1143 int row; /* current window row to display */
1144 linenr_T lnum; /* current buffer lnum to display */
1145 int idx; /* current index in w_lines[] */
1146 int srow; /* starting row of the current line */
1147
1148 int eof = FALSE; /* if TRUE, we hit the end of the file */
1149 int didline = FALSE; /* if TRUE, we finished the last line */
1150 int i;
1151 long j;
1152 static int recursive = FALSE; /* being called recursively */
1153 int old_botline = wp->w_botline;
1154#ifdef FEAT_FOLDING
1155 long fold_count;
1156#endif
1157#ifdef FEAT_SYN_HL
1158 /* remember what happened to the previous line, to know if
1159 * check_visual_highlight() can be used */
1160#define DID_NONE 1 /* didn't update a line */
1161#define DID_LINE 2 /* updated a normal line */
1162#define DID_FOLD 3 /* updated a folded line */
1163 int did_update = DID_NONE;
1164 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1165#endif
1166 linenr_T mod_top = 0;
1167 linenr_T mod_bot = 0;
1168#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1169 int save_got_int;
1170#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001171#ifdef SYN_TIME_LIMIT
1172 proftime_T syntax_tm;
1173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174
1175 type = wp->w_redr_type;
1176
1177 if (type == NOT_VALID)
1178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 wp->w_lines_valid = 0;
1181 }
1182
1183 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001184 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 {
1186 wp->w_redr_type = 0;
1187 return;
1188 }
1189
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 /* Window is zero-width: Only need to draw the separator. */
1191 if (wp->w_width == 0)
1192 {
1193 /* draw the vertical separator right of this window */
1194 draw_vsep_win(wp, 0);
1195 wp->w_redr_type = 0;
1196 return;
1197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001199#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001200 // If this window contains a terminal, redraw works completely differently.
1201 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001202 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001203 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001204# ifdef FEAT_MENU
1205 /* Draw the window toolbar, if there is one. */
1206 if (winbar_height(wp) > 0)
1207 redraw_win_toolbar(wp);
1208# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001209 wp->w_redr_type = 0;
1210 return;
1211 }
1212#endif
1213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001215 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216#endif
1217
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001218#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001219 /* Force redraw when width of 'number' or 'relativenumber' column
1220 * changes. */
1221 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001222 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001223 {
1224 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001225 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001226 }
1227 else
1228#endif
1229
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1231 {
1232 /*
1233 * When there are both inserted/deleted lines and specific lines to be
1234 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1235 * everything (only happens when redrawing is off for while).
1236 */
1237 type = NOT_VALID;
1238 }
1239 else
1240 {
1241 /*
1242 * Set mod_top to the first line that needs displaying because of
1243 * changes. Set mod_bot to the first line after the changes.
1244 */
1245 mod_top = wp->w_redraw_top;
1246 if (wp->w_redraw_bot != 0)
1247 mod_bot = wp->w_redraw_bot + 1;
1248 else
1249 mod_bot = 0;
1250 wp->w_redraw_top = 0; /* reset for next time */
1251 wp->w_redraw_bot = 0;
1252 if (buf->b_mod_set)
1253 {
1254 if (mod_top == 0 || mod_top > buf->b_mod_top)
1255 {
1256 mod_top = buf->b_mod_top;
1257#ifdef FEAT_SYN_HL
1258 /* Need to redraw lines above the change that may be included
1259 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001260 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001262 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 if (mod_top < 1)
1264 mod_top = 1;
1265 }
1266#endif
1267 }
1268 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1269 mod_bot = buf->b_mod_bot;
1270
1271#ifdef FEAT_SEARCH_EXTRA
1272 /* When 'hlsearch' is on and using a multi-line search pattern, a
1273 * change in one line may make the Search highlighting in a
1274 * previous line invalid. Simple solution: redraw all visible
1275 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001276 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001278 if (search_hl.rm.regprog != NULL
1279 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001281 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001282 {
1283 cur = wp->w_match_head;
1284 while (cur != NULL)
1285 {
1286 if (cur->match.regprog != NULL
1287 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001288 {
1289 top_to_mod = TRUE;
1290 break;
1291 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001292 cur = cur->next;
1293 }
1294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295#endif
1296 }
1297#ifdef FEAT_FOLDING
1298 if (mod_top != 0 && hasAnyFolding(wp))
1299 {
1300 linenr_T lnumt, lnumb;
1301
1302 /*
1303 * A change in a line can cause lines above it to become folded or
1304 * unfolded. Find the top most buffer line that may be affected.
1305 * If the line was previously folded and displayed, get the first
1306 * line of that fold. If the line is folded now, get the first
1307 * folded line. Use the minimum of these two.
1308 */
1309
1310 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1311 * the line below it. If there is no valid entry, use w_topline.
1312 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1313 * to this line. If there is no valid entry, use MAXLNUM. */
1314 lnumt = wp->w_topline;
1315 lnumb = MAXLNUM;
1316 for (i = 0; i < wp->w_lines_valid; ++i)
1317 if (wp->w_lines[i].wl_valid)
1318 {
1319 if (wp->w_lines[i].wl_lastlnum < mod_top)
1320 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1321 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1322 {
1323 lnumb = wp->w_lines[i].wl_lnum;
1324 /* When there is a fold column it might need updating
1325 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001326 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 ++lnumb;
1328 }
1329 }
1330
1331 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1332 if (mod_top > lnumt)
1333 mod_top = lnumt;
1334
1335 /* Now do the same for the bottom line (one above mod_bot). */
1336 --mod_bot;
1337 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1338 ++mod_bot;
1339 if (mod_bot < lnumb)
1340 mod_bot = lnumb;
1341 }
1342#endif
1343
1344 /* When a change starts above w_topline and the end is below
1345 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001346 * If the end of the change is above w_topline: do like no change was
1347 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 if (mod_top != 0 && mod_top < wp->w_topline)
1349 {
1350 if (mod_bot > wp->w_topline)
1351 mod_top = wp->w_topline;
1352#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001353 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 top_end = 1;
1355#endif
1356 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001357
1358 /* When line numbers are displayed need to redraw all lines below
1359 * inserted/deleted lines. */
1360 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1361 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
1363
1364 /*
1365 * When only displaying the lines at the top, set top_end. Used when
1366 * window has scrolled down for msg_scrolled.
1367 */
1368 if (type == REDRAW_TOP)
1369 {
1370 j = 0;
1371 for (i = 0; i < wp->w_lines_valid; ++i)
1372 {
1373 j += wp->w_lines[i].wl_size;
1374 if (j >= wp->w_upd_rows)
1375 {
1376 top_end = j;
1377 break;
1378 }
1379 }
1380 if (top_end == 0)
1381 /* not found (cannot happen?): redraw everything */
1382 type = NOT_VALID;
1383 else
1384 /* top area defined, the rest is VALID */
1385 type = VALID;
1386 }
1387
Bram Moolenaar367329b2007-08-30 11:53:22 +00001388 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001389 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1390 * non-zero and thus not FALSE) will indicate that screenclear() was not
1391 * called. */
1392 if (screen_cleared)
1393 screen_cleared = MAYBE;
1394
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 /*
1396 * If there are no changes on the screen that require a complete redraw,
1397 * handle three cases:
1398 * 1: we are off the top of the screen by a few lines: scroll down
1399 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1400 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1401 * w_lines[] that needs updating.
1402 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001403 if ((type == VALID || type == SOME_VALID
1404 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405#ifdef FEAT_DIFF
1406 && !wp->w_botfill && !wp->w_old_botfill
1407#endif
1408 )
1409 {
1410 if (mod_top != 0 && wp->w_topline == mod_top)
1411 {
1412 /*
1413 * w_topline is the first changed line, the scrolling will be done
1414 * further down.
1415 */
1416 }
1417 else if (wp->w_lines[0].wl_valid
1418 && (wp->w_topline < wp->w_lines[0].wl_lnum
1419#ifdef FEAT_DIFF
1420 || (wp->w_topline == wp->w_lines[0].wl_lnum
1421 && wp->w_topfill > wp->w_old_topfill)
1422#endif
1423 ))
1424 {
1425 /*
1426 * New topline is above old topline: May scroll down.
1427 */
1428#ifdef FEAT_FOLDING
1429 if (hasAnyFolding(wp))
1430 {
1431 linenr_T ln;
1432
1433 /* count the number of lines we are off, counting a sequence
1434 * of folded lines as one */
1435 j = 0;
1436 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1437 {
1438 ++j;
1439 if (j >= wp->w_height - 2)
1440 break;
1441 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1442 }
1443 }
1444 else
1445#endif
1446 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1447 if (j < wp->w_height - 2) /* not too far off */
1448 {
1449 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1450#ifdef FEAT_DIFF
1451 /* insert extra lines for previously invisible filler lines */
1452 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1453 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1454 - wp->w_old_topfill;
1455#endif
1456 if (i < wp->w_height - 2) /* less than a screen off */
1457 {
1458 /*
1459 * Try to insert the correct number of lines.
1460 * If not the last window, delete the lines at the bottom.
1461 * win_ins_lines may fail when the terminal can't do it.
1462 */
1463 if (i > 0)
1464 check_for_delay(FALSE);
1465 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1466 {
1467 if (wp->w_lines_valid != 0)
1468 {
1469 /* Need to update rows that are new, stop at the
1470 * first one that scrolled down. */
1471 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473
1474 /* Move the entries that were scrolled, disable
1475 * the entries for the lines to be redrawn. */
1476 if ((wp->w_lines_valid += j) > wp->w_height)
1477 wp->w_lines_valid = wp->w_height;
1478 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1479 wp->w_lines[idx] = wp->w_lines[idx - j];
1480 while (idx >= 0)
1481 wp->w_lines[idx--].wl_valid = FALSE;
1482 }
1483 }
1484 else
1485 mid_start = 0; /* redraw all lines */
1486 }
1487 else
1488 mid_start = 0; /* redraw all lines */
1489 }
1490 else
1491 mid_start = 0; /* redraw all lines */
1492 }
1493 else
1494 {
1495 /*
1496 * New topline is at or below old topline: May scroll up.
1497 * When topline didn't change, find first entry in w_lines[] that
1498 * needs updating.
1499 */
1500
1501 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1502 j = -1;
1503 row = 0;
1504 for (i = 0; i < wp->w_lines_valid; i++)
1505 {
1506 if (wp->w_lines[i].wl_valid
1507 && wp->w_lines[i].wl_lnum == wp->w_topline)
1508 {
1509 j = i;
1510 break;
1511 }
1512 row += wp->w_lines[i].wl_size;
1513 }
1514 if (j == -1)
1515 {
1516 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1517 * lines */
1518 mid_start = 0;
1519 }
1520 else
1521 {
1522 /*
1523 * Try to delete the correct number of lines.
1524 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1525 */
1526#ifdef FEAT_DIFF
1527 /* If the topline didn't change, delete old filler lines,
1528 * otherwise delete filler lines of the new topline... */
1529 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1530 row += wp->w_old_topfill;
1531 else
1532 row += diff_check_fill(wp, wp->w_topline);
1533 /* ... but don't delete new filler lines. */
1534 row -= wp->w_topfill;
1535#endif
1536 if (row > 0)
1537 {
1538 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001539 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1540 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 bot_start = wp->w_height - row;
1542 else
1543 mid_start = 0; /* redraw all lines */
1544 }
1545 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1546 {
1547 /*
1548 * Skip the lines (below the deleted lines) that are still
1549 * valid and don't need redrawing. Copy their info
1550 * upwards, to compensate for the deleted lines. Set
1551 * bot_start to the first row that needs redrawing.
1552 */
1553 bot_start = 0;
1554 idx = 0;
1555 for (;;)
1556 {
1557 wp->w_lines[idx] = wp->w_lines[j];
1558 /* stop at line that didn't fit, unless it is still
1559 * valid (no lines deleted) */
1560 if (row > 0 && bot_start + row
1561 + (int)wp->w_lines[j].wl_size > wp->w_height)
1562 {
1563 wp->w_lines_valid = idx + 1;
1564 break;
1565 }
1566 bot_start += wp->w_lines[idx++].wl_size;
1567
1568 /* stop at the last valid entry in w_lines[].wl_size */
1569 if (++j >= wp->w_lines_valid)
1570 {
1571 wp->w_lines_valid = idx;
1572 break;
1573 }
1574 }
1575#ifdef FEAT_DIFF
1576 /* Correct the first entry for filler lines at the top
1577 * when it won't get updated below. */
1578 if (wp->w_p_diff && bot_start > 0)
1579 wp->w_lines[0].wl_size =
1580 plines_win_nofill(wp, wp->w_topline, TRUE)
1581 + wp->w_topfill;
1582#endif
1583 }
1584 }
1585 }
1586
1587 /* When starting redraw in the first line, redraw all lines. When
1588 * there is only one window it's probably faster to clear the screen
1589 * first. */
1590 if (mid_start == 0)
1591 {
1592 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001593 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001594 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001595 /* Clear the screen when it was not done by win_del_lines() or
1596 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1597 * then. */
1598 if (screen_cleared != TRUE)
1599 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001600 /* The screen was cleared, redraw the tab pages line. */
1601 if (redraw_tabline)
1602 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001605
1606 /* When win_del_lines() or win_ins_lines() caused the screen to be
1607 * cleared (only happens for the first window) or when screenclear()
1608 * was called directly above, "must_redraw" will have been set to
1609 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1610 if (screen_cleared == TRUE)
1611 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613 else
1614 {
1615 /* Not VALID or INVERTED: redraw all lines. */
1616 mid_start = 0;
1617 mid_end = wp->w_height;
1618 }
1619
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001620 if (type == SOME_VALID)
1621 {
1622 /* SOME_VALID: redraw all lines. */
1623 mid_start = 0;
1624 mid_end = wp->w_height;
1625 type = NOT_VALID;
1626 }
1627
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 /* check if we are updating or removing the inverted part */
1629 if ((VIsual_active && buf == curwin->w_buffer)
1630 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1631 {
1632 linenr_T from, to;
1633
1634 if (VIsual_active)
1635 {
1636 if (VIsual_active
1637 && (VIsual_mode != wp->w_old_visual_mode
1638 || type == INVERTED_ALL))
1639 {
1640 /*
1641 * If the type of Visual selection changed, redraw the whole
1642 * selection. Also when the ownership of the X selection is
1643 * gained or lost.
1644 */
1645 if (curwin->w_cursor.lnum < VIsual.lnum)
1646 {
1647 from = curwin->w_cursor.lnum;
1648 to = VIsual.lnum;
1649 }
1650 else
1651 {
1652 from = VIsual.lnum;
1653 to = curwin->w_cursor.lnum;
1654 }
1655 /* redraw more when the cursor moved as well */
1656 if (wp->w_old_cursor_lnum < from)
1657 from = wp->w_old_cursor_lnum;
1658 if (wp->w_old_cursor_lnum > to)
1659 to = wp->w_old_cursor_lnum;
1660 if (wp->w_old_visual_lnum < from)
1661 from = wp->w_old_visual_lnum;
1662 if (wp->w_old_visual_lnum > to)
1663 to = wp->w_old_visual_lnum;
1664 }
1665 else
1666 {
1667 /*
1668 * Find the line numbers that need to be updated: The lines
1669 * between the old cursor position and the current cursor
1670 * position. Also check if the Visual position changed.
1671 */
1672 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1673 {
1674 from = curwin->w_cursor.lnum;
1675 to = wp->w_old_cursor_lnum;
1676 }
1677 else
1678 {
1679 from = wp->w_old_cursor_lnum;
1680 to = curwin->w_cursor.lnum;
1681 if (from == 0) /* Visual mode just started */
1682 from = to;
1683 }
1684
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001685 if (VIsual.lnum != wp->w_old_visual_lnum
1686 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 {
1688 if (wp->w_old_visual_lnum < from
1689 && wp->w_old_visual_lnum != 0)
1690 from = wp->w_old_visual_lnum;
1691 if (wp->w_old_visual_lnum > to)
1692 to = wp->w_old_visual_lnum;
1693 if (VIsual.lnum < from)
1694 from = VIsual.lnum;
1695 if (VIsual.lnum > to)
1696 to = VIsual.lnum;
1697 }
1698 }
1699
1700 /*
1701 * If in block mode and changed column or curwin->w_curswant:
1702 * update all lines.
1703 * First compute the actual start and end column.
1704 */
1705 if (VIsual_mode == Ctrl_V)
1706 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001707 colnr_T fromc, toc;
1708#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1709 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
Bram Moolenaar404406a2014-10-09 13:24:43 +02001711 if (curwin->w_p_lbr)
1712 ve_flags = VE_ALL;
1713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001715#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1716 ve_flags = save_ve_flags;
1717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 ++toc;
1719 if (curwin->w_curswant == MAXCOL)
1720 toc = MAXCOL;
1721
1722 if (fromc != wp->w_old_cursor_fcol
1723 || toc != wp->w_old_cursor_lcol)
1724 {
1725 if (from > VIsual.lnum)
1726 from = VIsual.lnum;
1727 if (to < VIsual.lnum)
1728 to = VIsual.lnum;
1729 }
1730 wp->w_old_cursor_fcol = fromc;
1731 wp->w_old_cursor_lcol = toc;
1732 }
1733 }
1734 else
1735 {
1736 /* Use the line numbers of the old Visual area. */
1737 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1738 {
1739 from = wp->w_old_cursor_lnum;
1740 to = wp->w_old_visual_lnum;
1741 }
1742 else
1743 {
1744 from = wp->w_old_visual_lnum;
1745 to = wp->w_old_cursor_lnum;
1746 }
1747 }
1748
1749 /*
1750 * There is no need to update lines above the top of the window.
1751 */
1752 if (from < wp->w_topline)
1753 from = wp->w_topline;
1754
1755 /*
1756 * If we know the value of w_botline, use it to restrict the update to
1757 * the lines that are visible in the window.
1758 */
1759 if (wp->w_valid & VALID_BOTLINE)
1760 {
1761 if (from >= wp->w_botline)
1762 from = wp->w_botline - 1;
1763 if (to >= wp->w_botline)
1764 to = wp->w_botline - 1;
1765 }
1766
1767 /*
1768 * Find the minimal part to be updated.
1769 * Watch out for scrolling that made entries in w_lines[] invalid.
1770 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1771 * top_end; need to redraw from top_end to the "to" line.
1772 * A middle mouse click with a Visual selection may change the text
1773 * above the Visual area and reset wl_valid, do count these for
1774 * mid_end (in srow).
1775 */
1776 if (mid_start > 0)
1777 {
1778 lnum = wp->w_topline;
1779 idx = 0;
1780 srow = 0;
1781 if (scrolled_down)
1782 mid_start = top_end;
1783 else
1784 mid_start = 0;
1785 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1786 {
1787 if (wp->w_lines[idx].wl_valid)
1788 mid_start += wp->w_lines[idx].wl_size;
1789 else if (!scrolled_down)
1790 srow += wp->w_lines[idx].wl_size;
1791 ++idx;
1792# ifdef FEAT_FOLDING
1793 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1794 lnum = wp->w_lines[idx].wl_lnum;
1795 else
1796# endif
1797 ++lnum;
1798 }
1799 srow += mid_start;
1800 mid_end = wp->w_height;
1801 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1802 {
1803 if (wp->w_lines[idx].wl_valid
1804 && wp->w_lines[idx].wl_lnum >= to + 1)
1805 {
1806 /* Only update until first row of this line */
1807 mid_end = srow;
1808 break;
1809 }
1810 srow += wp->w_lines[idx].wl_size;
1811 }
1812 }
1813 }
1814
1815 if (VIsual_active && buf == curwin->w_buffer)
1816 {
1817 wp->w_old_visual_mode = VIsual_mode;
1818 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1819 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001820 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 wp->w_old_curswant = curwin->w_curswant;
1822 }
1823 else
1824 {
1825 wp->w_old_visual_mode = 0;
1826 wp->w_old_cursor_lnum = 0;
1827 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001828 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830
1831#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1832 /* reset got_int, otherwise regexp won't work */
1833 save_got_int = got_int;
1834 got_int = 0;
1835#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001836#ifdef SYN_TIME_LIMIT
1837 /* Set the time limit to 'redrawtime'. */
1838 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001839 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841#ifdef FEAT_FOLDING
1842 win_foldinfo.fi_level = 0;
1843#endif
1844
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001845#ifdef FEAT_MENU
1846 /*
1847 * Draw the window toolbar, if there is one.
1848 * TODO: only when needed.
1849 */
1850 if (winbar_height(wp) > 0)
1851 redraw_win_toolbar(wp);
1852#endif
1853
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 /*
1855 * Update all the window rows.
1856 */
1857 idx = 0; /* first entry in w_lines[].wl_size */
1858 row = 0;
1859 srow = 0;
1860 lnum = wp->w_topline; /* first line shown in window */
1861 for (;;)
1862 {
1863 /* stop updating when reached the end of the window (check for _past_
1864 * the end of the window is at the end of the loop) */
1865 if (row == wp->w_height)
1866 {
1867 didline = TRUE;
1868 break;
1869 }
1870
1871 /* stop updating when hit the end of the file */
1872 if (lnum > buf->b_ml.ml_line_count)
1873 {
1874 eof = TRUE;
1875 break;
1876 }
1877
1878 /* Remember the starting row of the line that is going to be dealt
1879 * with. It is used further down when the line doesn't fit. */
1880 srow = row;
1881
1882 /*
1883 * Update a line when it is in an area that needs updating, when it
1884 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001885 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 * win_del_lines(), check if the current line includes it.
1887 * When syntax folding is being used, the saved syntax states will
1888 * already have been updated, we can't see where the syntax state is
1889 * the same again, just update until the end of the window.
1890 */
1891 if (row < top_end
1892 || (row >= mid_start && row < mid_end)
1893#ifdef FEAT_SEARCH_EXTRA
1894 || top_to_mod
1895#endif
1896 || idx >= wp->w_lines_valid
1897 || (row + wp->w_lines[idx].wl_size > bot_start)
1898 || (mod_top != 0
1899 && (lnum == mod_top
1900 || (lnum >= mod_top
1901 && (lnum < mod_bot
1902#ifdef FEAT_SYN_HL
1903 || did_update == DID_FOLD
1904 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001905 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 && (
1907# ifdef FEAT_FOLDING
1908 (foldmethodIsSyntax(wp)
1909 && hasAnyFolding(wp)) ||
1910# endif
1911 syntax_check_changed(lnum)))
1912#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001913#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001914 /* match in fixed position might need redraw
1915 * if lines were inserted or deleted */
1916 || (wp->w_match_head != NULL
1917 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 )))))
1920 {
1921#ifdef FEAT_SEARCH_EXTRA
1922 if (lnum == mod_top)
1923 top_to_mod = FALSE;
1924#endif
1925
1926 /*
1927 * When at start of changed lines: May scroll following lines
1928 * up or down to minimize redrawing.
1929 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001930 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 */
1932 if (lnum == mod_top
1933 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001934 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 {
1936 int old_rows = 0;
1937 int new_rows = 0;
1938 int xtra_rows;
1939 linenr_T l;
1940
1941 /* Count the old number of window rows, using w_lines[], which
1942 * should still contain the sizes for the lines as they are
1943 * currently displayed. */
1944 for (i = idx; i < wp->w_lines_valid; ++i)
1945 {
1946 /* Only valid lines have a meaningful wl_lnum. Invalid
1947 * lines are part of the changed area. */
1948 if (wp->w_lines[i].wl_valid
1949 && wp->w_lines[i].wl_lnum == mod_bot)
1950 break;
1951 old_rows += wp->w_lines[i].wl_size;
1952#ifdef FEAT_FOLDING
1953 if (wp->w_lines[i].wl_valid
1954 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1955 {
1956 /* Must have found the last valid entry above mod_bot.
1957 * Add following invalid entries. */
1958 ++i;
1959 while (i < wp->w_lines_valid
1960 && !wp->w_lines[i].wl_valid)
1961 old_rows += wp->w_lines[i++].wl_size;
1962 break;
1963 }
1964#endif
1965 }
1966
1967 if (i >= wp->w_lines_valid)
1968 {
1969 /* We can't find a valid line below the changed lines,
1970 * need to redraw until the end of the window.
1971 * Inserting/deleting lines has no use. */
1972 bot_start = 0;
1973 }
1974 else
1975 {
1976 /* Able to count old number of rows: Count new window
1977 * rows, and may insert/delete lines */
1978 j = idx;
1979 for (l = lnum; l < mod_bot; ++l)
1980 {
1981#ifdef FEAT_FOLDING
1982 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1983 ++new_rows;
1984 else
1985#endif
1986#ifdef FEAT_DIFF
1987 if (l == wp->w_topline)
1988 new_rows += plines_win_nofill(wp, l, TRUE)
1989 + wp->w_topfill;
1990 else
1991#endif
1992 new_rows += plines_win(wp, l, TRUE);
1993 ++j;
1994 if (new_rows > wp->w_height - row - 2)
1995 {
1996 /* it's getting too much, must redraw the rest */
1997 new_rows = 9999;
1998 break;
1999 }
2000 }
2001 xtra_rows = new_rows - old_rows;
2002 if (xtra_rows < 0)
2003 {
2004 /* May scroll text up. If there is not enough
2005 * remaining text or scrolling fails, must redraw the
2006 * rest. If scrolling works, must redraw the text
2007 * below the scrolled text. */
2008 if (row - xtra_rows >= wp->w_height - 2)
2009 mod_bot = MAXLNUM;
2010 else
2011 {
2012 check_for_delay(FALSE);
2013 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002014 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 mod_bot = MAXLNUM;
2016 else
2017 bot_start = wp->w_height + xtra_rows;
2018 }
2019 }
2020 else if (xtra_rows > 0)
2021 {
2022 /* May scroll text down. If there is not enough
2023 * remaining text of scrolling fails, must redraw the
2024 * rest. */
2025 if (row + xtra_rows >= wp->w_height - 2)
2026 mod_bot = MAXLNUM;
2027 else
2028 {
2029 check_for_delay(FALSE);
2030 if (win_ins_lines(wp, row + old_rows,
2031 xtra_rows, FALSE, FALSE) == FAIL)
2032 mod_bot = MAXLNUM;
2033 else if (top_end > row + old_rows)
2034 /* Scrolled the part at the top that requires
2035 * updating down. */
2036 top_end += xtra_rows;
2037 }
2038 }
2039
2040 /* When not updating the rest, may need to move w_lines[]
2041 * entries. */
2042 if (mod_bot != MAXLNUM && i != j)
2043 {
2044 if (j < i)
2045 {
2046 int x = row + new_rows;
2047
2048 /* move entries in w_lines[] upwards */
2049 for (;;)
2050 {
2051 /* stop at last valid entry in w_lines[] */
2052 if (i >= wp->w_lines_valid)
2053 {
2054 wp->w_lines_valid = j;
2055 break;
2056 }
2057 wp->w_lines[j] = wp->w_lines[i];
2058 /* stop at a line that won't fit */
2059 if (x + (int)wp->w_lines[j].wl_size
2060 > wp->w_height)
2061 {
2062 wp->w_lines_valid = j + 1;
2063 break;
2064 }
2065 x += wp->w_lines[j++].wl_size;
2066 ++i;
2067 }
2068 if (bot_start > x)
2069 bot_start = x;
2070 }
2071 else /* j > i */
2072 {
2073 /* move entries in w_lines[] downwards */
2074 j -= i;
2075 wp->w_lines_valid += j;
2076 if (wp->w_lines_valid > wp->w_height)
2077 wp->w_lines_valid = wp->w_height;
2078 for (i = wp->w_lines_valid; i - j >= idx; --i)
2079 wp->w_lines[i] = wp->w_lines[i - j];
2080
2081 /* The w_lines[] entries for inserted lines are
2082 * now invalid, but wl_size may be used above.
2083 * Reset to zero. */
2084 while (i >= idx)
2085 {
2086 wp->w_lines[i].wl_size = 0;
2087 wp->w_lines[i--].wl_valid = FALSE;
2088 }
2089 }
2090 }
2091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
2093
2094#ifdef FEAT_FOLDING
2095 /*
2096 * When lines are folded, display one line for all of them.
2097 * Otherwise, display normally (can be several display lines when
2098 * 'wrap' is on).
2099 */
2100 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2101 if (fold_count != 0)
2102 {
2103 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2104 ++row;
2105 --fold_count;
2106 wp->w_lines[idx].wl_folded = TRUE;
2107 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2108# ifdef FEAT_SYN_HL
2109 did_update = DID_FOLD;
2110# endif
2111 }
2112 else
2113#endif
2114 if (idx < wp->w_lines_valid
2115 && wp->w_lines[idx].wl_valid
2116 && wp->w_lines[idx].wl_lnum == lnum
2117 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002118 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 && srow + wp->w_lines[idx].wl_size > wp->w_height
2120#ifdef FEAT_DIFF
2121 && diff_check_fill(wp, lnum) == 0
2122#endif
2123 )
2124 {
2125 /* This line is not going to fit. Don't draw anything here,
2126 * will draw "@ " lines below. */
2127 row = wp->w_height + 1;
2128 }
2129 else
2130 {
2131#ifdef FEAT_SEARCH_EXTRA
2132 prepare_search_hl(wp, lnum);
2133#endif
2134#ifdef FEAT_SYN_HL
2135 /* Let the syntax stuff know we skipped a few lines. */
2136 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002137 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 syntax_end_parsing(syntax_last_parsed + 1);
2139#endif
2140
2141 /*
2142 * Display one line.
2143 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002144 row = win_line(wp, lnum, srow, wp->w_height,
2145 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146
2147#ifdef FEAT_FOLDING
2148 wp->w_lines[idx].wl_folded = FALSE;
2149 wp->w_lines[idx].wl_lastlnum = lnum;
2150#endif
2151#ifdef FEAT_SYN_HL
2152 did_update = DID_LINE;
2153 syntax_last_parsed = lnum;
2154#endif
2155 }
2156
2157 wp->w_lines[idx].wl_lnum = lnum;
2158 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002159
2160 /* Past end of the window or end of the screen. Note that after
2161 * resizing wp->w_height may be end up too big. That's a problem
2162 * elsewhere, but prevent a crash here. */
2163 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 {
2165 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002166 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2168 ++idx;
2169 break;
2170 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002171 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 wp->w_lines[idx].wl_size = row - srow;
2173 ++idx;
2174#ifdef FEAT_FOLDING
2175 lnum += fold_count + 1;
2176#else
2177 ++lnum;
2178#endif
2179 }
2180 else
2181 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002182 if (wp->w_p_rnu)
2183 {
2184 // 'relativenumber' set: The text doesn't need to be drawn, but
2185 // the number column nearly always does.
2186 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
2187 }
2188
2189 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 row += wp->w_lines[idx++].wl_size;
2191 if (row > wp->w_height) /* past end of screen */
2192 break;
2193#ifdef FEAT_FOLDING
2194 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2195#else
2196 ++lnum;
2197#endif
2198#ifdef FEAT_SYN_HL
2199 did_update = DID_NONE;
2200#endif
2201 }
2202
2203 if (lnum > buf->b_ml.ml_line_count)
2204 {
2205 eof = TRUE;
2206 break;
2207 }
2208 }
2209 /*
2210 * End of loop over all window lines.
2211 */
2212
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002213#ifdef FEAT_VTP
2214 /* Rewrite the character at the end of the screen line. */
2215 if (use_vtp())
2216 {
2217 int i;
2218
2219 for (i = 0; i < Rows; ++i)
2220# ifdef FEAT_MBYTE
2221 if (enc_utf8)
2222 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2223 LineOffset[i] + screen_Columns) > 1)
2224 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2225 else
2226 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2227 else
2228# endif
2229 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2230 }
2231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
2233 if (idx > wp->w_lines_valid)
2234 wp->w_lines_valid = idx;
2235
2236#ifdef FEAT_SYN_HL
2237 /*
2238 * Let the syntax stuff know we stop parsing here.
2239 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002240 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 syntax_end_parsing(syntax_last_parsed + 1);
2242#endif
2243
2244 /*
2245 * If we didn't hit the end of the file, and we didn't finish the last
2246 * line we were working on, then the line didn't fit.
2247 */
2248 wp->w_empty_rows = 0;
2249#ifdef FEAT_DIFF
2250 wp->w_filler_rows = 0;
2251#endif
2252 if (!eof && !didline)
2253 {
2254 if (lnum == wp->w_topline)
2255 {
2256 /*
2257 * Single line that does not fit!
2258 * Don't overwrite it, it can be edited.
2259 */
2260 wp->w_botline = lnum + 1;
2261 }
2262#ifdef FEAT_DIFF
2263 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2264 {
2265 /* Window ends in filler lines. */
2266 wp->w_botline = lnum;
2267 wp->w_filler_rows = wp->w_height - srow;
2268 }
2269#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002270 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2271 {
2272 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2273
2274 /*
2275 * Last line isn't finished: Display "@@@" in the last screen line.
2276 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002277 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002278 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002279 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002280 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002281 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002282 set_empty_rows(wp, srow);
2283 wp->w_botline = lnum;
2284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2286 {
2287 /*
2288 * Last line isn't finished: Display "@@@" at the end.
2289 */
2290 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2291 W_WINROW(wp) + wp->w_height,
2292 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002293 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 set_empty_rows(wp, srow);
2295 wp->w_botline = lnum;
2296 }
2297 else
2298 {
2299 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2300 wp->w_botline = lnum;
2301 }
2302 }
2303 else
2304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 if (eof) /* we hit the end of the file */
2307 {
2308 wp->w_botline = buf->b_ml.ml_line_count + 1;
2309#ifdef FEAT_DIFF
2310 j = diff_check_fill(wp, wp->w_botline);
2311 if (j > 0 && !wp->w_botfill)
2312 {
2313 /*
2314 * Display filler lines at the end of the file
2315 */
2316 if (char2cells(fill_diff) > 1)
2317 i = '-';
2318 else
2319 i = fill_diff;
2320 if (row + j > wp->w_height)
2321 j = wp->w_height - row;
2322 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2323 row += j;
2324 }
2325#endif
2326 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002327 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 wp->w_botline = lnum;
2329
2330 /* make sure the rest of the screen is blank */
2331 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002332 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 }
2334
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002335#ifdef SYN_TIME_LIMIT
2336 syn_set_timeout(NULL);
2337#endif
2338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 /* Reset the type of redrawing required, the window has been updated. */
2340 wp->w_redr_type = 0;
2341#ifdef FEAT_DIFF
2342 wp->w_old_topfill = wp->w_topfill;
2343 wp->w_old_botfill = wp->w_botfill;
2344#endif
2345
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002346 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
2348 /*
2349 * There is a trick with w_botline. If we invalidate it on each
2350 * change that might modify it, this will cause a lot of expensive
2351 * calls to plines() in update_topline() each time. Therefore the
2352 * value of w_botline is often approximated, and this value is used to
2353 * compute the value of w_topline. If the value of w_botline was
2354 * wrong, check that the value of w_topline is correct (cursor is on
2355 * the visible part of the text). If it's not, we need to redraw
2356 * again. Mostly this just means scrolling up a few lines, so it
2357 * doesn't look too bad. Only do this for the current window (where
2358 * changes are relevant).
2359 */
2360 wp->w_valid |= VALID_BOTLINE;
2361 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2362 {
2363 recursive = TRUE;
2364 curwin->w_valid &= ~VALID_TOPLINE;
2365 update_topline(); /* may invalidate w_botline again */
2366 if (must_redraw != 0)
2367 {
2368 /* Don't update for changes in buffer again. */
2369 i = curbuf->b_mod_set;
2370 curbuf->b_mod_set = FALSE;
2371 win_update(curwin);
2372 must_redraw = 0;
2373 curbuf->b_mod_set = i;
2374 }
2375 recursive = FALSE;
2376 }
2377 }
2378
2379#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2380 /* restore got_int, unless CTRL-C was hit while redrawing */
2381 if (!got_int)
2382 got_int = save_got_int;
2383#endif
2384}
2385
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386/*
2387 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2388 * as the filler character.
2389 */
2390 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002391win_draw_end(
2392 win_T *wp,
2393 int c1,
2394 int c2,
2395 int row,
2396 int endrow,
2397 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398{
2399#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2400 int n = 0;
2401# define FDC_OFF n
2402#else
2403# define FDC_OFF 0
2404#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002405#ifdef FEAT_FOLDING
2406 int fdc = compute_foldcolumn(wp, 0);
2407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409#ifdef FEAT_RIGHTLEFT
2410 if (wp->w_p_rl)
2411 {
2412 /* No check for cmdline window: should never be right-left. */
2413# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002414 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
2416 if (n > 0)
2417 {
2418 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002419 if (n > wp->w_width)
2420 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2422 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002423 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425# endif
2426# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002427 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 {
2429 int nn = n + 2;
2430
2431 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002432 if (nn > wp->w_width)
2433 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2435 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002436 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 n = nn;
2438 }
2439# endif
2440 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002441 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002442 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2444 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002445 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 }
2447 else
2448#endif
2449 {
2450#ifdef FEAT_CMDWIN
2451 if (cmdwin_type != 0 && wp == curwin)
2452 {
2453 /* draw the cmdline character in the leftmost column */
2454 n = 1;
2455 if (n > wp->w_width)
2456 n = wp->w_width;
2457 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002458 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002459 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
2461#endif
2462#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002463 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002465 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002468 if (nn > wp->w_width)
2469 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002471 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002472 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 n = nn;
2474 }
2475#endif
2476#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002477 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
2479 int nn = n + 2;
2480
2481 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002482 if (nn > wp->w_width)
2483 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002485 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002486 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 n = nn;
2488 }
2489#endif
2490 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002491 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002492 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 }
2494 set_empty_rows(wp, row);
2495}
2496
Bram Moolenaar1a384422010-07-14 19:53:30 +02002497#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002498static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002499
2500/*
2501 * Advance **color_cols and return TRUE when there are columns to draw.
2502 */
2503 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002504advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002505{
2506 while (**color_cols >= 0 && vcol > **color_cols)
2507 ++*color_cols;
2508 return (**color_cols >= 0);
2509}
2510#endif
2511
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002512#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2513/*
2514 * Copy "text" to ScreenLines using "attr".
2515 * Returns the next screen column.
2516 */
2517 static int
2518text_to_screenline(win_T *wp, char_u *text, int col)
2519{
2520 int off = (int)(current_ScreenLine - ScreenLines);
2521
2522#ifdef FEAT_MBYTE
2523 if (has_mbyte)
2524 {
2525 int cells;
2526 int u8c, u8cc[MAX_MCO];
2527 int i;
2528 int idx;
2529 int c_len;
2530 char_u *p;
2531# ifdef FEAT_ARABIC
2532 int prev_c = 0; /* previous Arabic character */
2533 int prev_c1 = 0; /* first composing char for prev_c */
2534# endif
2535
2536# ifdef FEAT_RIGHTLEFT
2537 if (wp->w_p_rl)
2538 idx = off;
2539 else
2540# endif
2541 idx = off + col;
2542
2543 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2544 for (p = text; *p != NUL; )
2545 {
2546 cells = (*mb_ptr2cells)(p);
2547 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002548 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002549# ifdef FEAT_RIGHTLEFT
2550 - (wp->w_p_rl ? col : 0)
2551# endif
2552 )
2553 break;
2554 ScreenLines[idx] = *p;
2555 if (enc_utf8)
2556 {
2557 u8c = utfc_ptr2char(p, u8cc);
2558 if (*p < 0x80 && u8cc[0] == 0)
2559 {
2560 ScreenLinesUC[idx] = 0;
2561#ifdef FEAT_ARABIC
2562 prev_c = u8c;
2563#endif
2564 }
2565 else
2566 {
2567#ifdef FEAT_ARABIC
2568 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2569 {
2570 /* Do Arabic shaping. */
2571 int pc, pc1, nc;
2572 int pcc[MAX_MCO];
2573 int firstbyte = *p;
2574
2575 /* The idea of what is the previous and next
2576 * character depends on 'rightleft'. */
2577 if (wp->w_p_rl)
2578 {
2579 pc = prev_c;
2580 pc1 = prev_c1;
2581 nc = utf_ptr2char(p + c_len);
2582 prev_c1 = u8cc[0];
2583 }
2584 else
2585 {
2586 pc = utfc_ptr2char(p + c_len, pcc);
2587 nc = prev_c;
2588 pc1 = pcc[0];
2589 }
2590 prev_c = u8c;
2591
2592 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2593 pc, pc1, nc);
2594 ScreenLines[idx] = firstbyte;
2595 }
2596 else
2597 prev_c = u8c;
2598#endif
2599 /* Non-BMP character: display as ? or fullwidth ?. */
2600#ifdef UNICODE16
2601 if (u8c >= 0x10000)
2602 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2603 else
2604#endif
2605 ScreenLinesUC[idx] = u8c;
2606 for (i = 0; i < Screen_mco; ++i)
2607 {
2608 ScreenLinesC[i][idx] = u8cc[i];
2609 if (u8cc[i] == 0)
2610 break;
2611 }
2612 }
2613 if (cells > 1)
2614 ScreenLines[idx + 1] = 0;
2615 }
2616 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2617 /* double-byte single width character */
2618 ScreenLines2[idx] = p[1];
2619 else if (cells > 1)
2620 /* double-width character */
2621 ScreenLines[idx + 1] = p[1];
2622 col += cells;
2623 idx += cells;
2624 p += c_len;
2625 }
2626 }
2627 else
2628#endif
2629 {
2630 int len = (int)STRLEN(text);
2631
Bram Moolenaar02631462017-09-22 15:20:32 +02002632 if (len > wp->w_width - col)
2633 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002634 if (len > 0)
2635 {
2636#ifdef FEAT_RIGHTLEFT
2637 if (wp->w_p_rl)
2638 STRNCPY(current_ScreenLine, text, len);
2639 else
2640#endif
2641 STRNCPY(current_ScreenLine + col, text, len);
2642 col += len;
2643 }
2644 }
2645 return col;
2646}
2647#endif
2648
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649#ifdef FEAT_FOLDING
2650/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002651 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2652 * space is available for window "wp", minus "col".
2653 */
2654 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002655compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002656{
2657 int fdc = wp->w_p_fdc;
2658 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002659 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002660
2661 if (fdc > wwidth - (col + wmw))
2662 fdc = wwidth - (col + wmw);
2663 return fdc;
2664}
2665
2666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 * Display one folded line.
2668 */
2669 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002670fold_line(
2671 win_T *wp,
2672 long fold_count,
2673 foldinfo_T *foldinfo,
2674 linenr_T lnum,
2675 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002677 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 pos_T *top, *bot;
2679 linenr_T lnume = lnum + fold_count - 1;
2680 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002681 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 int col;
2684 int txtcol;
2685 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002686 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
2688 /* Build the fold line:
2689 * 1. Add the cmdwin_type for the command-line window
2690 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002691 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 * 4. Compose the text
2693 * 5. Add the text
2694 * 6. set highlighting for the Visual area an other text
2695 */
2696 col = 0;
2697
2698 /*
2699 * 1. Add the cmdwin_type for the command-line window
2700 * Ignores 'rightleft', this window is never right-left.
2701 */
2702#ifdef FEAT_CMDWIN
2703 if (cmdwin_type != 0 && wp == curwin)
2704 {
2705 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002706 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707#ifdef FEAT_MBYTE
2708 if (enc_utf8)
2709 ScreenLinesUC[off] = 0;
2710#endif
2711 ++col;
2712 }
2713#endif
2714
2715 /*
2716 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002717 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002719 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 if (fdc > 0)
2721 {
2722 fill_foldcolumn(buf, wp, TRUE, lnum);
2723#ifdef FEAT_RIGHTLEFT
2724 if (wp->w_p_rl)
2725 {
2726 int i;
2727
Bram Moolenaar02631462017-09-22 15:20:32 +02002728 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002729 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 /* reverse the fold column */
2731 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002732 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 }
2734 else
2735#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002736 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 col += fdc;
2738 }
2739
2740#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002741# define RL_MEMSET(p, v, l) \
2742 do { \
2743 if (wp->w_p_rl) \
2744 for (ri = 0; ri < l; ++ri) \
2745 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2746 else \
2747 for (ri = 0; ri < l; ++ri) \
2748 ScreenAttrs[off + (p) + ri] = v; \
2749 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002751# define RL_MEMSET(p, v, l) \
2752 do { \
2753 for (ri = 0; ri < l; ++ri) \
2754 ScreenAttrs[off + (p) + ri] = v; \
2755 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756#endif
2757
Bram Moolenaar64486672010-05-16 15:46:46 +02002758 /* Set all attributes of the 'number' or 'relativenumber' column and the
2759 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002760 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
2762#ifdef FEAT_SIGNS
2763 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002764 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002766 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 if (len > 0)
2768 {
2769 if (len > 2)
2770 len = 2;
2771# ifdef FEAT_RIGHTLEFT
2772 if (wp->w_p_rl)
2773 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002774 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002775 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 else
2777# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002778 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 col += len;
2780 }
2781 }
2782#endif
2783
2784 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002785 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002787 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002789 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 if (len > 0)
2791 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002792 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002793 long num;
2794 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002795
2796 if (len > w + 1)
2797 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002798
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002799 if (wp->w_p_nu && !wp->w_p_rnu)
2800 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002801 num = (long)lnum;
2802 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002803 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002804 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002805 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002806 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002807 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002808 /* 'number' + 'relativenumber': cursor line shows absolute
2809 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002810 num = lnum;
2811 fmt = "%-*ld ";
2812 }
2813 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002814
Bram Moolenaar700e7342013-01-30 12:31:36 +01002815 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816#ifdef FEAT_RIGHTLEFT
2817 if (wp->w_p_rl)
2818 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002819 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002820 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 else
2822#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002823 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 col += len;
2825 }
2826 }
2827
2828 /*
2829 * 4. Compose the folded-line string with 'foldtext', if set.
2830 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002831 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
2833 txtcol = col; /* remember where text starts */
2834
2835 /*
2836 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2837 * Right-left text is put in columns 0 - number-col, normal text is put
2838 * in columns number-col - window-width.
2839 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002840 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841
2842 /* Fill the rest of the line with the fold filler */
2843#ifdef FEAT_RIGHTLEFT
2844 if (wp->w_p_rl)
2845 col -= txtcol;
2846#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002847 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848#ifdef FEAT_RIGHTLEFT
2849 - (wp->w_p_rl ? txtcol : 0)
2850#endif
2851 )
2852 {
2853#ifdef FEAT_MBYTE
2854 if (enc_utf8)
2855 {
2856 if (fill_fold >= 0x80)
2857 {
2858 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002859 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002860 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 }
2862 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002863 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002865 ScreenLines[off + col] = fill_fold;
2866 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002867 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002869 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002871 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 }
2873
2874 if (text != buf)
2875 vim_free(text);
2876
2877 /*
2878 * 6. set highlighting for the Visual area an other text.
2879 * If all folded lines are in the Visual area, highlight the line.
2880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2882 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002883 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
2885 /* Visual is after curwin->w_cursor */
2886 top = &curwin->w_cursor;
2887 bot = &VIsual;
2888 }
2889 else
2890 {
2891 /* Visual is before curwin->w_cursor */
2892 top = &VIsual;
2893 bot = &curwin->w_cursor;
2894 }
2895 if (lnum >= top->lnum
2896 && lnume <= bot->lnum
2897 && (VIsual_mode != 'v'
2898 || ((lnum > top->lnum
2899 || (lnum == top->lnum
2900 && top->col == 0))
2901 && (lnume < bot->lnum
2902 || (lnume == bot->lnum
2903 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002904 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 {
2906 if (VIsual_mode == Ctrl_V)
2907 {
2908 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002909 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002911 if (wp->w_old_cursor_lcol != MAXCOL
2912 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002913 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 len = wp->w_old_cursor_lcol;
2915 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002916 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002917 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002918 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
2920 }
2921 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002922 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002924 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 }
2927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002929#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002930 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2931 if (wp->w_p_cc_cols)
2932 {
2933 int i = 0;
2934 int j = wp->w_p_cc_cols[i];
2935 int old_txtcol = txtcol;
2936
2937 while (j > -1)
2938 {
2939 txtcol += j;
2940 if (wp->w_p_wrap)
2941 txtcol -= wp->w_skipcol;
2942 else
2943 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002944 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002945 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002946 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002947 txtcol = old_txtcol;
2948 j = wp->w_p_cc_cols[++i];
2949 }
2950 }
2951
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002952 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002953 if (wp->w_p_cuc)
2954 {
2955 txtcol += wp->w_virtcol;
2956 if (wp->w_p_wrap)
2957 txtcol -= wp->w_skipcol;
2958 else
2959 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002960 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002961 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002962 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002963 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
Bram Moolenaar02631462017-09-22 15:20:32 +02002966 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2967 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
2969 /*
2970 * Update w_cline_height and w_cline_folded if the cursor line was
2971 * updated (saves a call to plines() later).
2972 */
2973 if (wp == curwin
2974 && lnum <= curwin->w_cursor.lnum
2975 && lnume >= curwin->w_cursor.lnum)
2976 {
2977 curwin->w_cline_row = row;
2978 curwin->w_cline_height = 1;
2979 curwin->w_cline_folded = TRUE;
2980 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2981 }
2982}
2983
2984/*
2985 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2986 */
2987 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002988copy_text_attr(
2989 int off,
2990 char_u *buf,
2991 int len,
2992 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002994 int i;
2995
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 mch_memmove(ScreenLines + off, buf, (size_t)len);
2997# ifdef FEAT_MBYTE
2998 if (enc_utf8)
2999 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
3000# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003001 for (i = 0; i < len; ++i)
3002 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003}
3004
3005/*
3006 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003007 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 */
3009 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003010fill_foldcolumn(
3011 char_u *p,
3012 win_T *wp,
3013 int closed, /* TRUE of FALSE */
3014 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015{
3016 int i = 0;
3017 int level;
3018 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003019 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003020 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
3022 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003023 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024
3025 level = win_foldinfo.fi_level;
3026 if (level > 0)
3027 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003028 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003029 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003030
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 /* If the column is too narrow, we start at the lowest level that
3032 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003033 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 if (first_level < 1)
3035 first_level = 1;
3036
Bram Moolenaar1c934292015-01-27 16:39:29 +01003037 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 {
3039 if (win_foldinfo.fi_lnum == lnum
3040 && first_level + i >= win_foldinfo.fi_low_level)
3041 p[i] = '-';
3042 else if (first_level == 1)
3043 p[i] = '|';
3044 else if (first_level + i <= 9)
3045 p[i] = '0' + first_level + i;
3046 else
3047 p[i] = '>';
3048 if (first_level + i == level)
3049 break;
3050 }
3051 }
3052 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003053 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054}
3055#endif /* FEAT_FOLDING */
3056
3057/*
3058 * Display line "lnum" of window 'wp' on the screen.
3059 * Start at row "startrow", stop when "endrow" is reached.
3060 * wp->w_virtcol needs to be valid.
3061 *
3062 * Return the number of last row the line occupies.
3063 */
3064 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003065win_line(
3066 win_T *wp,
3067 linenr_T lnum,
3068 int startrow,
3069 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003070 int nochange UNUSED, // not updating for changed text
3071 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003073 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3075 int c = 0; /* init for GCC */
3076 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003077#ifdef FEAT_LINEBREAK
3078 long vcol_sbr = -1; /* virtual column after showbreak */
3079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 long vcol_prev = -1; /* "vcol" of previous character */
3081 char_u *line; /* current line */
3082 char_u *ptr; /* current position in "line" */
3083 int row; /* row in the window, excl w_winrow */
3084 int screen_row; /* row on the screen, incl w_winrow */
3085
3086 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3087 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003088 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003089 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 int c_extra = NUL; /* extra chars, all the same */
3091 int extra_attr = 0; /* attributes when n_extra != 0 */
3092 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3093 displaying lcs_eol at end-of-line */
3094 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3095 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3096
3097 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3098 int saved_n_extra = 0;
3099 char_u *saved_p_extra = NULL;
3100 int saved_c_extra = 0;
3101 int saved_char_attr = 0;
3102
3103 int n_attr = 0; /* chars with special attr */
3104 int saved_attr2 = 0; /* char_attr saved for n_attr */
3105 int n_attr3 = 0; /* chars with overruling special attr */
3106 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3107
3108 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3109
3110 int fromcol, tocol; /* start/end of inverting */
3111 int fromcol_prev = -2; /* start of inverting after cursor */
3112 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003114 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 pos_T pos;
3116 long v;
3117
3118 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003119 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3121 in this line */
3122 int attr = 0; /* attributes for area highlighting */
3123 int area_attr = 0; /* attributes desired by highlighting */
3124 int search_attr = 0; /* attributes desired by 'hlsearch' */
3125#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003126 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 int syntax_attr = 0; /* attributes desired by syntax */
3128 int has_syntax = FALSE; /* this buffer has syntax highl. */
3129 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003130 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003131 int draw_color_col = FALSE; /* highlight colorcolumn */
3132 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003133#endif
3134#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003135 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003136# define SPWORDLEN 150
3137 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003138 int nextlinecol = 0; /* column where nextline[] starts */
3139 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003140 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003141 int spell_attr = 0; /* attributes desired by spelling */
3142 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003143 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3144 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003145 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003146 static int cap_col = -1; /* column to check for Cap word */
3147 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003148 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149#endif
3150 int extra_check; /* has syntax or linebreak */
3151#ifdef FEAT_MBYTE
3152 int multi_attr = 0; /* attributes desired by multibyte */
3153 int mb_l = 1; /* multi-byte byte length */
3154 int mb_c = 0; /* decoded multi-byte character */
3155 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003156 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157#endif
3158#ifdef FEAT_DIFF
3159 int filler_lines; /* nr of filler lines to be drawn */
3160 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003161 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 int change_start = MAXCOL; /* first col of changed area */
3163 int change_end = -1; /* last col of changed area */
3164#endif
3165 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3166#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003167 int need_showbreak = FALSE; /* overlong line, skipping first x
3168 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003170#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003171 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003173 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174#endif
3175#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003176 matchitem_T *cur; /* points to the match list */
3177 match_T *shl; /* points to search_hl or a match */
3178 int shl_flag; /* flag to indicate whether search_hl
3179 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003180 int pos_inprogress; /* marks that position match search is
3181 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003182 int prevcol_hl_flag; /* flag to indicate whether prevcol
3183 equals startcol of search_hl or one
3184 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185#endif
3186#ifdef FEAT_ARABIC
3187 int prev_c = 0; /* previous Arabic character */
3188 int prev_c1 = 0; /* first composing char for prev_c */
3189#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003190#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003191 int did_line_attr = 0;
3192#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003193#ifdef FEAT_TERMINAL
3194 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003195 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197
3198 /* draw_state: items that are drawn in sequence: */
3199#define WL_START 0 /* nothing done yet */
3200#ifdef FEAT_CMDWIN
3201# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3202#else
3203# define WL_CMDLINE WL_START
3204#endif
3205#ifdef FEAT_FOLDING
3206# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3207#else
3208# define WL_FOLD WL_CMDLINE
3209#endif
3210#ifdef FEAT_SIGNS
3211# define WL_SIGN WL_FOLD + 1 /* column for signs */
3212#else
3213# define WL_SIGN WL_FOLD /* column for signs */
3214#endif
3215#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003216#ifdef FEAT_LINEBREAK
3217# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003219# define WL_BRI WL_NR
3220#endif
3221#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3222# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3223#else
3224# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225#endif
3226#define WL_LINE WL_SBR + 1 /* text in the line */
3227 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003228#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 int feedback_col = 0;
3230 int feedback_old_attr = -1;
3231#endif
3232
Bram Moolenaar860cae12010-06-05 23:22:07 +02003233#ifdef FEAT_CONCEAL
3234 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003235 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003236 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003237 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003238 int is_concealing = FALSE;
3239 int boguscols = 0; /* nonexistent columns added to force
3240 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003241 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003242 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003243 int match_conc = 0; /* cchar for match functions */
3244 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003245 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003246# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003247# define FIX_FOR_BOGUSCOLS \
3248 { \
3249 n_extra += vcol_off; \
3250 vcol -= vcol_off; \
3251 vcol_off = 0; \
3252 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003253 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003254 boguscols = 0; \
3255 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003256#else
3257# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
3260 if (startrow > endrow) /* past the end already! */
3261 return startrow;
3262
3263 row = startrow;
3264 screen_row = row + W_WINROW(wp);
3265
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003266 if (!number_only)
3267 {
3268 /*
3269 * To speed up the loop below, set extra_check when there is linebreak,
3270 * trailing white space and/or syntax processing to be done.
3271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003273 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274#else
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003275 extra_check = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276#endif
3277#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003278 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003279# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003280 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003281# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003282 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003284 /* Prepare for syntax highlighting in this line. When there is an
3285 * error, stop syntax highlighting. */
3286 save_did_emsg = did_emsg;
3287 did_emsg = FALSE;
3288 syntax_start(wp, lnum);
3289 if (did_emsg)
3290 wp->w_s->b_syn_error = TRUE;
3291 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003292 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003293 did_emsg = save_did_emsg;
3294#ifdef SYN_TIME_LIMIT
3295 if (!wp->w_s->b_syn_slow)
3296#endif
3297 {
3298 has_syntax = TRUE;
3299 extra_check = TRUE;
3300 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003303
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003304 /* Check for columns to display for 'colorcolumn'. */
3305 color_cols = wp->w_p_cc_cols;
3306 if (color_cols != NULL)
3307 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003308#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003309
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003310#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003311 if (term_show_buffer(wp->w_buffer))
3312 {
3313 extra_check = TRUE;
3314 get_term_attr = TRUE;
3315 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3316 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003317#endif
3318
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003319#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003320 if (wp->w_p_spell
3321 && *wp->w_s->b_p_spl != NUL
3322 && wp->w_s->b_langp.ga_len > 0
3323 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003324 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003325 /* Prepare for spell checking. */
3326 has_spell = TRUE;
3327 extra_check = TRUE;
3328
3329 /* Get the start of the next line, so that words that wrap to the next
3330 * line are found too: "et<line-break>al.".
3331 * Trick: skip a few chars for C/shell/Vim comments */
3332 nextline[SPWORDLEN] = NUL;
3333 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3334 {
3335 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3336 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3337 }
3338
3339 /* When a word wrapped from the previous line the start of the current
3340 * line is valid. */
3341 if (lnum == checked_lnum)
3342 cur_checked_col = checked_col;
3343 checked_lnum = 0;
3344
3345 /* When there was a sentence end in the previous line may require a
3346 * word starting with capital in this line. In line 1 always check
3347 * the first word. */
3348 if (lnum != capcol_lnum)
3349 cap_col = -1;
3350 if (lnum == 1)
3351 cap_col = 0;
3352 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354#endif
3355
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003356 /*
3357 * handle visual active in this window
3358 */
3359 fromcol = -10;
3360 tocol = MAXCOL;
3361 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003363 /* Visual is after curwin->w_cursor */
3364 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003366 top = &curwin->w_cursor;
3367 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003369 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003371 top = &VIsual;
3372 bot = &curwin->w_cursor;
3373 }
3374 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3375 if (VIsual_mode == Ctrl_V) /* block mode */
3376 {
3377 if (lnum_in_visual_area)
3378 {
3379 fromcol = wp->w_old_cursor_fcol;
3380 tocol = wp->w_old_cursor_lcol;
3381 }
3382 }
3383 else /* non-block mode */
3384 {
3385 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003387 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003389 if (VIsual_mode == 'V') /* linewise */
3390 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 else
3392 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003393 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3394 if (gchar_pos(top) == NUL)
3395 tocol = fromcol + 1;
3396 }
3397 }
3398 if (VIsual_mode != 'V' && lnum == bot->lnum)
3399 {
3400 if (*p_sel == 'e' && bot->col == 0
3401#ifdef FEAT_VIRTUALEDIT
3402 && bot->coladd == 0
3403#endif
3404 )
3405 {
3406 fromcol = -10;
3407 tocol = MAXCOL;
3408 }
3409 else if (bot->col == MAXCOL)
3410 tocol = MAXCOL;
3411 else
3412 {
3413 pos = *bot;
3414 if (*p_sel == 'e')
3415 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3416 else
3417 {
3418 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3419 ++tocol;
3420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 }
3422 }
3423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003425 /* Check if the character under the cursor should not be inverted */
3426 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003427#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003428 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003429#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003430 )
3431 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003433 /* if inverting in this line set area_highlighting */
3434 if (fromcol >= 0)
3435 {
3436 area_highlighting = TRUE;
3437 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003439 if ((clip_star.available && !clip_star.owned
3440 && clip_isautosel_star())
3441 || (clip_plus.available && !clip_plus.owned
3442 && clip_isautosel_plus()))
3443 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003448 /*
3449 * handle 'incsearch' and ":s///c" highlighting
3450 */
3451 else if (highlight_match
3452 && wp == curwin
3453 && lnum >= curwin->w_cursor.lnum
3454 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003456 if (lnum == curwin->w_cursor.lnum)
3457 getvcol(curwin, &(curwin->w_cursor),
3458 (colnr_T *)&fromcol, NULL, NULL);
3459 else
3460 fromcol = 0;
3461 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3462 {
3463 pos.lnum = lnum;
3464 pos.col = search_match_endcol;
3465 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3466 }
3467 else
3468 tocol = MAXCOL;
3469 /* do at least one character; happens when past end of line */
3470 if (fromcol == tocol)
3471 tocol = fromcol + 1;
3472 area_highlighting = TRUE;
3473 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 }
3476
3477#ifdef FEAT_DIFF
3478 filler_lines = diff_check(wp, lnum);
3479 if (filler_lines < 0)
3480 {
3481 if (filler_lines == -1)
3482 {
3483 if (diff_find_change(wp, lnum, &change_start, &change_end))
3484 diff_hlf = HLF_ADD; /* added line */
3485 else if (change_start == 0)
3486 diff_hlf = HLF_TXD; /* changed text */
3487 else
3488 diff_hlf = HLF_CHD; /* changed line */
3489 }
3490 else
3491 diff_hlf = HLF_ADD; /* added line */
3492 filler_lines = 0;
3493 area_highlighting = TRUE;
3494 }
3495 if (lnum == wp->w_topline)
3496 filler_lines = wp->w_topfill;
3497 filler_todo = filler_lines;
3498#endif
3499
3500#ifdef LINE_ATTR
3501# ifdef FEAT_SIGNS
3502 /* If this line has a sign with line highlighting set line_attr. */
3503 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3504 if (v != 0)
3505 line_attr = sign_get_attr((int)v, TRUE);
3506# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003507# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003509 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003510 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511# endif
3512 if (line_attr != 0)
3513 area_highlighting = TRUE;
3514#endif
3515
3516 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3517 ptr = line;
3518
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003519#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003520 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003521 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003522 /* For checking first word with a capital skip white space. */
3523 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003524 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003525
Bram Moolenaar30abd282005-06-22 22:35:10 +00003526 /* To be able to spell-check over line boundaries copy the end of the
3527 * current line into nextline[]. Above the start of the next line was
3528 * copied to nextline[SPWORDLEN]. */
3529 if (nextline[SPWORDLEN] == NUL)
3530 {
3531 /* No next line or it is empty. */
3532 nextlinecol = MAXCOL;
3533 nextline_idx = 0;
3534 }
3535 else
3536 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003537 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003538 if (v < SPWORDLEN)
3539 {
3540 /* Short line, use it completely and append the start of the
3541 * next line. */
3542 nextlinecol = 0;
3543 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003544 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003545 nextline_idx = v + 1;
3546 }
3547 else
3548 {
3549 /* Long line, use only the last SPWORDLEN bytes. */
3550 nextlinecol = v - SPWORDLEN;
3551 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3552 nextline_idx = SPWORDLEN + 1;
3553 }
3554 }
3555 }
3556#endif
3557
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003558 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003560 if (lcs_space || lcs_trail)
3561 extra_check = TRUE;
3562 /* find start of trailing whitespace */
3563 if (lcs_trail)
3564 {
3565 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003566 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003567 --trailcol;
3568 trailcol += (colnr_T) (ptr - line);
3569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
3571
3572 /*
3573 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3574 * first character to be displayed.
3575 */
3576 if (wp->w_p_wrap)
3577 v = wp->w_skipcol;
3578 else
3579 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003580 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
3582#ifdef FEAT_MBYTE
3583 char_u *prev_ptr = ptr;
3584#endif
3585 while (vcol < v && *ptr != NUL)
3586 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003587 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 vcol += c;
3589#ifdef FEAT_MBYTE
3590 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003592 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 }
3594
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003595 /* When:
3596 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003597 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003598 * - 'virtualedit' is set, or
3599 * - the visual mode is active,
3600 * the end of the line may be before the start of the displayed part.
3601 */
3602 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003603#ifdef FEAT_SYN_HL
3604 wp->w_p_cuc || draw_color_col ||
3605#endif
3606#ifdef FEAT_VIRTUALEDIT
3607 virtual_active() ||
3608#endif
3609 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003610 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
3614 /* Handle a character that's not completely on the screen: Put ptr at
3615 * that character but skip the first few screen characters. */
3616 if (vcol > v)
3617 {
3618 vcol -= c;
3619#ifdef FEAT_MBYTE
3620 ptr = prev_ptr;
3621#else
3622 --ptr;
3623#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003624 /* If the character fits on the screen, don't need to skip it.
3625 * Except for a TAB. */
3626 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003627#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003628 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003629#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003630 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003631 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
3633
3634 /*
3635 * Adjust for when the inverted text is before the screen,
3636 * and when the start of the inverted text is before the screen.
3637 */
3638 if (tocol <= vcol)
3639 fromcol = 0;
3640 else if (fromcol >= 0 && fromcol < vcol)
3641 fromcol = vcol;
3642
3643#ifdef FEAT_LINEBREAK
3644 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3645 if (wp->w_p_wrap)
3646 need_showbreak = TRUE;
3647#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003648#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003649 /* When spell checking a word we need to figure out the start of the
3650 * word and if it's badly spelled or not. */
3651 if (has_spell)
3652 {
3653 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003654 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003655 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003656
3657 pos = wp->w_cursor;
3658 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003659 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003660 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003661
3662 /* spell_move_to() may call ml_get() and make "line" invalid */
3663 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3664 ptr = line + linecol;
3665
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003666 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003667 {
3668 /* no bad word found at line start, don't check until end of a
3669 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003670 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003671 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003672 }
3673 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003674 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003675 /* bad word found, use attributes until end of word */
3676 word_end = wp->w_cursor.col + len + 1;
3677
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003678 /* Turn index into actual attributes. */
3679 if (spell_hlf != HLF_COUNT)
3680 spell_attr = highlight_attr[spell_hlf];
3681 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003682 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003683
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003684# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003685 /* Need to restart syntax highlighting for this line. */
3686 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003687 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003688# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003689 }
3690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692
3693 /*
3694 * Correct highlighting for cursor that can't be disabled.
3695 * Avoids having to check this for each character.
3696 */
3697 if (fromcol >= 0)
3698 {
3699 if (noinvcur)
3700 {
3701 if ((colnr_T)fromcol == wp->w_virtcol)
3702 {
3703 /* highlighting starts at cursor, let it start just after the
3704 * cursor */
3705 fromcol_prev = fromcol;
3706 fromcol = -1;
3707 }
3708 else if ((colnr_T)fromcol < wp->w_virtcol)
3709 /* restart highlighting after the cursor */
3710 fromcol_prev = wp->w_virtcol;
3711 }
3712 if (fromcol >= tocol)
3713 fromcol = -1;
3714 }
3715
3716#ifdef FEAT_SEARCH_EXTRA
3717 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003718 * Handle highlighting the last used search pattern and matches.
3719 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003721 cur = wp->w_match_head;
3722 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003723 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003725 if (shl_flag == FALSE)
3726 {
3727 shl = &search_hl;
3728 shl_flag = TRUE;
3729 }
3730 else
3731 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003732 shl->startcol = MAXCOL;
3733 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003735 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003736 v = (long)(ptr - line);
3737 if (cur != NULL)
3738 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003739 next_search_hl(wp, shl, lnum, (colnr_T)v,
3740 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003741
3742 /* Need to get the line again, a multi-line regexp may have made it
3743 * invalid. */
3744 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3745 ptr = line + v;
3746
3747 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003749 if (shl->lnum == lnum)
3750 shl->startcol = shl->rm.startpos[0].col;
3751 else
3752 shl->startcol = 0;
3753 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3754 - shl->rm.startpos[0].lnum)
3755 shl->endcol = shl->rm.endpos[0].col;
3756 else
3757 shl->endcol = MAXCOL;
3758 /* Highlight one character for an empty match. */
3759 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003762 if (has_mbyte && line[shl->endcol] != NUL)
3763 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3764 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003766 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003768 if ((long)shl->startcol < v) /* match at leftcol */
3769 {
3770 shl->attr_cur = shl->attr;
3771 search_attr = shl->attr;
3772 }
3773 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003775 if (shl != &search_hl && cur != NULL)
3776 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778#endif
3779
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003780#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003781 /* Cursor line highlighting for 'cursorline' in the current window. Not
3782 * when Visual mode is active, because it's not clear what is selected
3783 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003784 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003785 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003786 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003787 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003788 area_highlighting = TRUE;
3789 }
3790#endif
3791
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003792 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 col = 0;
3794#ifdef FEAT_RIGHTLEFT
3795 if (wp->w_p_rl)
3796 {
3797 /* Rightleft window: process the text in the normal direction, but put
3798 * it in current_ScreenLine[] from right to left. Start at the
3799 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003800 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 off += col;
3802 }
3803#endif
3804
3805 /*
3806 * Repeat for the whole displayed line.
3807 */
3808 for (;;)
3809 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003810#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003811 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 /* Skip this quickly when working on the text. */
3814 if (draw_state != WL_LINE)
3815 {
3816#ifdef FEAT_CMDWIN
3817 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3818 {
3819 draw_state = WL_CMDLINE;
3820 if (cmdwin_type != 0 && wp == curwin)
3821 {
3822 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003824 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003825 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 }
3827 }
3828#endif
3829
3830#ifdef FEAT_FOLDING
3831 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3832 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003833 int fdc = compute_foldcolumn(wp, 0);
3834
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003836 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003838 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003839 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003840 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003841 p_extra_free = alloc(12 + 1);
3842
3843 if (p_extra_free != NULL)
3844 {
3845 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3846 n_extra = fdc;
3847 p_extra_free[n_extra] = NUL;
3848 p_extra = p_extra_free;
3849 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003850 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 }
3853 }
3854#endif
3855
3856#ifdef FEAT_SIGNS
3857 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3858 {
3859 draw_state = WL_SIGN;
3860 /* Show the sign column when there are any signs in this
3861 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003862 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003864 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003866 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867# endif
3868
3869 /* Draw two cells with the sign value or blank. */
3870 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003871 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 n_extra = 2;
3873
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003874 if (row == startrow
3875#ifdef FEAT_DIFF
3876 + filler_lines && filler_todo <= 0
3877#endif
3878 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 {
3880 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3881 SIGN_TEXT);
3882# ifdef FEAT_SIGN_ICONS
3883 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3884 SIGN_ICON);
3885 if (gui.in_use && icon_sign != 0)
3886 {
3887 /* Use the image in this position. */
3888 c_extra = SIGN_BYTE;
3889# ifdef FEAT_NETBEANS_INTG
3890 if (buf_signcount(wp->w_buffer, lnum) > 1)
3891 c_extra = MULTISIGN_BYTE;
3892# endif
3893 char_attr = icon_sign;
3894 }
3895 else
3896# endif
3897 if (text_sign != 0)
3898 {
3899 p_extra = sign_get_text(text_sign);
3900 if (p_extra != NULL)
3901 {
3902 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003903 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 }
3905 char_attr = sign_get_attr(text_sign, FALSE);
3906 }
3907 }
3908 }
3909 }
3910#endif
3911
3912 if (draw_state == WL_NR - 1 && n_extra == 0)
3913 {
3914 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003915 /* Display the absolute or relative line number. After the
3916 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3917 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 && (row == startrow
3919#ifdef FEAT_DIFF
3920 + filler_lines
3921#endif
3922 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3923 {
3924 /* Draw the line number (empty space after wrapping). */
3925 if (row == startrow
3926#ifdef FEAT_DIFF
3927 + filler_lines
3928#endif
3929 )
3930 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003931 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003932 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003933
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003934 if (wp->w_p_nu && !wp->w_p_rnu)
3935 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003936 num = (long)lnum;
3937 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003938 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003939 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003940 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003941 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003942 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003943 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003944 num = lnum;
3945 fmt = "%-*ld ";
3946 }
3947 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003948
Bram Moolenaar700e7342013-01-30 12:31:36 +01003949 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003950 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 if (wp->w_skipcol > 0)
3952 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3953 *p_extra = '-';
3954#ifdef FEAT_RIGHTLEFT
3955 if (wp->w_p_rl) /* reverse line numbers */
3956 rl_mirror(extra);
3957#endif
3958 p_extra = extra;
3959 c_extra = NUL;
3960 }
3961 else
3962 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003963 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003964 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003965#ifdef FEAT_SYN_HL
3966 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003967 * the current line differently.
3968 * TODO: Can we use CursorLine instead of CursorLineNr
3969 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003970 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003971 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003972 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975 }
3976
Bram Moolenaar597a4222014-06-25 14:39:50 +02003977#ifdef FEAT_LINEBREAK
3978 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3979 && n_extra == 0 && *p_sbr != NUL)
3980 /* draw indent after showbreak value */
3981 draw_state = WL_BRI;
3982 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3983 /* After the showbreak, draw the breakindent */
3984 draw_state = WL_BRI - 1;
3985
3986 /* draw 'breakindent': indent wrapped text accordingly */
3987 if (draw_state == WL_BRI - 1 && n_extra == 0)
3988 {
3989 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003990 /* if need_showbreak is set, breakindent also applies */
3991 if (wp->w_p_bri && n_extra == 0
3992 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003993# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003994 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003995# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003996 )
3997 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003998 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003999# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004000 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004001 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004002 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004003# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004004 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4005 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004006 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004007# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004008 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004009# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004010 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004011 c_extra = ' ';
4012 n_extra = get_breakindent_win(wp,
4013 ml_get_buf(wp->w_buffer, lnum, FALSE));
4014 /* Correct end of highlighted area for 'breakindent',
4015 * required when 'linebreak' is also set. */
4016 if (tocol == vcol)
4017 tocol += n_extra;
4018 }
4019 }
4020#endif
4021
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4023 if (draw_state == WL_SBR - 1 && n_extra == 0)
4024 {
4025 draw_state = WL_SBR;
4026# ifdef FEAT_DIFF
4027 if (filler_todo > 0)
4028 {
4029 /* Draw "deleted" diff line(s). */
4030 if (char2cells(fill_diff) > 1)
4031 c_extra = '-';
4032 else
4033 c_extra = fill_diff;
4034# ifdef FEAT_RIGHTLEFT
4035 if (wp->w_p_rl)
4036 n_extra = col + 1;
4037 else
4038# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004039 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004040 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 }
4042# endif
4043# ifdef FEAT_LINEBREAK
4044 if (*p_sbr != NUL && need_showbreak)
4045 {
4046 /* Draw 'showbreak' at the start of each broken line. */
4047 p_extra = p_sbr;
4048 c_extra = NUL;
4049 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004050 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004052 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 /* Correct end of highlighted area for 'showbreak',
4054 * required when 'linebreak' is also set. */
4055 if (tocol == vcol)
4056 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004057#ifdef FEAT_SYN_HL
4058 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004059 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004060 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004061 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
4064# endif
4065 }
4066#endif
4067
4068 if (draw_state == WL_LINE - 1 && n_extra == 0)
4069 {
4070 draw_state = WL_LINE;
4071 if (saved_n_extra)
4072 {
4073 /* Continue item from end of wrapped line. */
4074 n_extra = saved_n_extra;
4075 c_extra = saved_c_extra;
4076 p_extra = saved_p_extra;
4077 char_attr = saved_char_attr;
4078 }
4079 else
4080 char_attr = 0;
4081 }
4082 }
4083
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004084 // When still displaying '$' of change command, stop at cursor.
4085 // When only displaying the (relative) line number and that's done,
4086 // stop here.
4087 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004088 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089#ifdef FEAT_DIFF
4090 && filler_todo <= 0
4091#endif
4092 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004093 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004095 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004096 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004097 /* Pretend we have finished updating the window. Except when
4098 * 'cursorcolumn' is set. */
4099#ifdef FEAT_SYN_HL
4100 if (wp->w_p_cuc)
4101 row = wp->w_cline_row + wp->w_cline_height;
4102 else
4103#endif
4104 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 break;
4106 }
4107
4108 if (draw_state == WL_LINE && area_highlighting)
4109 {
4110 /* handle Visual or match highlighting in this line */
4111 if (vcol == fromcol
4112#ifdef FEAT_MBYTE
4113 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4114 && (*mb_ptr2cells)(ptr) > 1)
4115#endif
4116 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004117 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 && vcol < tocol))
4119 area_attr = attr; /* start highlighting */
4120 else if (area_attr != 0
4121 && (vcol == tocol
4122 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124
4125#ifdef FEAT_SEARCH_EXTRA
4126 if (!n_extra)
4127 {
4128 /*
4129 * Check for start/end of search pattern match.
4130 * After end, check for start/end of next match.
4131 * When another match, have to check for start again.
4132 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004133 * Do this for 'search_hl' and the match list (ordered by
4134 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004136 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004137 cur = wp->w_match_head;
4138 shl_flag = FALSE;
4139 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004141 if (shl_flag == FALSE
4142 && ((cur != NULL
4143 && cur->priority > SEARCH_HL_PRIORITY)
4144 || cur == NULL))
4145 {
4146 shl = &search_hl;
4147 shl_flag = TRUE;
4148 }
4149 else
4150 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004151 if (cur != NULL)
4152 cur->pos.cur = 0;
4153 pos_inprogress = TRUE;
4154 while (shl->rm.regprog != NULL
4155 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004157 if (shl->startcol != MAXCOL
4158 && v >= (long)shl->startcol
4159 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004161#ifdef FEAT_MBYTE
4162 int tmp_col = v + MB_PTR2LEN(ptr);
4163
4164 if (shl->endcol < tmp_col)
4165 shl->endcol = tmp_col;
4166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004168#ifdef FEAT_CONCEAL
4169 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4170 == cur->hlg_id)
4171 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004172 has_match_conc =
4173 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004174 match_conc = cur->conceal_char;
4175 }
4176 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004177 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004180 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 {
4182 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004183 next_search_hl(wp, shl, lnum, (colnr_T)v,
4184 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004185 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004186 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
4188 /* Need to get the line again, a multi-line regexp
4189 * may have made it invalid. */
4190 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4191 ptr = line + v;
4192
4193 if (shl->lnum == lnum)
4194 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004195 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004197 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004199 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004201 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 {
4203 /* highlight empty match, try again after
4204 * it */
4205#ifdef FEAT_MBYTE
4206 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004207 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004208 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 else
4210#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004211 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 }
4213
4214 /* Loop to check if the match starts at the
4215 * current position */
4216 continue;
4217 }
4218 }
4219 break;
4220 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004221 if (shl != &search_hl && cur != NULL)
4222 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004224
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004225 /* Use attributes from match with highest priority among
4226 * 'search_hl' and the match list. */
4227 search_attr = search_hl.attr_cur;
4228 cur = wp->w_match_head;
4229 shl_flag = FALSE;
4230 while (cur != NULL || shl_flag == FALSE)
4231 {
4232 if (shl_flag == FALSE
4233 && ((cur != NULL
4234 && cur->priority > SEARCH_HL_PRIORITY)
4235 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004236 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004237 shl = &search_hl;
4238 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004239 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004240 else
4241 shl = &cur->hl;
4242 if (shl->attr_cur != 0)
4243 search_attr = shl->attr_cur;
4244 if (shl != &search_hl && cur != NULL)
4245 cur = cur->next;
4246 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004247 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004248 if (*ptr == NUL && (did_line_attr >= 1
4249 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004250 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
4252#endif
4253
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004255 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004257 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4258 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004260 if (diff_hlf == HLF_TXD && ptr - line > change_end
4261 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004263 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004264 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004265 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004268
4269 /* Decide which of the highlight attributes to use. */
4270 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004271#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004272 if (area_attr != 0)
4273 char_attr = hl_combine_attr(line_attr, area_attr);
4274 else if (search_attr != 0)
4275 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004276 /* Use line_attr when not in the Visual or 'incsearch' area
4277 * (area_attr may be 0 when "noinvcur" is set). */
4278 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004279 || vcol < fromcol || vcol_prev < fromcol_prev
4280 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004281 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004282#else
4283 if (area_attr != 0)
4284 char_attr = area_attr;
4285 else if (search_attr != 0)
4286 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004287#endif
4288 else
4289 {
4290 attr_pri = FALSE;
4291#ifdef FEAT_SYN_HL
4292 if (has_syntax)
4293 char_attr = syntax_attr;
4294 else
4295#endif
4296 char_attr = 0;
4297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299
4300 /*
4301 * Get the next character to put on the screen.
4302 */
4303 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004304 * The "p_extra" points to the extra stuff that is inserted to
4305 * represent special characters (non-printable stuff) and other
4306 * things. When all characters are the same, c_extra is used.
4307 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4308 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4310 */
4311 if (n_extra > 0)
4312 {
4313 if (c_extra != NUL)
4314 {
4315 c = c_extra;
4316#ifdef FEAT_MBYTE
4317 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004318 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
4320 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004321 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004322 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
4324 else
4325 mb_utf8 = FALSE;
4326#endif
4327 }
4328 else
4329 {
4330 c = *p_extra;
4331#ifdef FEAT_MBYTE
4332 if (has_mbyte)
4333 {
4334 mb_c = c;
4335 if (enc_utf8)
4336 {
4337 /* If the UTF-8 character is more than one byte:
4338 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004339 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 mb_utf8 = FALSE;
4341 if (mb_l > n_extra)
4342 mb_l = 1;
4343 else if (mb_l > 1)
4344 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004345 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004347 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
4349 }
4350 else
4351 {
4352 /* if this is a DBCS character, put it in "mb_c" */
4353 mb_l = MB_BYTE2LEN(c);
4354 if (mb_l >= n_extra)
4355 mb_l = 1;
4356 else if (mb_l > 1)
4357 mb_c = (c << 8) + p_extra[1];
4358 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004359 if (mb_l == 0) /* at the NUL at end-of-line */
4360 mb_l = 1;
4361
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 /* If a double-width char doesn't fit display a '>' in the
4363 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004364 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365# ifdef FEAT_RIGHTLEFT
4366 wp->w_p_rl ? (col <= 0) :
4367# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004368 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 && (*mb_char2cells)(mb_c) == 2)
4370 {
4371 c = '>';
4372 mb_c = c;
4373 mb_l = 1;
4374 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004375 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 /* put the pointer back to output the double-width
4377 * character at the start of the next line. */
4378 ++n_extra;
4379 --p_extra;
4380 }
4381 else
4382 {
4383 n_extra -= mb_l - 1;
4384 p_extra += mb_l - 1;
4385 }
4386 }
4387#endif
4388 ++p_extra;
4389 }
4390 --n_extra;
4391 }
4392 else
4393 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004394#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004395 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004396#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004397
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004398 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004399 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 /*
4401 * Get a character from the line itself.
4402 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004403 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004404#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004405 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407#ifdef FEAT_MBYTE
4408 if (has_mbyte)
4409 {
4410 mb_c = c;
4411 if (enc_utf8)
4412 {
4413 /* If the UTF-8 character is more than one byte: Decode it
4414 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004415 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 mb_utf8 = FALSE;
4417 if (mb_l > 1)
4418 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004419 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 /* Overlong encoded ASCII or ASCII with composing char
4421 * is displayed normally, except a NUL. */
4422 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004423 {
4424 c = mb_c;
4425# ifdef FEAT_LINEBREAK
4426 c0 = mb_c;
4427# endif
4428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004430
4431 /* At start of the line we can have a composing char.
4432 * Draw it as a space with a composing char. */
4433 if (utf_iscomposing(mb_c))
4434 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004435 int i;
4436
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004437 for (i = Screen_mco - 1; i > 0; --i)
4438 u8cc[i] = u8cc[i - 1];
4439 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004440 mb_c = ' ';
4441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443
4444 if ((mb_l == 1 && c >= 0x80)
4445 || (mb_l >= 1 && mb_c == 0)
4446 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004447# ifdef UNICODE16
4448 || mb_c >= 0x10000
4449# endif
4450 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 {
4452 /*
4453 * Illegal UTF-8 byte: display as <xx>.
4454 * Non-BMP character : display as ? or fullwidth ?.
4455 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004456# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004458# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 {
4460 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004461# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 if (wp->w_p_rl) /* reverse */
4463 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004464# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004466# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 else if (utf_char2cells(mb_c) != 2)
4468 STRCPY(extra, "?");
4469 else
4470 /* 0xff1f in UTF-8: full-width '?' */
4471 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
4474 p_extra = extra;
4475 c = *p_extra;
4476 mb_c = mb_ptr2char_adv(&p_extra);
4477 mb_utf8 = (c >= 0x80);
4478 n_extra = (int)STRLEN(p_extra);
4479 c_extra = NUL;
4480 if (area_attr == 0 && search_attr == 0)
4481 {
4482 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004483 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 saved_attr2 = char_attr; /* save current attr */
4485 }
4486 }
4487 else if (mb_l == 0) /* at the NUL at end-of-line */
4488 mb_l = 1;
4489#ifdef FEAT_ARABIC
4490 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4491 {
4492 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004493 int pc, pc1, nc;
4494 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495
4496 /* The idea of what is the previous and next
4497 * character depends on 'rightleft'. */
4498 if (wp->w_p_rl)
4499 {
4500 pc = prev_c;
4501 pc1 = prev_c1;
4502 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004503 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
4505 else
4506 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004507 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004509 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 }
4511 prev_c = mb_c;
4512
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004513 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 }
4515 else
4516 prev_c = mb_c;
4517#endif
4518 }
4519 else /* enc_dbcs */
4520 {
4521 mb_l = MB_BYTE2LEN(c);
4522 if (mb_l == 0) /* at the NUL at end-of-line */
4523 mb_l = 1;
4524 else if (mb_l > 1)
4525 {
4526 /* We assume a second byte below 32 is illegal.
4527 * Hopefully this is OK for all double-byte encodings!
4528 */
4529 if (ptr[1] >= 32)
4530 mb_c = (c << 8) + ptr[1];
4531 else
4532 {
4533 if (ptr[1] == NUL)
4534 {
4535 /* head byte at end of line */
4536 mb_l = 1;
4537 transchar_nonprint(extra, c);
4538 }
4539 else
4540 {
4541 /* illegal tail byte */
4542 mb_l = 2;
4543 STRCPY(extra, "XX");
4544 }
4545 p_extra = extra;
4546 n_extra = (int)STRLEN(extra) - 1;
4547 c_extra = NUL;
4548 c = *p_extra++;
4549 if (area_attr == 0 && search_attr == 0)
4550 {
4551 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004552 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 saved_attr2 = char_attr; /* save current attr */
4554 }
4555 mb_c = c;
4556 }
4557 }
4558 }
4559 /* If a double-width char doesn't fit display a '>' in the
4560 * last column; the character is displayed at the start of the
4561 * next line. */
4562 if ((
4563# ifdef FEAT_RIGHTLEFT
4564 wp->w_p_rl ? (col <= 0) :
4565# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004566 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 && (*mb_char2cells)(mb_c) == 2)
4568 {
4569 c = '>';
4570 mb_c = c;
4571 mb_utf8 = FALSE;
4572 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004573 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 /* Put pointer back so that the character will be
4575 * displayed at the start of the next line. */
4576 --ptr;
4577 }
4578 else if (*ptr != NUL)
4579 ptr += mb_l - 1;
4580
4581 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004582 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004583 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004584 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004587 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 c = ' ';
4589 if (area_attr == 0 && search_attr == 0)
4590 {
4591 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004592 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 saved_attr2 = char_attr; /* save current attr */
4594 }
4595 mb_c = c;
4596 mb_utf8 = FALSE;
4597 mb_l = 1;
4598 }
4599
4600 }
4601#endif
4602 ++ptr;
4603
4604 if (extra_check)
4605 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004606#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004607 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004608#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004609
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004610#ifdef FEAT_TERMINAL
4611 if (get_term_attr)
4612 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004613 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004614
4615 if (!attr_pri)
4616 char_attr = syntax_attr;
4617 else
4618 char_attr = hl_combine_attr(syntax_attr, char_attr);
4619 }
4620#endif
4621
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004622#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 /* Get syntax attribute, unless still at the start of the line
4624 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004625 v = (long)(ptr - line);
4626 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
4628 /* Get the syntax attribute for the character. If there
4629 * is an error, disable syntax highlighting. */
4630 save_did_emsg = did_emsg;
4631 did_emsg = FALSE;
4632
Bram Moolenaar217ad922005-03-20 22:37:15 +00004633 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004634# ifdef FEAT_SPELL
4635 has_spell ? &can_spell :
4636# endif
4637 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638
4639 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004640 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004641 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004642 has_syntax = FALSE;
4643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 else
4645 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004646#ifdef SYN_TIME_LIMIT
4647 if (wp->w_s->b_syn_slow)
4648 has_syntax = FALSE;
4649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650
4651 /* Need to get the line again, a multi-line regexp may
4652 * have made it invalid. */
4653 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4654 ptr = line + v;
4655
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004656 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004658 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004659 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004660# ifdef FEAT_CONCEAL
4661 /* no concealing past the end of the line, it interferes
4662 * with line highlighting */
4663 if (c == NUL)
4664 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004665 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004666 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004667# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004669#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004670
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004671#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004672 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004673 * Only do this when there is no syntax highlighting, the
4674 * @Spell cluster is not used or the current syntax item
4675 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004676 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004677 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004678 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004679# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004680 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004681 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004682# endif
4683 if (c != 0 && (
4684# ifdef FEAT_SYN_HL
4685 !has_syntax ||
4686# endif
4687 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004688 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004689 char_u *prev_ptr, *p;
4690 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004691 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004692# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004693 if (has_mbyte)
4694 {
4695 prev_ptr = ptr - mb_l;
4696 v -= mb_l - 1;
4697 }
4698 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004699# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004700 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004701
4702 /* Use nextline[] if possible, it has the start of the
4703 * next line concatenated. */
4704 if ((prev_ptr - line) - nextlinecol >= 0)
4705 p = nextline + (prev_ptr - line) - nextlinecol;
4706 else
4707 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004708 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004709 len = spell_check(wp, p, &spell_hlf, &cap_col,
4710 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004711 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004712
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004713 /* In Insert mode only highlight a word that
4714 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004715 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004716 && (State & INSERT) != 0
4717 && wp->w_cursor.lnum == lnum
4718 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004719 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004720 && wp->w_cursor.col < (colnr_T)word_end)
4721 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004722 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004723 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004724 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004725
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004726 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004727 && (p - nextline) + len > nextline_idx)
4728 {
4729 /* Remember that the good word continues at the
4730 * start of the next line. */
4731 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004732 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004733 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004734
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004735 /* Turn index into actual attributes. */
4736 if (spell_hlf != HLF_COUNT)
4737 spell_attr = highlight_attr[spell_hlf];
4738
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004739 if (cap_col > 0)
4740 {
4741 if (p != prev_ptr
4742 && (p - nextline) + cap_col >= nextline_idx)
4743 {
4744 /* Remember that the word in the next line
4745 * must start with a capital. */
4746 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004747 cap_col = (int)((p - nextline) + cap_col
4748 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004749 }
4750 else
4751 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004752 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004753 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004754 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004755 }
4756 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004757 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004758 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004759 char_attr = hl_combine_attr(char_attr, spell_attr);
4760 else
4761 char_attr = hl_combine_attr(spell_attr, char_attr);
4762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763#endif
4764#ifdef FEAT_LINEBREAK
4765 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004766 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004768 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004769 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004771# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004772 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004773# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004774 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004776 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004778 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004779
Bram Moolenaar597a4222014-06-25 14:39:50 +02004780 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004781 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004782 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004783 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004784# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004785 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004786 wp->w_buffer->b_p_vts_array) - 1;
4787# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004788 n_extra = (int)wp->w_buffer->b_p_ts
4789 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004790# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004791
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004792# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004793 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004794# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004796# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004797 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004798 {
4799#ifdef FEAT_CONCEAL
4800 if (c == TAB)
4801 /* See "Tab alignment" below. */
4802 FIX_FOR_BOGUSCOLS;
4803#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004804 if (!wp->w_p_list)
4805 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 }
4808#endif
4809
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004810 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4811 */
4812 if (wp->w_p_list
4813 && (((c == 160
4814#ifdef FEAT_MBYTE
4815 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4816#endif
4817 ) && lcs_nbsp)
4818 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4819 {
4820 c = (c == ' ') ? lcs_space : lcs_nbsp;
4821 if (area_attr == 0 && search_attr == 0)
4822 {
4823 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004824 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004825 saved_attr2 = char_attr; /* save current attr */
4826 }
4827#ifdef FEAT_MBYTE
4828 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004829 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004830 {
4831 mb_utf8 = TRUE;
4832 u8cc[0] = 0;
4833 c = 0xc0;
4834 }
4835 else
4836 mb_utf8 = FALSE;
4837#endif
4838 }
4839
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4841 {
4842 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004843 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 {
4845 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004846 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 saved_attr2 = char_attr; /* save current attr */
4848 }
4849#ifdef FEAT_MBYTE
4850 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004851 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 {
4853 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004854 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004855 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 }
4857 else
4858 mb_utf8 = FALSE;
4859#endif
4860 }
4861 }
4862
4863 /*
4864 * Handling of non-printable characters.
4865 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004866 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 {
4868 /*
4869 * when getting a character from the file, we may have to
4870 * turn it into something else on the way to putting it
4871 * into "ScreenLines".
4872 */
4873 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4874 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004875 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004876 long vcol_adjusted = vcol; /* removed showbreak length */
4877#ifdef FEAT_LINEBREAK
4878 /* only adjust the tab_len, when at the first column
4879 * after the showbreak value was drawn */
4880 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4881 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004884#ifdef FEAT_VARTABS
4885 tab_len = tabstop_padding(vcol_adjusted,
4886 wp->w_buffer->b_p_ts,
4887 wp->w_buffer->b_p_vts_array) - 1;
4888#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004889 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004890 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4891#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004892
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004893#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004894 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004895#endif
4896 /* tab amount depends on current column */
4897 n_extra = tab_len;
4898#ifdef FEAT_LINEBREAK
4899 else
4900 {
4901 char_u *p;
4902 int len = n_extra;
4903 int i;
4904 int saved_nextra = n_extra;
4905
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004906#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004907 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004908 /* there are characters to conceal */
4909 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004910 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4911 */
4912 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4913 && n_extra > tab_len)
4914 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004915#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004916
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004917 /* if n_extra > 0, it gives the number of chars, to
4918 * use for a tab, else we need to calculate the width
4919 * for a tab */
4920#ifdef FEAT_MBYTE
4921 len = (tab_len * mb_char2len(lcs_tab2));
4922 if (n_extra > 0)
4923 len += n_extra - tab_len;
4924#endif
4925 c = lcs_tab1;
4926 p = alloc((unsigned)(len + 1));
4927 vim_memset(p, ' ', len);
4928 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004929 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004930 p_extra_free = p;
4931 for (i = 0; i < tab_len; i++)
4932 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004933 if (*p == NUL)
4934 {
4935 tab_len = i;
4936 break;
4937 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004938#ifdef FEAT_MBYTE
4939 mb_char2bytes(lcs_tab2, p);
4940 p += mb_char2len(lcs_tab2);
4941 n_extra += mb_char2len(lcs_tab2)
4942 - (saved_nextra > 0 ? 1 : 0);
4943#else
4944 p[i] = lcs_tab2;
4945#endif
4946 }
4947 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004948#ifdef FEAT_CONCEAL
4949 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4950 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004951 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004952 n_extra -= vcol_off;
4953#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004954 }
4955#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004956#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004957 {
4958 int vc_saved = vcol_off;
4959
4960 /* Tab alignment should be identical regardless of
4961 * 'conceallevel' value. So tab compensates of all
4962 * previous concealed characters, and thus resets
4963 * vcol_off and boguscols accumulated so far in the
4964 * line. Note that the tab can be longer than
4965 * 'tabstop' when there are concealed characters. */
4966 FIX_FOR_BOGUSCOLS;
4967
4968 /* Make sure, the highlighting for the tab char will be
4969 * correctly set further below (effectively reverts the
4970 * FIX_FOR_BOGSUCOLS macro */
4971 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004972 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004973 tab_len += vc_saved;
4974 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976#ifdef FEAT_MBYTE
4977 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4978#endif
4979 if (wp->w_p_list)
4980 {
4981 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004982#ifdef FEAT_LINEBREAK
4983 if (wp->w_p_lbr)
4984 c_extra = NUL; /* using p_extra from above */
4985 else
4986#endif
4987 c_extra = lcs_tab2;
4988 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004989 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 saved_attr2 = char_attr; /* save current attr */
4991#ifdef FEAT_MBYTE
4992 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004993 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {
4995 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004996 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004997 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 }
4999#endif
5000 }
5001 else
5002 {
5003 c_extra = ' ';
5004 c = ' ';
5005 }
5006 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005007 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005008 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005009 || ((fromcol >= 0 || fromcol_prev >= 0)
5010 && tocol > vcol
5011 && VIsual_mode != Ctrl_V
5012 && (
5013# ifdef FEAT_RIGHTLEFT
5014 wp->w_p_rl ? (col >= 0) :
5015# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005016 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005017 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005018 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005019 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005020 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005022 /* Display a '$' after the line or highlight an extra
5023 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5025 /* For a diff line the highlighting continues after the
5026 * "$". */
5027 if (
5028# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005029 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030# ifdef LINE_ATTR
5031 &&
5032# endif
5033# endif
5034# ifdef LINE_ATTR
5035 line_attr == 0
5036# endif
5037 )
5038#endif
5039 {
5040#ifdef FEAT_VIRTUALEDIT
5041 /* In virtualedit, visual selections may extend
5042 * beyond end of line. */
5043 if (area_highlighting && virtual_active()
5044 && tocol != MAXCOL && vcol < tocol)
5045 n_extra = 0;
5046 else
5047#endif
5048 {
5049 p_extra = at_end_str;
5050 n_extra = 1;
5051 c_extra = NUL;
5052 }
5053 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005054 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005055 c = lcs_eol;
5056 else
5057 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 lcs_eol_one = -1;
5059 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005060 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005062 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 n_attr = 1;
5064 }
5065#ifdef FEAT_MBYTE
5066 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005067 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 {
5069 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005070 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005071 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
5073 else
5074 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5075#endif
5076 }
5077 else if (c != NUL)
5078 {
5079 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005080 if (n_extra == 0)
5081 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082#ifdef FEAT_RIGHTLEFT
5083 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5084 rl_mirror(p_extra); /* reverse "<12>" */
5085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005087#ifdef FEAT_LINEBREAK
5088 if (wp->w_p_lbr)
5089 {
5090 char_u *p;
5091
5092 c = *p_extra;
5093 p = alloc((unsigned)n_extra + 1);
5094 vim_memset(p, ' ', n_extra);
5095 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5096 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005097 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005098 p_extra_free = p_extra = p;
5099 }
5100 else
5101#endif
5102 {
5103 n_extra = byte2cells(c) - 1;
5104 c = *p_extra++;
5105 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005106 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
5108 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005109 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 saved_attr2 = char_attr; /* save current attr */
5111 }
5112#ifdef FEAT_MBYTE
5113 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5114#endif
5115 }
5116#ifdef FEAT_VIRTUALEDIT
5117 else if (VIsual_active
5118 && (VIsual_mode == Ctrl_V
5119 || VIsual_mode == 'v')
5120 && virtual_active()
5121 && tocol != MAXCOL
5122 && vcol < tocol
5123 && (
5124# ifdef FEAT_RIGHTLEFT
5125 wp->w_p_rl ? (col >= 0) :
5126# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005127 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
5129 c = ' ';
5130 --ptr; /* put it back at the NUL */
5131 }
5132#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005133#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 else if ((
5135# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005136 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005138# ifdef FEAT_TERMINAL
5139 term_attr != 0 ||
5140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 ) && (
5143# ifdef FEAT_RIGHTLEFT
5144 wp->w_p_rl ? (col >= 0) :
5145# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005146 (col
5147# ifdef FEAT_CONCEAL
5148 - boguscols
5149# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005150 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 {
5152 /* Highlight until the right side of the window */
5153 c = ' ';
5154 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005155
5156 /* Remember we do the char for line highlighting. */
5157 ++did_line_attr;
5158
5159 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005160 if (line_attr != 0 && char_attr == search_attr
5161 && (did_line_attr > 1
5162 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005163 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164# ifdef FEAT_DIFF
5165 if (diff_hlf == HLF_TXD)
5166 {
5167 diff_hlf = HLF_CHD;
5168 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005169 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005170 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005171 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5172 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005173 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
5176# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005177# ifdef FEAT_TERMINAL
5178 if (term_attr != 0)
5179 {
5180 char_attr = term_attr;
5181 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5182 char_attr = hl_combine_attr(char_attr,
5183 HL_ATTR(HLF_CUL));
5184 }
5185# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
5187#endif
5188 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005189
5190#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005191 if ( wp->w_p_cole > 0
5192 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005193 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005194 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005195 && !(lnum_in_visual_area
5196 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005197 {
5198 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005199 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005200 && (syn_get_sub_char() != NUL || match_conc
5201 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005202 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005203 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005204 /* First time at this concealed item: display one
5205 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005206 if (match_conc)
5207 c = match_conc;
5208 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005209 c = syn_get_sub_char();
5210 else if (lcs_conceal != NUL)
5211 c = lcs_conceal;
5212 else
5213 c = ' ';
5214
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005215 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005216
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005217 if (n_extra > 0)
5218 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005219 vcol += n_extra;
5220 if (wp->w_p_wrap && n_extra > 0)
5221 {
5222# ifdef FEAT_RIGHTLEFT
5223 if (wp->w_p_rl)
5224 {
5225 col -= n_extra;
5226 boguscols -= n_extra;
5227 }
5228 else
5229# endif
5230 {
5231 boguscols += n_extra;
5232 col += n_extra;
5233 }
5234 }
5235 n_extra = 0;
5236 n_attr = 0;
5237 }
5238 else if (n_skip == 0)
5239 {
5240 is_concealing = TRUE;
5241 n_skip = 1;
5242 }
5243# ifdef FEAT_MBYTE
5244 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005245 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005246 {
5247 mb_utf8 = TRUE;
5248 u8cc[0] = 0;
5249 c = 0xc0;
5250 }
5251 else
5252 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5253# endif
5254 }
5255 else
5256 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005257 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005258 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005259 }
5260#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 }
5262
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005263#ifdef FEAT_CONCEAL
5264 /* In the cursor line and we may be concealing characters: correct
5265 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005266 if (!did_wcol && draw_state == WL_LINE
5267 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005268 && conceal_cursor_line(wp)
5269 && (int)wp->w_virtcol <= vcol + n_skip)
5270 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005271# ifdef FEAT_RIGHTLEFT
5272 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005273 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005274 else
5275# endif
5276 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005277 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005278 did_wcol = TRUE;
5279 }
5280#endif
5281
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 /* Don't override visual selection highlighting. */
5283 if (n_attr > 0
5284 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005285 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 char_attr = extra_attr;
5287
Bram Moolenaar81695252004-12-29 20:58:21 +00005288#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 /* XIM don't send preedit_start and preedit_end, but they send
5290 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5291 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005292 if (p_imst == IM_ON_THE_SPOT
5293 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005294 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 && (State & INSERT)
5296 && !p_imdisable
5297 && im_is_preediting()
5298 && draw_state == WL_LINE)
5299 {
5300 colnr_T tcol;
5301
5302 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005303 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 else
5305 tcol = preedit_end_col;
5306 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5307 {
5308 if (feedback_old_attr < 0)
5309 {
5310 feedback_col = 0;
5311 feedback_old_attr = char_attr;
5312 }
5313 char_attr = im_get_feedback_attr(feedback_col);
5314 if (char_attr < 0)
5315 char_attr = feedback_old_attr;
5316 feedback_col++;
5317 }
5318 else if (feedback_old_attr >= 0)
5319 {
5320 char_attr = feedback_old_attr;
5321 feedback_old_attr = -1;
5322 feedback_col = 0;
5323 }
5324 }
5325#endif
5326 /*
5327 * Handle the case where we are in column 0 but not on the first
5328 * character of the line and the user wants us to show us a
5329 * special character (via 'listchars' option "precedes:<char>".
5330 */
5331 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005332 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5334#ifdef FEAT_DIFF
5335 && filler_todo <= 0
5336#endif
5337 && draw_state > WL_NR
5338 && c != NUL)
5339 {
5340 c = lcs_prec;
5341 lcs_prec_todo = NUL;
5342#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005343 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5344 {
5345 /* Double-width character being overwritten by the "precedes"
5346 * character, need to fill up half the character. */
5347 c_extra = MB_FILLER_CHAR;
5348 n_extra = 1;
5349 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005350 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005353 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 {
5355 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005356 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005357 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
5359 else
5360 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5361#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005362 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 {
5364 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005365 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 n_attr3 = 1;
5367 }
5368 }
5369
5370 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005371 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005373 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005374#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005375 || did_line_attr == 1
5376#endif
5377 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005379#ifdef FEAT_SEARCH_EXTRA
5380 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005381
5382 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005383 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005384 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005385#endif
5386
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005387 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 * highlight match at end of line. If it's beyond the last
5389 * char on the screen, just overwrite that one (tricky!) Not
5390 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005391#ifdef FEAT_SEARCH_EXTRA
5392 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005393 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005394 prevcol_hl_flag = TRUE;
5395 else
5396 {
5397 cur = wp->w_match_head;
5398 while (cur != NULL)
5399 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005400 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005401 {
5402 prevcol_hl_flag = TRUE;
5403 break;
5404 }
5405 cur = cur->next;
5406 }
5407 }
5408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005410 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005411 && (VIsual_mode != Ctrl_V
5412 || lnum == VIsual.lnum
5413 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005414 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415#ifdef FEAT_SEARCH_EXTRA
5416 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005417 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005418# ifdef FEAT_SYN_HL
5419 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5420 && !(wp == curwin && VIsual_active))
5421# endif
5422# ifdef FEAT_DIFF
5423 && diff_hlf == (hlf_T)0
5424# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005425# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005426 && did_line_attr <= 1
5427# endif
5428 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429#endif
5430 ))
5431 {
5432 int n = 0;
5433
5434#ifdef FEAT_RIGHTLEFT
5435 if (wp->w_p_rl)
5436 {
5437 if (col < 0)
5438 n = 1;
5439 }
5440 else
5441#endif
5442 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005443 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 n = -1;
5445 }
5446 if (n != 0)
5447 {
5448 /* At the window boundary, highlight the last character
5449 * instead (better than nothing). */
5450 off += n;
5451 col += n;
5452 }
5453 else
5454 {
5455 /* Add a blank character to highlight. */
5456 ScreenLines[off] = ' ';
5457#ifdef FEAT_MBYTE
5458 if (enc_utf8)
5459 ScreenLinesUC[off] = 0;
5460#endif
5461 }
5462#ifdef FEAT_SEARCH_EXTRA
5463 if (area_attr == 0)
5464 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005465 /* Use attributes from match with highest priority among
5466 * 'search_hl' and the match list. */
5467 char_attr = search_hl.attr;
5468 cur = wp->w_match_head;
5469 shl_flag = FALSE;
5470 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005471 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005472 if (shl_flag == FALSE
5473 && ((cur != NULL
5474 && cur->priority > SEARCH_HL_PRIORITY)
5475 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005476 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005477 shl = &search_hl;
5478 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005479 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005480 else
5481 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005482 if ((ptr - line) - 1 == (long)shl->startcol
5483 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005484 char_attr = shl->attr;
5485 if (shl != &search_hl && cur != NULL)
5486 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 }
5489#endif
5490 ScreenAttrs[off] = char_attr;
5491#ifdef FEAT_RIGHTLEFT
5492 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005495 --off;
5496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 else
5498#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005499 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005501 ++off;
5502 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005503 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005504#ifdef FEAT_SYN_HL
5505 eol_hl_off = 1;
5506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509
Bram Moolenaar91170f82006-05-05 21:15:17 +00005510 /*
5511 * At end of the text line.
5512 */
5513 if (c == NUL)
5514 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005515#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005516 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005517 if (wp->w_p_wrap)
5518 v = wp->w_skipcol;
5519 else
5520 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005521
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005522 /* check if line ends before left margin */
5523 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005524 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005525#ifdef FEAT_CONCEAL
5526 /* Get rid of the boguscols now, we want to draw until the right
5527 * edge for 'cursorcolumn'. */
5528 col -= boguscols;
5529 boguscols = 0;
5530#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005531
5532 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005533 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005534
5535 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005536 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5537 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005538 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005539 && lnum != wp->w_cursor.lnum)
5540 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005541# ifdef FEAT_RIGHTLEFT
5542 && !wp->w_p_rl
5543# endif
5544 )
5545 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005546 int rightmost_vcol = 0;
5547 int i;
5548
5549 if (wp->w_p_cuc)
5550 rightmost_vcol = wp->w_virtcol;
5551 if (draw_color_col)
5552 /* determine rightmost colorcolumn to possibly draw */
5553 for (i = 0; color_cols[i] >= 0; ++i)
5554 if (rightmost_vcol < color_cols[i])
5555 rightmost_vcol = color_cols[i];
5556
Bram Moolenaar02631462017-09-22 15:20:32 +02005557 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005558 {
5559 ScreenLines[off] = ' ';
5560#ifdef FEAT_MBYTE
5561 if (enc_utf8)
5562 ScreenLinesUC[off] = 0;
5563#endif
5564 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005565 if (draw_color_col)
5566 draw_color_col = advance_color_col(VCOL_HLC,
5567 &color_cols);
5568
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005569 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005570 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005571 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005572 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005573 else
5574 ScreenAttrs[off++] = 0;
5575
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005576 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005577 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005578
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005579 ++vcol;
5580 }
5581 }
5582#endif
5583
Bram Moolenaar53f81742017-09-22 14:35:51 +02005584 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005585 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 row++;
5587
5588 /*
5589 * Update w_cline_height and w_cline_folded if the cursor line was
5590 * updated (saves a call to plines() later).
5591 */
5592 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5593 {
5594 curwin->w_cline_row = startrow;
5595 curwin->w_cline_height = row - startrow;
5596#ifdef FEAT_FOLDING
5597 curwin->w_cline_folded = FALSE;
5598#endif
5599 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5600 }
5601
5602 break;
5603 }
5604
5605 /* line continues beyond line end */
5606 if (lcs_ext
5607 && !wp->w_p_wrap
5608#ifdef FEAT_DIFF
5609 && filler_todo <= 0
5610#endif
5611 && (
5612#ifdef FEAT_RIGHTLEFT
5613 wp->w_p_rl ? col == 0 :
5614#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005615 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005617 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5619 {
5620 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005621 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622#ifdef FEAT_MBYTE
5623 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005624 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 {
5626 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005627 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005628 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
5630 else
5631 mb_utf8 = FALSE;
5632#endif
5633 }
5634
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005635#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005636 /* advance to the next 'colorcolumn' */
5637 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005638 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005639
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005640 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005641 * highlight the cursor position itself.
5642 * Also highlight the 'colorcolumn' if it is different than
5643 * 'cursorcolumn' */
5644 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005645 if (draw_state == WL_LINE && !lnum_in_visual_area
5646 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005647 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005648 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005649 && lnum != wp->w_cursor.lnum)
5650 {
5651 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005652 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005653 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005654 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005655 {
5656 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005657 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005658 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005659 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005660#endif
5661
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 /*
5663 * Store character to be displayed.
5664 * Skip characters that are left of the screen for 'nowrap'.
5665 */
5666 vcol_prev = vcol;
5667 if (draw_state < WL_LINE || n_skip <= 0)
5668 {
5669 /*
5670 * Store the character.
5671 */
5672#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5673 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5674 {
5675 /* A double-wide character is: put first halve in left cell. */
5676 --off;
5677 --col;
5678 }
5679#endif
5680 ScreenLines[off] = c;
5681#ifdef FEAT_MBYTE
5682 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005683 {
5684 if ((mb_c & 0xff00) == 0x8e00)
5685 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 else if (enc_utf8)
5689 {
5690 if (mb_utf8)
5691 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005692 int i;
5693
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005695 if ((c & 0xff) == 0)
5696 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005697 for (i = 0; i < Screen_mco; ++i)
5698 {
5699 ScreenLinesC[i][off] = u8cc[i];
5700 if (u8cc[i] == 0)
5701 break;
5702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 }
5704 else
5705 ScreenLinesUC[off] = 0;
5706 }
5707 if (multi_attr)
5708 {
5709 ScreenAttrs[off] = multi_attr;
5710 multi_attr = 0;
5711 }
5712 else
5713#endif
5714 ScreenAttrs[off] = char_attr;
5715
5716#ifdef FEAT_MBYTE
5717 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5718 {
5719 /* Need to fill two screen columns. */
5720 ++off;
5721 ++col;
5722 if (enc_utf8)
5723 /* UTF-8: Put a 0 in the second screen char. */
5724 ScreenLines[off] = 0;
5725 else
5726 /* DBCS: Put second byte in the second screen char. */
5727 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005728 if (draw_state > WL_NR
5729#ifdef FEAT_DIFF
5730 && filler_todo <= 0
5731#endif
5732 )
5733 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 /* When "tocol" is halfway a character, set it to the end of
5735 * the character, otherwise highlighting won't stop. */
5736 if (tocol == vcol)
5737 ++tocol;
5738#ifdef FEAT_RIGHTLEFT
5739 if (wp->w_p_rl)
5740 {
5741 /* now it's time to backup one cell */
5742 --off;
5743 --col;
5744 }
5745#endif
5746 }
5747#endif
5748#ifdef FEAT_RIGHTLEFT
5749 if (wp->w_p_rl)
5750 {
5751 --off;
5752 --col;
5753 }
5754 else
5755#endif
5756 {
5757 ++off;
5758 ++col;
5759 }
5760 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005761#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005762 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005763 {
5764 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005765 ++vcol_off;
5766 if (n_extra > 0)
5767 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005768 if (wp->w_p_wrap)
5769 {
5770 /*
5771 * Special voodoo required if 'wrap' is on.
5772 *
5773 * Advance the column indicator to force the line
5774 * drawing to wrap early. This will make the line
5775 * take up the same screen space when parts are concealed,
5776 * so that cursor line computations aren't messed up.
5777 *
5778 * To avoid the fictitious advance of 'col' causing
5779 * trailing junk to be written out of the screen line
5780 * we are building, 'boguscols' keeps track of the number
5781 * of bad columns we have advanced.
5782 */
5783 if (n_extra > 0)
5784 {
5785 vcol += n_extra;
5786# ifdef FEAT_RIGHTLEFT
5787 if (wp->w_p_rl)
5788 {
5789 col -= n_extra;
5790 boguscols -= n_extra;
5791 }
5792 else
5793# endif
5794 {
5795 col += n_extra;
5796 boguscols += n_extra;
5797 }
5798 n_extra = 0;
5799 n_attr = 0;
5800 }
5801
5802
5803# ifdef FEAT_MBYTE
5804 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5805 {
5806 /* Need to fill two screen columns. */
5807# ifdef FEAT_RIGHTLEFT
5808 if (wp->w_p_rl)
5809 {
5810 --boguscols;
5811 --col;
5812 }
5813 else
5814# endif
5815 {
5816 ++boguscols;
5817 ++col;
5818 }
5819 }
5820# endif
5821
5822# ifdef FEAT_RIGHTLEFT
5823 if (wp->w_p_rl)
5824 {
5825 --boguscols;
5826 --col;
5827 }
5828 else
5829# endif
5830 {
5831 ++boguscols;
5832 ++col;
5833 }
5834 }
5835 else
5836 {
5837 if (n_extra > 0)
5838 {
5839 vcol += n_extra;
5840 n_extra = 0;
5841 n_attr = 0;
5842 }
5843 }
5844
5845 }
5846#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 else
5848 --n_skip;
5849
Bram Moolenaar64486672010-05-16 15:46:46 +02005850 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5851 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005852 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853#ifdef FEAT_DIFF
5854 && filler_todo <= 0
5855#endif
5856 )
5857 ++vcol;
5858
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005859#ifdef FEAT_SYN_HL
5860 if (vcol_save_attr >= 0)
5861 char_attr = vcol_save_attr;
5862#endif
5863
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 /* restore attributes after "predeces" in 'listchars' */
5865 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5866 char_attr = saved_attr3;
5867
5868 /* restore attributes after last 'listchars' or 'number' char */
5869 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5870 char_attr = saved_attr2;
5871
5872 /*
5873 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005874 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 */
5876 if ((
5877#ifdef FEAT_RIGHTLEFT
5878 wp->w_p_rl ? (col < 0) :
5879#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005880 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 && (*ptr != NUL
5882#ifdef FEAT_DIFF
5883 || filler_todo > 0
5884#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005885 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5887 )
5888 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005889#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005890 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005891 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005892 boguscols = 0;
5893#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005894 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005895 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 ++row;
5898 ++screen_row;
5899
5900 /* When not wrapping and finished diff lines, or when displayed
5901 * '$' and highlighting until last column, break here. */
5902 if ((!wp->w_p_wrap
5903#ifdef FEAT_DIFF
5904 && filler_todo <= 0
5905#endif
5906 ) || lcs_eol_one == -1)
5907 break;
5908
5909 /* When the window is too narrow draw all "@" lines. */
5910 if (draw_state != WL_LINE
5911#ifdef FEAT_DIFF
5912 && filler_todo <= 0
5913#endif
5914 )
5915 {
5916 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 row = endrow;
5919 }
5920
5921 /* When line got too long for screen break here. */
5922 if (row == endrow)
5923 {
5924 ++row;
5925 break;
5926 }
5927
5928 if (screen_cur_row == screen_row - 1
5929#ifdef FEAT_DIFF
5930 && filler_todo <= 0
5931#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005932 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 {
5934 /* Remember that the line wraps, used for modeless copy. */
5935 LineWraps[screen_row - 1] = TRUE;
5936
5937 /*
5938 * Special trick to make copy/paste of wrapped lines work with
5939 * xterm/screen: write an extra character beyond the end of
5940 * the line. This will work with all terminal types
5941 * (regardless of the xn,am settings).
5942 * Only do this on a fast tty.
5943 * Only do this if the cursor is on the current line
5944 * (something has been written in it).
5945 * Don't do this for the GUI.
5946 * Don't do this for double-width characters.
5947 * Don't do this for a window not at the right screen border.
5948 */
5949 if (p_tf
5950#ifdef FEAT_GUI
5951 && !gui.in_use
5952#endif
5953#ifdef FEAT_MBYTE
5954 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005955 && ((*mb_off2cells)(LineOffset[screen_row],
5956 LineOffset[screen_row] + screen_Columns)
5957 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005959 + (int)Columns - 2,
5960 LineOffset[screen_row] + screen_Columns)
5961 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962#endif
5963 )
5964 {
5965 /* First make sure we are at the end of the screen line,
5966 * then output the same character again to let the
5967 * terminal know about the wrap. If the terminal doesn't
5968 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005969 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 screen_char(LineOffset[screen_row - 1]
5971 + (unsigned)Columns - 1,
5972 screen_row - 1, (int)(Columns - 1));
5973
5974#ifdef FEAT_MBYTE
5975 /* When there is a multi-byte character, just output a
5976 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005977 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5978 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 out_char(' ');
5980 else
5981#endif
5982 out_char(ScreenLines[LineOffset[screen_row - 1]
5983 + (Columns - 1)]);
5984 /* force a redraw of the first char on the next line */
5985 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5986 screen_start(); /* don't know where cursor is now */
5987 }
5988 }
5989
5990 col = 0;
5991 off = (unsigned)(current_ScreenLine - ScreenLines);
5992#ifdef FEAT_RIGHTLEFT
5993 if (wp->w_p_rl)
5994 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005995 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 off += col;
5997 }
5998#endif
5999
6000 /* reset the drawing state for the start of a wrapped line */
6001 draw_state = WL_START;
6002 saved_n_extra = n_extra;
6003 saved_p_extra = p_extra;
6004 saved_c_extra = c_extra;
6005 saved_char_attr = char_attr;
6006 n_extra = 0;
6007 lcs_prec_todo = lcs_prec;
6008#ifdef FEAT_LINEBREAK
6009# ifdef FEAT_DIFF
6010 if (filler_todo <= 0)
6011# endif
6012 need_showbreak = TRUE;
6013#endif
6014#ifdef FEAT_DIFF
6015 --filler_todo;
6016 /* When the filler lines are actually below the last line of the
6017 * file, don't draw the line itself, break here. */
6018 if (filler_todo == 0 && wp->w_botfill)
6019 break;
6020#endif
6021 }
6022
6023 } /* for every character in the line */
6024
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006025#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006026 /* After an empty line check first word for capital. */
6027 if (*skipwhite(line) == NUL)
6028 {
6029 capcol_lnum = lnum + 1;
6030 cap_col = 0;
6031 }
6032#endif
6033
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006034 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 return row;
6036}
6037
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006038#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006039static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006040
6041/*
6042 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006043 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006044 */
6045 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006046comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006047{
6048 int i;
6049
6050 for (i = 0; i < Screen_mco; ++i)
6051 {
6052 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6053 return TRUE;
6054 if (ScreenLinesC[i][off_from] == 0)
6055 break;
6056 }
6057 return FALSE;
6058}
6059#endif
6060
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061/*
6062 * Check whether the given character needs redrawing:
6063 * - the (first byte of the) character is different
6064 * - the attributes are different
6065 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006066 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 */
6068 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006069char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
6071 if (cols > 0
6072 && ((ScreenLines[off_from] != ScreenLines[off_to]
6073 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6074
6075#ifdef FEAT_MBYTE
6076 || (enc_dbcs != 0
6077 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6078 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6079 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6080 : (cols > 1 && ScreenLines[off_from + 1]
6081 != ScreenLines[off_to + 1])))
6082 || (enc_utf8
6083 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6084 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006085 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006086 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6087 && ScreenLines[off_from + 1]
6088 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089#endif
6090 ))
6091 return TRUE;
6092 return FALSE;
6093}
6094
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006095#if defined(FEAT_TERMINAL) || defined(PROTO)
6096/*
6097 * Return the index in ScreenLines[] for the current screen line.
6098 */
6099 int
6100screen_get_current_line_off()
6101{
6102 return (int)(current_ScreenLine - ScreenLines);
6103}
6104#endif
6105
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106/*
6107 * Move one "cooked" screen line to the screen, but only the characters that
6108 * have actually changed. Handle insert/delete character.
6109 * "coloff" gives the first column on the screen for this line.
6110 * "endcol" gives the columns where valid characters are.
6111 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6112 * needs to be cleared, negative otherwise.
6113 * "rlflag" is TRUE in a rightleft window:
6114 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6115 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6116 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006117 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006118screen_line(
6119 int row,
6120 int coloff,
6121 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006122 int clear_width,
6123 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
6125 unsigned off_from;
6126 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006127#ifdef FEAT_MBYTE
6128 unsigned max_off_from;
6129 unsigned max_off_to;
6130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 int force = FALSE; /* force update rest of the line */
6134 int redraw_this /* bool: does character need redraw? */
6135#ifdef FEAT_GUI
6136 = TRUE /* For GUI when while-loop empty */
6137#endif
6138 ;
6139 int redraw_next; /* redraw_this for next character */
6140#ifdef FEAT_MBYTE
6141 int clear_next = FALSE;
6142 int char_cells; /* 1: normal char */
6143 /* 2: occupies two display cells */
6144# define CHAR_CELLS char_cells
6145#else
6146# define CHAR_CELLS 1
6147#endif
6148
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006149 /* Check for illegal row and col, just in case. */
6150 if (row >= Rows)
6151 row = Rows - 1;
6152 if (endcol > Columns)
6153 endcol = Columns;
6154
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155# ifdef FEAT_CLIPBOARD
6156 clip_may_clear_selection(row, row);
6157# endif
6158
6159 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6160 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006161#ifdef FEAT_MBYTE
6162 max_off_from = off_from + screen_Columns;
6163 max_off_to = LineOffset[row] + screen_Columns;
6164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165
6166#ifdef FEAT_RIGHTLEFT
6167 if (rlflag)
6168 {
6169 /* Clear rest first, because it's left of the text. */
6170 if (clear_width > 0)
6171 {
6172 while (col <= endcol && ScreenLines[off_to] == ' '
6173 && ScreenAttrs[off_to] == 0
6174# ifdef FEAT_MBYTE
6175 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6176# endif
6177 )
6178 {
6179 ++off_to;
6180 ++col;
6181 }
6182 if (col <= endcol)
6183 screen_fill(row, row + 1, col + coloff,
6184 endcol + coloff + 1, ' ', ' ', 0);
6185 }
6186 col = endcol + 1;
6187 off_to = LineOffset[row] + col + coloff;
6188 off_from += col;
6189 endcol = (clear_width > 0 ? clear_width : -clear_width);
6190 }
6191#endif /* FEAT_RIGHTLEFT */
6192
6193 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6194
6195 while (col < endcol)
6196 {
6197#ifdef FEAT_MBYTE
6198 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006199 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 else
6201 char_cells = 1;
6202#endif
6203
6204 redraw_this = redraw_next;
6205 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6206 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6207
6208#ifdef FEAT_GUI
6209 /* If the next character was bold, then redraw the current character to
6210 * remove any pixels that might have spilt over into us. This only
6211 * happens in the GUI.
6212 */
6213 if (redraw_next && gui.in_use)
6214 {
6215 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006216 if (hl > HL_ALL)
6217 hl = syn_attr2attr(hl);
6218 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 redraw_this = TRUE;
6220 }
6221#endif
6222
6223 if (redraw_this)
6224 {
6225 /*
6226 * Special handling when 'xs' termcap flag set (hpterm):
6227 * Attributes for characters are stored at the position where the
6228 * cursor is when writing the highlighting code. The
6229 * start-highlighting code must be written with the cursor on the
6230 * first highlighted character. The stop-highlighting code must
6231 * be written with the cursor just after the last highlighted
6232 * character.
6233 * Overwriting a character doesn't remove it's highlighting. Need
6234 * to clear the rest of the line, and force redrawing it
6235 * completely.
6236 */
6237 if ( p_wiv
6238 && !force
6239#ifdef FEAT_GUI
6240 && !gui.in_use
6241#endif
6242 && ScreenAttrs[off_to] != 0
6243 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6244 {
6245 /*
6246 * Need to remove highlighting attributes here.
6247 */
6248 windgoto(row, col + coloff);
6249 out_str(T_CE); /* clear rest of this screen line */
6250 screen_start(); /* don't know where cursor is now */
6251 force = TRUE; /* force redraw of rest of the line */
6252 redraw_next = TRUE; /* or else next char would miss out */
6253
6254 /*
6255 * If the previous character was highlighted, need to stop
6256 * highlighting at this character.
6257 */
6258 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6259 {
6260 screen_attr = ScreenAttrs[off_to - 1];
6261 term_windgoto(row, col + coloff);
6262 screen_stop_highlight();
6263 }
6264 else
6265 screen_attr = 0; /* highlighting has stopped */
6266 }
6267#ifdef FEAT_MBYTE
6268 if (enc_dbcs != 0)
6269 {
6270 /* Check if overwriting a double-byte with a single-byte or
6271 * the other way around requires another character to be
6272 * redrawn. For UTF-8 this isn't needed, because comparing
6273 * ScreenLinesUC[] is sufficient. */
6274 if (char_cells == 1
6275 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006276 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 {
6278 /* Writing a single-cell character over a double-cell
6279 * character: need to redraw the next cell. */
6280 ScreenLines[off_to + 1] = 0;
6281 redraw_next = TRUE;
6282 }
6283 else if (char_cells == 2
6284 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006285 && (*mb_off2cells)(off_to, max_off_to) == 1
6286 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 {
6288 /* Writing the second half of a double-cell character over
6289 * a double-cell character: need to redraw the second
6290 * cell. */
6291 ScreenLines[off_to + 2] = 0;
6292 redraw_next = TRUE;
6293 }
6294
6295 if (enc_dbcs == DBCS_JPNU)
6296 ScreenLines2[off_to] = ScreenLines2[off_from];
6297 }
6298 /* When writing a single-width character over a double-width
6299 * character and at the end of the redrawn text, need to clear out
6300 * the right halve of the old character.
6301 * Also required when writing the right halve of a double-width
6302 * char over the left halve of an existing one. */
6303 if (has_mbyte && col + char_cells == endcol
6304 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006305 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006307 && (*mb_off2cells)(off_to, max_off_to) == 1
6308 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 clear_next = TRUE;
6310#endif
6311
6312 ScreenLines[off_to] = ScreenLines[off_from];
6313#ifdef FEAT_MBYTE
6314 if (enc_utf8)
6315 {
6316 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6317 if (ScreenLinesUC[off_from] != 0)
6318 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006319 int i;
6320
6321 for (i = 0; i < Screen_mco; ++i)
6322 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 }
6324 }
6325 if (char_cells == 2)
6326 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6327#endif
6328
6329#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006330 /* The bold trick makes a single column of pixels appear in the
6331 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 * character should be redrawn too. This happens for our own GUI
6333 * and for some xterms. */
6334 if (
6335# ifdef FEAT_GUI
6336 gui.in_use
6337# endif
6338# if defined(FEAT_GUI) && defined(UNIX)
6339 ||
6340# endif
6341# ifdef UNIX
6342 term_is_xterm
6343# endif
6344 )
6345 {
6346 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006347 if (hl > HL_ALL)
6348 hl = syn_attr2attr(hl);
6349 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 redraw_next = TRUE;
6351 }
6352#endif
6353 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6354#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006355 /* For simplicity set the attributes of second half of a
6356 * double-wide character equal to the first half. */
6357 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006359
6360 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 else
6363#endif
6364 screen_char(off_to, row, col + coloff);
6365 }
6366 else if ( p_wiv
6367#ifdef FEAT_GUI
6368 && !gui.in_use
6369#endif
6370 && col + coloff > 0)
6371 {
6372 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6373 {
6374 /*
6375 * Don't output stop-highlight when moving the cursor, it will
6376 * stop the highlighting when it should continue.
6377 */
6378 screen_attr = 0;
6379 }
6380 else if (screen_attr != 0)
6381 screen_stop_highlight();
6382 }
6383
6384 off_to += CHAR_CELLS;
6385 off_from += CHAR_CELLS;
6386 col += CHAR_CELLS;
6387 }
6388
6389#ifdef FEAT_MBYTE
6390 if (clear_next)
6391 {
6392 /* Clear the second half of a double-wide character of which the left
6393 * half was overwritten with a single-wide character. */
6394 ScreenLines[off_to] = ' ';
6395 if (enc_utf8)
6396 ScreenLinesUC[off_to] = 0;
6397 screen_char(off_to, row, col + coloff);
6398 }
6399#endif
6400
6401 if (clear_width > 0
6402#ifdef FEAT_RIGHTLEFT
6403 && !rlflag
6404#endif
6405 )
6406 {
6407#ifdef FEAT_GUI
6408 int startCol = col;
6409#endif
6410
6411 /* blank out the rest of the line */
6412 while (col < clear_width && ScreenLines[off_to] == ' '
6413 && ScreenAttrs[off_to] == 0
6414#ifdef FEAT_MBYTE
6415 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6416#endif
6417 )
6418 {
6419 ++off_to;
6420 ++col;
6421 }
6422 if (col < clear_width)
6423 {
6424#ifdef FEAT_GUI
6425 /*
6426 * In the GUI, clearing the rest of the line may leave pixels
6427 * behind if the first character cleared was bold. Some bold
6428 * fonts spill over the left. In this case we redraw the previous
6429 * character too. If we didn't skip any blanks above, then we
6430 * only redraw if the character wasn't already redrawn anyway.
6431 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006432 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 {
6434 hl = ScreenAttrs[off_to];
6435 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006436 {
6437 int prev_cells = 1;
6438# ifdef FEAT_MBYTE
6439 if (enc_utf8)
6440 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6441 * that its width is 2. */
6442 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6443 else if (enc_dbcs != 0)
6444 {
6445 /* find previous character by counting from first
6446 * column and get its width. */
6447 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006448 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006449
6450 while (off < off_to)
6451 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006452 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006453 off += prev_cells;
6454 }
6455 }
6456
6457 if (enc_dbcs != 0 && prev_cells > 1)
6458 screen_char_2(off_to - prev_cells, row,
6459 col + coloff - prev_cells);
6460 else
6461# endif
6462 screen_char(off_to - prev_cells, row,
6463 col + coloff - prev_cells);
6464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 }
6466#endif
6467 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6468 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 off_to += clear_width - col;
6470 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 }
6472 }
6473
6474 if (clear_width > 0)
6475 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 /* For a window that's left of another, draw the separator char. */
6477 if (col + coloff < Columns)
6478 {
6479 int c;
6480
6481 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006482 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006483#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006484 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6485 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 || ScreenAttrs[off_to] != hl)
6488 {
6489 ScreenLines[off_to] = c;
6490 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006491#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 if (enc_utf8)
6493 {
6494 if (c >= 0x80)
6495 {
6496 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006497 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 }
6499 else
6500 ScreenLinesUC[off_to] = 0;
6501 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 screen_char(off_to, row, col + coloff);
6504 }
6505 }
6506 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 LineWraps[row] = FALSE;
6508 }
6509}
6510
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006511#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006513 * Mirror text "str" for right-left displaying.
6514 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006516 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006517rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518{
6519 char_u *p1, *p2;
6520 int t;
6521
6522 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6523 {
6524 t = *p1;
6525 *p1 = *p2;
6526 *p2 = t;
6527 }
6528}
6529#endif
6530
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531/*
6532 * mark all status lines for redraw; used after first :cd
6533 */
6534 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006535status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536{
6537 win_T *wp;
6538
Bram Moolenaar29323592016-07-24 22:04:11 +02006539 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 if (wp->w_status_height)
6541 {
6542 wp->w_redr_status = TRUE;
6543 redraw_later(VALID);
6544 }
6545}
6546
6547/*
6548 * mark all status lines of the current buffer for redraw
6549 */
6550 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006551status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552{
6553 win_T *wp;
6554
Bram Moolenaar29323592016-07-24 22:04:11 +02006555 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6557 {
6558 wp->w_redr_status = TRUE;
6559 redraw_later(VALID);
6560 }
6561}
6562
6563/*
6564 * Redraw all status lines that need to be redrawn.
6565 */
6566 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006567redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568{
6569 win_T *wp;
6570
Bram Moolenaar29323592016-07-24 22:04:11 +02006571 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006573 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006574 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006575 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
Bram Moolenaar4033c552017-09-16 20:54:51 +02006578#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579/*
6580 * Redraw all status lines at the bottom of frame "frp".
6581 */
6582 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006583win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584{
6585 if (frp->fr_layout == FR_LEAF)
6586 frp->fr_win->w_redr_status = TRUE;
6587 else if (frp->fr_layout == FR_ROW)
6588 {
6589 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6590 win_redraw_last_status(frp);
6591 }
6592 else /* frp->fr_layout == FR_COL */
6593 {
6594 frp = frp->fr_child;
6595 while (frp->fr_next != NULL)
6596 frp = frp->fr_next;
6597 win_redraw_last_status(frp);
6598 }
6599}
6600#endif
6601
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602/*
6603 * Draw the verticap separator right of window "wp" starting with line "row".
6604 */
6605 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006606draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607{
6608 int hl;
6609 int c;
6610
6611 if (wp->w_vsep_width)
6612 {
6613 /* draw the vertical separator right of this window */
6614 c = fillchar_vsep(&hl);
6615 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6616 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6617 c, ' ', hl);
6618 }
6619}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620
6621#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006622static int status_match_len(expand_T *xp, char_u *s);
6623static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624
6625/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006626 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 */
6628 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006629status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630{
6631 int len = 0;
6632
6633#ifdef FEAT_MENU
6634 int emenu = (xp->xp_context == EXPAND_MENUS
6635 || xp->xp_context == EXPAND_MENUNAMES);
6636
6637 /* Check for menu separators - replace with '|'. */
6638 if (emenu && menu_is_separator(s))
6639 return 1;
6640#endif
6641
6642 while (*s != NUL)
6643 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006644 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006645 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006646 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 }
6648
6649 return len;
6650}
6651
6652/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006653 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006654 * These are backslashes used for escaping. Do show backslashes in help tags.
6655 */
6656 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006657skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006658{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006659 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006660#ifdef FEAT_MENU
6661 || ((xp->xp_context == EXPAND_MENUS
6662 || xp->xp_context == EXPAND_MENUNAMES)
6663 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6664#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006665 )
6666 {
6667#ifndef BACKSLASH_IN_FILENAME
6668 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6669 return 2;
6670#endif
6671 return 1;
6672 }
6673 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006674}
6675
6676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 * Show wildchar matches in the status line.
6678 * Show at least the "match" item.
6679 * We start at item 'first_match' in the list and show all matches that fit.
6680 *
6681 * If inversion is possible we use it. Else '=' characters are used.
6682 */
6683 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006684win_redr_status_matches(
6685 expand_T *xp,
6686 int num_matches,
6687 char_u **matches, /* list of matches */
6688 int match,
6689 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690{
6691#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6692 int row;
6693 char_u *buf;
6694 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006695 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 int fillchar;
6697 int attr;
6698 int i;
6699 int highlight = TRUE;
6700 char_u *selstart = NULL;
6701 int selstart_col = 0;
6702 char_u *selend = NULL;
6703 static int first_match = 0;
6704 int add_left = FALSE;
6705 char_u *s;
6706#ifdef FEAT_MENU
6707 int emenu;
6708#endif
6709#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6710 int l;
6711#endif
6712
6713 if (matches == NULL) /* interrupted completion? */
6714 return;
6715
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006716#ifdef FEAT_MBYTE
6717 if (has_mbyte)
6718 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6719 else
6720#endif
6721 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 if (buf == NULL)
6723 return;
6724
6725 if (match == -1) /* don't show match but original text */
6726 {
6727 match = 0;
6728 highlight = FALSE;
6729 }
6730 /* count 1 for the ending ">" */
6731 clen = status_match_len(xp, L_MATCH(match)) + 3;
6732 if (match == 0)
6733 first_match = 0;
6734 else if (match < first_match)
6735 {
6736 /* jumping left, as far as we can go */
6737 first_match = match;
6738 add_left = TRUE;
6739 }
6740 else
6741 {
6742 /* check if match fits on the screen */
6743 for (i = first_match; i < match; ++i)
6744 clen += status_match_len(xp, L_MATCH(i)) + 2;
6745 if (first_match > 0)
6746 clen += 2;
6747 /* jumping right, put match at the left */
6748 if ((long)clen > Columns)
6749 {
6750 first_match = match;
6751 /* if showing the last match, we can add some on the left */
6752 clen = 2;
6753 for (i = match; i < num_matches; ++i)
6754 {
6755 clen += status_match_len(xp, L_MATCH(i)) + 2;
6756 if ((long)clen >= Columns)
6757 break;
6758 }
6759 if (i == num_matches)
6760 add_left = TRUE;
6761 }
6762 }
6763 if (add_left)
6764 while (first_match > 0)
6765 {
6766 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6767 if ((long)clen >= Columns)
6768 break;
6769 --first_match;
6770 }
6771
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006772 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773
6774 if (first_match == 0)
6775 {
6776 *buf = NUL;
6777 len = 0;
6778 }
6779 else
6780 {
6781 STRCPY(buf, "< ");
6782 len = 2;
6783 }
6784 clen = len;
6785
6786 i = first_match;
6787 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6788 {
6789 if (i == match)
6790 {
6791 selstart = buf + len;
6792 selstart_col = clen;
6793 }
6794
6795 s = L_MATCH(i);
6796 /* Check for menu separators - replace with '|' */
6797#ifdef FEAT_MENU
6798 emenu = (xp->xp_context == EXPAND_MENUS
6799 || xp->xp_context == EXPAND_MENUNAMES);
6800 if (emenu && menu_is_separator(s))
6801 {
6802 STRCPY(buf + len, transchar('|'));
6803 l = (int)STRLEN(buf + len);
6804 len += l;
6805 clen += l;
6806 }
6807 else
6808#endif
6809 for ( ; *s != NUL; ++s)
6810 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006811 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 clen += ptr2cells(s);
6813#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006814 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 {
6816 STRNCPY(buf + len, s, l);
6817 s += l - 1;
6818 len += l;
6819 }
6820 else
6821#endif
6822 {
6823 STRCPY(buf + len, transchar_byte(*s));
6824 len += (int)STRLEN(buf + len);
6825 }
6826 }
6827 if (i == match)
6828 selend = buf + len;
6829
6830 *(buf + len++) = ' ';
6831 *(buf + len++) = ' ';
6832 clen += 2;
6833 if (++i == num_matches)
6834 break;
6835 }
6836
6837 if (i != num_matches)
6838 {
6839 *(buf + len++) = '>';
6840 ++clen;
6841 }
6842
6843 buf[len] = NUL;
6844
6845 row = cmdline_row - 1;
6846 if (row >= 0)
6847 {
6848 if (wild_menu_showing == 0)
6849 {
6850 if (msg_scrolled > 0)
6851 {
6852 /* Put the wildmenu just above the command line. If there is
6853 * no room, scroll the screen one line up. */
6854 if (cmdline_row == Rows - 1)
6855 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006856 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 ++msg_scrolled;
6858 }
6859 else
6860 {
6861 ++cmdline_row;
6862 ++row;
6863 }
6864 wild_menu_showing = WM_SCROLLED;
6865 }
6866 else
6867 {
6868 /* Create status line if needed by setting 'laststatus' to 2.
6869 * Set 'winminheight' to zero to avoid that the window is
6870 * resized. */
6871 if (lastwin->w_status_height == 0)
6872 {
6873 save_p_ls = p_ls;
6874 save_p_wmh = p_wmh;
6875 p_ls = 2;
6876 p_wmh = 0;
6877 last_status(FALSE);
6878 }
6879 wild_menu_showing = WM_SHOWN;
6880 }
6881 }
6882
6883 screen_puts(buf, row, 0, attr);
6884 if (selstart != NULL && highlight)
6885 {
6886 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006887 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 }
6889
6890 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6891 }
6892
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 vim_free(buf);
6895}
6896#endif
6897
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898/*
6899 * Redraw the status line of window wp.
6900 *
6901 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006902 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6903 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006905 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006906win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907{
6908 int row;
6909 char_u *p;
6910 int len;
6911 int fillchar;
6912 int attr;
6913 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006914 static int busy = FALSE;
6915
6916 /* It's possible to get here recursively when 'statusline' (indirectly)
6917 * invokes ":redrawstatus". Simply ignore the call then. */
6918 if (busy)
6919 return;
6920 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921
6922 wp->w_redr_status = FALSE;
6923 if (wp->w_status_height == 0)
6924 {
6925 /* no status line, can only be last window */
6926 redraw_cmdline = TRUE;
6927 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006928 else if (!redrawing()
6929#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006930 // don't update status line when popup menu is visible and may be
6931 // drawn over it, unless it will be redrawn later
6932 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006933#endif
6934 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 {
6936 /* Don't redraw right now, do it later. */
6937 wp->w_redr_status = TRUE;
6938 }
6939#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006940 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 {
6942 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006943 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 }
6945#endif
6946 else
6947 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006948 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006950 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 p = NameBuff;
6952 len = (int)STRLEN(p);
6953
Bram Moolenaard85f2712017-07-28 21:51:57 +02006954 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955#ifdef FEAT_QUICKFIX
6956 || wp->w_p_pvw
6957#endif
6958 || bufIsChanged(wp->w_buffer)
6959 || wp->w_buffer->b_p_ro)
6960 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006961 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006963 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 len += (int)STRLEN(p + len);
6965 }
6966#ifdef FEAT_QUICKFIX
6967 if (wp->w_p_pvw)
6968 {
6969 STRCPY(p + len, _("[Preview]"));
6970 len += (int)STRLEN(p + len);
6971 }
6972#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006973 if (bufIsChanged(wp->w_buffer)
6974#ifdef FEAT_TERMINAL
6975 && !bt_terminal(wp->w_buffer)
6976#endif
6977 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 {
6979 STRCPY(p + len, "[+]");
6980 len += 3;
6981 }
6982 if (wp->w_buffer->b_p_ro)
6983 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006984 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006985 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 }
6987
Bram Moolenaar02631462017-09-22 15:20:32 +02006988 this_ru_col = ru_col - (Columns - wp->w_width);
6989 if (this_ru_col < (wp->w_width + 1) / 2)
6990 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 if (this_ru_col <= 1)
6992 {
6993 p = (char_u *)"<"; /* No room for file name! */
6994 len = 1;
6995 }
6996 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997#ifdef FEAT_MBYTE
6998 if (has_mbyte)
6999 {
7000 int clen = 0, i;
7001
7002 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02007003 clen = mb_string2cells(p, -1);
7004
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 /* Find first character that will fit.
7006 * Going from start to end is much faster for DBCS. */
7007 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007008 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 clen -= (*mb_ptr2cells)(p + i);
7010 len = clen;
7011 if (i > 0)
7012 {
7013 p = p + i - 1;
7014 *p = '<';
7015 ++len;
7016 }
7017
7018 }
7019 else
7020#endif
7021 if (len > this_ru_col - 1)
7022 {
7023 p += len - (this_ru_col - 1);
7024 *p = '<';
7025 len = this_ru_col - 1;
7026 }
7027
7028 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007029 screen_puts(p, row, wp->w_wincol, attr);
7030 screen_fill(row, row + 1, len + wp->w_wincol,
7031 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007033 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7035 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007036 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037
7038#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007039 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040#endif
7041 }
7042
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 /*
7044 * May need to draw the character below the vertical separator.
7045 */
7046 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7047 {
7048 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007049 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 else
7051 fillchar = fillchar_vsep(&attr);
7052 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7053 attr);
7054 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007055 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056}
7057
Bram Moolenaar238a5642006-02-21 22:12:05 +00007058#ifdef FEAT_STL_OPT
7059/*
7060 * Redraw the status line according to 'statusline' and take care of any
7061 * errors encountered.
7062 */
7063 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007064redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007065{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007066 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007067 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007068
7069 /* When called recursively return. This can happen when the statusline
7070 * contains an expression that triggers a redraw. */
7071 if (entered)
7072 return;
7073 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007074
Bram Moolenaara742e082016-04-05 21:10:38 +02007075 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007076 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007077 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007078 {
7079 /* When there is an error disable the statusline, otherwise the
7080 * display is messed up with errors and a redraw triggers the problem
7081 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007082 set_string_option_direct((char_u *)"statusline", -1,
7083 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007084 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007085 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007086 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007087 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007088}
7089#endif
7090
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091/*
7092 * Return TRUE if the status line of window "wp" is connected to the status
7093 * line of the window right of it. If not, then it's a vertical separator.
7094 * Only call if (wp->w_vsep_width != 0).
7095 */
7096 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007097stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098{
7099 frame_T *fr;
7100
7101 fr = wp->w_frame;
7102 while (fr->fr_parent != NULL)
7103 {
7104 if (fr->fr_parent->fr_layout == FR_COL)
7105 {
7106 if (fr->fr_next != NULL)
7107 break;
7108 }
7109 else
7110 {
7111 if (fr->fr_next != NULL)
7112 return TRUE;
7113 }
7114 fr = fr->fr_parent;
7115 }
7116 return FALSE;
7117}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120/*
7121 * Get the value to show for the language mappings, active 'keymap'.
7122 */
7123 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007124get_keymap_str(
7125 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007126 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007127 char_u *buf, /* buffer for the result */
7128 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129{
7130 char_u *p;
7131
7132 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7133 return FALSE;
7134
7135 {
7136#ifdef FEAT_EVAL
7137 buf_T *old_curbuf = curbuf;
7138 win_T *old_curwin = curwin;
7139 char_u *s;
7140
7141 curbuf = wp->w_buffer;
7142 curwin = wp;
7143 STRCPY(buf, "b:keymap_name"); /* must be writable */
7144 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007145 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 --emsg_skip;
7147 curbuf = old_curbuf;
7148 curwin = old_curwin;
7149 if (p == NULL || *p == NUL)
7150#endif
7151 {
7152#ifdef FEAT_KEYMAP
7153 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7154 p = wp->w_buffer->b_p_keymap;
7155 else
7156#endif
7157 p = (char_u *)"lang";
7158 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007159 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 buf[0] = NUL;
7161#ifdef FEAT_EVAL
7162 vim_free(s);
7163#endif
7164 }
7165 return buf[0] != NUL;
7166}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167
7168#if defined(FEAT_STL_OPT) || defined(PROTO)
7169/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007170 * Redraw the status line or ruler of window "wp".
7171 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 */
7173 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007174win_redr_custom(
7175 win_T *wp,
7176 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007178 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 int attr;
7180 int curattr;
7181 int row;
7182 int col = 0;
7183 int maxwidth;
7184 int width;
7185 int n;
7186 int len;
7187 int fillchar;
7188 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007189 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007191 struct stl_hlrec hltab[STL_MAX_ITEM];
7192 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007193 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007194 win_T *ewp;
7195 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196
Bram Moolenaar1d633412013-12-11 15:52:01 +01007197 /* There is a tiny chance that this gets called recursively: When
7198 * redrawing a status line triggers redrawing the ruler or tabline.
7199 * Avoid trouble by not allowing recursion. */
7200 if (entered)
7201 return;
7202 entered = TRUE;
7203
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007205 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007207 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007208 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007209 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007210 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007211 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007212 maxwidth = Columns;
7213# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007214 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007215# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007217 else
7218 {
7219 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007220 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007221 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007222
7223 if (draw_ruler)
7224 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007225 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007226 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007227 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007228 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007229 if (*++stl == '-')
7230 stl++;
7231 if (atoi((char *)stl))
7232 while (VIM_ISDIGIT(*stl))
7233 stl++;
7234 if (*stl++ != '(')
7235 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007236 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007237 col = ru_col - (Columns - wp->w_width);
7238 if (col < (wp->w_width + 1) / 2)
7239 col = (wp->w_width + 1) / 2;
7240 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007241 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007242 {
7243 row = Rows - 1;
7244 --maxwidth; /* writing in last column may cause scrolling */
7245 fillchar = ' ';
7246 attr = 0;
7247 }
7248
7249# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007250 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007251# endif
7252 }
7253 else
7254 {
7255 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007256 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007257 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007258 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007259# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007260 use_sandbox = was_set_insecurely((char_u *)"statusline",
7261 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007262# endif
7263 }
7264
Bram Moolenaar53f81742017-09-22 14:35:51 +02007265 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007266 }
7267
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007269 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270
Bram Moolenaar61452852011-02-01 18:01:11 +01007271 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7272 * the cursor away and back. */
7273 ewp = wp == NULL ? curwin : wp;
7274 p_crb_save = ewp->w_p_crb;
7275 ewp->w_p_crb = FALSE;
7276
Bram Moolenaar362f3562009-11-03 16:20:34 +00007277 /* Make a copy, because the statusline may include a function call that
7278 * might change the option value and free the memory. */
7279 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007280 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007281 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007282 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007283 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007284 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007286 /* Make all characters printable. */
7287 p = transstr(buf);
7288 if (p != NULL)
7289 {
7290 vim_strncpy(buf, p, sizeof(buf) - 1);
7291 vim_free(p);
7292 }
7293
7294 /* fill up with "fillchar" */
7295 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007296 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 {
7298#ifdef FEAT_MBYTE
7299 len += (*mb_char2bytes)(fillchar, buf + len);
7300#else
7301 buf[len++] = fillchar;
7302#endif
7303 ++width;
7304 }
7305 buf[len] = NUL;
7306
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007307 /*
7308 * Draw each snippet with the specified highlighting.
7309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 curattr = attr;
7311 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007312 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007314 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 screen_puts_len(p, len, row, col, curattr);
7316 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007317 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007319 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007321 else if (hltab[n].userhl < 0)
7322 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007323#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007324 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7325 && wp->w_status_height != 0)
7326 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007327 else if (wp != NULL && bt_terminal(wp->w_buffer)
7328 && wp->w_status_height != 0)
7329 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007330#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007331 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007332 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007334 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 }
7336 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007337
7338 if (wp == NULL)
7339 {
7340 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7341 col = 0;
7342 len = 0;
7343 p = buf;
7344 fillchar = 0;
7345 for (n = 0; tabtab[n].start != NULL; n++)
7346 {
7347 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7348 while (col < len)
7349 TabPageIdxs[col++] = fillchar;
7350 p = tabtab[n].start;
7351 fillchar = tabtab[n].userhl;
7352 }
7353 while (col < Columns)
7354 TabPageIdxs[col++] = fillchar;
7355 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007356
7357theend:
7358 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359}
7360
7361#endif /* FEAT_STL_OPT */
7362
7363/*
7364 * Output a single character directly to the screen and update ScreenLines.
7365 */
7366 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007367screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 char_u buf[MB_MAXBYTES + 1];
7370
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007371#ifdef FEAT_MBYTE
7372 if (has_mbyte)
7373 buf[(*mb_char2bytes)(c, buf)] = NUL;
7374 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007376 {
7377 buf[0] = c;
7378 buf[1] = NUL;
7379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 screen_puts(buf, row, col, attr);
7381}
7382
7383/*
7384 * Get a single character directly from ScreenLines into "bytes[]".
7385 * Also return its attribute in *attrp;
7386 */
7387 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007388screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389{
7390 unsigned off;
7391
7392 /* safety check */
7393 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7394 {
7395 off = LineOffset[row] + col;
7396 *attrp = ScreenAttrs[off];
7397 bytes[0] = ScreenLines[off];
7398 bytes[1] = NUL;
7399
7400#ifdef FEAT_MBYTE
7401 if (enc_utf8 && ScreenLinesUC[off] != 0)
7402 bytes[utfc_char2bytes(off, bytes)] = NUL;
7403 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7404 {
7405 bytes[0] = ScreenLines[off];
7406 bytes[1] = ScreenLines2[off];
7407 bytes[2] = NUL;
7408 }
7409 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7410 {
7411 bytes[1] = ScreenLines[off + 1];
7412 bytes[2] = NUL;
7413 }
7414#endif
7415 }
7416}
7417
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007418#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007419static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007420
7421/*
7422 * Return TRUE if composing characters for screen posn "off" differs from
7423 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007424 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007425 */
7426 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007427screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007428{
7429 int i;
7430
7431 for (i = 0; i < Screen_mco; ++i)
7432 {
7433 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7434 return TRUE;
7435 if (u8cc[i] == 0)
7436 break;
7437 }
7438 return FALSE;
7439}
7440#endif
7441
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442/*
7443 * Put string '*text' on the screen at position 'row' and 'col', with
7444 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7445 * Note: only outputs within one row, message is truncated at screen boundary!
7446 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7447 */
7448 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007449screen_puts(
7450 char_u *text,
7451 int row,
7452 int col,
7453 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454{
7455 screen_puts_len(text, -1, row, col, attr);
7456}
7457
7458/*
7459 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7460 * a NUL.
7461 */
7462 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007463screen_puts_len(
7464 char_u *text,
7465 int textlen,
7466 int row,
7467 int col,
7468 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469{
7470 unsigned off;
7471 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007472 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 int c;
7474#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007475 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 int mbyte_blen = 1;
7477 int mbyte_cells = 1;
7478 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007479 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 int clear_next_cell = FALSE;
7481# ifdef FEAT_ARABIC
7482 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007483 int pc, nc, nc1;
7484 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485# endif
7486#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007487#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7488 int force_redraw_this;
7489 int force_redraw_next = FALSE;
7490#endif
7491 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492
7493 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7494 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007495 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496
Bram Moolenaarc236c162008-07-13 17:41:49 +00007497#ifdef FEAT_MBYTE
7498 /* When drawing over the right halve of a double-wide char clear out the
7499 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007500 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007501# ifdef FEAT_GUI
7502 && !gui.in_use
7503# endif
7504 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007505 {
7506 ScreenLines[off - 1] = ' ';
7507 ScreenAttrs[off - 1] = 0;
7508 if (enc_utf8)
7509 {
7510 ScreenLinesUC[off - 1] = 0;
7511 ScreenLinesC[0][off - 1] = 0;
7512 }
7513 /* redraw the previous cell, make it empty */
7514 screen_char(off - 1, row, col - 1);
7515 /* force the cell at "col" to be redrawn */
7516 force_redraw_next = TRUE;
7517 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007518#endif
7519
Bram Moolenaar367329b2007-08-30 11:53:22 +00007520#ifdef FEAT_MBYTE
7521 max_off = LineOffset[row] + screen_Columns;
7522#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007523 while (col < screen_Columns
7524 && (len < 0 || (int)(ptr - text) < len)
7525 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 {
7527 c = *ptr;
7528#ifdef FEAT_MBYTE
7529 /* check if this is the first byte of a multibyte */
7530 if (has_mbyte)
7531 {
7532 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007533 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007535 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7537 mbyte_cells = 1;
7538 else if (enc_dbcs != 0)
7539 mbyte_cells = mbyte_blen;
7540 else /* enc_utf8 */
7541 {
7542 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007543 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 (int)((text + len) - ptr));
7545 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007546 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007548# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 /* Non-BMP character: display as ? or fullwidth ?. */
7550 if (u8c >= 0x10000)
7551 {
7552 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7553 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007554 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007556# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557# ifdef FEAT_ARABIC
7558 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7559 {
7560 /* Do Arabic shaping. */
7561 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7562 {
7563 /* Past end of string to be displayed. */
7564 nc = NUL;
7565 nc1 = NUL;
7566 }
7567 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007568 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007569 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7570 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007571 nc1 = pcc[0];
7572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 pc = prev_c;
7574 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007575 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 }
7577 else
7578 prev_c = u8c;
7579# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007580 if (col + mbyte_cells > screen_Columns)
7581 {
7582 /* Only 1 cell left, but character requires 2 cells:
7583 * display a '>' in the last column to avoid wrapping. */
7584 c = '>';
7585 mbyte_cells = 1;
7586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 }
7588 }
7589#endif
7590
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007591#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7592 force_redraw_this = force_redraw_next;
7593 force_redraw_next = FALSE;
7594#endif
7595
7596 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597#ifdef FEAT_MBYTE
7598 || (mbyte_cells == 2
7599 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7600 || (enc_dbcs == DBCS_JPNU
7601 && c == 0x8e
7602 && ScreenLines2[off] != ptr[1])
7603 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007604 && (ScreenLinesUC[off] !=
7605 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7606 || (ScreenLinesUC[off] != 0
7607 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608#endif
7609 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007610 || exmode_active;
7611
7612 if (need_redraw
7613#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7614 || force_redraw_this
7615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 )
7617 {
7618#if defined(FEAT_GUI) || defined(UNIX)
7619 /* The bold trick makes a single row of pixels appear in the next
7620 * character. When a bold character is removed, the next
7621 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007622 * and for some xterms. */
7623 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624# ifdef FEAT_GUI
7625 gui.in_use
7626# endif
7627# if defined(FEAT_GUI) && defined(UNIX)
7628 ||
7629# endif
7630# ifdef UNIX
7631 term_is_xterm
7632# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007633 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007635 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007637 if (n > HL_ALL)
7638 n = syn_attr2attr(n);
7639 if (n & HL_BOLD)
7640 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 }
7642#endif
7643#ifdef FEAT_MBYTE
7644 /* When at the end of the text and overwriting a two-cell
7645 * character with a one-cell character, need to clear the next
7646 * cell. Also when overwriting the left halve of a two-cell char
7647 * with the right halve of a two-cell char. Do this only once
7648 * (mb_off2cells() may return 2 on the right halve). */
7649 if (clear_next_cell)
7650 clear_next_cell = FALSE;
7651 else if (has_mbyte
7652 && (len < 0 ? ptr[mbyte_blen] == NUL
7653 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007654 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007656 && (*mb_off2cells)(off, max_off) == 1
7657 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 clear_next_cell = TRUE;
7659
7660 /* Make sure we never leave a second byte of a double-byte behind,
7661 * it confuses mb_off2cells(). */
7662 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007663 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007665 && (*mb_off2cells)(off, max_off) == 1
7666 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 ScreenLines[off + mbyte_blen] = 0;
7668#endif
7669 ScreenLines[off] = c;
7670 ScreenAttrs[off] = attr;
7671#ifdef FEAT_MBYTE
7672 if (enc_utf8)
7673 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007674 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 ScreenLinesUC[off] = 0;
7676 else
7677 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007678 int i;
7679
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007681 for (i = 0; i < Screen_mco; ++i)
7682 {
7683 ScreenLinesC[i][off] = u8cc[i];
7684 if (u8cc[i] == 0)
7685 break;
7686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 }
7688 if (mbyte_cells == 2)
7689 {
7690 ScreenLines[off + 1] = 0;
7691 ScreenAttrs[off + 1] = attr;
7692 }
7693 screen_char(off, row, col);
7694 }
7695 else if (mbyte_cells == 2)
7696 {
7697 ScreenLines[off + 1] = ptr[1];
7698 ScreenAttrs[off + 1] = attr;
7699 screen_char_2(off, row, col);
7700 }
7701 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7702 {
7703 ScreenLines2[off] = ptr[1];
7704 screen_char(off, row, col);
7705 }
7706 else
7707#endif
7708 screen_char(off, row, col);
7709 }
7710#ifdef FEAT_MBYTE
7711 if (has_mbyte)
7712 {
7713 off += mbyte_cells;
7714 col += mbyte_cells;
7715 ptr += mbyte_blen;
7716 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007717 {
7718 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007720 len = -1;
7721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 }
7723 else
7724#endif
7725 {
7726 ++off;
7727 ++col;
7728 ++ptr;
7729 }
7730 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007731
7732#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7733 /* If we detected the next character needs to be redrawn, but the text
7734 * doesn't extend up to there, update the character here. */
7735 if (force_redraw_next && col < screen_Columns)
7736 {
7737# ifdef FEAT_MBYTE
7738 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7739 screen_char_2(off, row, col);
7740 else
7741# endif
7742 screen_char(off, row, col);
7743 }
7744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745}
7746
7747#ifdef FEAT_SEARCH_EXTRA
7748/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007749 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 */
7751 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007752start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
7754 if (p_hls && !no_hlsearch)
7755 {
7756 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007757 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007758# ifdef FEAT_RELTIME
7759 /* Set the time limit to 'redrawtime'. */
7760 profile_setlimit(p_rdt, &search_hl.tm);
7761# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 }
7763}
7764
7765/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007766 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 */
7768 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007769end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770{
7771 if (search_hl.rm.regprog != NULL)
7772 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007773 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 search_hl.rm.regprog = NULL;
7775 }
7776}
7777
7778/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007779 * Init for calling prepare_search_hl().
7780 */
7781 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007782init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007783{
7784 matchitem_T *cur;
7785
7786 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7787 * match */
7788 cur = wp->w_match_head;
7789 while (cur != NULL)
7790 {
7791 cur->hl.rm = cur->match;
7792 if (cur->hlg_id == 0)
7793 cur->hl.attr = 0;
7794 else
7795 cur->hl.attr = syn_id2attr(cur->hlg_id);
7796 cur->hl.buf = wp->w_buffer;
7797 cur->hl.lnum = 0;
7798 cur->hl.first_lnum = 0;
7799# ifdef FEAT_RELTIME
7800 /* Set the time limit to 'redrawtime'. */
7801 profile_setlimit(p_rdt, &(cur->hl.tm));
7802# endif
7803 cur = cur->next;
7804 }
7805 search_hl.buf = wp->w_buffer;
7806 search_hl.lnum = 0;
7807 search_hl.first_lnum = 0;
7808 /* time limit is set at the toplevel, for all windows */
7809}
7810
7811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 * Advance to the match in window "wp" line "lnum" or past it.
7813 */
7814 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007815prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007817 matchitem_T *cur; /* points to the match list */
7818 match_T *shl; /* points to search_hl or a match */
7819 int shl_flag; /* flag to indicate whether search_hl
7820 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007821 int pos_inprogress; /* marks that position match search is
7822 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 int n;
7824
7825 /*
7826 * When using a multi-line pattern, start searching at the top
7827 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007828 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007830 cur = wp->w_match_head;
7831 shl_flag = FALSE;
7832 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007834 if (shl_flag == FALSE)
7835 {
7836 shl = &search_hl;
7837 shl_flag = TRUE;
7838 }
7839 else
7840 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 if (shl->rm.regprog != NULL
7842 && shl->lnum == 0
7843 && re_multiline(shl->rm.regprog))
7844 {
7845 if (shl->first_lnum == 0)
7846 {
7847# ifdef FEAT_FOLDING
7848 for (shl->first_lnum = lnum;
7849 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7850 if (hasFoldingWin(wp, shl->first_lnum - 1,
7851 NULL, NULL, TRUE, NULL))
7852 break;
7853# else
7854 shl->first_lnum = wp->w_topline;
7855# endif
7856 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007857 if (cur != NULL)
7858 cur->pos.cur = 0;
7859 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007861 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7862 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007864 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7865 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007866 pos_inprogress = cur == NULL || cur->pos.cur == 0
7867 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 if (shl->lnum != 0)
7869 {
7870 shl->first_lnum = shl->lnum
7871 + shl->rm.endpos[0].lnum
7872 - shl->rm.startpos[0].lnum;
7873 n = shl->rm.endpos[0].col;
7874 }
7875 else
7876 {
7877 ++shl->first_lnum;
7878 n = 0;
7879 }
7880 }
7881 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007882 if (shl != &search_hl && cur != NULL)
7883 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 }
7885}
7886
7887/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007888 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 * Uses shl->buf.
7890 * Sets shl->lnum and shl->rm contents.
7891 * Note: Assumes a previous match is always before "lnum", unless
7892 * shl->lnum is zero.
7893 * Careful: Any pointers for buffer lines will become invalid.
7894 */
7895 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007896next_search_hl(
7897 win_T *win,
7898 match_T *shl, /* points to search_hl or a match */
7899 linenr_T lnum,
7900 colnr_T mincol, /* minimal column for a match */
7901 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
7903 linenr_T l;
7904 colnr_T matchcol;
7905 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007906 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007908 // for :{range}s/pat only highlight inside the range
7909 if (lnum < search_first_line || lnum > search_last_line)
7910 {
7911 shl->lnum = 0;
7912 return;
7913 }
7914
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 if (shl->lnum != 0)
7916 {
7917 /* Check for three situations:
7918 * 1. If the "lnum" is below a previous match, start a new search.
7919 * 2. If the previous match includes "mincol", use it.
7920 * 3. Continue after the previous match.
7921 */
7922 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7923 if (lnum > l)
7924 shl->lnum = 0;
7925 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7926 return;
7927 }
7928
7929 /*
7930 * Repeat searching for a match until one is found that includes "mincol"
7931 * or none is found in this line.
7932 */
7933 called_emsg = FALSE;
7934 for (;;)
7935 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007936#ifdef FEAT_RELTIME
7937 /* Stop searching after passing the time limit. */
7938 if (profile_passed_limit(&(shl->tm)))
7939 {
7940 shl->lnum = 0; /* no match found in time */
7941 break;
7942 }
7943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 /* Three situations:
7945 * 1. No useful previous match: search from start of line.
7946 * 2. Not Vi compatible or empty match: continue at next character.
7947 * Break the loop if this is beyond the end of the line.
7948 * 3. Vi compatible searching: continue at end of previous match.
7949 */
7950 if (shl->lnum == 0)
7951 matchcol = 0;
7952 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7953 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007954 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007956 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007957
7958 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007959 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007960 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007962 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 shl->lnum = 0;
7964 break;
7965 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007966#ifdef FEAT_MBYTE
7967 if (has_mbyte)
7968 matchcol += mb_ptr2len(ml);
7969 else
7970#endif
7971 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 }
7973 else
7974 matchcol = shl->rm.endpos[0].col;
7975
7976 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007977 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007979 /* Remember whether shl->rm is using a copy of the regprog in
7980 * cur->match. */
7981 int regprog_is_copy = (shl != &search_hl && cur != NULL
7982 && shl == &cur->hl
7983 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007984 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007985
Bram Moolenaarb3414592014-06-17 17:48:32 +02007986 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7987 matchcol,
7988#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007989 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007990#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007991 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007992#endif
7993 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007994 /* Copy the regprog, in case it got freed and recompiled. */
7995 if (regprog_is_copy)
7996 cur->match.regprog = cur->hl.rm.regprog;
7997
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007998 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007999 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008000 /* Error while handling regexp: stop using this regexp. */
8001 if (shl == &search_hl)
8002 {
8003 /* don't free regprog in the match list, it's a copy */
8004 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008005 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008006 }
8007 shl->rm.regprog = NULL;
8008 shl->lnum = 0;
8009 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8010 message */
8011 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008012 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008013 }
8014 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008015 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008016 else
8017 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 if (nmatched == 0)
8019 {
8020 shl->lnum = 0; /* no match found */
8021 break;
8022 }
8023 if (shl->rm.startpos[0].lnum > 0
8024 || shl->rm.startpos[0].col >= mincol
8025 || nmatched > 1
8026 || shl->rm.endpos[0].col > mincol)
8027 {
8028 shl->lnum += shl->rm.startpos[0].lnum;
8029 break; /* useful match found */
8030 }
8031 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008032
8033 // Restore called_emsg for assert_fails().
8034 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036
Bram Moolenaar85077472016-10-16 14:35:48 +02008037/*
8038 * If there is a match fill "shl" and return one.
8039 * Return zero otherwise.
8040 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008041 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008042next_search_hl_pos(
8043 match_T *shl, /* points to a match */
8044 linenr_T lnum,
8045 posmatch_T *posmatch, /* match positions */
8046 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008047{
8048 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008049 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008050
Bram Moolenaarb3414592014-06-17 17:48:32 +02008051 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8052 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008053 llpos_T *pos = &posmatch->pos[i];
8054
8055 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008056 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008057 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008058 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008059 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008060 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008061 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008062 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008063 /* if this match comes before the one at "found" then swap
8064 * them */
8065 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008066 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008067 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008068
Bram Moolenaar85077472016-10-16 14:35:48 +02008069 *pos = posmatch->pos[found];
8070 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008071 }
8072 }
8073 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008074 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008075 }
8076 }
8077 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008078 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008079 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008080 colnr_T start = posmatch->pos[found].col == 0
8081 ? 0 : posmatch->pos[found].col - 1;
8082 colnr_T end = posmatch->pos[found].col == 0
8083 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008084
Bram Moolenaar85077472016-10-16 14:35:48 +02008085 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008086 shl->rm.startpos[0].lnum = 0;
8087 shl->rm.startpos[0].col = start;
8088 shl->rm.endpos[0].lnum = 0;
8089 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008090 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008091 posmatch->cur = found + 1;
8092 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008093 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008094 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008095}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008096#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008097
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008099screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100{
8101 attrentry_T *aep = NULL;
8102
8103 screen_attr = attr;
8104 if (full_screen
8105#ifdef WIN3264
8106 && termcap_active
8107#endif
8108 )
8109 {
8110#ifdef FEAT_GUI
8111 if (gui.in_use)
8112 {
8113 char buf[20];
8114
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008115 /* The GUI handles this internally. */
8116 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 OUT_STR(buf);
8118 }
8119 else
8120#endif
8121 {
8122 if (attr > HL_ALL) /* special HL attr. */
8123 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008124 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 aep = syn_cterm_attr2entry(attr);
8126 else
8127 aep = syn_term_attr2entry(attr);
8128 if (aep == NULL) /* did ":syntax clear" */
8129 attr = 0;
8130 else
8131 attr = aep->ae_attr;
8132 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008133 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008135 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008136#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008137 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8138 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8139 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008140#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008141 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008142 /* If the Normal FG color has BOLD attribute and the new HL
8143 * has a FG color defined, clear BOLD. */
8144 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008145 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008147 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008148 out_str(T_UCS);
8149 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008150 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8151 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008153 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008155 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008157 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008158 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159
8160 /*
8161 * Output the color or start string after bold etc., in case the
8162 * bold etc. override the color setting.
8163 */
8164 if (aep != NULL)
8165 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008166#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008167 /* When 'termguicolors' is set but fg or bg is unset,
8168 * fall back to the cterm colors. This helps for SpellBad,
8169 * where the GUI uses a red undercurl. */
8170 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008172 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008173 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008174 }
8175 else
8176#endif
8177 if (t_colors > 1)
8178 {
8179 if (aep->ae_u.cterm.fg_color)
8180 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8181 }
8182#ifdef FEAT_TERMGUICOLORS
8183 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8184 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008185 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008186 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 }
8188 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008189#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008190 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008192 if (aep->ae_u.cterm.bg_color)
8193 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8194 }
8195
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008196 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008197 {
8198 if (aep->ae_u.term.start != NULL)
8199 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 }
8201 }
8202 }
8203 }
8204}
8205
8206 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008207screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208{
8209 int do_ME = FALSE; /* output T_ME code */
8210
8211 if (screen_attr != 0
8212#ifdef WIN3264
8213 && termcap_active
8214#endif
8215 )
8216 {
8217#ifdef FEAT_GUI
8218 if (gui.in_use)
8219 {
8220 char buf[20];
8221
8222 /* use internal GUI code */
8223 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8224 OUT_STR(buf);
8225 }
8226 else
8227#endif
8228 {
8229 if (screen_attr > HL_ALL) /* special HL attr. */
8230 {
8231 attrentry_T *aep;
8232
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008233 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {
8235 /*
8236 * Assume that t_me restores the original colors!
8237 */
8238 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008239 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008240#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008241 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8242 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8243 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008244#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008245 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008246#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008247 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8248 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8249 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008250#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008251 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 do_ME = TRUE;
8253 }
8254 else
8255 {
8256 aep = syn_term_attr2entry(screen_attr);
8257 if (aep != NULL && aep->ae_u.term.stop != NULL)
8258 {
8259 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8260 do_ME = TRUE;
8261 else
8262 out_str(aep->ae_u.term.stop);
8263 }
8264 }
8265 if (aep == NULL) /* did ":syntax clear" */
8266 screen_attr = 0;
8267 else
8268 screen_attr = aep->ae_attr;
8269 }
8270
8271 /*
8272 * Often all ending-codes are equal to T_ME. Avoid outputting the
8273 * same sequence several times.
8274 */
8275 if (screen_attr & HL_STANDOUT)
8276 {
8277 if (STRCMP(T_SE, T_ME) == 0)
8278 do_ME = TRUE;
8279 else
8280 out_str(T_SE);
8281 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008282 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008283 {
8284 if (STRCMP(T_UCE, T_ME) == 0)
8285 do_ME = TRUE;
8286 else
8287 out_str(T_UCE);
8288 }
8289 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008290 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {
8292 if (STRCMP(T_UE, T_ME) == 0)
8293 do_ME = TRUE;
8294 else
8295 out_str(T_UE);
8296 }
8297 if (screen_attr & HL_ITALIC)
8298 {
8299 if (STRCMP(T_CZR, T_ME) == 0)
8300 do_ME = TRUE;
8301 else
8302 out_str(T_CZR);
8303 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008304 if (screen_attr & HL_STRIKETHROUGH)
8305 {
8306 if (STRCMP(T_STE, T_ME) == 0)
8307 do_ME = TRUE;
8308 else
8309 out_str(T_STE);
8310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8312 out_str(T_ME);
8313
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008314#ifdef FEAT_TERMGUICOLORS
8315 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008317 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008318 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008319 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008320 term_bg_rgb_color(cterm_normal_bg_gui_color);
8321 }
8322 else
8323#endif
8324 {
8325 if (t_colors > 1)
8326 {
8327 /* set Normal cterm colors */
8328 if (cterm_normal_fg_color != 0)
8329 term_fg_color(cterm_normal_fg_color - 1);
8330 if (cterm_normal_bg_color != 0)
8331 term_bg_color(cterm_normal_bg_color - 1);
8332 if (cterm_normal_fg_bold)
8333 out_str(T_MD);
8334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 }
8336 }
8337 }
8338 screen_attr = 0;
8339}
8340
8341/*
8342 * Reset the colors for a cterm. Used when leaving Vim.
8343 * The machine specific code may override this again.
8344 */
8345 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008346reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008348 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {
8350 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008351#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008352 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8353 || cterm_normal_bg_gui_color != INVALCOLOR)
8354 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008355#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {
8359 out_str(T_OP);
8360 screen_attr = -1;
8361 }
8362 if (cterm_normal_fg_bold)
8363 {
8364 out_str(T_ME);
8365 screen_attr = -1;
8366 }
8367 }
8368}
8369
8370/*
8371 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8372 * using the attributes from ScreenAttrs["off"].
8373 */
8374 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008375screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376{
8377 int attr;
8378
8379 /* Check for illegal values, just in case (could happen just after
8380 * resizing). */
8381 if (row >= screen_Rows || col >= screen_Columns)
8382 return;
8383
Bram Moolenaar494838a2015-02-10 19:20:37 +01008384 /* Outputting a character in the last cell on the screen may scroll the
8385 * screen up. Only do it when the "xn" termcap property is set, otherwise
8386 * mark the character invalid (update it when scrolled up). */
8387 if (*T_XN == NUL
8388 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389#ifdef FEAT_RIGHTLEFT
8390 /* account for first command-line character in rightleft mode */
8391 && !cmdmsg_rl
8392#endif
8393 )
8394 {
8395 ScreenAttrs[off] = (sattr_T)-1;
8396 return;
8397 }
8398
8399 /*
8400 * Stop highlighting first, so it's easier to move the cursor.
8401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 if (screen_char_attr != 0)
8403 attr = screen_char_attr;
8404 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 attr = ScreenAttrs[off];
8406 if (screen_attr != attr)
8407 screen_stop_highlight();
8408
8409 windgoto(row, col);
8410
8411 if (screen_attr != attr)
8412 screen_start_highlight(attr);
8413
8414#ifdef FEAT_MBYTE
8415 if (enc_utf8 && ScreenLinesUC[off] != 0)
8416 {
8417 char_u buf[MB_MAXBYTES + 1];
8418
Bram Moolenaarcb070082016-04-02 22:14:51 +02008419 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008420 {
8421 if (*p_ambw == 'd'
8422# ifdef FEAT_GUI
8423 && !gui.in_use
8424# endif
8425 )
8426 {
8427 /* Clear the two screen cells. If the character is actually
8428 * single width it won't change the second cell. */
8429 out_str((char_u *)" ");
8430 term_windgoto(row, col);
8431 }
8432 /* not sure where the cursor is after drawing the ambiguous width
8433 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008434 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008435 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008436 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008438
8439 /* Convert the UTF-8 character to bytes and write it. */
8440 buf[utfc_char2bytes(off, buf)] = NUL;
8441 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 }
8443 else
8444#endif
8445 {
8446#ifdef FEAT_MBYTE
8447 out_flush_check();
8448#endif
8449 out_char(ScreenLines[off]);
8450#ifdef FEAT_MBYTE
8451 /* double-byte character in single-width cell */
8452 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8453 out_char(ScreenLines2[off]);
8454#endif
8455 }
8456
8457 screen_cur_col++;
8458}
8459
8460#ifdef FEAT_MBYTE
8461
8462/*
8463 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8464 * on the screen at position 'row' and 'col'.
8465 * The attributes of the first byte is used for all. This is required to
8466 * output the two bytes of a double-byte character with nothing in between.
8467 */
8468 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008469screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470{
8471 /* Check for illegal values (could be wrong when screen was resized). */
8472 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8473 return;
8474
8475 /* Outputting the last character on the screen may scrollup the screen.
8476 * Don't to it! Mark the character invalid (update it when scrolled up) */
8477 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8478 {
8479 ScreenAttrs[off] = (sattr_T)-1;
8480 return;
8481 }
8482
8483 /* Output the first byte normally (positions the cursor), then write the
8484 * second byte directly. */
8485 screen_char(off, row, col);
8486 out_char(ScreenLines[off + 1]);
8487 ++screen_cur_col;
8488}
8489#endif
8490
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491/*
8492 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8493 * This uses the contents of ScreenLines[] and doesn't change it.
8494 */
8495 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008496screen_draw_rectangle(
8497 int row,
8498 int col,
8499 int height,
8500 int width,
8501 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502{
8503 int r, c;
8504 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008505#ifdef FEAT_MBYTE
8506 int max_off;
8507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008509 /* Can't use ScreenLines unless initialized */
8510 if (ScreenLines == NULL)
8511 return;
8512
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 if (invert)
8514 screen_char_attr = HL_INVERSE;
8515 for (r = row; r < row + height; ++r)
8516 {
8517 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008518#ifdef FEAT_MBYTE
8519 max_off = off + screen_Columns;
8520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 for (c = col; c < col + width; ++c)
8522 {
8523#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008524 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 {
8526 screen_char_2(off + c, r, c);
8527 ++c;
8528 }
8529 else
8530#endif
8531 {
8532 screen_char(off + c, r, c);
8533#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008534 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 ++c;
8536#endif
8537 }
8538 }
8539 }
8540 screen_char_attr = 0;
8541}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543/*
8544 * Redraw the characters for a vertically split window.
8545 */
8546 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008547redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548{
8549 int col;
8550 int width;
8551
8552# ifdef FEAT_CLIPBOARD
8553 clip_may_clear_selection(row, end - 1);
8554# endif
8555
8556 if (wp == NULL)
8557 {
8558 col = 0;
8559 width = Columns;
8560 }
8561 else
8562 {
8563 col = wp->w_wincol;
8564 width = wp->w_width;
8565 }
8566 screen_draw_rectangle(row, col, end - row, width, FALSE);
8567}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008569 static void
8570space_to_screenline(int off, int attr)
8571{
8572 ScreenLines[off] = ' ';
8573 ScreenAttrs[off] = attr;
8574# ifdef FEAT_MBYTE
8575 if (enc_utf8)
8576 ScreenLinesUC[off] = 0;
8577# endif
8578}
8579
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580/*
8581 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8582 * with character 'c1' in first column followed by 'c2' in the other columns.
8583 * Use attributes 'attr'.
8584 */
8585 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008586screen_fill(
8587 int start_row,
8588 int end_row,
8589 int start_col,
8590 int end_col,
8591 int c1,
8592 int c2,
8593 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594{
8595 int row;
8596 int col;
8597 int off;
8598 int end_off;
8599 int did_delete;
8600 int c;
8601 int norm_term;
8602#if defined(FEAT_GUI) || defined(UNIX)
8603 int force_next = FALSE;
8604#endif
8605
8606 if (end_row > screen_Rows) /* safety check */
8607 end_row = screen_Rows;
8608 if (end_col > screen_Columns) /* safety check */
8609 end_col = screen_Columns;
8610 if (ScreenLines == NULL
8611 || start_row >= end_row
8612 || start_col >= end_col) /* nothing to do */
8613 return;
8614
8615 /* it's a "normal" terminal when not in a GUI or cterm */
8616 norm_term = (
8617#ifdef FEAT_GUI
8618 !gui.in_use &&
8619#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008620 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 for (row = start_row; row < end_row; ++row)
8622 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008623#ifdef FEAT_MBYTE
8624 if (has_mbyte
8625# ifdef FEAT_GUI
8626 && !gui.in_use
8627# endif
8628 )
8629 {
8630 /* When drawing over the right halve of a double-wide char clear
8631 * out the left halve. When drawing over the left halve of a
8632 * double wide-char clear out the right halve. Only needed in a
8633 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008634 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008635 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008636 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008637 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008638 }
8639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 /*
8641 * Try to use delete-line termcap code, when no attributes or in a
8642 * "normal" terminal, where a bold/italic space is just a
8643 * space.
8644 */
8645 did_delete = FALSE;
8646 if (c2 == ' '
8647 && end_col == Columns
8648 && can_clear(T_CE)
8649 && (attr == 0
8650 || (norm_term
8651 && attr <= HL_ALL
8652 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8653 {
8654 /*
8655 * check if we really need to clear something
8656 */
8657 col = start_col;
8658 if (c1 != ' ') /* don't clear first char */
8659 ++col;
8660
8661 off = LineOffset[row] + col;
8662 end_off = LineOffset[row] + end_col;
8663
8664 /* skip blanks (used often, keep it fast!) */
8665#ifdef FEAT_MBYTE
8666 if (enc_utf8)
8667 while (off < end_off && ScreenLines[off] == ' '
8668 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8669 ++off;
8670 else
8671#endif
8672 while (off < end_off && ScreenLines[off] == ' '
8673 && ScreenAttrs[off] == 0)
8674 ++off;
8675 if (off < end_off) /* something to be cleared */
8676 {
8677 col = off - LineOffset[row];
8678 screen_stop_highlight();
8679 term_windgoto(row, col);/* clear rest of this screen line */
8680 out_str(T_CE);
8681 screen_start(); /* don't know where cursor is now */
8682 col = end_col - col;
8683 while (col--) /* clear chars in ScreenLines */
8684 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008685 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 ++off;
8687 }
8688 }
8689 did_delete = TRUE; /* the chars are cleared now */
8690 }
8691
8692 off = LineOffset[row] + start_col;
8693 c = c1;
8694 for (col = start_col; col < end_col; ++col)
8695 {
8696 if (ScreenLines[off] != c
8697#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008698 || (enc_utf8 && (int)ScreenLinesUC[off]
8699 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700#endif
8701 || ScreenAttrs[off] != attr
8702#if defined(FEAT_GUI) || defined(UNIX)
8703 || force_next
8704#endif
8705 )
8706 {
8707#if defined(FEAT_GUI) || defined(UNIX)
8708 /* The bold trick may make a single row of pixels appear in
8709 * the next character. When a bold character is removed, the
8710 * next character should be redrawn too. This happens for our
8711 * own GUI and for some xterms. */
8712 if (
8713# ifdef FEAT_GUI
8714 gui.in_use
8715# endif
8716# if defined(FEAT_GUI) && defined(UNIX)
8717 ||
8718# endif
8719# ifdef UNIX
8720 term_is_xterm
8721# endif
8722 )
8723 {
8724 if (ScreenLines[off] != ' '
8725 && (ScreenAttrs[off] > HL_ALL
8726 || ScreenAttrs[off] & HL_BOLD))
8727 force_next = TRUE;
8728 else
8729 force_next = FALSE;
8730 }
8731#endif
8732 ScreenLines[off] = c;
8733#ifdef FEAT_MBYTE
8734 if (enc_utf8)
8735 {
8736 if (c >= 0x80)
8737 {
8738 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008739 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 }
8741 else
8742 ScreenLinesUC[off] = 0;
8743 }
8744#endif
8745 ScreenAttrs[off] = attr;
8746 if (!did_delete || c != ' ')
8747 screen_char(off, row, col);
8748 }
8749 ++off;
8750 if (col == start_col)
8751 {
8752 if (did_delete)
8753 break;
8754 c = c2;
8755 }
8756 }
8757 if (end_col == Columns)
8758 LineWraps[row] = FALSE;
8759 if (row == Rows - 1) /* overwritten the command line */
8760 {
8761 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008762 if (start_col == 0 && end_col == Columns
8763 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008765 if (start_col == 0)
8766 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 }
8768 }
8769}
8770
8771/*
8772 * Check if there should be a delay. Used before clearing or redrawing the
8773 * screen or the command line.
8774 */
8775 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008776check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777{
8778 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8779 && !did_wait_return
8780 && emsg_silent == 0)
8781 {
8782 out_flush();
8783 ui_delay(1000L, TRUE);
8784 emsg_on_display = FALSE;
8785 if (check_msg_scroll)
8786 msg_scroll = FALSE;
8787 }
8788}
8789
8790/*
8791 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008792 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 * Returns TRUE if there is a valid screen to write to.
8794 * Returns FALSE when starting up and screen not initialized yet.
8795 */
8796 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008797screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008799 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 return (ScreenLines != NULL);
8801}
8802
8803/*
8804 * Resize the shell to Rows and Columns.
8805 * Allocate ScreenLines[] and associated items.
8806 *
8807 * There may be some time between setting Rows and Columns and (re)allocating
8808 * ScreenLines[]. This happens when starting up and when (manually) changing
8809 * the shell size. Always use screen_Rows and screen_Columns to access items
8810 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8811 * final size of the shell is needed.
8812 */
8813 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008814screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815{
8816 int new_row, old_row;
8817#ifdef FEAT_GUI
8818 int old_Rows;
8819#endif
8820 win_T *wp;
8821 int outofmem = FALSE;
8822 int len;
8823 schar_T *new_ScreenLines;
8824#ifdef FEAT_MBYTE
8825 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008826 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008828 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829#endif
8830 sattr_T *new_ScreenAttrs;
8831 unsigned *new_LineOffset;
8832 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008833 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008834 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008836 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008837 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008839retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 /*
8841 * Allocation of the screen buffers is done only when the size changes and
8842 * when Rows and Columns have been set and we have started doing full
8843 * screen stuff.
8844 */
8845 if ((ScreenLines != NULL
8846 && Rows == screen_Rows
8847 && Columns == screen_Columns
8848#ifdef FEAT_MBYTE
8849 && enc_utf8 == (ScreenLinesUC != NULL)
8850 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008851 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852#endif
8853 )
8854 || Rows == 0
8855 || Columns == 0
8856 || (!full_screen && ScreenLines == NULL))
8857 return;
8858
8859 /*
8860 * It's possible that we produce an out-of-memory message below, which
8861 * will cause this function to be called again. To break the loop, just
8862 * return here.
8863 */
8864 if (entered)
8865 return;
8866 entered = TRUE;
8867
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008868 /*
8869 * Note that the window sizes are updated before reallocating the arrays,
8870 * thus we must not redraw here!
8871 */
8872 ++RedrawingDisabled;
8873
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 win_new_shellsize(); /* fit the windows in the new sized shell */
8875
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 comp_col(); /* recompute columns for shown command and ruler */
8877
8878 /*
8879 * We're changing the size of the screen.
8880 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8881 * - Move lines from the old arrays into the new arrays, clear extra
8882 * lines (unless the screen is going to be cleared).
8883 * - Free the old arrays.
8884 *
8885 * If anything fails, make ScreenLines NULL, so we don't do anything!
8886 * Continuing with the old ScreenLines may result in a crash, because the
8887 * size is wrong.
8888 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008889 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008891 if (aucmd_win != NULL)
8892 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893
8894 new_ScreenLines = (schar_T *)lalloc((long_u)(
8895 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8896#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008897 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 if (enc_utf8)
8899 {
8900 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8901 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008902 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008903 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8905 }
8906 if (enc_dbcs == DBCS_JPNU)
8907 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8908 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8909#endif
8910 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8911 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8912 new_LineOffset = (unsigned *)lalloc((long_u)(
8913 Rows * sizeof(unsigned)), FALSE);
8914 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008915 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008917 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 {
8919 if (win_alloc_lines(wp) == FAIL)
8920 {
8921 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008922 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 }
8924 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008925 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8926 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008927 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008928give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008930#ifdef FEAT_MBYTE
8931 for (i = 0; i < p_mco; ++i)
8932 if (new_ScreenLinesC[i] == NULL)
8933 break;
8934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 if (new_ScreenLines == NULL
8936#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008937 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8939#endif
8940 || new_ScreenAttrs == NULL
8941 || new_LineOffset == NULL
8942 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008943 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 || outofmem)
8945 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008946 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008947 {
8948 /* guess the size */
8949 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8950
8951 /* Remember we did this to avoid getting outofmem messages over
8952 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008953 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008954 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008955 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01008957 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008958 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008959 VIM_CLEAR(new_ScreenLinesC[i]);
8960 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01008962 VIM_CLEAR(new_ScreenAttrs);
8963 VIM_CLEAR(new_LineOffset);
8964 VIM_CLEAR(new_LineWraps);
8965 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 }
8967 else
8968 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008969 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008970
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 for (new_row = 0; new_row < Rows; ++new_row)
8972 {
8973 new_LineOffset[new_row] = new_row * Columns;
8974 new_LineWraps[new_row] = FALSE;
8975
8976 /*
8977 * If the screen is not going to be cleared, copy as much as
8978 * possible from the old screen to the new one and clear the rest
8979 * (used when resizing the window at the "--more--" prompt or when
8980 * executing an external command, for the GUI).
8981 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008982 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 {
8984 (void)vim_memset(new_ScreenLines + new_row * Columns,
8985 ' ', (size_t)Columns * sizeof(schar_T));
8986#ifdef FEAT_MBYTE
8987 if (enc_utf8)
8988 {
8989 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8990 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008991 for (i = 0; i < p_mco; ++i)
8992 (void)vim_memset(new_ScreenLinesC[i]
8993 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 0, (size_t)Columns * sizeof(u8char_T));
8995 }
8996 if (enc_dbcs == DBCS_JPNU)
8997 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8998 0, (size_t)Columns * sizeof(schar_T));
8999#endif
9000 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9001 0, (size_t)Columns * sizeof(sattr_T));
9002 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009003 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 {
9005 if (screen_Columns < Columns)
9006 len = screen_Columns;
9007 else
9008 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009009#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009010 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009011 * may be invalid now. Also when p_mco changes. */
9012 if (!(enc_utf8 && ScreenLinesUC == NULL)
9013 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009014#endif
9015 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9016 ScreenLines + LineOffset[old_row],
9017 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009019 if (enc_utf8 && ScreenLinesUC != NULL
9020 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 {
9022 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9023 ScreenLinesUC + LineOffset[old_row],
9024 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009025 for (i = 0; i < p_mco; ++i)
9026 mch_memmove(new_ScreenLinesC[i]
9027 + new_LineOffset[new_row],
9028 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 (size_t)len * sizeof(u8char_T));
9030 }
9031 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9032 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9033 ScreenLines2 + LineOffset[old_row],
9034 (size_t)len * sizeof(schar_T));
9035#endif
9036 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9037 ScreenAttrs + LineOffset[old_row],
9038 (size_t)len * sizeof(sattr_T));
9039 }
9040 }
9041 }
9042 /* Use the last line of the screen for the current line. */
9043 current_ScreenLine = new_ScreenLines + Rows * Columns;
9044 }
9045
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009046 free_screenlines();
9047
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 ScreenLines = new_ScreenLines;
9049#ifdef FEAT_MBYTE
9050 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009051 for (i = 0; i < p_mco; ++i)
9052 ScreenLinesC[i] = new_ScreenLinesC[i];
9053 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 ScreenLines2 = new_ScreenLines2;
9055#endif
9056 ScreenAttrs = new_ScreenAttrs;
9057 LineOffset = new_LineOffset;
9058 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009059 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060
9061 /* It's important that screen_Rows and screen_Columns reflect the actual
9062 * size of ScreenLines[]. Set them before calling anything. */
9063#ifdef FEAT_GUI
9064 old_Rows = screen_Rows;
9065#endif
9066 screen_Rows = Rows;
9067 screen_Columns = Columns;
9068
9069 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009070 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 screenclear2();
9072
9073#ifdef FEAT_GUI
9074 else if (gui.in_use
9075 && !gui.starting
9076 && ScreenLines != NULL
9077 && old_Rows != Rows)
9078 {
9079 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9080 /*
9081 * Adjust the position of the cursor, for when executing an external
9082 * command.
9083 */
9084 if (msg_row >= Rows) /* Rows got smaller */
9085 msg_row = Rows - 1; /* put cursor at last row */
9086 else if (Rows > old_Rows) /* Rows got bigger */
9087 msg_row += Rows - old_Rows; /* put cursor in same place */
9088 if (msg_col >= Columns) /* Columns got smaller */
9089 msg_col = Columns - 1; /* put cursor at last column */
9090 }
9091#endif
9092
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009094 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009095
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009096 /*
9097 * Do not apply autocommands more than 3 times to avoid an endless loop
9098 * in case applying autocommands always changes Rows or Columns.
9099 */
9100 if (starting == 0 && ++retry_count <= 3)
9101 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009102 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009103 /* In rare cases, autocommands may have altered Rows or Columns,
9104 * jump back to check if we need to allocate the screen again. */
9105 goto retry;
9106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107}
9108
9109 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009110free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009111{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009112#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009113 int i;
9114
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009115 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009116 for (i = 0; i < Screen_mco; ++i)
9117 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009118 vim_free(ScreenLines2);
9119#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009120 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009121 vim_free(ScreenAttrs);
9122 vim_free(LineOffset);
9123 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009124 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009125}
9126
9127 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009128screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129{
9130 check_for_delay(FALSE);
9131 screenalloc(FALSE); /* allocate screen buffers if size changed */
9132 screenclear2(); /* clear the screen */
9133}
9134
9135 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009136screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137{
9138 int i;
9139
9140 if (starting == NO_SCREEN || ScreenLines == NULL
9141#ifdef FEAT_GUI
9142 || (gui.in_use && gui.starting)
9143#endif
9144 )
9145 return;
9146
9147#ifdef FEAT_GUI
9148 if (!gui.in_use)
9149#endif
9150 screen_attr = -1; /* force setting the Normal colors */
9151 screen_stop_highlight(); /* don't want highlighting here */
9152
9153#ifdef FEAT_CLIPBOARD
9154 /* disable selection without redrawing it */
9155 clip_scroll_selection(9999);
9156#endif
9157
9158 /* blank out ScreenLines */
9159 for (i = 0; i < Rows; ++i)
9160 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009161 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 LineWraps[i] = FALSE;
9163 }
9164
9165 if (can_clear(T_CL))
9166 {
9167 out_str(T_CL); /* clear the display */
9168 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009169 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 }
9171 else
9172 {
9173 /* can't clear the screen, mark all chars with invalid attributes */
9174 for (i = 0; i < Rows; ++i)
9175 lineinvalid(LineOffset[i], (int)Columns);
9176 clear_cmdline = TRUE;
9177 }
9178
9179 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9180
9181 win_rest_invalid(firstwin);
9182 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009183 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 if (must_redraw == CLEAR) /* no need to clear again */
9185 must_redraw = NOT_VALID;
9186 compute_cmdrow();
9187 msg_row = cmdline_row; /* put cursor on last line for messages */
9188 msg_col = 0;
9189 screen_start(); /* don't know where cursor is now */
9190 msg_scrolled = 0; /* can't scroll back */
9191 msg_didany = FALSE;
9192 msg_didout = FALSE;
9193}
9194
9195/*
9196 * Clear one line in ScreenLines.
9197 */
9198 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009199lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
9201 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9202#ifdef FEAT_MBYTE
9203 if (enc_utf8)
9204 (void)vim_memset(ScreenLinesUC + off, 0,
9205 (size_t)width * sizeof(u8char_T));
9206#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009207 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208}
9209
9210/*
9211 * Mark one line in ScreenLines invalid by setting the attributes to an
9212 * invalid value.
9213 */
9214 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009215lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216{
9217 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9218}
9219
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220/*
9221 * Copy part of a Screenline for vertically split window "wp".
9222 */
9223 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009224linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225{
9226 unsigned off_to = LineOffset[to] + wp->w_wincol;
9227 unsigned off_from = LineOffset[from] + wp->w_wincol;
9228
9229 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9230 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009231#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 if (enc_utf8)
9233 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009234 int i;
9235
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9237 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009238 for (i = 0; i < p_mco; ++i)
9239 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9240 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 }
9242 if (enc_dbcs == DBCS_JPNU)
9243 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9244 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9247 wp->w_width * sizeof(sattr_T));
9248}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249
9250/*
9251 * Return TRUE if clearing with term string "p" would work.
9252 * It can't work when the string is empty or it won't set the right background.
9253 */
9254 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009255can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256{
9257 return (*p != NUL && (t_colors <= 1
9258#ifdef FEAT_GUI
9259 || gui.in_use
9260#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009261#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009262 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009263 || (!p_tgc && cterm_normal_bg_color == 0)
9264#else
9265 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009266#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009267 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268}
9269
9270/*
9271 * Reset cursor position. Use whenever cursor was moved because of outputting
9272 * something directly to the screen (shell commands) or a terminal control
9273 * code.
9274 */
9275 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009276screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277{
9278 screen_cur_row = screen_cur_col = 9999;
9279}
9280
9281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 * Move the cursor to position "row","col" in the screen.
9283 * This tries to find the most efficient way to move, minimizing the number of
9284 * characters sent to the terminal.
9285 */
9286 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009287windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009289 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 int i;
9291 int plan;
9292 int cost;
9293 int wouldbe_col;
9294 int noinvcurs;
9295 char_u *bs;
9296 int goto_cost;
9297 int attr;
9298
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009299#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9301
9302#define PLAN_LE 1
9303#define PLAN_CR 2
9304#define PLAN_NL 3
9305#define PLAN_WRITE 4
9306 /* Can't use ScreenLines unless initialized */
9307 if (ScreenLines == NULL)
9308 return;
9309
9310 if (col != screen_cur_col || row != screen_cur_row)
9311 {
9312 /* Check for valid position. */
9313 if (row < 0) /* window without text lines? */
9314 row = 0;
9315 if (row >= screen_Rows)
9316 row = screen_Rows - 1;
9317 if (col >= screen_Columns)
9318 col = screen_Columns - 1;
9319
9320 /* check if no cursor movement is allowed in highlight mode */
9321 if (screen_attr && *T_MS == NUL)
9322 noinvcurs = HIGHL_COST;
9323 else
9324 noinvcurs = 0;
9325 goto_cost = GOTO_COST + noinvcurs;
9326
9327 /*
9328 * Plan how to do the positioning:
9329 * 1. Use CR to move it to column 0, same row.
9330 * 2. Use T_LE to move it a few columns to the left.
9331 * 3. Use NL to move a few lines down, column 0.
9332 * 4. Move a few columns to the right with T_ND or by writing chars.
9333 *
9334 * Don't do this if the cursor went beyond the last column, the cursor
9335 * position is unknown then (some terminals wrap, some don't )
9336 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009337 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 * characters to move the cursor to the right.
9339 */
9340 if (row >= screen_cur_row && screen_cur_col < Columns)
9341 {
9342 /*
9343 * If the cursor is in the same row, bigger col, we can use CR
9344 * or T_LE.
9345 */
9346 bs = NULL; /* init for GCC */
9347 attr = screen_attr;
9348 if (row == screen_cur_row && col < screen_cur_col)
9349 {
9350 /* "le" is preferred over "bc", because "bc" is obsolete */
9351 if (*T_LE)
9352 bs = T_LE; /* "cursor left" */
9353 else
9354 bs = T_BC; /* "backspace character (old) */
9355 if (*bs)
9356 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9357 else
9358 cost = 999;
9359 if (col + 1 < cost) /* using CR is less characters */
9360 {
9361 plan = PLAN_CR;
9362 wouldbe_col = 0;
9363 cost = 1; /* CR is just one character */
9364 }
9365 else
9366 {
9367 plan = PLAN_LE;
9368 wouldbe_col = col;
9369 }
9370 if (noinvcurs) /* will stop highlighting */
9371 {
9372 cost += noinvcurs;
9373 attr = 0;
9374 }
9375 }
9376
9377 /*
9378 * If the cursor is above where we want to be, we can use CR LF.
9379 */
9380 else if (row > screen_cur_row)
9381 {
9382 plan = PLAN_NL;
9383 wouldbe_col = 0;
9384 cost = (row - screen_cur_row) * 2; /* CR LF */
9385 if (noinvcurs) /* will stop highlighting */
9386 {
9387 cost += noinvcurs;
9388 attr = 0;
9389 }
9390 }
9391
9392 /*
9393 * If the cursor is in the same row, smaller col, just use write.
9394 */
9395 else
9396 {
9397 plan = PLAN_WRITE;
9398 wouldbe_col = screen_cur_col;
9399 cost = 0;
9400 }
9401
9402 /*
9403 * Check if any characters that need to be written have the
9404 * correct attributes. Also avoid UTF-8 characters.
9405 */
9406 i = col - wouldbe_col;
9407 if (i > 0)
9408 cost += i;
9409 if (cost < goto_cost && i > 0)
9410 {
9411 /*
9412 * Check if the attributes are correct without additionally
9413 * stopping highlighting.
9414 */
9415 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9416 while (i && *p++ == attr)
9417 --i;
9418 if (i != 0)
9419 {
9420 /*
9421 * Try if it works when highlighting is stopped here.
9422 */
9423 if (*--p == 0)
9424 {
9425 cost += noinvcurs;
9426 while (i && *p++ == 0)
9427 --i;
9428 }
9429 if (i != 0)
9430 cost = 999; /* different attributes, don't do it */
9431 }
9432#ifdef FEAT_MBYTE
9433 if (enc_utf8)
9434 {
9435 /* Don't use an UTF-8 char for positioning, it's slow. */
9436 for (i = wouldbe_col; i < col; ++i)
9437 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9438 {
9439 cost = 999;
9440 break;
9441 }
9442 }
9443#endif
9444 }
9445
9446 /*
9447 * We can do it without term_windgoto()!
9448 */
9449 if (cost < goto_cost)
9450 {
9451 if (plan == PLAN_LE)
9452 {
9453 if (noinvcurs)
9454 screen_stop_highlight();
9455 while (screen_cur_col > col)
9456 {
9457 out_str(bs);
9458 --screen_cur_col;
9459 }
9460 }
9461 else if (plan == PLAN_CR)
9462 {
9463 if (noinvcurs)
9464 screen_stop_highlight();
9465 out_char('\r');
9466 screen_cur_col = 0;
9467 }
9468 else if (plan == PLAN_NL)
9469 {
9470 if (noinvcurs)
9471 screen_stop_highlight();
9472 while (screen_cur_row < row)
9473 {
9474 out_char('\n');
9475 ++screen_cur_row;
9476 }
9477 screen_cur_col = 0;
9478 }
9479
9480 i = col - screen_cur_col;
9481 if (i > 0)
9482 {
9483 /*
9484 * Use cursor-right if it's one character only. Avoids
9485 * removing a line of pixels from the last bold char, when
9486 * using the bold trick in the GUI.
9487 */
9488 if (T_ND[0] != NUL && T_ND[1] == NUL)
9489 {
9490 while (i-- > 0)
9491 out_char(*T_ND);
9492 }
9493 else
9494 {
9495 int off;
9496
9497 off = LineOffset[row] + screen_cur_col;
9498 while (i-- > 0)
9499 {
9500 if (ScreenAttrs[off] != screen_attr)
9501 screen_stop_highlight();
9502#ifdef FEAT_MBYTE
9503 out_flush_check();
9504#endif
9505 out_char(ScreenLines[off]);
9506#ifdef FEAT_MBYTE
9507 if (enc_dbcs == DBCS_JPNU
9508 && ScreenLines[off] == 0x8e)
9509 out_char(ScreenLines2[off]);
9510#endif
9511 ++off;
9512 }
9513 }
9514 }
9515 }
9516 }
9517 else
9518 cost = 999;
9519
9520 if (cost >= goto_cost)
9521 {
9522 if (noinvcurs)
9523 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009524 if (row == screen_cur_row && (col > screen_cur_col)
9525 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 term_cursor_right(col - screen_cur_col);
9527 else
9528 term_windgoto(row, col);
9529 }
9530 screen_cur_row = row;
9531 screen_cur_col = col;
9532 }
9533}
9534
9535/*
9536 * Set cursor to its position in the current window.
9537 */
9538 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009539setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009541 setcursor_mayforce(FALSE);
9542}
9543
9544/*
9545 * Set cursor to its position in the current window.
9546 * When "force" is TRUE also when not redrawing.
9547 */
9548 void
9549setcursor_mayforce(int force)
9550{
9551 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 {
9553 validate_cursor();
9554 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009555 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009557 /* With 'rightleft' set and the cursor on a double-wide
9558 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009559 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009561 (has_mbyte
9562 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9563 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564# endif
9565 1)) :
9566#endif
9567 curwin->w_wcol));
9568 }
9569}
9570
9571
9572/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009573 * Insert 'line_count' lines at 'row' in window 'wp'.
9574 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9575 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 * scrolling.
9577 * Returns FAIL if the lines are not inserted, OK for success.
9578 */
9579 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009580win_ins_lines(
9581 win_T *wp,
9582 int row,
9583 int line_count,
9584 int invalid,
9585 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586{
9587 int did_delete;
9588 int nextrow;
9589 int lastrow;
9590 int retval;
9591
9592 if (invalid)
9593 wp->w_lines_valid = 0;
9594
9595 if (wp->w_height < 5)
9596 return FAIL;
9597
9598 if (line_count > wp->w_height - row)
9599 line_count = wp->w_height - row;
9600
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009601 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 if (retval != MAYBE)
9603 return retval;
9604
9605 /*
9606 * If there is a next window or a status line, we first try to delete the
9607 * lines at the bottom to avoid messing what is after the window.
9608 * If this fails and there are following windows, don't do anything to avoid
9609 * messing up those windows, better just redraw.
9610 */
9611 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 if (wp->w_next != NULL || wp->w_status_height)
9613 {
9614 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009615 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 did_delete = TRUE;
9617 else if (wp->w_next)
9618 return FAIL;
9619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 /*
9621 * if no lines deleted, blank the lines that will end up below the window
9622 */
9623 if (!did_delete)
9624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009627 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 lastrow = nextrow + line_count;
9629 if (lastrow > Rows)
9630 lastrow = Rows;
9631 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009632 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 ' ', ' ', 0);
9634 }
9635
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009636 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 == FAIL)
9638 {
9639 /* deletion will have messed up other windows */
9640 if (did_delete)
9641 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 win_rest_invalid(W_NEXT(wp));
9644 }
9645 return FAIL;
9646 }
9647
9648 return OK;
9649}
9650
9651/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009652 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9654 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9655 * scrolling
9656 * Return OK for success, FAIL if the lines are not deleted.
9657 */
9658 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009659win_del_lines(
9660 win_T *wp,
9661 int row,
9662 int line_count,
9663 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009664 int mayclear,
9665 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666{
9667 int retval;
9668
9669 if (invalid)
9670 wp->w_lines_valid = 0;
9671
9672 if (line_count > wp->w_height - row)
9673 line_count = wp->w_height - row;
9674
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009675 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 if (retval != MAYBE)
9677 return retval;
9678
9679 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009680 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 return FAIL;
9682
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 /*
9684 * If there are windows or status lines below, try to put them at the
9685 * correct place. If we can't do that, they have to be redrawn.
9686 */
9687 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9688 {
9689 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009690 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 {
9692 wp->w_redr_status = TRUE;
9693 win_rest_invalid(wp->w_next);
9694 }
9695 }
9696 /*
9697 * If this is the last window and there is no status line, redraw the
9698 * command line later.
9699 */
9700 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 redraw_cmdline = TRUE;
9702 return OK;
9703}
9704
9705/*
9706 * Common code for win_ins_lines() and win_del_lines().
9707 * Returns OK or FAIL when the work has been done.
9708 * Returns MAYBE when not finished yet.
9709 */
9710 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009711win_do_lines(
9712 win_T *wp,
9713 int row,
9714 int line_count,
9715 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009716 int del,
9717 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718{
9719 int retval;
9720
9721 if (!redrawing() || line_count <= 0)
9722 return FAIL;
9723
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009724 /* When inserting lines would result in loss of command output, just redraw
9725 * the lines. */
9726 if (no_win_do_lines_ins && !del)
9727 return FAIL;
9728
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009730 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009732 if (!no_win_do_lines_ins)
9733 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 return FAIL;
9735 }
9736
9737 /*
9738 * Delete all remaining lines
9739 */
9740 if (row + line_count >= wp->w_height)
9741 {
9742 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009743 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 ' ', ' ', 0);
9745 return OK;
9746 }
9747
9748 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009749 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009751 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009753 if (!no_win_do_lines_ins)
9754 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755
9756 /*
9757 * If the terminal can set a scroll region, use that.
9758 * Always do this in a vertically split window. This will redraw from
9759 * ScreenLines[] when t_CV isn't defined. That's faster than using
9760 * win_line().
9761 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009762 * a character in the lower right corner of the scroll region may cause a
9763 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009765 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 scroll_region_set(wp, row);
9769 if (del)
9770 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009771 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 else
9773 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009774 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 scroll_region_reset();
9777 return retval;
9778 }
9779
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9781 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782
9783 return MAYBE;
9784}
9785
9786/*
9787 * window 'wp' and everything after it is messed up, mark it for redraw
9788 */
9789 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009790win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 {
9794 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 wp->w_redr_status = TRUE;
9796 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 }
9798 redraw_cmdline = TRUE;
9799}
9800
9801/*
9802 * The rest of the routines in this file perform screen manipulations. The
9803 * given operation is performed physically on the screen. The corresponding
9804 * change is also made to the internal screen image. In this way, the editor
9805 * anticipates the effect of editing changes on the appearance of the screen.
9806 * That way, when we call screenupdate a complete redraw isn't usually
9807 * necessary. Another advantage is that we can keep adding code to anticipate
9808 * screen changes, and in the meantime, everything still works.
9809 */
9810
9811/*
9812 * types for inserting or deleting lines
9813 */
9814#define USE_T_CAL 1
9815#define USE_T_CDL 2
9816#define USE_T_AL 3
9817#define USE_T_CE 4
9818#define USE_T_DL 5
9819#define USE_T_SR 6
9820#define USE_NL 7
9821#define USE_T_CD 8
9822#define USE_REDRAW 9
9823
9824/*
9825 * insert lines on the screen and update ScreenLines[]
9826 * 'end' is the line after the scrolled part. Normally it is Rows.
9827 * When scrolling region used 'off' is the offset from the top for the region.
9828 * 'row' and 'end' are relative to the start of the region.
9829 *
9830 * return FAIL for failure, OK for success.
9831 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009832 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009833screen_ins_lines(
9834 int off,
9835 int row,
9836 int line_count,
9837 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009838 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009839 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840{
9841 int i;
9842 int j;
9843 unsigned temp;
9844 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009845 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 int type;
9847 int result_empty;
9848 int can_ce = can_clear(T_CE);
9849
9850 /*
9851 * FAIL if
9852 * - there is no valid screen
9853 * - the screen has to be redrawn completely
9854 * - the line count is less than one
9855 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009856 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009858 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9859#ifdef FEAT_CLIPBOARD
9860 || (clip_star.state != SELECT_CLEARED
9861 && redrawing_for_callback > 0)
9862#endif
9863 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 return FAIL;
9865
9866 /*
9867 * There are seven ways to insert lines:
9868 * 0. When in a vertically split window and t_CV isn't set, redraw the
9869 * characters from ScreenLines[].
9870 * 1. Use T_CD (clear to end of display) if it exists and the result of
9871 * the insert is just empty lines
9872 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9873 * present or line_count > 1. It looks better if we do all the inserts
9874 * at once.
9875 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9876 * insert is just empty lines and T_CE is not present or line_count >
9877 * 1.
9878 * 4. Use T_AL (insert line) if it exists.
9879 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9880 * just empty lines.
9881 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9882 * just empty lines.
9883 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9884 * the 'da' flag is not set or we have clear line capability.
9885 * 8. redraw the characters from ScreenLines[].
9886 *
9887 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9888 * the scrollbar for the window. It does have insert line, use that if it
9889 * exists.
9890 */
9891 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9893 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009894 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 type = USE_T_CD;
9896 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9897 type = USE_T_CAL;
9898 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9899 type = USE_T_CDL;
9900 else if (*T_AL != NUL)
9901 type = USE_T_AL;
9902 else if (can_ce && result_empty)
9903 type = USE_T_CE;
9904 else if (*T_DL != NUL && result_empty)
9905 type = USE_T_DL;
9906 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9907 type = USE_T_SR;
9908 else
9909 return FAIL;
9910
9911 /*
9912 * For clearing the lines screen_del_lines() is used. This will also take
9913 * care of t_db if necessary.
9914 */
9915 if (type == USE_T_CD || type == USE_T_CDL ||
9916 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009917 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918
9919 /*
9920 * If text is retained below the screen, first clear or delete as many
9921 * lines at the bottom of the window as are about to be inserted so that
9922 * the deleted lines won't later surface during a screen_del_lines.
9923 */
9924 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009925 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926
9927#ifdef FEAT_CLIPBOARD
9928 /* Remove a modeless selection when inserting lines halfway the screen
9929 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009930 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009931 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 else
9933 clip_scroll_selection(-line_count);
9934#endif
9935
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936#ifdef FEAT_GUI
9937 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9938 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009939 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940#endif
9941
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009942 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9943 cursor_col = wp->w_wincol;
9944
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 if (*T_CCS != NUL) /* cursor relative to region */
9946 cursor_row = row;
9947 else
9948 cursor_row = row + off;
9949
9950 /*
9951 * Shift LineOffset[] line_count down to reflect the inserted lines.
9952 * Clear the inserted lines in ScreenLines[].
9953 */
9954 row += off;
9955 end += off;
9956 for (i = 0; i < line_count; ++i)
9957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 if (wp != NULL && wp->w_width != Columns)
9959 {
9960 /* need to copy part of a line */
9961 j = end - 1 - i;
9962 while ((j -= line_count) >= row)
9963 linecopy(j + line_count, j, wp);
9964 j += line_count;
9965 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009966 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9967 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 else
9969 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9970 LineWraps[j] = FALSE;
9971 }
9972 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 {
9974 j = end - 1 - i;
9975 temp = LineOffset[j];
9976 while ((j -= line_count) >= row)
9977 {
9978 LineOffset[j + line_count] = LineOffset[j];
9979 LineWraps[j + line_count] = LineWraps[j];
9980 }
9981 LineOffset[j + line_count] = temp;
9982 LineWraps[j + line_count] = FALSE;
9983 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009984 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 else
9986 lineinvalid(temp, (int)Columns);
9987 }
9988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989
9990 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009991 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009992 if (clear_attr != 0)
9993 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 /* redraw the characters */
9996 if (type == USE_REDRAW)
9997 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009998 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 {
10000 term_append_lines(line_count);
10001 screen_start(); /* don't know where cursor is now */
10002 }
10003 else
10004 {
10005 for (i = 0; i < line_count; i++)
10006 {
10007 if (type == USE_T_AL)
10008 {
10009 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010010 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 out_str(T_AL);
10012 }
10013 else /* type == USE_T_SR */
10014 out_str(T_SR);
10015 screen_start(); /* don't know where cursor is now */
10016 }
10017 }
10018
10019 /*
10020 * With scroll-reverse and 'da' flag set we need to clear the lines that
10021 * have been scrolled down into the region.
10022 */
10023 if (type == USE_T_SR && *T_DA)
10024 {
10025 for (i = 0; i < line_count; ++i)
10026 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010027 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 out_str(T_CE);
10029 screen_start(); /* don't know where cursor is now */
10030 }
10031 }
10032
10033#ifdef FEAT_GUI
10034 gui_can_update_cursor();
10035 if (gui.in_use)
10036 out_flush(); /* always flush after a scroll */
10037#endif
10038 return OK;
10039}
10040
10041/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010042 * Delete lines on the screen and update ScreenLines[].
10043 * "end" is the line after the scrolled part. Normally it is Rows.
10044 * When scrolling region used "off" is the offset from the top for the region.
10045 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 *
10047 * Return OK for success, FAIL if the lines are not deleted.
10048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010050screen_del_lines(
10051 int off,
10052 int row,
10053 int line_count,
10054 int end,
10055 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010056 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010057 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058{
10059 int j;
10060 int i;
10061 unsigned temp;
10062 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010063 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 int cursor_end;
10065 int result_empty; /* result is empty until end of region */
10066 int can_delete; /* deleting line codes can be used */
10067 int type;
10068
10069 /*
10070 * FAIL if
10071 * - there is no valid screen
10072 * - the screen has to be redrawn completely
10073 * - the line count is less than one
10074 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010075 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010077 if (!screen_valid(TRUE) || line_count <= 0
10078 || (!force && line_count > p_ttyscroll)
10079#ifdef FEAT_CLIPBOARD
10080 || (clip_star.state != SELECT_CLEARED
10081 && redrawing_for_callback > 0)
10082#endif
10083 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 return FAIL;
10085
10086 /*
10087 * Check if the rest of the current region will become empty.
10088 */
10089 result_empty = row + line_count >= end;
10090
10091 /*
10092 * We can delete lines only when 'db' flag not set or when 'ce' option
10093 * available.
10094 */
10095 can_delete = (*T_DB == NUL || can_clear(T_CE));
10096
10097 /*
10098 * There are six ways to delete lines:
10099 * 0. When in a vertically split window and t_CV isn't set, redraw the
10100 * characters from ScreenLines[].
10101 * 1. Use T_CD if it exists and the result is empty.
10102 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10103 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10104 * none of the other ways work.
10105 * 4. Use T_CE (erase line) if the result is empty.
10106 * 5. Use T_DL (delete line) if it exists.
10107 * 6. redraw the characters from ScreenLines[].
10108 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10110 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010111 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 type = USE_T_CD;
10113#if defined(__BEOS__) && defined(BEOS_DR8)
10114 /*
10115 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10116 * its internal termcap... this works okay for tests which test *T_DB !=
10117 * NUL. It has the disadvantage that the user cannot use any :set t_*
10118 * command to get T_DB (back) to empty_option, only :set term=... will do
10119 * the trick...
10120 * Anyway, this hack will hopefully go away with the next OS release.
10121 * (Olaf Seibert)
10122 */
10123 else if (row == 0 && T_DB == empty_option
10124 && (line_count == 1 || *T_CDL == NUL))
10125#else
10126 else if (row == 0 && (
10127#ifndef AMIGA
10128 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10129 * up, so use delete-line command */
10130 line_count == 1 ||
10131#endif
10132 *T_CDL == NUL))
10133#endif
10134 type = USE_NL;
10135 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10136 type = USE_T_CDL;
10137 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010138 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 type = USE_T_CE;
10140 else if (*T_DL != NUL && can_delete)
10141 type = USE_T_DL;
10142 else if (*T_CDL != NUL && can_delete)
10143 type = USE_T_CDL;
10144 else
10145 return FAIL;
10146
10147#ifdef FEAT_CLIPBOARD
10148 /* Remove a modeless selection when deleting lines halfway the screen or
10149 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010150 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010151 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 else
10153 clip_scroll_selection(line_count);
10154#endif
10155
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156#ifdef FEAT_GUI
10157 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10158 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010159 gui_dont_update_cursor(gui.cursor_row >= row + off
10160 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161#endif
10162
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010163 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10164 cursor_col = wp->w_wincol;
10165
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 if (*T_CCS != NUL) /* cursor relative to region */
10167 {
10168 cursor_row = row;
10169 cursor_end = end;
10170 }
10171 else
10172 {
10173 cursor_row = row + off;
10174 cursor_end = end + off;
10175 }
10176
10177 /*
10178 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10179 * Clear the inserted lines in ScreenLines[].
10180 */
10181 row += off;
10182 end += off;
10183 for (i = 0; i < line_count; ++i)
10184 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 if (wp != NULL && wp->w_width != Columns)
10186 {
10187 /* need to copy part of a line */
10188 j = row + i;
10189 while ((j += line_count) <= end - 1)
10190 linecopy(j - line_count, j, wp);
10191 j -= line_count;
10192 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010193 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10194 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 else
10196 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10197 LineWraps[j] = FALSE;
10198 }
10199 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 {
10201 /* whole width, moving the line pointers is faster */
10202 j = row + i;
10203 temp = LineOffset[j];
10204 while ((j += line_count) <= end - 1)
10205 {
10206 LineOffset[j - line_count] = LineOffset[j];
10207 LineWraps[j - line_count] = LineWraps[j];
10208 }
10209 LineOffset[j - line_count] = temp;
10210 LineWraps[j - line_count] = FALSE;
10211 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010212 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 else
10214 lineinvalid(temp, (int)Columns);
10215 }
10216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010218 if (screen_attr != clear_attr)
10219 screen_stop_highlight();
10220 if (clear_attr != 0)
10221 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 /* redraw the characters */
10224 if (type == USE_REDRAW)
10225 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010226 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010228 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 out_str(T_CD);
10230 screen_start(); /* don't know where cursor is now */
10231 }
10232 else if (type == USE_T_CDL)
10233 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010234 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 term_delete_lines(line_count);
10236 screen_start(); /* don't know where cursor is now */
10237 }
10238 /*
10239 * Deleting lines at top of the screen or scroll region: Just scroll
10240 * the whole screen (scroll region) up by outputting newlines on the
10241 * last line.
10242 */
10243 else if (type == USE_NL)
10244 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010245 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 for (i = line_count; --i >= 0; )
10247 out_char('\n'); /* cursor will remain on same line */
10248 }
10249 else
10250 {
10251 for (i = line_count; --i >= 0; )
10252 {
10253 if (type == USE_T_DL)
10254 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010255 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 out_str(T_DL); /* delete a line */
10257 }
10258 else /* type == USE_T_CE */
10259 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010260 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 out_str(T_CE); /* erase a line */
10262 }
10263 screen_start(); /* don't know where cursor is now */
10264 }
10265 }
10266
10267 /*
10268 * If the 'db' flag is set, we need to clear the lines that have been
10269 * scrolled up at the bottom of the region.
10270 */
10271 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10272 {
10273 for (i = line_count; i > 0; --i)
10274 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010275 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 out_str(T_CE); /* erase a line */
10277 screen_start(); /* don't know where cursor is now */
10278 }
10279 }
10280
10281#ifdef FEAT_GUI
10282 gui_can_update_cursor();
10283 if (gui.in_use)
10284 out_flush(); /* always flush after a scroll */
10285#endif
10286
10287 return OK;
10288}
10289
10290/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010291 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 *
10293 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10294 * If clear_cmdline is FALSE there may be a message there that needs to be
10295 * cleared only if a mode is shown.
10296 * Return the length of the message (0 if no message).
10297 */
10298 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010299showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300{
10301 int need_clear;
10302 int length = 0;
10303 int do_mode;
10304 int attr;
10305 int nwr_save;
10306#ifdef FEAT_INS_EXPAND
10307 int sub_attr;
10308#endif
10309
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010310 do_mode = ((p_smd && msg_silent == 0)
10311 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010312 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010313 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010314 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 {
10316 /*
10317 * Don't show mode right now, when not redrawing or inside a mapping.
10318 * Call char_avail() only when we are going to show something, because
10319 * it takes a bit of time.
10320 */
10321 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10322 {
10323 redraw_cmdline = TRUE; /* show mode later */
10324 return 0;
10325 }
10326
10327 nwr_save = need_wait_return;
10328
10329 /* wait a bit before overwriting an important message */
10330 check_for_delay(FALSE);
10331
10332 /* if the cmdline is more than one line high, erase top lines */
10333 need_clear = clear_cmdline;
10334 if (clear_cmdline && cmdline_row < Rows - 1)
10335 msg_clr_cmdline(); /* will reset clear_cmdline */
10336
10337 /* Position on the last line in the window, column 0 */
10338 msg_pos_mode();
10339 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010340 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 if (do_mode)
10342 {
10343 MSG_PUTS_ATTR("--", attr);
10344#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010345 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010346# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010347 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010348# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010349 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010350# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010351 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010352# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 MSG_PUTS_ATTR(" IM", attr);
10354# else
10355 MSG_PUTS_ATTR(" XIM", attr);
10356# endif
10357#endif
10358#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10359 if (gui.in_use)
10360 {
10361 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010362 {
10363 /* HANGUL */
10364 if (enc_utf8)
10365 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10366 else
10367 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 }
10370#endif
10371#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010372 /* CTRL-X in Insert mode */
10373 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 {
10375 /* These messages can get long, avoid a wrap in a narrow
10376 * window. Prefer showing edit_submode_extra. */
10377 length = (Rows - msg_row) * Columns - 3;
10378 if (edit_submode_extra != NULL)
10379 length -= vim_strsize(edit_submode_extra);
10380 if (length > 0)
10381 {
10382 if (edit_submode_pre != NULL)
10383 length -= vim_strsize(edit_submode_pre);
10384 if (length - vim_strsize(edit_submode) > 0)
10385 {
10386 if (edit_submode_pre != NULL)
10387 msg_puts_attr(edit_submode_pre, attr);
10388 msg_puts_attr(edit_submode, attr);
10389 }
10390 if (edit_submode_extra != NULL)
10391 {
10392 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10393 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010394 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 else
10396 sub_attr = attr;
10397 msg_puts_attr(edit_submode_extra, sub_attr);
10398 }
10399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 }
10401 else
10402#endif
10403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 if (State & VREPLACE_FLAG)
10405 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010406 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10408 else if (State & INSERT)
10409 {
10410#ifdef FEAT_RIGHTLEFT
10411 if (p_ri)
10412 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10413#endif
10414 MSG_PUTS_ATTR(_(" INSERT"), attr);
10415 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010416 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417 MSG_PUTS_ATTR(_(" (insert)"), attr);
10418 else if (restart_edit == 'R')
10419 MSG_PUTS_ATTR(_(" (replace)"), attr);
10420 else if (restart_edit == 'V')
10421 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10422#ifdef FEAT_RIGHTLEFT
10423 if (p_hkmap)
10424 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10425# ifdef FEAT_FKMAP
10426 if (p_fkmap)
10427 MSG_PUTS_ATTR(farsi_text_5, attr);
10428# endif
10429#endif
10430#ifdef FEAT_KEYMAP
10431 if (State & LANGMAP)
10432 {
10433# ifdef FEAT_ARABIC
10434 if (curwin->w_p_arab)
10435 MSG_PUTS_ATTR(_(" Arabic"), attr);
10436 else
10437# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010438 if (get_keymap_str(curwin, (char_u *)" (%s)",
10439 NameBuff, MAXPATHL))
10440 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 }
10442#endif
10443 if ((State & INSERT) && p_paste)
10444 MSG_PUTS_ATTR(_(" (paste)"), attr);
10445
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 if (VIsual_active)
10447 {
10448 char *p;
10449
10450 /* Don't concatenate separate words to avoid translation
10451 * problems. */
10452 switch ((VIsual_select ? 4 : 0)
10453 + (VIsual_mode == Ctrl_V) * 2
10454 + (VIsual_mode == 'V'))
10455 {
10456 case 0: p = N_(" VISUAL"); break;
10457 case 1: p = N_(" VISUAL LINE"); break;
10458 case 2: p = N_(" VISUAL BLOCK"); break;
10459 case 4: p = N_(" SELECT"); break;
10460 case 5: p = N_(" SELECT LINE"); break;
10461 default: p = N_(" SELECT BLOCK"); break;
10462 }
10463 MSG_PUTS_ATTR(_(p), attr);
10464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 MSG_PUTS_ATTR(" --", attr);
10466 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010467
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 need_clear = TRUE;
10469 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010470 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471#ifdef FEAT_INS_EXPAND
10472 && edit_submode == NULL /* otherwise it gets too long */
10473#endif
10474 )
10475 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010476 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 need_clear = TRUE;
10478 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010479
10480 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 if (need_clear || clear_cmdline)
10482 msg_clr_eos();
10483 msg_didout = FALSE; /* overwrite this message */
10484 length = msg_col;
10485 msg_col = 0;
10486 need_wait_return = nwr_save; /* never ask for hit-return for this */
10487 }
10488 else if (clear_cmdline && msg_silent == 0)
10489 /* Clear the whole command line. Will reset "clear_cmdline". */
10490 msg_clr_cmdline();
10491
10492#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 /* In Visual mode the size of the selected area must be redrawn. */
10494 if (VIsual_active)
10495 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496
10497 /* If the last window has no status line, the ruler is after the mode
10498 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010499 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010500 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501#endif
10502 redraw_cmdline = FALSE;
10503 clear_cmdline = FALSE;
10504
10505 return length;
10506}
10507
10508/*
10509 * Position for a mode message.
10510 */
10511 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010512msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513{
10514 msg_col = 0;
10515 msg_row = Rows - 1;
10516}
10517
10518/*
10519 * Delete mode message. Used when ESC is typed which is expected to end
10520 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010521 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 */
10523 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010524unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525{
10526 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010527 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 */
10529 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10530 redraw_cmdline = TRUE; /* delete mode later */
10531 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010532 clearmode();
10533}
10534
10535/*
10536 * Clear the mode message.
10537 */
10538 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010539clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010540{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010541 int save_msg_row = msg_row;
10542 int save_msg_col = msg_col;
10543
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010544 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010545 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010546 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010547 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010548
10549 msg_col = save_msg_col;
10550 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551}
10552
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010553 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010554recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010555{
10556 MSG_PUTS_ATTR(_("recording"), attr);
10557 if (!shortmess(SHM_RECORDING))
10558 {
10559 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010560 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010561 MSG_PUTS_ATTR(s, attr);
10562 }
10563}
10564
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010565/*
10566 * Draw the tab pages line at the top of the Vim window.
10567 */
10568 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010569draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010570{
10571 int tabcount = 0;
10572 tabpage_T *tp;
10573 int tabwidth;
10574 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010575 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010576 int attr;
10577 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010578 win_T *cwp;
10579 int wincount;
10580 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010581 int c;
10582 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010583 int attr_sel = HL_ATTR(HLF_TPS);
10584 int attr_nosel = HL_ATTR(HLF_TP);
10585 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010586 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010587 int room;
10588 int use_sep_chars = (t_colors < 8
10589#ifdef FEAT_GUI
10590 && !gui.in_use
10591#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010592#ifdef FEAT_TERMGUICOLORS
10593 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010594#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010595 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010596
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010597 if (ScreenLines == NULL)
10598 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010599 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010600
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010601#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010602 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010603 if (gui_use_tabline())
10604 {
10605 gui_update_tabline();
10606 return;
10607 }
10608#endif
10609
10610 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010611 return;
10612
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010613#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010614
10615 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10616 for (scol = 0; scol < Columns; ++scol)
10617 TabPageIdxs[scol] = 0;
10618
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010619 /* Use the 'tabline' option if it's set. */
10620 if (*p_tal != NUL)
10621 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010622 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010623
10624 /* Check for an error. If there is one we would loop in redrawing the
10625 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010626 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010627 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010628 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010629 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010630 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010631 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010632 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010633 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010634#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010635 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010636 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010637 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010638
Bram Moolenaar238a5642006-02-21 22:12:05 +000010639 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10640 if (tabwidth < 6)
10641 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010642
Bram Moolenaar238a5642006-02-21 22:12:05 +000010643 attr = attr_nosel;
10644 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010645 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010646 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10647 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010648 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010649 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010650
Bram Moolenaar238a5642006-02-21 22:12:05 +000010651 if (tp->tp_topframe == topframe)
10652 attr = attr_sel;
10653 if (use_sep_chars && col > 0)
10654 screen_putchar('|', 0, col++, attr);
10655
10656 if (tp->tp_topframe != topframe)
10657 attr = attr_nosel;
10658
10659 screen_putchar(' ', 0, col++, attr);
10660
10661 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010662 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010663 cwp = curwin;
10664 wp = firstwin;
10665 }
10666 else
10667 {
10668 cwp = tp->tp_curwin;
10669 wp = tp->tp_firstwin;
10670 }
10671
10672 modified = FALSE;
10673 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10674 if (bufIsChanged(wp->w_buffer))
10675 modified = TRUE;
10676 if (modified || wincount > 1)
10677 {
10678 if (wincount > 1)
10679 {
10680 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010681 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010682 if (col + len >= Columns - 3)
10683 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010684 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010685#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010686 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010687#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010688 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010689#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010690 );
10691 col += len;
10692 }
10693 if (modified)
10694 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10695 screen_putchar(' ', 0, col++, attr);
10696 }
10697
10698 room = scol - col + tabwidth - 1;
10699 if (room > 0)
10700 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010701 /* Get buffer name in NameBuff[] */
10702 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010703 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010704 len = vim_strsize(NameBuff);
10705 p = NameBuff;
10706#ifdef FEAT_MBYTE
10707 if (has_mbyte)
10708 while (len > room)
10709 {
10710 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010711 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010712 }
10713 else
10714#endif
10715 if (len > room)
10716 {
10717 p += len - room;
10718 len = room;
10719 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010720 if (len > Columns - col - 1)
10721 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010722
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010723 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010724 col += len;
10725 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010726 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010727
10728 /* Store the tab page number in TabPageIdxs[], so that
10729 * jump_to_mouse() knows where each one is. */
10730 ++tabcount;
10731 while (scol < col)
10732 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010733 }
10734
Bram Moolenaar238a5642006-02-21 22:12:05 +000010735 if (use_sep_chars)
10736 c = '_';
10737 else
10738 c = ' ';
10739 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010740
10741 /* Put an "X" for closing the current tab if there are several. */
10742 if (first_tabpage->tp_next != NULL)
10743 {
10744 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10745 TabPageIdxs[Columns - 1] = -999;
10746 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010747 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010748
10749 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10750 * set. */
10751 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010752}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010753
10754/*
10755 * Get buffer name for "buf" into NameBuff[].
10756 * Takes care of special buffer names and translates special characters.
10757 */
10758 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010759get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010760{
10761 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010762 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010763 else
10764 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10765 trans_characters(NameBuff, MAXPATHL);
10766}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010767
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768/*
10769 * Get the character to use in a status line. Get its attributes in "*attr".
10770 */
10771 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010772fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773{
10774 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010775
10776#ifdef FEAT_TERMINAL
10777 if (bt_terminal(wp->w_buffer))
10778 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010779 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010780 {
10781 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010782 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010783 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010784 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010785 {
10786 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010787 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010788 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010789 }
10790 else
10791#endif
10792 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010794 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 fill = fill_stl;
10796 }
10797 else
10798 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010799 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800 fill = fill_stlnc;
10801 }
10802 /* Use fill when there is highlighting, and highlighting of current
10803 * window differs, or the fillchars differ, or this is not the
10804 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010805 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010806 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 || (fill_stl != fill_stlnc)))
10808 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010809 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 return '^';
10811 return '=';
10812}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814/*
10815 * Get the character to use in a separator between vertically split windows.
10816 * Get its attributes in "*attr".
10817 */
10818 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010819fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010821 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 if (*attr == 0 && fill_vert == ' ')
10823 return '|';
10824 else
10825 return fill_vert;
10826}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827
10828/*
10829 * Return TRUE if redrawing should currently be done.
10830 */
10831 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010832redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010834#ifdef FEAT_EVAL
10835 if (disable_redraw_for_testing)
10836 return 0;
10837 else
10838#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010839 return ((!RedrawingDisabled
10840#ifdef FEAT_EVAL
10841 || ignore_redraw_flag_for_testing
10842#endif
10843 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844}
10845
10846/*
10847 * Return TRUE if printing messages should currently be done.
10848 */
10849 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010850messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851{
10852 return (!(p_lz && char_avail() && !KeyTyped));
10853}
10854
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010855#ifdef FEAT_MENU
10856/*
10857 * Draw the window toolbar.
10858 */
10859 static void
10860redraw_win_toolbar(win_T *wp)
10861{
10862 vimmenu_T *menu;
10863 int item_idx = 0;
10864 int item_count = 0;
10865 int col = 0;
10866 int next_col;
10867 int off = (int)(current_ScreenLine - ScreenLines);
10868 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10869 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10870
10871 vim_free(wp->w_winbar_items);
10872 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10873 ++item_count;
10874 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10875 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10876
10877 /* TODO: use fewer spaces if there is not enough room */
10878 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010879 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010880 {
10881 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010882 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010883 break;
10884 if (col > 1)
10885 {
10886 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010887 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010888 break;
10889 }
10890
10891 wp->w_winbar_items[item_idx].wb_startcol = col;
10892 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010893 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010894 break;
10895
10896 next_col = text_to_screenline(wp, menu->name, col);
10897 while (col < next_col)
10898 {
10899 ScreenAttrs[off + col] = button_attr;
10900 ++col;
10901 }
10902 wp->w_winbar_items[item_idx].wb_endcol = col;
10903 wp->w_winbar_items[item_idx].wb_menu = menu;
10904 ++item_idx;
10905
Bram Moolenaar02631462017-09-22 15:20:32 +020010906 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010907 break;
10908 space_to_screenline(off + col, button_attr);
10909 ++col;
10910 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010911 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010912 {
10913 space_to_screenline(off + col, fill_attr);
10914 ++col;
10915 }
10916 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10917
Bram Moolenaar02631462017-09-22 15:20:32 +020010918 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10919 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010920}
10921#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010922
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923/*
10924 * Show current status info in ruler and various other places
10925 * If always is FALSE, only show ruler if position has changed.
10926 */
10927 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010928showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929{
10930 if (!always && !redrawing())
10931 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010932#ifdef FEAT_INS_EXPAND
10933 if (pum_visible())
10934 {
10935 /* Don't redraw right now, do it later. */
10936 curwin->w_redr_status = TRUE;
10937 return;
10938 }
10939#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010940#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010941 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010942 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010943 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 else
10946#endif
10947#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010948 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949#endif
10950
10951#ifdef FEAT_TITLE
10952 if (need_maketitle
10953# ifdef FEAT_STL_OPT
10954 || (p_icon && (stl_syntax & STL_IN_ICON))
10955 || (p_title && (stl_syntax & STL_IN_TITLE))
10956# endif
10957 )
10958 maketitle();
10959#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010960 /* Redraw the tab pages line if needed. */
10961 if (redraw_tabline)
10962 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963}
10964
10965#ifdef FEAT_CMDL_INFO
10966 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010967win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010969#define RULER_BUF_LEN 70
10970 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 int row;
10972 int fillchar;
10973 int attr;
10974 int empty_line = FALSE;
10975 colnr_T virtcol;
10976 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010977 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 int this_ru_col;
10980 int off = 0;
10981 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982
10983 /* If 'ruler' off or redrawing disabled, don't do anything */
10984 if (!p_ru)
10985 return;
10986
10987 /*
10988 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10989 * after deleting lines, before cursor.lnum is corrected.
10990 */
10991 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10992 return;
10993
10994#ifdef FEAT_INS_EXPAND
10995 /* Don't draw the ruler while doing insert-completion, it might overwrite
10996 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 if (edit_submode != NULL)
10999 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011000 // Don't draw the ruler when the popup menu is visible, it may overlap.
11001 // Except when the popup menu will be redrawn anyway.
11002 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011003 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004#endif
11005
11006#ifdef FEAT_STL_OPT
11007 if (*p_ruf)
11008 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011009 int save_called_emsg = called_emsg;
11010
11011 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011013 if (called_emsg)
11014 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011015 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011016 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 return;
11018 }
11019#endif
11020
11021 /*
11022 * Check if not in Insert mode and the line is empty (will show "0-1").
11023 */
11024 if (!(State & INSERT)
11025 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11026 empty_line = TRUE;
11027
11028 /*
11029 * Only draw the ruler when something changed.
11030 */
11031 validate_virtcol_win(wp);
11032 if ( redraw_cmdline
11033 || always
11034 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11035 || wp->w_cursor.col != wp->w_ru_cursor.col
11036 || wp->w_virtcol != wp->w_ru_virtcol
11037#ifdef FEAT_VIRTUALEDIT
11038 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11039#endif
11040 || wp->w_topline != wp->w_ru_topline
11041 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11042#ifdef FEAT_DIFF
11043 || wp->w_topfill != wp->w_ru_topfill
11044#endif
11045 || empty_line != wp->w_ru_empty)
11046 {
11047 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 if (wp->w_status_height)
11049 {
11050 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011051 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011052 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011053 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 }
11055 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 {
11057 row = Rows - 1;
11058 fillchar = ' ';
11059 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 width = Columns;
11061 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 }
11063
11064 /* In list mode virtcol needs to be recomputed */
11065 virtcol = wp->w_virtcol;
11066 if (wp->w_p_list && lcs_tab1 == NUL)
11067 {
11068 wp->w_p_list = FALSE;
11069 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11070 wp->w_p_list = TRUE;
11071 }
11072
11073 /*
11074 * Some sprintfs return the length, some return a pointer.
11075 * To avoid portability problems we use strlen() here.
11076 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011077 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11079 ? 0L
11080 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011081 len = STRLEN(buffer);
11082 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11084 (int)virtcol + 1);
11085
11086 /*
11087 * Add a "50%" if there is room for it.
11088 * On the last line, don't print in the last column (scrolls the
11089 * screen up on some terminals).
11090 */
11091 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011092 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 this_ru_col = ru_col - (Columns - width);
11097 if (this_ru_col < 0)
11098 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 /* Never use more than half the window/screen width, leave the other
11100 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011101 if (this_ru_col < (width + 1) / 2)
11102 this_ru_col = (width + 1) / 2;
11103 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011105 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011106 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 {
11108#ifdef FEAT_MBYTE
11109 if (has_mbyte)
11110 i += (*mb_char2bytes)(fillchar, buffer + i);
11111 else
11112#endif
11113 buffer[i++] = fillchar;
11114 ++o;
11115 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011116 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 }
11118 /* Truncate at window boundary. */
11119#ifdef FEAT_MBYTE
11120 if (has_mbyte)
11121 {
11122 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011123 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 {
11125 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011126 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127 {
11128 buffer[i] = NUL;
11129 break;
11130 }
11131 }
11132 }
11133 else
11134#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011135 if (this_ru_col + (int)STRLEN(buffer) > width)
11136 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137
Bram Moolenaar4033c552017-09-16 20:54:51 +020011138 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 i = redraw_cmdline;
11140 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011141 this_ru_col + off + (int)STRLEN(buffer),
11142 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 fillchar, fillchar, attr);
11144 /* don't redraw the cmdline because of showing the ruler */
11145 redraw_cmdline = i;
11146 wp->w_ru_cursor = wp->w_cursor;
11147 wp->w_ru_virtcol = wp->w_virtcol;
11148 wp->w_ru_empty = empty_line;
11149 wp->w_ru_topline = wp->w_topline;
11150 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11151#ifdef FEAT_DIFF
11152 wp->w_ru_topfill = wp->w_topfill;
11153#endif
11154 }
11155}
11156#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011157
11158#if defined(FEAT_LINEBREAK) || defined(PROTO)
11159/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011160 * Return the width of the 'number' and 'relativenumber' column.
11161 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011162 * Otherwise it depends on 'numberwidth' and the line count.
11163 */
11164 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011165number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011166{
11167 int n;
11168 linenr_T lnum;
11169
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011170 if (wp->w_p_rnu && !wp->w_p_nu)
11171 /* cursor line shows "0" */
11172 lnum = wp->w_height;
11173 else
11174 /* cursor line shows absolute line number */
11175 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011176
Bram Moolenaar6b314672015-03-20 15:42:10 +010011177 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011178 return wp->w_nrwidth_width;
11179 wp->w_nrwidth_line_count = lnum;
11180
11181 n = 0;
11182 do
11183 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011184 lnum /= 10;
11185 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011186 } while (lnum > 0);
11187
11188 /* 'numberwidth' gives the minimal width plus one */
11189 if (n < wp->w_p_nuw - 1)
11190 n = wp->w_p_nuw - 1;
11191
11192 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011193 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011194 return n;
11195}
11196#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011197
11198/*
11199 * Return the current cursor column. This is the actual position on the
11200 * screen. First column is 0.
11201 */
11202 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011203screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011204{
11205 return screen_cur_col;
11206}
11207
11208/*
11209 * Return the current cursor row. This is the actual position on the screen.
11210 * First row is 0.
11211 */
11212 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011213screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011214{
11215 return screen_cur_row;
11216}